JS-Interpreter icon indicating copy to clipboard operation
JS-Interpreter copied to clipboard

pseudoToNative of boxed primitive return empty object

Open jcubic opened this issue 1 year ago • 0 comments

I'm writing unit tests in my fork, and I think that I've bumped into a bug:

test('it shoud box primitive value when using Object constructor', () => {
    const code = `var obj = Object(10); obj;`;
    const inter = new Interpreter(code);
    inter.run();
    expect(inter.pseudoToNative(inter.value)).toEqual(new Number(10));
});
AssertionError: expected {} to deeply equal 10

- Expected
+ Received

- Number {}
+ Object {}

pseudoToNative converts boxed number 10 into an empty object.

This test pass:

expect(inter.pseudoToNative(inter.value)).toEqual({})

I can create PR with a fix. This code seems to work:

  if ((pseudoObj.proto === this.NUMBER.properties['prototype'] ||
       pseudoObj.proto === this.BOOLEAN.properties['prototype'] ||
       pseudoObj.proto === this.STRING.properties['prototype']) &&
      (pseudoObj.data === true || pseudoObj.data === false ||
       typeof pseudoObj.data === 'string' || typeof pseudoObj.data === 'number')) {
      var nativeBox = Object(pseudoObj.data);
      cycles.native.push(nativeBox);
      return nativeBox;
  }

I'm not sure what kind of code do you prefer, but I would wrap this into a function, something like isBoxedValue.

jcubic avatar Oct 10 '24 16:10 jcubic