Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cykoder opened this issue Aug 13, 2021 · 3 comments
Open

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

cykoder opened this issue Aug 13, 2021 · 3 comments

Comments

@cykoder
Copy link

cykoder commented Aug 13, 2021

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
@ftk
Copy link
Owner

ftk commented Aug 14, 2021

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

@cykoder
Copy link
Author

cykoder commented Aug 14, 2021

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++

@cykoder
Copy link
Author

cykoder commented Jun 26, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants