Open sazlang opened 6 years ago
Yes, you can use https://duktape.org/api.html#duk_def_prop.
It works similarly to Object.defineProperty() but takes the getter/setter argument via the value stack, see the API documentation for examples.
Hmm. Thanks. But what about capturing setters/getters for all dynamic properties of JS object?
What dynamic properties do you mean?
Do you mean capturing all property accesses, regardless of property name? If so, getters/setters don't allow you to do that, but you can use a Proxy object for that.
Yes, I mean. I will look for Proxy object.
There are some Ecmascript-based Proxy examples here: https://wiki.duktape.org/HowtoVirtualProperties.html. The C API call for creating a Proxy is https://duktape.org/api.html#duk_push_proxy if you'd rather initialize the Proxy from C code.
Proxy is a generic Ecmascript concept so almost any Proxy tutorial should be usable. Duktape doesn't implement all traps yet, but get
, set
, has
, delete
are probably what you need and they are implemented.
@sazlang Any updates or ready to close?
Is there any way to bind getter and setter for JS variable from C? Something like that.