Description
Doc: http://doc.trolltech.com/4.7/qscriptengine.html#newQObject-2
The use case for this overload is to "promote" an existing script object to a QObject wrapper object. For example, a factory method for QFoo could look like this:
static QScriptValue constructFoo(QScriptContext *ctx, QScriptEngine *eng) { if (ctx->isCalledAsConstructor()) return eng->newQObject(ctx->thisObject(), new QFoo()); return ctx->throwError("Please use 'new' operator"); }
When the script "new QFoo" is executed, the VM constructs a standard JS object and passes it as the thisObject() to constructFoo. We then effectively change the type ([[Class]] in ECMA spec speak) to be a QObject wrapper. This avoids a wasted object allocation (calling the "normal" newQObject(), which returns a brand new object), and also means we don't have to worry about preserving the prototype relationship (the _proto_ of the thisObject() will be QFoo.prototype, as expected).
The problem with this API is that this is not how JavaScriptCore or V8 implement objects. There is no mutable [[Class]] property like we had in the QtScript "classic" back-end. Once an object has been created, its class can't be changed.
The way we solve this currently is by implementing a special QScriptObject class which has a delegate (corresponding to [[Class]]) which provides the necessary level of indirection. However, this is slow, and it only works for objects that are subclasses of QScriptObject (i.e. those we implement), not any JS object. We'd like to get rid of this, and have the same constraint as JavaScriptCore/V8; once an object has been constructed, its [[Class]] can't be changed.
Attachments
Issue Links
- relates to
-
QTBUG-15849 Deprecate QScriptEngine::newVariant() overload that takes existing script object as argument
-
- Closed
-