vm2
vm2 copied to clipboard
Objects behave differently from the expected behavior
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
]
This are known issues.
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
Added to the readme.