PyMiniRacer icon indicating copy to clipboard operation
PyMiniRacer copied to clipboard

Properties of JS/ES6 class created in its constructor are undefined when method is called on the JS object later

Open s-m-e opened this issue 3 years ago • 3 comments

Steps to reproduce

The following piece of code reproduces the issue. There are variations of the problem, as shown in the test method in the JS class.

from py_mini_racer import py_mini_racer

ctx = py_mini_racer.MiniRacer()

ctx.eval("""
class Bar {
    constructor(param) {
        this._param = [];
        for (let item of param) {
            this._param.push(item);
        };
        this._verify = 7;
    }
    test() {
        // OK - Result is [0, 1, 2, 3]
        // return [...Array(4).keys()]; 

        // ERROR - Result is a TypeError: Cannot read property 'length' of undefined
        // return [...Array(this._param.length).keys()];
        
        // BAD - Result is [0], this._verify is undefined
        return [...Array(this._verify).keys()]; 
    }
}
""")

ctx.eval("""
let foo = new Bar([10, 11, 12, 13]);
""")

b = ctx.call('foo.test')  # Used "eval" in earlier versions here ... worked
print(b)

It appears that objects do get constructed, but the JS' class constructor either does not run or something else happens so that properties are literally undefined when an object is touched later.

Background: I am trying to get some code running that I last touched in 2019. Back in the day, I used ctx.eval to call the method and return an array - it did work if I recall correctly. The performance was impressive. Now, ctx.eval apparently does not handle arrays anymore so following the readme file, I switched to ctx.call.

Disclaimer: I really suck at writing JS - so this could be (part of) the issue.

Expected behavior

this.property should have been initialized by the constructor and be available in other subsequently called methods.

Actual behavior

this.property is undefined.

System configuration

PyMiniRacer version: 0.6.0 Python version: 3.9.10 (x86 64bit Linux)

s-m-e avatar Jun 11 '22 21:06 s-m-e

b = ctx.execute('foo.test()')

This appears to do what I want.

s-m-e avatar Jun 12 '22 18:06 s-m-e