[Qt-interest] Correct use of QScriptContext, and simulating load().

David Wilson dw at botanicus.net
Sat Apr 24 01:36:14 CEST 2010


Hi there,

I am working on an experiment to port a proprietary scripting engine
to QtScript, and find myself confused by the correct use of
QScriptContext, especially regarding generating backtraces and the
activation object.

One of the features I need is a JavaScript-visible load() function, in
the style of the same function provided by Mozilla Rhino's JavaScript
engine. load() accepts a file name and evaluates that file's contents
in the local scope, similarly to eval("contents-of-file") but with the
compiled code correctly associated with the original source file name.

In order to make this work using QScriptEngine::evaluate(), currently
I seem to need to pushContext() once at startup in order to get a
reference to QScriptContext::activationObject(), and then for every
(possibly recursive) call to load(), call pushContext() and
setActivationObject() using the reference I previously got a hold of.

If I don't do this, variables declared with "var ..." don't appear in
the scope of the script that executed the load()  (the load appears to
get a fresh activation object).

This seems to all work well, except when I call
uncaughtExceptionBacktrace() after evaluate(), I am only shown the
trace up to the start of, it appears, the last pushContext().

Looking at QScriptEngineDebugger, it seems that I must walk the stack
of QScriptContexts, manually rebuilding the stack trace, before
returning it to the user, however doing so seems "odd", and makes me
wonder if my assumptions elsewhere are correct. :)

Does anyone have experience of this?

Thanks,


David



More information about the Qt-interest-old mailing list