vm2 icon indicating copy to clipboard operation
vm2 copied to clipboard

Objects behave differently from the expected behavior

Open wvad opened this issue 3 years ago • 2 comments

const { VM } = require('vm2');
const { deepStrictEqual } = require('node:assert');

const vmFunc = new VM().run(`obj => {
  obj = Object.create(obj);
  obj.aa = 1;
  return Reflect.getOwnPropertyDescriptor(obj, 'aa')?.value;
}`);

const obj01 = { aa: 0 };
const res01 = vmFunc(obj01);

const func = obj => {
  obj = Object.create(obj);
  obj.aa = 1;
  return Reflect.getOwnPropertyDescriptor(obj, 'aa')?.value;
};

const obj02 = { aa: 0 };
const res02 = func(obj02);

deepStrictEqual([obj01.aa, res01], [obj02.aa, res02]);
AssertionError [ERR_ASSERTION] [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected

  [
+   1,
+   undefined
-   0,
-   1
  ]

wvad avatar Sep 17 '22 14:09 wvad

This are known issues.

XmiliaH avatar Sep 17 '22 15:09 XmiliaH

I think you should add to known issues that if Object.create is used with a proxied object as the first argument, it won't work as expected. https://github.com/patriksimek/vm2/issues/471#issue-1376790466

wvad avatar Sep 18 '22 06:09 wvad

Added to the readme.

XmiliaH avatar Nov 22 '22 13:11 XmiliaH