Subj : Re: Create an instance of a JS object from C To : spellcaster From : Brendan Eich Date : Fri Dec 12 2003 09:55 am spellcaster wrote: >Hi! > >I'm pretty new to spidermonkey and I'm amazed fast I could integrate >it into my app. I guess I'm just blind, but I can't find a function to >create an instance of a Class within the context. > > By "context" at the end of that sentence, do you mean "scope" or "global object"? I'm always vigilant to distinguish the two, since JSContext in the JS API is for execution, not scope. You need one JSContext per thread concurrently executing JS or calling the API, but you might need more or fewer scope chains ending in one or more global objects. Consider the browser embedding, which appears to be single-threaded, but where each window, frame, or iframe is its own global object. >Now I want to populate my context with some instances of Dummy >objects. Right now what I do is something like this: > >-- Code -- >jsval rval; >char* script = "test = new Dummy();"; >JS_EvaluateScript( > jsContext, jsGlobalObj, > script, strlen(script), > "[init]", __LINE__, > &rval); >-- /Code -- > >To ensure that a variable named "test" of type "Dummy" exists in the >context. >Is there a pure C way of doing this? Calling JS_EvaluateScript() seems >hackish... > What about JS_NewObject, JS_ConstructObject, and JS_ConstructObjectWithArguments? If you want to call JS_NewObject and then to define the resulting new object as the value of some named property in another object, use JS_DefineObject. /be .