PetterS / quickjs

Thin Python wrapper of https://bellard.org/quickjs/
https://github.com/bellard/QuickJS
MIT License
179 stars 19 forks source link

Implement Object.get and Object.set #81

Open qwenger opened 2 years ago

qwenger commented 2 years ago

Implement Object.get and Object.set using the same ideas as Context.get and Context.set.

Currently it is very tedious to get or set an object property. When the object is only known via its Python wrapper, one has to first rebind it to the global context using Context.set and then access it with Context.get. With this, one could directly access or set the property via the Python wrapper.

The json() method is not sufficient for this, as it does not expose properties from the prototype.

Discussion:

Further discussion (for the future?):

PetterS commented 2 years ago

When I originally created this library, I did the bare minimum for it to be useful in production. The API was deliberately kept small to reduce the likelihood of bugs. However, I don't use it in production any more, so if you wish to make it easier to use, we can do so. Just make sure everything including failures/exceptions is covered by tests.

What about add_callable? Should Object also support it? Should we merge set and add_callable?

May be a good idea to merge them, yes.

What about having a more transparent interface between JS and Python? Like being able to access properties of Object with the standard dot syntax (and/or square brackets lookup?)?

I like this. Can be implemented in Python code as a convenience.

qwenger commented 2 years ago

What about add_callable? Should Object also support it? Should we merge set and add_callable?

May be a good idea to merge them, yes.

Could you please have a look at #82? It is an alternative proposal that goes further by merging set and add_callable and moving the get/set methods to Object.