diff options
author | Kent Hansen <khansen@trolltech.com> | 2009-07-27 17:42:26 (GMT) |
---|---|---|
committer | Kent Hansen <khansen@trolltech.com> | 2009-07-27 18:01:19 (GMT) |
commit | 8923339fa086cbf6adc404fb18dcda6c1206985d (patch) | |
tree | 13ad42fc95c63e9f49a971a42fab7ddb1c6af61c /src/script/bridge/qscriptvariant.cpp | |
parent | 117802b5ca71478d01bb79f88aa3596729b0a590 (diff) | |
download | Qt-8923339fa086cbf6adc404fb18dcda6c1206985d.zip Qt-8923339fa086cbf6adc404fb18dcda6c1206985d.tar.gz Qt-8923339fa086cbf6adc404fb18dcda6c1206985d.tar.bz2 |
make QScriptEngine::setGlobalObject() work to some extent
JSC requires that the global object is actually a
JSGlobalObject instance, whereas QScriptEngine::setGlobalObject()
allows any object to be set as the global object. The way we
solve this is by proxying from an internal global object to the
custom (user-set) object.
We need to take care that the internal global object is never
actually exposed through our API; a brilliantly named helper
function, toUsableValue(), makes that happen.
Evaluating "var a = 10" with a custom global object doesn't work
yet; the variable always ends up in the internal Global Object.
For variable assignments, JSC appears to bypass the normal
JSObject::put() and instead use
JSGlobalObject::copyGlobals{From,To}(), which means I can't
intercept and proxy the assignments.
This commit enough to get the Context2D example working. There's
another bug with iteration of the built-in Global Object's
properties (non-enumerable properties are always skipped by the
JSC C++ API, whereas with QScriptValueIterator they should not
be), but that's a totally separate issue.
Diffstat (limited to 'src/script/bridge/qscriptvariant.cpp')
-rw-r--r-- | src/script/bridge/qscriptvariant.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/script/bridge/qscriptvariant.cpp b/src/script/bridge/qscriptvariant.cpp index 46da70a..752dd09 100644 --- a/src/script/bridge/qscriptvariant.cpp +++ b/src/script/bridge/qscriptvariant.cpp @@ -13,6 +13,9 @@ #ifndef QT_NO_SCRIPT +#include "../api/qscriptengine.h" +#include "../api/qscriptengine_p.h" + #include "Error.h" #include "PrototypeFunction.h" #include "JSString.h" @@ -28,6 +31,7 @@ namespace QScript { JSC::UString qtStringToJSCUString(const QString &str); +QScriptEnginePrivate *scriptEngineFromExec(JSC::ExecState*); QVariantDelegate::QVariantDelegate(const QVariant &value) : m_value(value) @@ -56,6 +60,8 @@ QScriptObjectDelegate::Type QVariantDelegate::type() const static JSC::JSValue JSC_HOST_CALL variantProtoFuncToString(JSC::ExecState *exec, JSC::JSObject*, JSC::JSValue thisValue, const JSC::ArgList&) { + QScriptEnginePrivate *engine = scriptEngineFromExec(exec); + thisValue = engine->toUsableValue(thisValue); if (!thisValue.isObject(&QScriptObject::info)) return throwError(exec, JSC::TypeError, "This object is not a QVariant"); QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(thisValue))->delegate(); @@ -69,6 +75,8 @@ static JSC::JSValue JSC_HOST_CALL variantProtoFuncToString(JSC::ExecState *exec, static JSC::JSValue JSC_HOST_CALL variantProtoFuncValueOf(JSC::ExecState *exec, JSC::JSObject*, JSC::JSValue thisValue, const JSC::ArgList&) { + QScriptEnginePrivate *engine = scriptEngineFromExec(exec); + thisValue = engine->toUsableValue(thisValue); if (!thisValue.isObject(&QScriptObject::info)) return throwError(exec, JSC::TypeError); QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(thisValue))->delegate(); |