[setting followup-to jseng group where you're likely to get
better answers to JS engine questions.]

My Comments below. People like brendan and shaver might have
better suggestions.

Peter Wilson wrote:
> 
> I'm seeking some clarification/explanation/help with calling into the
> JavaScript engine from 'C'. First of all - generally I;ve had no problems
> with the engine and found it very easy to integrate - however I'm having
> some problems getting the variables created where I expect them when I
> recursively call the engine. Example:
> 
> 1.    I register a JavaScript function with a native implementation - say
> 'MyFunction'
> 2.    'C' calls JavaScript engine to run a script.
> 3.    The script calls 'MyFunction'
> 4.    My Function somehow gets another JavaScript fragment (maybe from a
> file, or from a string in a similar way to eval)
> 5.    'MyFunction' wants to call back (recursively) into the engine to
> execute this fragment.
> 
> There are a number of options I'd like to have when I execute this script:
> Option 1. Executes 'like a JavaScript' function
> To do this I'd like to create a call object and have that used as the target
> for 'var' declarations, rather than the global object. I can't work out how
> to do this. Internally the engine only seems to do this if the 'js_Execute'
> function is called with a 'down' object - which is the stack frame of the
> caller. I can't see a way of doing this from the public interface.

I'm thinking JS_CompileFunction then JS_CallFunction.

> 
> Option 2. Execute the script fragment 'as though it were a statement in the
> current parent script'.
> In this case the call object is that of the parent - if its in a function -
> or the global object otherwise. The creation point for 'var' declarations is
> that of the parent calling script. Can't see how to do this - the 'eval'
> implementation does this internally I think by getting the appropriate
> objects then passing them to 'js_Execute'. Possible option is the debug
> interface 'JS_EvaluateInStackFrame' function - but actually this is not a
> debug operation - rather something I would like the option of doing from my
> application.

I'd go with JS_EvaluateInStackFrame. You get the stack frame via
JS_FrameIterator. This seems like a fairly unusual thing to be
doing. From the point of view of the JS code that calls your
native function, the native function will be potentially causing
side effects to its locals even though the locals where not
passed to the function. Personally, I wouldn't worry about using
things in jsdbgapi.h if I'm planning on doing something this
unusual.

John.

> 
> Option 3. Exeucte in the global context - even if the parent was nested 'n'
> levels deep in function calls. This seems to be the only option I can
> legitimately do from the existing API.
> 
> I'm happy to explain what I'm trying to do and why - but that would make
> this message even longer!
> 
> Comments greatly appreciated!
> thanks in advance
> Peter Wilson

Reply via email to