ftk / quickjspp

QuickJS C++ wrapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Assigning native C++ object a new property doesnt get remembered

cykoder opened this issue · comments

commented

First off I'm not sure if this is a quickjshpp bug or a quickjs bug. If I can I'll try test in quickjs too but time is limited right now. So here's the problem:

I have a native object that I have exposed to JS, lets call it "events". It is a property of a global "program" (native c++ instance exposed to JS as global variable). If I were to assign a new method to it, such as:

program.events.addListener = function() { }

and then try to call it on the next line, it is undefined:

program.events.addListener(); // results in error
console.log('addListener', program.events.addListener) // results undefined
commented

This functionality is not supported right now.
What happens if you add a method to prototype, e.g. program.events.__proto__.addListener = function() { }?

commented

That seems to work as expected - so for methods I guess this workaround is good for now. The same cant be said for variables though. It's quite important for how JS works to be able to assign any value to any object and it should persist. I'm happy to try help implement if you can give me some pointers on where to look for it.

To clarify, I can add a variable to the prototype and it seems to work when accessing from the prototype:

program.events.__proto__.myVar = true;
console.log(program.events.__proto__.myVar); // true
program.events.myVar = false;
console.log(program.events.myVar); // true, should be false

But as you can see, trying to do the same with just myVar (with or without prototype.myVar existing) doesnt work. Either returns true when it should be false if __proto__.myVar is set or undefined if its not.

If the object is constructed in JS and is of native type then I can assign variables to it and have them persist, just not when exposed from C++

commented

@ftk this seems to be resolved in the sharedptr branch!