PetterS / quickjs

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

Add/set variables #54

Closed qwenger closed 3 years ago

qwenger commented 3 years ago

Hi,

quickjs provides ways of evaluating code and getting the result (ctx.eval), adding functions (ctx.add_callable) and getting values (ctx.get).

AFAICS, there is currently no proper way of adding/setting variables.

I've been able to work around this via json dumping:

ctx.eval(f"my_variable_name = {json.dumps(variable)};")

It however feels a bit hacky. I think that it would be nice to be able to set variables directly, e.g. as

ctx.set("my_variable_name", variable)

or by adding an optional argument to eval, which would bind the return value (js-side) of the evaluation to a variable:

ctx.eval(variable, "my_variable_name")
PetterS commented 3 years ago

A set method on a context sounds reasonable.

qwenger commented 3 years ago

Done in #55.