quickjs icon indicating copy to clipboard operation
quickjs copied to clipboard

Implement Object.get and Object.set

Open qwenger opened this issue 2 years ago • 2 comments

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:

  • What about merging these methods with those of Context, as in, treating the Context as an Object? (Respectively, remove the get, set, etc. methods of Context, add a generic get_global or similar to get the global object from the context)

Further discussion (for the future?):

  • What about add_callable? Should Object also support it? Should we merge set and add_callable?
  • 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?)?

qwenger avatar Apr 08 '22 16:04 qwenger

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.

PetterS avatar Apr 11 '22 09:04 PetterS

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.

qwenger avatar Apr 11 '22 16:04 qwenger