Re: How to add named variables to a SpiderMonkey context?
James Stortz <[email protected]> Wed, 15 Jul 2020 14:31:57 -0400
| Newsgroups | gmane.comp.mozilla.devel.jseng |
|---|---|
| Message-ID | <CAE0WHd82jwDx8y1Lw5rEjRZqAuvvWammWXx1LgJY3og9YUHd2Q@mail.gmail.com> |
The C++ engine is just the underlying implementation of the JS paradigm, so, the logic will be the same as your script, but low level. 1 Create the variable handle 2 Assign it a value (such as object, array, function, string, etc.) 3 Define it on an object (such as the global scope) You will have to follow these steps to define each and every property or method. You can also set flags such as read only, permanent, enumerable, etc. You can also define prototypes and instantiate objects from those types instead of just generic objects. see: JS::RootedValue and (.setString or .setObject or .setBoolean or ...) JS_DefineFunction or JS_SetFunction JS_DefineProperty or JS_SetProperty JS_DefineElement or JS_SetElement and: JSPROP_PERMANENT and ... JSClass and JSClassOps and JS_InitClass On 7/15/20, Kent Williams <[email protected]> wrote: > This seems super-obvious to me but it's a part of using the SpiderMonkey > C++ API I haven't had to use before. > > 1. How do you add named objects to an interpreter context? Like there > should be functions like > > JS_AddNamedObject(JSContext *cx, char *name, JS::HandleObject obj); > JS_AddNamedValue(JSContext *cx, char *name, JS::HandleValue obj); > > 2. Similarly there should be functions to look up values in the > Interpreter context by name. > JS_GetNamedValue(JSContext *cx, char *name, JS::HandleValue &value); > > Am I just missing where this stuff is in the API? > > > USE CASE: I want replace making assignments by building up a script and > then evaluating it, to doing the assignment directly with C++ api. > > It should faster to not parse and execute a script, and it would also > get rid of a bunch of escaping you need to do for JS code to interpret > the value literals properly. > > _______________________________________________ > dev-tech-js-engine mailing list > [email protected] > https://lists.mozilla.org/listinfo/dev-tech-js-engine >