justjake / quickjs-emscripten

Safely execute untrusted Javascript in your Javascript, and execute synchronous code that uses async functions
https://www.npmjs.com/package/quickjs-emscripten
Other
1.3k stars 100 forks source link

How to delete the field of an object through `callFunction`? #45

Closed rxliuli closed 3 years ago

rxliuli commented 3 years ago

I wrote a unit test to try, but there are some problems with the result

image

import { getQuickJS, QuickJSVm } from 'quickjs-emscripten'
import { withScope } from '../withScope'

describe('测试 deleteKey', () => {
  let vm: QuickJSVm = undefined as any
  beforeEach(async () => {
    const quickJS = await getQuickJS()
    vm = quickJS.createVm()
  })

  afterEach(() => {
    vm.dispose()
    vm = undefined as any
  })
  it('基本示例', () => {
    withScope(vm, (vm) => {
      vm.setProp(vm.global, 'name', vm.newString('liuli'))
      const deleteProperty = vm.unwrapResult(
        vm.evalCode('Reflect.deleteProperty'),
      )
      expect(vm.dump(vm.getProp(vm.global, 'name'))).toBe('liuli')
      vm.callFunction(
        deleteProperty,
        vm.unwrapResult(vm.evalCode('globalThis')),
        vm.newString('name'),
      ) // Not deleted here
      console.log(vm.dump(vm.getProp(vm.global, 'name')))
      vm.evalCode(`Reflect.deleteProperty(globalThis, 'name')`) // This step really takes effect, but I want to do it from the main process
      console.log(vm.dump(vm.getProp(vm.global, 'name')))
    }).dispose()
  })
})
rxliuli commented 3 years ago

Sorry i didn't pass this