diff options
Diffstat (limited to 'src/script/api')
-rw-r--r-- | src/script/api/qscriptcontext.cpp | 14 | ||||
-rw-r--r-- | src/script/api/qscriptcontextinfo.cpp | 2 | ||||
-rw-r--r-- | src/script/api/qscriptengine.cpp | 1100 | ||||
-rw-r--r-- | src/script/api/qscriptengine.h | 2 | ||||
-rw-r--r-- | src/script/api/qscriptengine_p.h | 558 | ||||
-rw-r--r-- | src/script/api/qscriptengineagent.cpp | 10 | ||||
-rw-r--r-- | src/script/api/qscriptengineagent_p.h | 7 | ||||
-rw-r--r-- | src/script/api/qscriptprogram.cpp | 11 | ||||
-rw-r--r-- | src/script/api/qscriptprogram_p.h | 4 | ||||
-rw-r--r-- | src/script/api/qscriptstring.cpp | 6 | ||||
-rw-r--r-- | src/script/api/qscriptvalue.cpp | 609 | ||||
-rw-r--r-- | src/script/api/qscriptvalue_p.h | 27 | ||||
-rw-r--r-- | src/script/api/qscriptvalueiterator.cpp | 72 |
13 files changed, 1563 insertions, 859 deletions
diff --git a/src/script/api/qscriptcontext.cpp b/src/script/api/qscriptcontext.cpp index b1732ee..639af80 100644 --- a/src/script/api/qscriptcontext.cpp +++ b/src/script/api/qscriptcontext.cpp @@ -161,6 +161,7 @@ QScriptContext::QScriptContext() QScriptValue QScriptContext::throwValue(const QScriptValue &value) { JSC::CallFrame *frame = QScriptEnginePrivate::frameForContext(this); + QScript::APIShim shim(QScript::scriptEngineFromExec(frame)); JSC::JSValue jscValue = QScript::scriptEngineFromExec(frame)->scriptValueToJSCValue(value); frame->setException(jscValue); return value; @@ -183,6 +184,7 @@ QScriptValue QScriptContext::throwValue(const QScriptValue &value) QScriptValue QScriptContext::throwError(Error error, const QString &text) { JSC::CallFrame *frame = QScriptEnginePrivate::frameForContext(this); + QScript::APIShim shim(QScript::scriptEngineFromExec(frame)); JSC::ErrorType jscError = JSC::GeneralError; switch (error) { case UnknownError: @@ -218,6 +220,7 @@ QScriptValue QScriptContext::throwError(Error error, const QString &text) QScriptValue QScriptContext::throwError(const QString &text) { JSC::CallFrame *frame = QScriptEnginePrivate::frameForContext(this); + QScript::APIShim shim(QScript::scriptEngineFromExec(frame)); JSC::JSObject *result = JSC::throwError(frame, JSC::GeneralError, text); return QScript::scriptEngineFromExec(frame)->scriptValueFromJSCValue(result); } @@ -265,6 +268,7 @@ QScriptValue QScriptContext::argument(int index) const QScriptValue QScriptContext::callee() const { const JSC::CallFrame *frame = QScriptEnginePrivate::frameForContext(this); + QScript::APIShim shim(QScript::scriptEngineFromExec(frame)); return QScript::scriptEngineFromExec(frame)->scriptValueFromJSCValue(frame->callee()); } @@ -286,6 +290,7 @@ QScriptValue QScriptContext::callee() const QScriptValue QScriptContext::argumentsObject() const { JSC::CallFrame *frame = const_cast<JSC::ExecState*>(QScriptEnginePrivate::frameForContext(this)); + QScript::APIShim shim(QScript::scriptEngineFromExec(frame)); if (frame == frame->lexicalGlobalObject()->globalExec()) { // <global> context doesn't have arguments. return an empty object @@ -322,6 +327,7 @@ QScriptValue QScriptContext::argumentsObject() const bool QScriptContext::isCalledAsConstructor() const { JSC::CallFrame *frame = const_cast<JSC::ExecState*>(QScriptEnginePrivate::frameForContext(this)); + QScript::APIShim shim(QScript::scriptEngineFromExec(frame)); //For native functions, look up flags. uint flags = QScriptEnginePrivate::contextFlags(frame); @@ -355,6 +361,7 @@ bool QScriptContext::isCalledAsConstructor() const QScriptContext *QScriptContext::parentContext() const { const JSC::CallFrame *frame = QScriptEnginePrivate::frameForContext(this); + QScript::APIShim shim(QScript::scriptEngineFromExec(frame)); JSC::CallFrame *callerFrame = frame->callerFrame()->removeHostCallFrameFlag(); return QScriptEnginePrivate::contextForFrame(callerFrame); } @@ -412,6 +419,7 @@ void QScriptContext::setReturnValue(const QScriptValue &result) QScriptValue QScriptContext::activationObject() const { JSC::CallFrame *frame = const_cast<JSC::ExecState*>(QScriptEnginePrivate::frameForContext(this)); + QScript::APIShim shim(QScript::scriptEngineFromExec(frame)); JSC::JSObject *result = 0; uint flags = QScriptEnginePrivate::contextFlags(frame); @@ -477,6 +485,7 @@ void QScriptContext::setActivationObject(const QScriptValue &activation) } JSC::CallFrame *frame = QScriptEnginePrivate::frameForContext(this); QScriptEnginePrivate *engine = QScript::scriptEngineFromExec(frame); + QScript::APIShim shim(engine); JSC::JSObject *object = JSC::asObject(engine->scriptValueToJSCValue(activation)); if (object == engine->originalGlobalObjectProxy) object = engine->originalGlobalObject(); @@ -521,6 +530,7 @@ QScriptValue QScriptContext::thisObject() const { JSC::CallFrame *frame = const_cast<JSC::ExecState*>(QScriptEnginePrivate::frameForContext(this)); QScriptEnginePrivate *engine = QScript::scriptEngineFromExec(frame); + QScript::APIShim shim(engine); JSC::JSValue result = engine->thisForContext(frame); if (!result || result.isNull()) result = frame->globalThisValue(); @@ -536,6 +546,7 @@ QScriptValue QScriptContext::thisObject() const void QScriptContext::setThisObject(const QScriptValue &thisObject) { JSC::CallFrame *frame = QScriptEnginePrivate::frameForContext(this); + QScript::APIShim shim(QScript::scriptEngineFromExec(frame)); if (!thisObject.isObject()) return; if (thisObject.engine() != engine()) { @@ -662,6 +673,7 @@ QScriptValueList QScriptContext::scopeChain() const activationObject(); //ensure the creation of the normal scope for native context const JSC::CallFrame *frame = QScriptEnginePrivate::frameForContext(this); QScriptEnginePrivate *engine = QScript::scriptEngineFromExec(frame); + QScript::APIShim shim(engine); QScriptValueList result; JSC::ScopeChainNode *node = frame->scopeChain(); JSC::ScopeChainIterator it(node); @@ -700,6 +712,7 @@ void QScriptContext::pushScope(const QScriptValue &object) } JSC::CallFrame *frame = QScriptEnginePrivate::frameForContext(this); QScriptEnginePrivate *engine = QScript::scriptEngineFromExec(frame); + QScript::APIShim shim(engine); JSC::JSObject *jscObject = JSC::asObject(engine->scriptValueToJSCValue(object)); if (jscObject == engine->originalGlobalObjectProxy) jscObject = engine->originalGlobalObject(); @@ -733,6 +746,7 @@ QScriptValue QScriptContext::popScope() JSC::ScopeChainNode *scope = frame->scopeChain(); Q_ASSERT(scope != 0); QScriptEnginePrivate *engine = QScript::scriptEngineFromExec(frame); + QScript::APIShim shim(engine); QScriptValue result = engine->scriptValueFromJSCValue(scope->object); if (!scope->next) { // We cannot have a null scope chain, so just zap the object pointer. diff --git a/src/script/api/qscriptcontextinfo.cpp b/src/script/api/qscriptcontextinfo.cpp index cc5fcc1..ebb1770 100644 --- a/src/script/api/qscriptcontextinfo.cpp +++ b/src/script/api/qscriptcontextinfo.cpp @@ -181,7 +181,7 @@ QScriptContextInfoPrivate::QScriptContextInfoPrivate(const QScriptContext *conte // Get the others informations: JSC::JSObject *callee = frame->callee(); if (callee && callee->inherits(&JSC::InternalFunction::info)) - functionName = JSC::asInternalFunction(callee)->name(&frame->globalData()); + functionName = JSC::asInternalFunction(callee)->name(frame); if (callee && callee->inherits(&JSC::JSFunction::info)) { functionType = QScriptContextInfo::ScriptFunction; JSC::FunctionExecutable *body = JSC::asFunction(callee)->jsExecutable(); diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index 2422108..2417d80 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -24,7 +24,6 @@ #include "config.h" #include "qscriptengine.h" #include "qscriptsyntaxchecker_p.h" -#include "qnumeric.h" #include "qscriptengine_p.h" #include "qscriptengineagent_p.h" @@ -41,12 +40,11 @@ #include <QtCore/qstringlist.h> #include <QtCore/qmetaobject.h> +#include <math.h> + #include "Error.h" -#include "JSArray.h" #include "JSLock.h" #include "Interpreter.h" -#include "DateConstructor.h" -#include "RegExpConstructor.h" #include "PrototypeFunction.h" #include "InitializeThreading.h" @@ -56,11 +54,10 @@ #include "TimeoutChecker.h" #include "JSFunction.h" #include "Parser.h" +#include "PropertyNameArray.h" #include "Operations.h" -#include "utils/qscriptdate_p.h" #include "bridge/qscriptfunction_p.h" -#include "bridge/qscriptobject_p.h" #include "bridge/qscriptclassobject_p.h" #include "bridge/qscriptvariant_p.h" #include "bridge/qscriptqobject_p.h" @@ -263,6 +260,22 @@ QT_BEGIN_NAMESPACE whether an engine is currently running a script by calling isEvaluating(). + \section1 Garbage Collection + + Qt Script objects may be garbage collected when they are no longer + referenced. There is no guarantee as to when automatic garbage + collection will take place. + + The collectGarbage() function can be called to explicitly request + garbage collection. + + The reportAdditionalMemoryCost() function can be called to indicate + that a Qt Script object occupies memory that isn't managed by the + scripting environment. Reporting the additional cost makes it more + likely that the garbage collector will be triggered. This can be + useful, for example, when many custom, native Qt Script objects are + allocated. + \section1 Core Debugging/Tracing Facilities Since Qt 4.4, you can be notified of events pertaining to script @@ -295,6 +308,7 @@ QT_BEGIN_NAMESPACE \value ExcludeSuperClassProperties The script object will not expose properties inherited from the superclass. \value ExcludeSuperClassContents Shorthand form for ExcludeSuperClassMethods | ExcludeSuperClassProperties \value ExcludeDeleteLater The script object will not expose the QObject::deleteLater() slot. + \value ExcludeSlots The script object will not expose the QObject's slots. \value AutoCreateDynamicProperties Properties that don't already exist in the QObject will be created as dynamic properties of that object, rather than as properties of the script object. \value PreferExistingWrapperObject If a wrapper object with the requested configuration already exists, return that object. \value SkipMethodsInEnumeration Don't include methods (signals and slots) when enumerating the object's properties. @@ -328,6 +342,89 @@ public: namespace QScript { +static const qsreal D32 = 4294967296.0; + +qint32 ToInt32(qsreal n) +{ + if (qIsNaN(n) || qIsInf(n) || (n == 0)) + return 0; + + qsreal sign = (n < 0) ? -1.0 : 1.0; + qsreal abs_n = fabs(n); + + n = ::fmod(sign * ::floor(abs_n), D32); + const double D31 = D32 / 2.0; + + if (sign == -1 && n < -D31) + n += D32; + + else if (sign != -1 && n >= D31) + n -= D32; + + return qint32 (n); +} + +quint32 ToUInt32(qsreal n) +{ + if (qIsNaN(n) || qIsInf(n) || (n == 0)) + return 0; + + qsreal sign = (n < 0) ? -1.0 : 1.0; + qsreal abs_n = fabs(n); + + n = ::fmod(sign * ::floor(abs_n), D32); + + if (n < 0) + n += D32; + + return quint32 (n); +} + +quint16 ToUInt16(qsreal n) +{ + static const qsreal D16 = 65536.0; + + if (qIsNaN(n) || qIsInf(n) || (n == 0)) + return 0; + + qsreal sign = (n < 0) ? -1.0 : 1.0; + qsreal abs_n = fabs(n); + + n = ::fmod(sign * ::floor(abs_n), D16); + + if (n < 0) + n += D16; + + return quint16 (n); +} + +qsreal ToInteger(qsreal n) +{ + if (qIsNaN(n)) + return 0; + + if (n == 0 || qIsInf(n)) + return n; + + int sign = n < 0 ? -1 : 1; + return sign * ::floor(::fabs(n)); +} + +#ifdef Q_CC_MSVC +// MSVC2008 crashes if these are inlined. + +QString ToString(qsreal value) +{ + return JSC::UString::from(value); +} + +qsreal ToNumber(const QString &value) +{ + return ((JSC::UString)value).toDouble(); +} + +#endif + void GlobalClientData::mark(JSC::MarkStack& markStack) { engine->mark(markStack); @@ -482,11 +579,9 @@ JSC::JSValue JSC_HOST_CALL functionDisconnect(JSC::ExecState *exec, JSC::JSObjec if (isFunction(arg1)) slot = arg1; else { - // ### don't go via QScriptValue QScript::SaveFrameHelper saveFrame(engine, exec); - QScriptValue tmp = engine->scriptValueFromJSCValue(arg0); - QString propertyName(arg1.toString(exec)); - slot = engine->scriptValueToJSCValue(tmp.property(propertyName, QScriptValue::ResolvePrototype)); + JSC::UString propertyName = QScriptEnginePrivate::toString(exec, arg1); + slot = QScriptEnginePrivate::property(exec, arg0, propertyName, QScriptValue::ResolvePrototype); } } @@ -566,11 +661,9 @@ JSC::JSValue JSC_HOST_CALL functionConnect(JSC::ExecState *exec, JSC::JSObject * if (isFunction(arg1)) slot = arg1; else { - // ### don't go via QScriptValue QScript::SaveFrameHelper saveFrame(engine, exec); - QScriptValue tmp = engine->scriptValueFromJSCValue(arg0); - QString propertyName = arg1.toString(exec); - slot = engine->scriptValueToJSCValue(tmp.property(propertyName, QScriptValue::ResolvePrototype)); + JSC::UString propertyName = QScriptEnginePrivate::toString(exec, arg1); + slot = QScriptEnginePrivate::property(exec, arg0, propertyName, QScriptValue::ResolvePrototype); } } @@ -647,19 +740,19 @@ JSC::JSValue JSC_HOST_CALL functionQsTranslate(JSC::ExecState *exec, JSC::JSObje if ((args.size() > 4) && !args.at(4).isNumber()) return JSC::throwError(exec, JSC::GeneralError, "qsTranslate(): fifth argument (n) must be a number"); #ifndef QT_NO_QOBJECT - QString context(args.at(0).toString(exec)); + JSC::UString context = args.at(0).toString(exec); #endif - QString text(args.at(1).toString(exec)); + JSC::UString text = args.at(1).toString(exec); #ifndef QT_NO_QOBJECT - QString comment; + JSC::UString comment; if (args.size() > 2) comment = args.at(2).toString(exec); QCoreApplication::Encoding encoding = QCoreApplication::CodecForTr; if (args.size() > 3) { - QString encStr(args.at(3).toString(exec)); - if (encStr == QLatin1String("CodecForTr")) + JSC::UString encStr = args.at(3).toString(exec); + if (encStr == "CodecForTr") encoding = QCoreApplication::CodecForTr; - else if (encStr == QLatin1String("UnicodeUTF8")) + else if (encStr == "UnicodeUTF8") encoding = QCoreApplication::UnicodeUTF8; else return JSC::throwError(exec, JSC::GeneralError, QString::fromLatin1("qsTranslate(): invalid encoding '%s'").arg(encStr)); @@ -668,11 +761,11 @@ JSC::JSValue JSC_HOST_CALL functionQsTranslate(JSC::ExecState *exec, JSC::JSObje if (args.size() > 4) n = args.at(4).toInt32(exec); #endif - QString result; + JSC::UString result; #ifndef QT_NO_QOBJECT - result = QCoreApplication::translate(context.toLatin1().constData(), - text.toLatin1().constData(), - comment.toLatin1().constData(), + result = QCoreApplication::translate(QScript::convertToLatin1(context).constData(), + QScript::convertToLatin1(text).constData(), + QScript::convertToLatin1(comment).constData(), encoding, n); #else result = text; @@ -698,25 +791,25 @@ JSC::JSValue JSC_HOST_CALL functionQsTr(JSC::ExecState *exec, JSC::JSObject*, JS if ((args.size() > 2) && !args.at(2).isNumber()) return JSC::throwError(exec, JSC::GeneralError, "qsTranslate(): third argument (n) must be a number"); #ifndef QT_NO_QOBJECT - QString context; + JSC::UString context; QScriptContext *ctx = QScriptEnginePrivate::contextForFrame(exec); if (ctx && ctx->parentContext()) context = QFileInfo(QScriptContextInfo(ctx->parentContext()).fileName()).baseName(); #endif - QString text(args.at(0).toString(exec)); + JSC::UString text = args.at(0).toString(exec); #ifndef QT_NO_QOBJECT - QString comment; + JSC::UString comment; if (args.size() > 1) comment = args.at(1).toString(exec); int n = -1; if (args.size() > 2) n = args.at(2).toInt32(exec); #endif - QString result; + JSC::UString result; #ifndef QT_NO_QOBJECT - result = QCoreApplication::translate(context.toLatin1().constData(), - text.toLatin1().constData(), - comment.toLatin1().constData(), + result = QCoreApplication::translate(QScript::convertToLatin1(context).constData(), + QScript::convertToLatin1(text).constData(), + QScript::convertToLatin1(comment).constData(), QCoreApplication::CodecForTr, n); #else result = text; @@ -782,7 +875,7 @@ QScriptEnginePrivate::QScriptEnginePrivate() return; } JSC::initializeThreading(); - + JSC::IdentifierTable *oldTable = JSC::currentIdentifierTable(); globalData = JSC::JSGlobalData::create().releaseRef(); globalData->clientData = new QScript::GlobalClientData(this); JSC::JSGlobalObject *globalObject = new (globalData)QScript::GlobalObject(); @@ -818,10 +911,13 @@ QScriptEnginePrivate::QScriptEnginePrivate() activeAgent = 0; agentLineNumber = -1; processEventsInterval = -1; + JSC::setCurrentIdentifierTable(oldTable); } QScriptEnginePrivate::~QScriptEnginePrivate() { + QScript::APIShim shim(this); + //disconnect all loadedScripts and generate all jsc::debugger::scriptUnload events QHash<intptr_t,QScript::UStringSourceProviderWithFeedback*>::const_iterator it; for (it = loadedScripts.constBegin(); it != loadedScripts.constEnd(); ++it) @@ -844,23 +940,15 @@ QScriptEnginePrivate::~QScriptEnginePrivate() } } -QScriptValue QScriptEnginePrivate::scriptValueFromVariant(const QVariant &v) -{ - Q_Q(QScriptEngine); - QScriptValue result = q->create(v.userType(), v.data()); - Q_ASSERT(result.isValid()); - return result; -} - -QVariant QScriptEnginePrivate::scriptValueToVariant(const QScriptValue &value, int targetType) +QVariant QScriptEnginePrivate::jscValueToVariant(JSC::ExecState *exec, JSC::JSValue value, int targetType) { QVariant v(targetType, (void *)0); - if (QScriptEnginePrivate::convert(value, targetType, v.data(), this)) + if (convertValue(exec, value, targetType, v.data())) return v; if (uint(targetType) == QVariant::LastType) - return value.toVariant(); - if (value.isVariant()) { - v = value.toVariant(); + return toVariant(exec, value); + if (isVariant(value)) { + v = variantValue(value); if (v.canConvert(QVariant::Type(targetType))) { v.convert(QVariant::Type(targetType)); return v; @@ -871,88 +959,60 @@ QVariant QScriptEnginePrivate::scriptValueToVariant(const QScriptValue &value, i return QVariant(targetType, *reinterpret_cast<void* *>(v.data())); } } - return QVariant(); } -JSC::JSValue QScriptEnginePrivate::jscValueFromVariant(const QVariant &v) +JSC::JSValue QScriptEnginePrivate::arrayFromStringList(JSC::ExecState *exec, const QStringList &lst) { - // ### it's inefficient to convert to QScriptValue and then to JSValue - QScriptValue vv = scriptValueFromVariant(v); - QScriptValuePrivate *p = QScriptValuePrivate::get(vv); - switch (p->type) { - case QScriptValuePrivate::JavaScriptCore: - return p->jscValue; - case QScriptValuePrivate::Number: - return JSC::jsNumber(currentFrame, p->numberValue); - case QScriptValuePrivate::String: { - JSC::UString str = p->stringValue; - return JSC::jsString(currentFrame, str); - } - } - return JSC::JSValue(); -} - -QVariant QScriptEnginePrivate::jscValueToVariant(JSC::JSValue value, int targetType) -{ - // ### it's inefficient to convert to QScriptValue and then to QVariant - return scriptValueToVariant(scriptValueFromJSCValue(value), targetType); -} - -QScriptValue QScriptEnginePrivate::arrayFromStringList(const QStringList &lst) -{ - Q_Q(QScriptEngine); - QScriptValue arr = q->newArray(lst.size()); + JSC::JSValue arr = newArray(exec, lst.size()); for (int i = 0; i < lst.size(); ++i) - arr.setProperty(i, QScriptValue(q, lst.at(i))); + setProperty(exec, arr, i, JSC::jsString(exec, lst.at(i))); return arr; } -QStringList QScriptEnginePrivate::stringListFromArray(const QScriptValue &arr) +QStringList QScriptEnginePrivate::stringListFromArray(JSC::ExecState *exec, JSC::JSValue arr) { QStringList lst; - uint len = arr.property(QLatin1String("length")).toUInt32(); + uint len = toUInt32(exec, property(exec, arr, exec->propertyNames().length)); for (uint i = 0; i < len; ++i) - lst.append(arr.property(i).toString()); + lst.append(toString(exec, property(exec, arr, i))); return lst; } -QScriptValue QScriptEnginePrivate::arrayFromVariantList(const QVariantList &lst) +JSC::JSValue QScriptEnginePrivate::arrayFromVariantList(JSC::ExecState *exec, const QVariantList &lst) { - Q_Q(QScriptEngine); - QScriptValue arr = q->newArray(lst.size()); + JSC::JSValue arr = newArray(exec, lst.size()); for (int i = 0; i < lst.size(); ++i) - arr.setProperty(i, scriptValueFromVariant(lst.at(i))); + setProperty(exec, arr, i, jscValueFromVariant(exec, lst.at(i))); return arr; } -QVariantList QScriptEnginePrivate::variantListFromArray(const QScriptValue &arr) +QVariantList QScriptEnginePrivate::variantListFromArray(JSC::ExecState *exec, JSC::JSValue arr) { QVariantList lst; - uint len = arr.property(QLatin1String("length")).toUInt32(); + uint len = toUInt32(exec, property(exec, arr, exec->propertyNames().length)); for (uint i = 0; i < len; ++i) - lst.append(arr.property(i).toVariant()); + lst.append(toVariant(exec, property(exec, arr, i))); return lst; } -QScriptValue QScriptEnginePrivate::objectFromVariantMap(const QVariantMap &vmap) +JSC::JSValue QScriptEnginePrivate::objectFromVariantMap(JSC::ExecState *exec, const QVariantMap &vmap) { - Q_Q(QScriptEngine); - QScriptValue obj = q->newObject(); + JSC::JSValue obj = JSC::constructEmptyObject(exec); QVariantMap::const_iterator it; for (it = vmap.constBegin(); it != vmap.constEnd(); ++it) - obj.setProperty(it.key(), scriptValueFromVariant(it.value())); + setProperty(exec, obj, it.key(), jscValueFromVariant(exec, it.value())); return obj; } -QVariantMap QScriptEnginePrivate::variantMapFromObject(const QScriptValue &obj) +QVariantMap QScriptEnginePrivate::variantMapFromObject(JSC::ExecState *exec, JSC::JSValue obj) { + JSC::PropertyNameArray propertyNames(exec); + JSC::asObject(obj)->getOwnPropertyNames(exec, propertyNames, JSC::IncludeDontEnumProperties); QVariantMap vmap; - QScriptValueIterator it(obj); - while (it.hasNext()) { - it.next(); - vmap.insert(it.name(), it.value().toVariant()); - } + JSC::PropertyNameArray::const_iterator it = propertyNames.begin(); + for( ; it != propertyNames.end(); ++it) + vmap.insert(it->ustring(), toVariant(exec, property(exec, obj, *it))); return vmap; } @@ -1150,7 +1210,14 @@ bool QScriptEnginePrivate::isCollecting() const void QScriptEnginePrivate::collectGarbage() { JSC::JSLock lock(false); - globalData->heap.collect(); + QScript::APIShim shim(this); + globalData->heap.collectAllGarbage(); +} + +void QScriptEnginePrivate::reportAdditionalMemoryCost(int size) +{ + if (size > 0) + globalData->heap.reportExtraMemoryCost(size); } QScript::TimeoutCheckerProxy *QScriptEnginePrivate::timeoutChecker() const @@ -1181,7 +1248,7 @@ JSC::JSValue QScriptEnginePrivate::evaluateHelper(JSC::ExecState *exec, intptr_t debugger->evaluateStart(sourceId); q->clearExceptions(); - JSC::DynamicGlobalObjectScope dynamicGlobalObjectScope(exec, exec->scopeChain()->globalObject()); + JSC::DynamicGlobalObjectScope dynamicGlobalObjectScope(exec, exec->scopeChain()->globalObject); if (compile) { JSC::JSObject* error = executable->compile(exec, exec->scopeChain()); @@ -1285,13 +1352,13 @@ JSC::JSValue QScriptEnginePrivate::newQMetaObject( return result; } -bool QScriptEnginePrivate::convertToNativeQObject(const QScriptValue &value, +bool QScriptEnginePrivate::convertToNativeQObject(JSC::ExecState *exec, JSC::JSValue value, const QByteArray &targetType, void **result) { if (!targetType.endsWith('*')) return false; - if (QObject *qobject = value.toQObject()) { + if (QObject *qobject = toQObject(exec, value)) { int start = targetType.startsWith("const ") ? 6 : 0; QByteArray className = targetType.mid(start, targetType.size()-start-1); if (void *instance = qobject->qt_metacast(className)) { @@ -1432,6 +1499,339 @@ void QScriptEnginePrivate::detachAllRegisteredScriptStrings() registeredScriptStrings = 0; } +#ifndef QT_NO_REGEXP + +extern QString qt_regexp_toCanonical(const QString &, QRegExp::PatternSyntax); + +JSC::JSValue QScriptEnginePrivate::newRegExp(JSC::ExecState *exec, const QRegExp ®exp) +{ + JSC::JSValue buf[2]; + JSC::ArgList args(buf, sizeof(buf)); + + //convert the pattern to a ECMAScript pattern + QString pattern = qt_regexp_toCanonical(regexp.pattern(), regexp.patternSyntax()); + if (regexp.isMinimal()) { + QString ecmaPattern; + int len = pattern.length(); + ecmaPattern.reserve(len); + int i = 0; + const QChar *wc = pattern.unicode(); + bool inBracket = false; + while (i < len) { + QChar c = wc[i++]; + ecmaPattern += c; + switch (c.unicode()) { + case '?': + case '+': + case '*': + case '}': + if (!inBracket) + ecmaPattern += QLatin1Char('?'); + break; + case '\\': + if (i < len) + ecmaPattern += wc[i++]; + break; + case '[': + inBracket = true; + break; + case ']': + inBracket = false; + break; + default: + break; + } + } + pattern = ecmaPattern; + } + + JSC::UString jscPattern = pattern; + QString flags; + if (regexp.caseSensitivity() == Qt::CaseInsensitive) + flags.append(QLatin1Char('i')); + JSC::UString jscFlags = flags; + buf[0] = JSC::jsString(exec, jscPattern); + buf[1] = JSC::jsString(exec, jscFlags); + return JSC::constructRegExp(exec, args); +} + +#endif + +JSC::JSValue QScriptEnginePrivate::newRegExp(JSC::ExecState *exec, const QString &pattern, const QString &flags) +{ + JSC::JSValue buf[2]; + JSC::ArgList args(buf, sizeof(buf)); + JSC::UString jscPattern = pattern; + QString strippedFlags; + if (flags.contains(QLatin1Char('i'))) + strippedFlags += QLatin1Char('i'); + if (flags.contains(QLatin1Char('m'))) + strippedFlags += QLatin1Char('m'); + if (flags.contains(QLatin1Char('g'))) + strippedFlags += QLatin1Char('g'); + JSC::UString jscFlags = strippedFlags; + buf[0] = JSC::jsString(exec, jscPattern); + buf[1] = JSC::jsString(exec, jscFlags); + return JSC::constructRegExp(exec, args); +} + +JSC::JSValue QScriptEnginePrivate::newVariant(const QVariant &value) +{ + QScriptObject *obj = new (currentFrame) QScriptObject(variantWrapperObjectStructure); + obj->setDelegate(new QScript::QVariantDelegate(value)); + JSC::JSValue proto = defaultPrototype(value.userType()); + if (proto) + obj->setPrototype(proto); + return obj; +} + +JSC::JSValue QScriptEnginePrivate::newVariant(JSC::JSValue objectValue, + const QVariant &value) +{ + if (!isObject(objectValue)) + return newVariant(value); + JSC::JSObject *jscObject = JSC::asObject(objectValue); + if (!jscObject->inherits(&QScriptObject::info)) { + qWarning("QScriptEngine::newVariant(): changing class of non-QScriptObject not supported"); + return JSC::JSValue(); + } + QScriptObject *jscScriptObject = static_cast<QScriptObject*>(jscObject); + if (!isVariant(objectValue)) { + jscScriptObject->setDelegate(new QScript::QVariantDelegate(value)); + } else { + setVariantValue(objectValue, value); + } + return objectValue; +} + +#ifndef QT_NO_REGEXP + +QRegExp QScriptEnginePrivate::toRegExp(JSC::ExecState *exec, JSC::JSValue value) +{ + if (!isRegExp(value)) + return QRegExp(); + QString pattern = toString(exec, property(exec, value, "source", QScriptValue::ResolvePrototype)); + Qt::CaseSensitivity kase = Qt::CaseSensitive; + if (toBool(exec, property(exec, value, "ignoreCase", QScriptValue::ResolvePrototype))) + kase = Qt::CaseInsensitive; + return QRegExp(pattern, kase, QRegExp::RegExp2); +} + +#endif + +QVariant QScriptEnginePrivate::toVariant(JSC::ExecState *exec, JSC::JSValue value) +{ + if (!value) { + return QVariant(); + } else if (isObject(value)) { + if (isVariant(value)) + return variantValue(value); +#ifndef QT_NO_QOBJECT + else if (isQObject(value)) + return qVariantFromValue(toQObject(exec, value)); +#endif + else if (isDate(value)) + return QVariant(toDateTime(exec, value)); +#ifndef QT_NO_REGEXP + else if (isRegExp(value)) + return QVariant(toRegExp(exec, value)); +#endif + else if (isArray(value)) + return variantListFromArray(exec, value); + else if (QScriptDeclarativeClass *dc = declarativeClass(value)) + return dc->toVariant(declarativeObject(value)); + // try to convert to primitive + JSC::JSValue savedException; + saveException(exec, &savedException); + JSC::JSValue prim = value.toPrimitive(exec); + restoreException(exec, savedException); + if (!prim.isObject()) + return toVariant(exec, prim); + } else if (value.isNumber()) { + return QVariant(toNumber(exec, value)); + } else if (value.isString()) { + return QVariant(toString(exec, value)); + } else if (value.isBoolean()) { + return QVariant(toBool(exec, value)); + } + return QVariant(); +} + +JSC::JSValue QScriptEnginePrivate::propertyHelper(JSC::ExecState *exec, JSC::JSValue value, const JSC::Identifier &id, int resolveMode) +{ + JSC::JSValue result; + if (!(resolveMode & QScriptValue::ResolvePrototype)) { + // Look in the object's own properties + JSC::JSObject *object = JSC::asObject(value); + JSC::PropertySlot slot(object); + if (object->getOwnPropertySlot(exec, id, slot)) + result = slot.getValue(exec, id); + } + if (!result && (resolveMode & QScriptValue::ResolveScope)) { + // ### check if it's a function object and look in the scope chain + JSC::JSValue scope = property(exec, value, "__qt_scope__", QScriptValue::ResolveLocal); + if (isObject(scope)) + result = property(exec, scope, id, resolveMode); + } + return result; +} + +JSC::JSValue QScriptEnginePrivate::propertyHelper(JSC::ExecState *exec, JSC::JSValue value, quint32 index, int resolveMode) +{ + JSC::JSValue result; + if (!(resolveMode & QScriptValue::ResolvePrototype)) { + // Look in the object's own properties + JSC::JSObject *object = JSC::asObject(value); + JSC::PropertySlot slot(object); + if (object->getOwnPropertySlot(exec, index, slot)) + result = slot.getValue(exec, index); + } + return result; +} + +void QScriptEnginePrivate::setProperty(JSC::ExecState *exec, JSC::JSValue objectValue, const JSC::Identifier &id, + JSC::JSValue value, const QScriptValue::PropertyFlags &flags) +{ + JSC::JSObject *thisObject = JSC::asObject(objectValue); + JSC::JSValue setter = thisObject->lookupSetter(exec, id); + JSC::JSValue getter = thisObject->lookupGetter(exec, id); + if ((flags & QScriptValue::PropertyGetter) || (flags & QScriptValue::PropertySetter)) { + if (!value) { + // deleting getter/setter + if ((flags & QScriptValue::PropertyGetter) && (flags & QScriptValue::PropertySetter)) { + // deleting both: just delete the property + thisObject->deleteProperty(exec, id); + } else if (flags & QScriptValue::PropertyGetter) { + // preserve setter, if there is one + thisObject->deleteProperty(exec, id); + if (setter && setter.isObject()) + thisObject->defineSetter(exec, id, JSC::asObject(setter)); + } else { // flags & QScriptValue::PropertySetter + // preserve getter, if there is one + thisObject->deleteProperty(exec, id); + if (getter && getter.isObject()) + thisObject->defineGetter(exec, id, JSC::asObject(getter)); + } + } else { + if (value.isObject()) { // ### should check if it has callData() + // defining getter/setter + if (id == exec->propertyNames().underscoreProto) { + qWarning("QScriptValue::setProperty() failed: " + "cannot set getter or setter of native property `__proto__'"); + } else { + if (flags & QScriptValue::PropertyGetter) + thisObject->defineGetter(exec, id, JSC::asObject(value)); + if (flags & QScriptValue::PropertySetter) + thisObject->defineSetter(exec, id, JSC::asObject(value)); + } + } else { + qWarning("QScriptValue::setProperty(): getter/setter must be a function"); + } + } + } else { + // setting the value + if (getter && getter.isObject() && !(setter && setter.isObject())) { + qWarning("QScriptValue::setProperty() failed: " + "property '%s' has a getter but no setter", + qPrintable(QString(id.ustring()))); + return; + } + if (!value) { + // ### check if it's a getter/setter property + thisObject->deleteProperty(exec, id); + } else if (flags != QScriptValue::KeepExistingFlags) { + if (thisObject->hasOwnProperty(exec, id)) + thisObject->deleteProperty(exec, id); // ### hmmm - can't we just update the attributes? + unsigned attribs = 0; + if (flags & QScriptValue::ReadOnly) + attribs |= JSC::ReadOnly; + if (flags & QScriptValue::SkipInEnumeration) + attribs |= JSC::DontEnum; + if (flags & QScriptValue::Undeletable) + attribs |= JSC::DontDelete; + attribs |= flags & QScriptValue::UserRange; + thisObject->putWithAttributes(exec, id, value, attribs); + } else { + JSC::PutPropertySlot slot; + thisObject->put(exec, id, value, slot); + } + } +} + +void QScriptEnginePrivate::setProperty(JSC::ExecState *exec, JSC::JSValue objectValue, quint32 index, + JSC::JSValue value, const QScriptValue::PropertyFlags &flags) +{ + if (!value) { + JSC::asObject(objectValue)->deleteProperty(exec, index); + } else { + if ((flags & QScriptValue::PropertyGetter) || (flags & QScriptValue::PropertySetter)) { + // fall back to string-based setProperty(), since there is no + // JSC::JSObject::defineGetter(unsigned) + setProperty(exec, objectValue, JSC::Identifier::from(exec, index), value, flags); + } else { + if (flags != QScriptValue::KeepExistingFlags) { + // if (JSC::asObject(d->jscValue)->hasOwnProperty(exec, arrayIndex)) + // JSC::asObject(d->jscValue)->deleteProperty(exec, arrayIndex); + unsigned attribs = 0; + if (flags & QScriptValue::ReadOnly) + attribs |= JSC::ReadOnly; + if (flags & QScriptValue::SkipInEnumeration) + attribs |= JSC::DontEnum; + if (flags & QScriptValue::Undeletable) + attribs |= JSC::DontDelete; + attribs |= flags & QScriptValue::UserRange; + JSC::asObject(objectValue)->putWithAttributes(exec, index, value, attribs); + } else { + JSC::asObject(objectValue)->put(exec, index, value); + } + } + } +} + +QScriptValue::PropertyFlags QScriptEnginePrivate::propertyFlags(JSC::ExecState *exec, JSC::JSValue value, const JSC::Identifier &id, + const QScriptValue::ResolveFlags &mode) +{ + JSC::JSObject *object = JSC::asObject(value); + unsigned attribs = 0; + JSC::PropertyDescriptor descriptor; + if (object->getOwnPropertyDescriptor(exec, id, descriptor)) + attribs = descriptor.attributes(); + else { + if ((mode & QScriptValue::ResolvePrototype) && object->prototype() && object->prototype().isObject()) { + JSC::JSValue proto = object->prototype(); + return propertyFlags(exec, proto, id, mode); + } + return 0; + } + QScriptValue::PropertyFlags result = 0; + if (attribs & JSC::ReadOnly) + result |= QScriptValue::ReadOnly; + if (attribs & JSC::DontEnum) + result |= QScriptValue::SkipInEnumeration; + if (attribs & JSC::DontDelete) + result |= QScriptValue::Undeletable; + //We cannot rely on attribs JSC::Setter/Getter because they are not necesserly set by JSC (bug?) + if (attribs & JSC::Getter || !object->lookupGetter(exec, id).isUndefinedOrNull()) + result |= QScriptValue::PropertyGetter; + if (attribs & JSC::Setter || !object->lookupSetter(exec, id).isUndefinedOrNull()) + result |= QScriptValue::PropertySetter; +#ifndef QT_NO_QOBJECT + if (attribs & QScript::QObjectMemberAttribute) + result |= QScriptValue::QObjectMember; +#endif + result |= QScriptValue::PropertyFlag(attribs & QScriptValue::UserRange); + return result; +} + +QScriptString QScriptEnginePrivate::toStringHandle(const JSC::Identifier &name) +{ + QScriptString result; + QScriptStringPrivate *p = new QScriptStringPrivate(this, name, QScriptStringPrivate::HeapAllocated); + QScriptStringPrivate::init(result, p); + registerScriptString(p); + return result; +} + #ifdef QT_NO_QOBJECT QScriptEngine::QScriptEngine() @@ -1504,6 +1904,7 @@ QScriptEngine::~QScriptEngine() QScriptValue QScriptEngine::globalObject() const { Q_D(const QScriptEngine); + QScript::APIShim shim(const_cast<QScriptEnginePrivate*>(d)); JSC::JSObject *result = d->globalObject(); return const_cast<QScriptEnginePrivate*>(d)->scriptValueFromJSCValue(result); } @@ -1525,6 +1926,7 @@ void QScriptEngine::setGlobalObject(const QScriptValue &object) Q_D(QScriptEngine); if (!object.isObject()) return; + QScript::APIShim shim(d); JSC::JSObject *jscObject = JSC::asObject(d->scriptValueToJSCValue(object)); d->setGlobalObject(jscObject); } @@ -1581,6 +1983,7 @@ QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature fun, int length) { Q_D(QScriptEngine); + QScript::APIShim shim(d); JSC::ExecState* exec = d->currentFrame; JSC::JSValue function = new (exec)QScript::FunctionWrapper(exec, length, JSC::Identifier(exec, ""), fun); QScriptValue result = d->scriptValueFromJSCValue(function); @@ -1604,56 +2007,8 @@ extern QString qt_regexp_toCanonical(const QString &, QRegExp::PatternSyntax); QScriptValue QScriptEngine::newRegExp(const QRegExp ®exp) { Q_D(QScriptEngine); - JSC::ExecState* exec = d->currentFrame; - JSC::JSValue buf[2]; - JSC::ArgList args(buf, sizeof(buf)); - - //convert the pattern to a ECMAScript pattern - QString pattern = qt_regexp_toCanonical(regexp.pattern(), regexp.patternSyntax()); - if (regexp.isMinimal()) { - QString ecmaPattern; - int len = pattern.length(); - ecmaPattern.reserve(len); - int i = 0; - const QChar *wc = pattern.unicode(); - bool inBracket = false; - while (i < len) { - QChar c = wc[i++]; - ecmaPattern += c; - switch (c.unicode()) { - case '?': - case '+': - case '*': - case '}': - if (!inBracket) - ecmaPattern += QLatin1Char('?'); - break; - case '\\': - if (i < len) - ecmaPattern += wc[i++]; - break; - case '[': - inBracket = true; - break; - case ']': - inBracket = false; - break; - default: - break; - } - } - pattern = ecmaPattern; - } - - JSC::UString jscPattern = pattern; - QString flags; - if (regexp.caseSensitivity() == Qt::CaseInsensitive) - flags.append(QLatin1Char('i')); - JSC::UString jscFlags = flags; - buf[0] = JSC::jsString(exec, jscPattern); - buf[1] = JSC::jsString(exec, jscFlags); - JSC::JSObject* result = JSC::constructRegExp(exec, args); - return d->scriptValueFromJSCValue(result); + QScript::APIShim shim(d); + return d->scriptValueFromJSCValue(d->newRegExp(d->currentFrame, regexp)); } #endif // QT_NO_REGEXP @@ -1666,19 +2021,13 @@ QScriptValue QScriptEngine::newRegExp(const QRegExp ®exp) prototype; otherwise, the prototype will be the Object prototype object. - \sa setDefaultPrototype(), QScriptValue::toVariant() + \sa setDefaultPrototype(), QScriptValue::toVariant(), reportAdditionalMemoryCost() */ QScriptValue QScriptEngine::newVariant(const QVariant &value) { Q_D(QScriptEngine); - JSC::ExecState* exec = d->currentFrame; - QScriptObject *obj = new (exec) QScriptObject(d->variantWrapperObjectStructure); - obj->setDelegate(new QScript::QVariantDelegate(value)); - QScriptValue result = d->scriptValueFromJSCValue(obj); - QScriptValue proto = defaultPrototype(value.userType()); - if (proto.isValid()) - result.setPrototype(proto); - return result; + QScript::APIShim shim(d); + return d->scriptValueFromJSCValue(d->newVariant(value)); } /*! @@ -1704,24 +2053,16 @@ QScriptValue QScriptEngine::newVariant(const QVariant &value) true), you can pass QScriptContext::thisObject() (the default constructed script object) to this function to initialize the new object. + + \sa reportAdditionalMemoryCost() */ QScriptValue QScriptEngine::newVariant(const QScriptValue &object, const QVariant &value) { - if (!object.isObject()) - return newVariant(value); - JSC::JSObject *jscObject = JSC::asObject(QScriptValuePrivate::get(object)->jscValue); - if (!jscObject->inherits(&QScriptObject::info)) { - qWarning("QScriptEngine::newVariant(): changing class of non-QScriptObject not supported"); - return QScriptValue(); - } - QScriptObject *jscScriptObject = static_cast<QScriptObject*>(jscObject); - if (!object.isVariant()) { - jscScriptObject->setDelegate(new QScript::QVariantDelegate(value)); - } else { - QScriptValuePrivate::get(object)->setVariantValue(value); - } - return object; + Q_D(QScriptEngine); + QScript::APIShim shim(d); + JSC::JSValue jsObject = d->scriptValueToJSCValue(object); + return d->scriptValueFromJSCValue(d->newVariant(jsObject, value)); } #ifndef QT_NO_QOBJECT @@ -1745,12 +2086,13 @@ QScriptValue QScriptEngine::newVariant(const QScriptValue &object, wrapper object (either by script code or C++) will result in a script exception. - \sa QScriptValue::toQObject() + \sa QScriptValue::toQObject(), reportAdditionalMemoryCost() */ QScriptValue QScriptEngine::newQObject(QObject *object, ValueOwnership ownership, const QObjectWrapOptions &options) { Q_D(QScriptEngine); + QScript::APIShim shim(d); JSC::JSValue jscQObject = d->newQObject(object, ownership, options); return d->scriptValueFromJSCValue(jscQObject); } @@ -1779,14 +2121,18 @@ QScriptValue QScriptEngine::newQObject(QObject *object, ValueOwnership ownership (QScriptContext::isCalledAsConstructor() returns true), you can pass QScriptContext::thisObject() (the default constructed script object) to this function to initialize the new object. + + \sa reportAdditionalMemoryCost() */ QScriptValue QScriptEngine::newQObject(const QScriptValue &scriptObject, QObject *qtObject, ValueOwnership ownership, const QObjectWrapOptions &options) { + Q_D(QScriptEngine); if (!scriptObject.isObject()) return newQObject(qtObject, ownership, options); + QScript::APIShim shim(d); JSC::JSObject *jscObject = JSC::asObject(QScriptValuePrivate::get(scriptObject)->jscValue); if (!jscObject->inherits(&QScriptObject::info)) { qWarning("QScriptEngine::newQObject(): changing class of non-QScriptObject not supported"); @@ -1817,9 +2163,8 @@ QScriptValue QScriptEngine::newQObject(const QScriptValue &scriptObject, QScriptValue QScriptEngine::newObject() { Q_D(QScriptEngine); - JSC::ExecState* exec = d->currentFrame; - JSC::JSObject *result = new (exec)QScriptObject(d->scriptObjectStructure); - return d->scriptValueFromJSCValue(result); + QScript::APIShim shim(d); + return d->scriptValueFromJSCValue(d->newObject()); } /*! @@ -1834,12 +2179,13 @@ QScriptValue QScriptEngine::newObject() \a data, if specified, is set as the internal data of the new object (using QScriptValue::setData()). - \sa QScriptValue::scriptClass() + \sa QScriptValue::scriptClass(), reportAdditionalMemoryCost() */ QScriptValue QScriptEngine::newObject(QScriptClass *scriptClass, const QScriptValue &data) { Q_D(QScriptEngine); + QScript::APIShim shim(d); JSC::ExecState* exec = d->currentFrame; QScriptObject *result = new (exec) QScriptObject(d->scriptObjectStructure); result->setDelegate(new QScript::ClassObjectDelegate(scriptClass)); @@ -1907,6 +2253,7 @@ QScriptValue QScriptEngine::newActivationObject() QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature fun, int length) { Q_D(QScriptEngine); + QScript::APIShim shim(d); JSC::ExecState* exec = d->currentFrame; JSC::JSValue function = new (exec)QScript::FunctionWrapper(exec, length, JSC::Identifier(exec, ""), fun); QScriptValue result = d->scriptValueFromJSCValue(function); @@ -1924,6 +2271,7 @@ QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature fun, in QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionWithArgSignature fun, void *arg) { Q_D(QScriptEngine); + QScript::APIShim shim(d); JSC::ExecState* exec = d->currentFrame; JSC::JSValue function = new (exec)QScript::FunctionWithArgWrapper(exec, /*length=*/0, JSC::Identifier(exec, ""), fun, arg); QScriptValue result = d->scriptValueFromJSCValue(function); @@ -1942,9 +2290,8 @@ QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionWithArgSignature QScriptValue QScriptEngine::newArray(uint length) { Q_D(QScriptEngine); - JSC::ExecState* exec = d->currentFrame; - JSC::JSArray* result = JSC::constructEmptyArray(exec, length); - return d->scriptValueFromJSCValue(result); + QScript::APIShim shim(d); + return d->scriptValueFromJSCValue(d->newArray(d->currentFrame, length)); } /*! @@ -1957,22 +2304,8 @@ QScriptValue QScriptEngine::newArray(uint length) QScriptValue QScriptEngine::newRegExp(const QString &pattern, const QString &flags) { Q_D(QScriptEngine); - JSC::ExecState* exec = d->currentFrame; - JSC::JSValue buf[2]; - JSC::ArgList args(buf, sizeof(buf)); - JSC::UString jscPattern = pattern; - QString strippedFlags; - if (flags.contains(QLatin1Char('i'))) - strippedFlags += QLatin1Char('i'); - if (flags.contains(QLatin1Char('m'))) - strippedFlags += QLatin1Char('m'); - if (flags.contains(QLatin1Char('g'))) - strippedFlags += QLatin1Char('g'); - JSC::UString jscFlags = strippedFlags; - buf[0] = JSC::jsString(exec, jscPattern); - buf[1] = JSC::jsString(exec, jscFlags); - JSC::JSObject* result = JSC::constructRegExp(exec, args); - return d->scriptValueFromJSCValue(result); + QScript::APIShim shim(d); + return d->scriptValueFromJSCValue(d->newRegExp(d->currentFrame, pattern, flags)); } /*! @@ -1983,11 +2316,8 @@ QScriptValue QScriptEngine::newRegExp(const QString &pattern, const QString &fla QScriptValue QScriptEngine::newDate(qsreal value) { Q_D(QScriptEngine); - JSC::ExecState* exec = d->currentFrame; - JSC::JSValue val = JSC::jsNumber(exec, value); - JSC::ArgList args(&val, 1); - JSC::JSObject *result = JSC::constructDate(exec, args); - return d->scriptValueFromJSCValue(result); + QScript::APIShim shim(d); + return d->scriptValueFromJSCValue(d->newDate(d->currentFrame, value)); } /*! @@ -1997,7 +2327,9 @@ QScriptValue QScriptEngine::newDate(qsreal value) */ QScriptValue QScriptEngine::newDate(const QDateTime &value) { - return newDate(QScript::FromDateTime(value)); + Q_D(QScriptEngine); + QScript::APIShim shim(d); + return d->scriptValueFromJSCValue(d->newDate(d->currentFrame, value)); } #ifndef QT_NO_QOBJECT @@ -2020,6 +2352,7 @@ QScriptValue QScriptEngine::newQMetaObject( const QMetaObject *metaObject, const QScriptValue &ctor) { Q_D(QScriptEngine); + QScript::APIShim shim(d); JSC::JSValue jscCtor = d->scriptValueToJSCValue(ctor); JSC::JSValue jscQMetaObject = d->newQMetaObject(metaObject, jscCtor); return d->scriptValueFromJSCValue(jscQMetaObject); @@ -2194,15 +2527,16 @@ QScriptSyntaxCheckResult QScriptEnginePrivate::checkSyntax(const QString &progra QScriptValue QScriptEngine::evaluate(const QString &program, const QString &fileName, int lineNumber) { Q_D(QScriptEngine); + QScript::APIShim shim(d); WTF::PassRefPtr<QScript::UStringSourceProviderWithFeedback> provider = QScript::UStringSourceProviderWithFeedback::create(program, fileName, lineNumber, d); intptr_t sourceId = provider->asID(); JSC::SourceCode source(provider, lineNumber); //after construction of SourceCode provider variable will be null. JSC::ExecState* exec = d->currentFrame; - JSC::EvalExecutable executable(exec, source); + WTF::RefPtr<JSC::EvalExecutable> executable = JSC::EvalExecutable::create(exec, source); bool compile = true; - return d->scriptValueFromJSCValue(d->evaluateHelper(exec, sourceId, &executable, compile)); + return d->scriptValueFromJSCValue(d->evaluateHelper(exec, sourceId, executable.get(), compile)); } /*! @@ -2219,6 +2553,7 @@ QScriptValue QScriptEngine::evaluate(const QScriptProgram &program) if (!program_d) return QScriptValue(); + QScript::APIShim shim(d); JSC::ExecState* exec = d->currentFrame; JSC::EvalExecutable *executable = program_d->executable(exec, d); bool compile = !program_d->isCompiled; @@ -2270,6 +2605,7 @@ QScriptContext *QScriptEngine::currentContext() const QScriptContext *QScriptEngine::pushContext() { Q_D(QScriptEngine); + QScript::APIShim shim(d); JSC::CallFrame* newFrame = d->pushContext(d->currentFrame, d->currentFrame->globalData().dynamicGlobalObject, JSC::ArgList(), /*callee = */0); @@ -2331,7 +2667,7 @@ JSC::CallFrame *QScriptEnginePrivate::pushContext(JSC::CallFrame *exec, JSC::JSV newCallFrame->init(0, /*vPC=*/0, exec->scopeChain(), exec, flags | ShouldRestoreCallFrame, argc, callee); } else { JSC::JSObject *jscObject = originalGlobalObject(); - JSC::ScopeChainNode *scn = new JSC::ScopeChainNode(0, jscObject, &exec->globalData(), jscObject); + JSC::ScopeChainNode *scn = new JSC::ScopeChainNode(0, jscObject, &exec->globalData(), exec->lexicalGlobalObject(), jscObject); newCallFrame->init(0, /*vPC=*/0, scn, exec, flags | ShouldRestoreCallFrame, argc, callee); } } else { @@ -2361,6 +2697,7 @@ void QScriptEngine::popContext() if (agent()) agent()->contextPop(); Q_D(QScriptEngine); + QScript::APIShim shim(d); if (d->currentFrame->returnPC() != 0 || d->currentFrame->codeBlock() != 0 || !currentContext()->parentContext()) { qWarning("QScriptEngine::popContext() doesn't match with pushContext()"); @@ -2556,128 +2893,128 @@ void QScriptEngine::setDefaultPrototype(int metaTypeId, const QScriptValue &prot QScriptValue QScriptEngine::create(int type, const void *ptr) { Q_D(QScriptEngine); - return d->create(type, ptr); + QScript::APIShim shim(d); + return d->scriptValueFromJSCValue(d->create(d->currentFrame, type, ptr)); } -QScriptValue QScriptEnginePrivate::create(int type, const void *ptr) +JSC::JSValue QScriptEnginePrivate::create(JSC::ExecState *exec, int type, const void *ptr) { - Q_Q(QScriptEngine); Q_ASSERT(ptr != 0); - QScriptValue result; - QScriptTypeInfo *info = m_typeInfos.value(type); + JSC::JSValue result; + QScriptEnginePrivate *eng = exec ? QScript::scriptEngineFromExec(exec) : 0; + QScriptTypeInfo *info = eng ? eng->m_typeInfos.value(type) : 0; if (info && info->marshal) { - result = info->marshal(q, ptr); + result = eng->scriptValueToJSCValue(info->marshal(eng->q_func(), ptr)); } else { // check if it's one of the types we know switch (QMetaType::Type(type)) { case QMetaType::Void: - return QScriptValue(q, QScriptValue::UndefinedValue); + return JSC::jsUndefined(); case QMetaType::Bool: - return QScriptValue(q, *reinterpret_cast<const bool*>(ptr)); + return JSC::jsBoolean(*reinterpret_cast<const bool*>(ptr)); case QMetaType::Int: - return QScriptValue(q, *reinterpret_cast<const int*>(ptr)); + return JSC::jsNumber(exec, *reinterpret_cast<const int*>(ptr)); case QMetaType::UInt: - return QScriptValue(q, *reinterpret_cast<const uint*>(ptr)); + return JSC::jsNumber(exec, *reinterpret_cast<const uint*>(ptr)); case QMetaType::LongLong: - return QScriptValue(q, qsreal(*reinterpret_cast<const qlonglong*>(ptr))); + return JSC::jsNumber(exec, qsreal(*reinterpret_cast<const qlonglong*>(ptr))); case QMetaType::ULongLong: #if defined(Q_OS_WIN) && defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 12008804 #pragma message("** NOTE: You need the Visual Studio Processor Pack to compile support for 64bit unsigned integers.") - return QScriptValue(q, qsreal((qlonglong)*reinterpret_cast<const qulonglong*>(ptr))); + return JSC::jsNumber(exec, qsreal((qlonglong)*reinterpret_cast<const qulonglong*>(ptr))); #elif defined(Q_CC_MSVC) && !defined(Q_CC_MSVC_NET) - return QScriptValue(q, qsreal((qlonglong)*reinterpret_cast<const qulonglong*>(ptr))); + return JSC::jsNumber(exec, qsreal((qlonglong)*reinterpret_cast<const qulonglong*>(ptr))); #else - return QScriptValue(q, qsreal(*reinterpret_cast<const qulonglong*>(ptr))); + return JSC::jsNumber(exec, qsreal(*reinterpret_cast<const qulonglong*>(ptr))); #endif case QMetaType::Double: - return QScriptValue(q, qsreal(*reinterpret_cast<const double*>(ptr))); + return JSC::jsNumber(exec, qsreal(*reinterpret_cast<const double*>(ptr))); case QMetaType::QString: - return QScriptValue(q, *reinterpret_cast<const QString*>(ptr)); + return JSC::jsString(exec, *reinterpret_cast<const QString*>(ptr)); case QMetaType::Float: - return QScriptValue(q, *reinterpret_cast<const float*>(ptr)); + return JSC::jsNumber(exec, *reinterpret_cast<const float*>(ptr)); case QMetaType::Short: - return QScriptValue(q, *reinterpret_cast<const short*>(ptr)); + return JSC::jsNumber(exec, *reinterpret_cast<const short*>(ptr)); case QMetaType::UShort: - return QScriptValue(q, *reinterpret_cast<const unsigned short*>(ptr)); + return JSC::jsNumber(exec, *reinterpret_cast<const unsigned short*>(ptr)); case QMetaType::Char: - return QScriptValue(q, *reinterpret_cast<const char*>(ptr)); + return JSC::jsNumber(exec, *reinterpret_cast<const char*>(ptr)); case QMetaType::UChar: - return QScriptValue(q, *reinterpret_cast<const unsigned char*>(ptr)); + return JSC::jsNumber(exec, *reinterpret_cast<const unsigned char*>(ptr)); case QMetaType::QChar: - return QScriptValue(q, (*reinterpret_cast<const QChar*>(ptr)).unicode()); + return JSC::jsNumber(exec, (*reinterpret_cast<const QChar*>(ptr)).unicode()); case QMetaType::QStringList: - result = arrayFromStringList(*reinterpret_cast<const QStringList *>(ptr)); + result = arrayFromStringList(exec, *reinterpret_cast<const QStringList *>(ptr)); break; case QMetaType::QVariantList: - result = arrayFromVariantList(*reinterpret_cast<const QVariantList *>(ptr)); + result = arrayFromVariantList(exec, *reinterpret_cast<const QVariantList *>(ptr)); break; case QMetaType::QVariantMap: - result = objectFromVariantMap(*reinterpret_cast<const QVariantMap *>(ptr)); + result = objectFromVariantMap(exec, *reinterpret_cast<const QVariantMap *>(ptr)); break; case QMetaType::QDateTime: - result = q->newDate(*reinterpret_cast<const QDateTime *>(ptr)); + result = newDate(exec, *reinterpret_cast<const QDateTime *>(ptr)); break; case QMetaType::QDate: - result = q->newDate(QDateTime(*reinterpret_cast<const QDate *>(ptr))); + result = newDate(exec, QDateTime(*reinterpret_cast<const QDate *>(ptr))); break; #ifndef QT_NO_REGEXP case QMetaType::QRegExp: - result = q->newRegExp(*reinterpret_cast<const QRegExp *>(ptr)); + result = newRegExp(exec, *reinterpret_cast<const QRegExp *>(ptr)); break; #endif #ifndef QT_NO_QOBJECT case QMetaType::QObjectStar: case QMetaType::QWidgetStar: - result = q->newQObject(*reinterpret_cast<QObject* const *>(ptr)); + result = eng->newQObject(*reinterpret_cast<QObject* const *>(ptr)); break; #endif + case QMetaType::QVariant: + result = jscValueFromVariant(exec, *reinterpret_cast<const QVariant*>(ptr)); + break; default: if (type == qMetaTypeId<QScriptValue>()) { - result = *reinterpret_cast<const QScriptValue*>(ptr); - if (!result.isValid()) - return QScriptValue(q, QScriptValue::UndefinedValue); + result = eng->scriptValueToJSCValue(*reinterpret_cast<const QScriptValue*>(ptr)); + if (!result) + return JSC::jsUndefined(); } #ifndef QT_NO_QOBJECT // lazy registration of some common list types else if (type == qMetaTypeId<QObjectList>()) { - qScriptRegisterSequenceMetaType<QObjectList>(q); - return create(type, ptr); + qScriptRegisterSequenceMetaType<QObjectList>(eng->q_func()); + return create(exec, type, ptr); } #endif else if (type == qMetaTypeId<QList<int> >()) { - qScriptRegisterSequenceMetaType<QList<int> >(q); - return create(type, ptr); + qScriptRegisterSequenceMetaType<QList<int> >(eng->q_func()); + return create(exec, type, ptr); } else { QByteArray typeName = QMetaType::typeName(type); - if (typeName == "QVariant") - result = scriptValueFromVariant(*reinterpret_cast<const QVariant*>(ptr)); if (typeName.endsWith('*') && !*reinterpret_cast<void* const *>(ptr)) - return QScriptValue(q, QScriptValue::NullValue); + return JSC::jsNull(); else - result = q->newVariant(QVariant(type, ptr)); + result = eng->newVariant(QVariant(type, ptr)); } } } - if (result.isObject() && info && info->prototype - && JSC::JSValue::strictEqual(scriptValueToJSCValue(result.prototype()), originalGlobalObject()->objectPrototype())) { - result.setPrototype(scriptValueFromJSCValue(info->prototype)); + if (result && result.isObject() && info && info->prototype + && JSC::JSValue::strictEqual(exec, JSC::asObject(result)->prototype(), eng->originalGlobalObject()->objectPrototype())) { + JSC::asObject(result)->setPrototype(info->prototype); } return result; } -bool QScriptEnginePrivate::convert(const QScriptValue &value, - int type, void *ptr, - QScriptEnginePrivate *eng) +bool QScriptEnginePrivate::convertValue(JSC::ExecState *exec, JSC::JSValue value, + int type, void *ptr) { - if (!eng) - eng = QScriptValuePrivate::getEngine(value); + QScriptEnginePrivate *eng = exec ? QScript::scriptEngineFromExec(exec) : 0; if (eng) { QScriptTypeInfo *info = eng->m_typeInfos.value(type); if (info && info->demarshal) { - info->demarshal(value, ptr); + info->demarshal(eng->scriptValueFromJSCValue(value), ptr); return true; } } @@ -2685,78 +3022,78 @@ bool QScriptEnginePrivate::convert(const QScriptValue &value, // check if it's one of the types we know switch (QMetaType::Type(type)) { case QMetaType::Bool: - *reinterpret_cast<bool*>(ptr) = value.toBoolean(); + *reinterpret_cast<bool*>(ptr) = toBool(exec, value); return true; case QMetaType::Int: - *reinterpret_cast<int*>(ptr) = value.toInt32(); + *reinterpret_cast<int*>(ptr) = toInt32(exec, value); return true; case QMetaType::UInt: - *reinterpret_cast<uint*>(ptr) = value.toUInt32(); + *reinterpret_cast<uint*>(ptr) = toUInt32(exec, value); return true; case QMetaType::LongLong: - *reinterpret_cast<qlonglong*>(ptr) = qlonglong(value.toInteger()); + *reinterpret_cast<qlonglong*>(ptr) = qlonglong(toInteger(exec, value)); return true; case QMetaType::ULongLong: - *reinterpret_cast<qulonglong*>(ptr) = qulonglong(value.toInteger()); + *reinterpret_cast<qulonglong*>(ptr) = qulonglong(toInteger(exec, value)); return true; case QMetaType::Double: - *reinterpret_cast<double*>(ptr) = value.toNumber(); + *reinterpret_cast<double*>(ptr) = toNumber(exec, value); return true; case QMetaType::QString: if (value.isUndefined() || value.isNull()) *reinterpret_cast<QString*>(ptr) = QString(); else - *reinterpret_cast<QString*>(ptr) = value.toString(); + *reinterpret_cast<QString*>(ptr) = toString(exec, value); return true; case QMetaType::Float: - *reinterpret_cast<float*>(ptr) = value.toNumber(); + *reinterpret_cast<float*>(ptr) = toNumber(exec, value); return true; case QMetaType::Short: - *reinterpret_cast<short*>(ptr) = short(value.toInt32()); + *reinterpret_cast<short*>(ptr) = short(toInt32(exec, value)); return true; case QMetaType::UShort: - *reinterpret_cast<unsigned short*>(ptr) = value.toUInt16(); + *reinterpret_cast<unsigned short*>(ptr) = QScript::ToUInt16(toNumber(exec, value)); return true; case QMetaType::Char: - *reinterpret_cast<char*>(ptr) = char(value.toInt32()); + *reinterpret_cast<char*>(ptr) = char(toInt32(exec, value)); return true; case QMetaType::UChar: - *reinterpret_cast<unsigned char*>(ptr) = (unsigned char)(value.toInt32()); + *reinterpret_cast<unsigned char*>(ptr) = (unsigned char)(toInt32(exec, value)); return true; case QMetaType::QChar: if (value.isString()) { - QString str = value.toString(); + QString str = toString(exec, value); *reinterpret_cast<QChar*>(ptr) = str.isEmpty() ? QChar() : str.at(0); } else { - *reinterpret_cast<QChar*>(ptr) = QChar(value.toUInt16()); + *reinterpret_cast<QChar*>(ptr) = QChar(QScript::ToUInt16(toNumber(exec, value))); } return true; case QMetaType::QDateTime: - if (value.isDate()) { - *reinterpret_cast<QDateTime *>(ptr) = value.toDateTime(); + if (isDate(value)) { + *reinterpret_cast<QDateTime *>(ptr) = toDateTime(exec, value); return true; } break; case QMetaType::QDate: - if (value.isDate()) { - *reinterpret_cast<QDate *>(ptr) = value.toDateTime().date(); + if (isDate(value)) { + *reinterpret_cast<QDate *>(ptr) = toDateTime(exec, value).date(); return true; } break; #ifndef QT_NO_REGEXP case QMetaType::QRegExp: - if (value.isRegExp()) { - *reinterpret_cast<QRegExp *>(ptr) = value.toRegExp(); + if (isRegExp(value)) { + *reinterpret_cast<QRegExp *>(ptr) = toRegExp(exec, value); return true; } break; #endif #ifndef QT_NO_QOBJECT case QMetaType::QObjectStar: - if (value.isQObject() || value.isNull()) { - *reinterpret_cast<QObject* *>(ptr) = value.toQObject(); + if (isQObject(value) || value.isNull()) { + *reinterpret_cast<QObject* *>(ptr) = toQObject(exec, value); return true; } break; case QMetaType::QWidgetStar: - if (value.isQObject() || value.isNull()) { - QObject *qo = value.toQObject(); + if (isQObject(value) || value.isNull()) { + QObject *qo = toQObject(exec, value); if (!qo || qo->isWidgetType()) { *reinterpret_cast<QWidget* *>(ptr) = reinterpret_cast<QWidget*>(qo); return true; @@ -2764,48 +3101,51 @@ bool QScriptEnginePrivate::convert(const QScriptValue &value, } break; #endif case QMetaType::QStringList: - if (value.isArray()) { - *reinterpret_cast<QStringList *>(ptr) = stringListFromArray(value); + if (isArray(value)) { + *reinterpret_cast<QStringList *>(ptr) = stringListFromArray(exec, value); return true; } break; case QMetaType::QVariantList: - if (value.isArray()) { - *reinterpret_cast<QVariantList *>(ptr) = variantListFromArray(value); + if (isArray(value)) { + *reinterpret_cast<QVariantList *>(ptr) = variantListFromArray(exec, value); return true; } break; case QMetaType::QVariantMap: - if (value.isObject()) { - *reinterpret_cast<QVariantMap *>(ptr) = variantMapFromObject(value); + if (isObject(value)) { + *reinterpret_cast<QVariantMap *>(ptr) = variantMapFromObject(exec, value); return true; } break; + case QMetaType::QVariant: + *reinterpret_cast<QVariant*>(ptr) = toVariant(exec, value); + return true; default: ; } QByteArray name = QMetaType::typeName(type); #ifndef QT_NO_QOBJECT - if (convertToNativeQObject(value, name, reinterpret_cast<void* *>(ptr))) + if (convertToNativeQObject(exec, value, name, reinterpret_cast<void* *>(ptr))) return true; #endif - if (value.isVariant() && name.endsWith('*')) { + if (isVariant(value) && name.endsWith('*')) { int valueType = QMetaType::type(name.left(name.size()-1)); - QVariant &var = QScriptValuePrivate::get(value)->variantValue(); + QVariant &var = variantValue(value); if (valueType == var.userType()) { *reinterpret_cast<void* *>(ptr) = var.data(); return true; } else { // look in the prototype chain - QScriptValue proto = value.prototype(); + JSC::JSValue proto = JSC::asObject(value)->prototype(); while (proto.isObject()) { bool canCast = false; - if (proto.isVariant()) { - canCast = (type == proto.toVariant().userType()) - || (valueType && (valueType == proto.toVariant().userType())); + if (isVariant(proto)) { + canCast = (type == variantValue(proto).userType()) + || (valueType && (valueType == variantValue(proto).userType())); } #ifndef QT_NO_QOBJECT - else if (proto.isQObject()) { + else if (isQObject(proto)) { QByteArray className = name.left(name.size()-1); - if (QObject *qobject = proto.toQObject()) + if (QObject *qobject = toQObject(exec, proto)) canCast = qobject->qt_metacast(className) != 0; } #endif @@ -2817,7 +3157,7 @@ bool QScriptEnginePrivate::convert(const QScriptValue &value, *reinterpret_cast<void* *>(ptr) = var.data(); return true; } - proto = proto.prototype(); + proto = JSC::asObject(proto)->prototype(); } } } else if (value.isNull() && name.endsWith('*')) { @@ -2826,10 +3166,7 @@ bool QScriptEnginePrivate::convert(const QScriptValue &value, } else if (type == qMetaTypeId<QScriptValue>()) { if (!eng) return false; - *reinterpret_cast<QScriptValue*>(ptr) = value; - return true; - } else if (name == "QVariant") { - *reinterpret_cast<QVariant*>(ptr) = value.toVariant(); + *reinterpret_cast<QScriptValue*>(ptr) = eng->scriptValueFromJSCValue(value); return true; } @@ -2839,14 +3176,14 @@ bool QScriptEnginePrivate::convert(const QScriptValue &value, if (!eng) return false; qScriptRegisterSequenceMetaType<QObjectList>(eng->q_func()); - return convert(value, type, ptr, eng); + return convertValue(exec, value, type, ptr); } #endif else if (type == qMetaTypeId<QList<int> >()) { if (!eng) return false; qScriptRegisterSequenceMetaType<QList<int> >(eng->q_func()); - return convert(value, type, ptr, eng); + return convertValue(exec, value, type, ptr); } #if 0 @@ -2858,6 +3195,102 @@ bool QScriptEnginePrivate::convert(const QScriptValue &value, return false; } +bool QScriptEnginePrivate::convertNumber(qsreal value, int type, void *ptr) +{ + switch (QMetaType::Type(type)) { + case QMetaType::Bool: + *reinterpret_cast<bool*>(ptr) = QScript::ToBool(value); + return true; + case QMetaType::Int: + *reinterpret_cast<int*>(ptr) = QScript::ToInt32(value); + return true; + case QMetaType::UInt: + *reinterpret_cast<uint*>(ptr) = QScript::ToUInt32(value); + return true; + case QMetaType::LongLong: + *reinterpret_cast<qlonglong*>(ptr) = qlonglong(QScript::ToInteger(value)); + return true; + case QMetaType::ULongLong: + *reinterpret_cast<qulonglong*>(ptr) = qulonglong(QScript::ToInteger(value)); + return true; + case QMetaType::Double: + *reinterpret_cast<double*>(ptr) = value; + return true; + case QMetaType::QString: + *reinterpret_cast<QString*>(ptr) = QScript::ToString(value); + return true; + case QMetaType::Float: + *reinterpret_cast<float*>(ptr) = value; + return true; + case QMetaType::Short: + *reinterpret_cast<short*>(ptr) = short(QScript::ToInt32(value)); + return true; + case QMetaType::UShort: + *reinterpret_cast<unsigned short*>(ptr) = QScript::ToUInt16(value); + return true; + case QMetaType::Char: + *reinterpret_cast<char*>(ptr) = char(QScript::ToInt32(value)); + return true; + case QMetaType::UChar: + *reinterpret_cast<unsigned char*>(ptr) = (unsigned char)(QScript::ToInt32(value)); + return true; + case QMetaType::QChar: + *reinterpret_cast<QChar*>(ptr) = QChar(QScript::ToUInt16(value)); + return true; + default: + break; + } + return false; +} + +bool QScriptEnginePrivate::convertString(const QString &value, int type, void *ptr) +{ + switch (QMetaType::Type(type)) { + case QMetaType::Bool: + *reinterpret_cast<bool*>(ptr) = QScript::ToBool(value); + return true; + case QMetaType::Int: + *reinterpret_cast<int*>(ptr) = QScript::ToInt32(value); + return true; + case QMetaType::UInt: + *reinterpret_cast<uint*>(ptr) = QScript::ToUInt32(value); + return true; + case QMetaType::LongLong: + *reinterpret_cast<qlonglong*>(ptr) = qlonglong(QScript::ToInteger(value)); + return true; + case QMetaType::ULongLong: + *reinterpret_cast<qulonglong*>(ptr) = qulonglong(QScript::ToInteger(value)); + return true; + case QMetaType::Double: + *reinterpret_cast<double*>(ptr) = QScript::ToNumber(value); + return true; + case QMetaType::QString: + *reinterpret_cast<QString*>(ptr) = value; + return true; + case QMetaType::Float: + *reinterpret_cast<float*>(ptr) = QScript::ToNumber(value); + return true; + case QMetaType::Short: + *reinterpret_cast<short*>(ptr) = short(QScript::ToInt32(value)); + return true; + case QMetaType::UShort: + *reinterpret_cast<unsigned short*>(ptr) = QScript::ToUInt16(value); + return true; + case QMetaType::Char: + *reinterpret_cast<char*>(ptr) = char(QScript::ToInt32(value)); + return true; + case QMetaType::UChar: + *reinterpret_cast<unsigned char*>(ptr) = (unsigned char)(QScript::ToInt32(value)); + return true; + case QMetaType::QChar: + *reinterpret_cast<QChar*>(ptr) = QChar(QScript::ToUInt16(value)); + return true; + default: + break; + } + return false; +} + bool QScriptEnginePrivate::hasDemarshalFunction(int type) const { QScriptTypeInfo *info = m_typeInfos.value(type); @@ -2870,7 +3303,8 @@ bool QScriptEnginePrivate::hasDemarshalFunction(int type) const bool QScriptEngine::convert(const QScriptValue &value, int type, void *ptr) { Q_D(QScriptEngine); - return QScriptEnginePrivate::convert(value, type, ptr, d); + QScript::APIShim shim(d); + return QScriptEnginePrivate::convertValue(d->currentFrame, d->scriptValueToJSCValue(value), type, ptr); } /*! @@ -2878,7 +3312,24 @@ bool QScriptEngine::convert(const QScriptValue &value, int type, void *ptr) */ bool QScriptEngine::convertV2(const QScriptValue &value, int type, void *ptr) { - return QScriptEnginePrivate::convert(value, type, ptr, /*engine=*/0); + QScriptValuePrivate *vp = QScriptValuePrivate::get(value); + if (vp) { + switch (vp->type) { + case QScriptValuePrivate::JavaScriptCore: { + if (vp->engine) { + QScript::APIShim shim(vp->engine); + return QScriptEnginePrivate::convertValue(vp->engine->currentFrame, vp->jscValue, type, ptr); + } else { + return QScriptEnginePrivate::convertValue(0, vp->jscValue, type, ptr); + } + } + case QScriptValuePrivate::Number: + return QScriptEnginePrivate::convertNumber(vp->numberValue, type, ptr); + case QScriptValuePrivate::String: + return QScriptEnginePrivate::convertString(vp->stringValue, type, ptr); + } + } + return false; } /*! @@ -2889,6 +3340,7 @@ void QScriptEngine::registerCustomType(int type, MarshalFunction mf, const QScriptValue &prototype) { Q_D(QScriptEngine); + QScript::APIShim shim(d); QScriptTypeInfo *info = d->m_typeInfos.value(type); if (!info) { info = new QScriptTypeInfo(); @@ -2921,6 +3373,7 @@ void QScriptEngine::registerCustomType(int type, MarshalFunction mf, void QScriptEngine::installTranslatorFunctions(const QScriptValue &object) { Q_D(QScriptEngine); + QScript::APIShim shim(d); JSC::ExecState* exec = d->currentFrame; JSC::JSValue jscObject = d->scriptValueToJSCValue(object); JSC::JSGlobalObject *glob = d->originalGlobalObject(); @@ -2954,6 +3407,7 @@ QScriptValue QScriptEngine::importExtension(const QString &extension) Q_UNUSED(extension); #else Q_D(QScriptEngine); + QScript::APIShim shim(d); if (d->importedExtensions.contains(extension)) return undefinedValue(); // already imported @@ -3450,6 +3904,8 @@ QStringList QScriptEngine::importedExtensions() const been created). However, you can call this function to explicitly request that garbage collection should be performed as soon as possible. + + \sa reportAdditionalMemoryCost() */ void QScriptEngine::collectGarbage() { @@ -3458,6 +3914,33 @@ void QScriptEngine::collectGarbage() } /*! + \since 4.7 + + Reports an additional memory cost of the given \a size, measured in + bytes, to the garbage collector. + + This function can be called to indicate that a Qt Script object has + memory associated with it that isn't managed by Qt Script itself. + Reporting the additional cost makes it more likely that the garbage + collector will be triggered. + + Note that if the additional memory is shared with objects outside + the scripting environment, the cost should not be reported, since + collecting the Qt Script object would not cause the memory to be + freed anyway. + + Negative \a size values are ignored, i.e. this function can't be + used to report that the additional memory has been deallocated. + + \sa collectGarbage() +*/ +void QScriptEngine::reportAdditionalMemoryCost(int size) +{ + Q_D(QScriptEngine); + d->reportAdditionalMemoryCost(size); +} + +/*! Sets the interval between calls to QCoreApplication::processEvents to \a interval milliseconds. @@ -3565,6 +4048,7 @@ bool qScriptConnect(QObject *sender, const char *signal, if (receiver.isObject() && (receiver.engine() != function.engine())) return false; QScriptEnginePrivate *engine = QScriptEnginePrivate::get(function.engine()); + QScript::APIShim shim(engine); JSC::JSValue jscReceiver = engine->scriptValueToJSCValue(receiver); JSC::JSValue jscFunction = engine->scriptValueToJSCValue(function); return engine->scriptConnect(sender, signal, jscReceiver, jscFunction, @@ -3591,6 +4075,7 @@ bool qScriptDisconnect(QObject *sender, const char *signal, if (receiver.isObject() && (receiver.engine() != function.engine())) return false; QScriptEnginePrivate *engine = QScriptEnginePrivate::get(function.engine()); + QScript::APIShim shim(engine); JSC::JSValue jscReceiver = engine->scriptValueToJSCValue(receiver); JSC::JSValue jscFunction = engine->scriptValueToJSCValue(function); return engine->scriptDisconnect(sender, signal, jscReceiver, jscFunction); @@ -3670,11 +4155,8 @@ QScriptEngineAgent *QScriptEngine::agent() const QScriptString QScriptEngine::toStringHandle(const QString &str) { Q_D(QScriptEngine); - QScriptString result; - QScriptStringPrivate *p = new QScriptStringPrivate(d, JSC::Identifier(d->currentFrame, str), QScriptStringPrivate::HeapAllocated); - QScriptStringPrivate::init(result, p); - d->registerScriptString(p); - return result; + QScript::APIShim shim(d); + return d->toStringHandle(JSC::Identifier(d->currentFrame, str)); } /*! @@ -3699,6 +4181,7 @@ QScriptString QScriptEngine::toStringHandle(const QString &str) QScriptValue QScriptEngine::toObject(const QScriptValue &value) { Q_D(QScriptEngine); + QScript::APIShim shim(d); JSC::JSValue jscValue = d->scriptValueToJSCValue(value); if (!jscValue || jscValue.isUndefined() || jscValue.isNull()) return QScriptValue(); @@ -3850,4 +4333,9 @@ Q_AUTOTEST_EXPORT bool qt_script_isJITEnabled() } #endif +#ifdef Q_CC_MSVC +// Try to prevent compiler from crashing. +#pragma optimize("", off) +#endif + QT_END_NAMESPACE diff --git a/src/script/api/qscriptengine.h b/src/script/api/qscriptengine.h index 2ce3183..3212ed5 100644 --- a/src/script/api/qscriptengine.h +++ b/src/script/api/qscriptengine.h @@ -125,6 +125,7 @@ public: ExcludeSuperClassContents = 0x0006, SkipMethodsInEnumeration = 0x0008, ExcludeDeleteLater = 0x0010, + ExcludeSlots = 0x0020, AutoCreateDynamicProperties = 0x0100, PreferExistingWrapperObject = 0x0200 @@ -232,6 +233,7 @@ public: QStringList importedExtensions() const; void collectGarbage(); + void reportAdditionalMemoryCost(int size); void setProcessEventsInterval(int interval); int processEventsInterval() const; diff --git a/src/script/api/qscriptengine_p.h b/src/script/api/qscriptengine_p.h index 401d6d2..5c2007f 100644 --- a/src/script/api/qscriptengine_p.h +++ b/src/script/api/qscriptengine_p.h @@ -37,14 +37,30 @@ #include "private/qobject_p.h" +#include <QtCore/qdatetime.h> #include <QtCore/qhash.h> +#include <QtCore/qnumeric.h> +#include <QtCore/qregexp.h> #include <QtCore/qset.h> #include "qscriptvalue_p.h" #include "qscriptstring_p.h" - +#include "bridge/qscriptclassobject_p.h" +#include "bridge/qscriptdeclarativeclass_p.h" +#include "bridge/qscriptdeclarativeobject_p.h" +#include "bridge/qscriptobject_p.h" +#include "bridge/qscriptqobject_p.h" +#include "bridge/qscriptvariant_p.h" +#include "utils/qscriptdate_p.h" + +#include "DateConstructor.h" +#include "DateInstance.h" #include "Debugger.h" +#include "ErrorInstance.h" +#include "JSArray.h" #include "Lexer.h" #include "RefPtr.h" +#include "RegExpConstructor.h" +#include "RegExpObject.h" #include "SourceProvider.h" #include "Structure.h" #include "JSGlobalObject.h" @@ -83,10 +99,33 @@ namespace QScript #endif class TimeoutCheckerProxy; + qint32 ToInt32(qsreal); + quint32 ToUInt32(qsreal); + quint16 ToUInt16(qsreal); + qsreal ToInteger(qsreal); + + inline bool ToBool(qsreal); + inline bool ToBool(const QString &); + inline qint32 ToInt32(const QString &); + inline quint32 ToUInt32(const QString &); + inline quint16 ToUInt16(const QString &); + inline qsreal ToInteger(const QString &); +#ifdef Q_CC_MSVC + // MSVC2008 crashes if these are inlined. + qsreal ToNumber(const QString &); + QString ToString(qsreal); +#else + inline qsreal ToNumber(const QString &); + inline QString ToString(qsreal); +#endif + //some conversion helper functions inline QScriptEnginePrivate *scriptEngineFromExec(const JSC::ExecState *exec); bool isFunction(JSC::JSValue value); + inline void convertToLatin1_helper(const UChar *i, int length, char *s); + inline QByteArray convertToLatin1(const JSC::UString &str); + class UStringSourceProviderWithFeedback; struct GlobalClientData : public JSC::JSGlobalData::ClientData @@ -114,29 +153,72 @@ public: static QScriptEnginePrivate *get(QScriptEngine *q) { return q ? q->d_func() : 0; } static QScriptEngine *get(QScriptEnginePrivate *d) { return d ? d->q_func() : 0; } - static bool convert(const QScriptValue &value, - int type, void *ptr, - QScriptEnginePrivate *eng); - QScriptValue create(int type, const void *ptr); + static inline bool isArray(JSC::JSValue); + static inline bool isDate(JSC::JSValue); + static inline bool isError(JSC::JSValue); + static inline bool isObject(JSC::JSValue); + static inline bool isRegExp(JSC::JSValue); + static inline bool isVariant(JSC::JSValue); + static inline bool isQObject(JSC::JSValue); + static inline bool isQMetaObject(JSC::JSValue); + + static inline bool toBool(JSC::ExecState *, JSC::JSValue); + static inline qsreal toInteger(JSC::ExecState *, JSC::JSValue); + static inline qsreal toNumber(JSC::ExecState *, JSC::JSValue); + static inline qint32 toInt32(JSC::ExecState *, JSC::JSValue); + static inline quint32 toUInt32(JSC::ExecState *, JSC::JSValue); + static inline quint16 toUInt16(JSC::ExecState *, JSC::JSValue); + static inline JSC::UString toString(JSC::ExecState *, JSC::JSValue); + + static inline QDateTime toDateTime(JSC::ExecState *, JSC::JSValue); +#ifndef QT_NO_REGEXP + static QRegExp toRegExp(JSC::ExecState*, JSC::JSValue); +#endif + static QVariant toVariant(JSC::ExecState *, JSC::JSValue); + static inline QObject *toQObject(JSC::ExecState *, JSC::JSValue); + static inline const QMetaObject *toQMetaObject(JSC::ExecState *, JSC::JSValue); + + static inline JSC::JSValue property(JSC::ExecState*, JSC::JSValue, const JSC::Identifier &id, + int resolveMode = QScriptValue::ResolvePrototype); + static JSC::JSValue propertyHelper(JSC::ExecState*, JSC::JSValue, const JSC::Identifier &id, int resolveMode); + static inline JSC::JSValue property(JSC::ExecState*, JSC::JSValue, quint32 index, + int resolveMode = QScriptValue::ResolvePrototype); + static JSC::JSValue propertyHelper(JSC::ExecState*, JSC::JSValue, quint32, int resolveMode); + static inline JSC::JSValue property(JSC::ExecState*, JSC::JSValue, const JSC::UString &, int resolveMode); + static inline void setProperty(JSC::ExecState*, JSC::JSValue object, const JSC::UString &name, JSC::JSValue, + const QScriptValue::PropertyFlags &flags = QScriptValue::KeepExistingFlags); + static void setProperty(JSC::ExecState*, JSC::JSValue object, const JSC::Identifier &id, JSC::JSValue, + const QScriptValue::PropertyFlags &flags = QScriptValue::KeepExistingFlags); + static void setProperty(JSC::ExecState*, JSC::JSValue object, quint32 index, JSC::JSValue, + const QScriptValue::PropertyFlags &flags = QScriptValue::KeepExistingFlags); + static QScriptValue::PropertyFlags propertyFlags(JSC::ExecState*, JSC::JSValue value, + const JSC::Identifier &id, const QScriptValue::ResolveFlags &mode); + static inline QScriptValue::PropertyFlags propertyFlags(JSC::ExecState*, JSC::JSValue value, + const JSC::UString &name, const QScriptValue::ResolveFlags &mode); + + static bool convertValue(JSC::ExecState*, JSC::JSValue value, + int type, void *ptr); + static bool convertNumber(qsreal, int type, void *ptr); + static bool convertString(const QString &, int type, void *ptr); + static JSC::JSValue create(JSC::ExecState*, int type, const void *ptr); bool hasDemarshalFunction(int type) const; inline QScriptValue scriptValueFromJSCValue(JSC::JSValue value); inline JSC::JSValue scriptValueToJSCValue(const QScriptValue &value); - QScriptValue scriptValueFromVariant(const QVariant &value); - QVariant scriptValueToVariant(const QScriptValue &value, int targetType); - - JSC::JSValue jscValueFromVariant(const QVariant &value); - QVariant jscValueToVariant(JSC::JSValue value, int targetType); + static inline JSC::JSValue jscValueFromVariant(JSC::ExecState*, const QVariant &value); + static QVariant jscValueToVariant(JSC::ExecState*, JSC::JSValue value, int targetType); + static inline QVariant &variantValue(JSC::JSValue value); + static inline void setVariantValue(JSC::JSValue objectValue, const QVariant &value); - QScriptValue arrayFromStringList(const QStringList &lst); - static QStringList stringListFromArray(const QScriptValue &arr); + static JSC::JSValue arrayFromStringList(JSC::ExecState*, const QStringList &lst); + static QStringList stringListFromArray(JSC::ExecState*, JSC::JSValue arr); - QScriptValue arrayFromVariantList(const QVariantList &lst); - static QVariantList variantListFromArray(const QScriptValue &arr); + static JSC::JSValue arrayFromVariantList(JSC::ExecState*, const QVariantList &lst); + static QVariantList variantListFromArray(JSC::ExecState*, JSC::JSValue arr); - QScriptValue objectFromVariantMap(const QVariantMap &vmap); - static QVariantMap variantMapFromObject(const QScriptValue &obj); + static JSC::JSValue objectFromVariantMap(JSC::ExecState*, const QVariantMap &vmap); + static QVariantMap variantMapFromObject(JSC::ExecState*, JSC::JSValue obj); JSC::JSValue defaultPrototype(int metaTypeId) const; void setDefaultPrototype(int metaTypeId, JSC::JSValue prototype); @@ -162,6 +244,7 @@ public: void mark(JSC::MarkStack& markStack); bool isCollecting() const; void collectGarbage(); + void reportAdditionalMemoryCost(int size); //flags that we set on the return value register for native function. (ie when codeBlock is 0) enum ContextFlags { @@ -177,10 +260,44 @@ public: void agentDeleted(QScriptEngineAgent *agent); + static inline void saveException(JSC::ExecState *, JSC::JSValue *); + static inline void restoreException(JSC::ExecState *, JSC::JSValue); + void setCurrentException(QScriptValue exception) { m_currentException = exception; } QScriptValue currentException() const { return m_currentException; } void clearCurrentException() { m_currentException.d_ptr.reset(); } + static QScriptSyntaxCheckResult checkSyntax(const QString &program); + static bool canEvaluate(const QString &program); + + inline QScriptValuePrivate *allocateScriptValuePrivate(size_t); + inline void freeScriptValuePrivate(QScriptValuePrivate *p); + + inline void registerScriptValue(QScriptValuePrivate *value); + inline void unregisterScriptValue(QScriptValuePrivate *value); + void detachAllRegisteredScriptValues(); + + inline void registerScriptString(QScriptStringPrivate *value); + inline void unregisterScriptString(QScriptStringPrivate *value); + void detachAllRegisteredScriptStrings(); + QScriptString toStringHandle(const JSC::Identifier &name); + + static inline JSC::JSValue newArray(JSC::ExecState *, uint length); + static inline JSC::JSValue newDate(JSC::ExecState *, qsreal value); + static inline JSC::JSValue newDate(JSC::ExecState *, const QDateTime &); + inline JSC::JSValue newObject(); + +#ifndef QT_NO_REGEXP + static JSC::JSValue newRegExp(JSC::ExecState *, const QRegExp &); +#endif + + static JSC::JSValue newRegExp(JSC::ExecState *, const QString &pattern, const QString &flags); + JSC::JSValue newVariant(const QVariant &); + JSC::JSValue newVariant(JSC::JSValue objectValue, const QVariant &); + + static inline QScriptDeclarativeClass *declarativeClass(JSC::JSValue); + static inline QScriptDeclarativeClass::Object *declarativeObject(JSC::JSValue); + #ifndef QT_NO_QOBJECT JSC::JSValue newQObject(QObject *object, QScriptEngine::ValueOwnership ownership = QScriptEngine::QtOwnership, @@ -188,9 +305,7 @@ public: JSC::JSValue newQMetaObject(const QMetaObject *metaObject, JSC::JSValue ctor); - static QScriptSyntaxCheckResult checkSyntax(const QString &program); - static bool canEvaluate(const QString &program); - static bool convertToNativeQObject(const QScriptValue &value, + static bool convertToNativeQObject(JSC::ExecState*, JSC::JSValue, const QByteArray &targetType, void **result); @@ -220,17 +335,6 @@ public: bool scriptDisconnect(JSC::JSValue signal, JSC::JSValue receiver, JSC::JSValue function); - inline QScriptValuePrivate *allocateScriptValuePrivate(size_t); - inline void freeScriptValuePrivate(QScriptValuePrivate *p); - - inline void registerScriptValue(QScriptValuePrivate *value); - inline void unregisterScriptValue(QScriptValuePrivate *value); - void detachAllRegisteredScriptValues(); - - inline void registerScriptString(QScriptStringPrivate *value); - inline void unregisterScriptString(QScriptStringPrivate *value); - void detachAllRegisteredScriptStrings(); - // private slots void _q_objectDestroyed(QObject *); #endif @@ -281,6 +385,23 @@ public: namespace QScript { +class APIShim +{ +public: + APIShim(QScriptEnginePrivate *engine) + : m_engine(engine), m_oldTable(JSC::setCurrentIdentifierTable(engine->globalData->identifierTable)) + { + } + ~APIShim() + { + JSC::setCurrentIdentifierTable(m_oldTable); + } + +private: + QScriptEnginePrivate *m_engine; + JSC::IdentifierTable *m_oldTable; +}; + /*Helper class. Main purpose is to give debugger feedback about unloading and loading scripts. It keeps pointer to JSGlobalObject assuming that it is always the same - there is no way to update this data. Class is internal and used as an implementation detail in and only in QScriptEngine::evaluate.*/ @@ -372,6 +493,66 @@ inline QScriptEnginePrivate *scriptEngineFromExec(const JSC::ExecState *exec) return static_cast<GlobalClientData*>(exec->globalData().clientData)->engine; } +#ifndef Q_CC_MSVC +// MSVC2008 crashes if these are inlined. + +inline QString ToString(qsreal value) +{ + return JSC::UString::from(value); +} + +inline qsreal ToNumber(const QString &value) +{ + return ((JSC::UString)value).toDouble(); +} + +#endif + +inline qint32 ToInt32(const QString &value) +{ + return ToInt32(ToNumber(value)); +} + +inline quint32 ToUInt32(const QString &value) +{ + return ToUInt32(ToNumber(value)); +} + +inline quint16 ToUInt16(const QString &value) +{ + return ToUInt16(ToNumber(value)); +} + +inline qsreal ToInteger(const QString &value) +{ + return ToInteger(ToNumber(value)); +} + +inline bool ToBool(qsreal value) +{ + return (value != 0) && !qIsNaN(value); +} + +inline bool ToBool(const QString &value) +{ + return !value.isEmpty(); +} + +inline void convertToLatin1_helper(const UChar *i, int length, char *s) +{ + const UChar *e = i + length; + while (i != e) + *(s++) = (uchar) *(i++); + *s = '\0'; +} + +inline QByteArray convertToLatin1(const JSC::UString &str) +{ + QByteArray ba(str.size(), Qt::Uninitialized); + convertToLatin1_helper(str.data(), str.size(), ba.data()); + return ba; +} + } // namespace QScript inline QScriptValuePrivate *QScriptEnginePrivate::allocateScriptValuePrivate(size_t size) @@ -417,6 +598,13 @@ inline void QScriptEnginePrivate::unregisterScriptValue(QScriptValuePrivate *val value->next = 0; } +inline JSC::JSValue QScriptEnginePrivate::jscValueFromVariant(JSC::ExecState *exec, const QVariant &v) +{ + JSC::JSValue result = create(exec, v.userType(), v.data()); + Q_ASSERT(result); + return result; +} + inline QScriptValue QScriptEnginePrivate::scriptValueFromJSCValue(JSC::JSValue value) { if (!value) @@ -478,32 +666,83 @@ inline void QScriptValuePrivate::initFrom(const QString &value) engine->registerScriptValue(this); } -inline QScriptValue QScriptValuePrivate::property(const QString &name, int resolveMode) const +inline JSC::JSValue QScriptEnginePrivate::property(JSC::ExecState *exec, JSC::JSValue value, const JSC::UString &name, int resolveMode) { - JSC::ExecState *exec = engine->currentFrame; - return property(JSC::Identifier(exec, name), resolveMode); + return property(exec, value, JSC::Identifier(exec, name), resolveMode); } -inline QScriptValue QScriptValuePrivate::property(const JSC::Identifier &id, int resolveMode) const +inline JSC::JSValue QScriptEnginePrivate::property(JSC::ExecState *exec, JSC::JSValue value, const JSC::Identifier &id, int resolveMode) { - Q_ASSERT(isObject()); - JSC::ExecState *exec = engine->currentFrame; - JSC::JSObject *object = JSC::asObject(jscValue); + Q_ASSERT(isObject(value)); + JSC::JSObject *object = JSC::asObject(value); JSC::PropertySlot slot(object); if ((resolveMode & QScriptValue::ResolvePrototype) && object->getPropertySlot(exec, id, slot)) - return engine->scriptValueFromJSCValue(slot.getValue(exec, id)); - return propertyHelper(id, resolveMode); + return slot.getValue(exec, id); + return propertyHelper(exec, value, id, resolveMode); } -inline QScriptValue QScriptValuePrivate::property(quint32 index, int resolveMode) const +inline JSC::JSValue QScriptEnginePrivate::property(JSC::ExecState *exec, JSC::JSValue value, quint32 index, int resolveMode) { - Q_ASSERT(isObject()); - JSC::ExecState *exec = engine->currentFrame; - JSC::JSObject *object = JSC::asObject(jscValue); + Q_ASSERT(isObject(value)); + JSC::JSObject *object = JSC::asObject(value); JSC::PropertySlot slot(object); if ((resolveMode & QScriptValue::ResolvePrototype) && object->getPropertySlot(exec, index, slot)) - return engine->scriptValueFromJSCValue(slot.getValue(exec, index)); - return propertyHelper(index, resolveMode); + return slot.getValue(exec, index); + return propertyHelper(exec, value, index, resolveMode); +} + +inline QScriptValue::PropertyFlags QScriptEnginePrivate::propertyFlags(JSC::ExecState *exec, JSC::JSValue value, + const JSC::UString &name, + const QScriptValue::ResolveFlags &mode) +{ + return propertyFlags(exec, value, JSC::Identifier(exec, name), mode); +} + +inline void QScriptEnginePrivate::setProperty(JSC::ExecState *exec, JSC::JSValue objectValue, const JSC::UString &name, + JSC::JSValue value, const QScriptValue::PropertyFlags &flags) +{ + setProperty(exec, objectValue, JSC::Identifier(exec, name), value, flags); +} + +inline JSC::JSValue QScriptValuePrivate::property(const JSC::Identifier &id, const QScriptValue::ResolveFlags &resolveMode) const +{ + return QScriptEnginePrivate::property(engine->currentFrame, jscValue, id, resolveMode); +} + +inline JSC::JSValue QScriptValuePrivate::property(quint32 index, const QScriptValue::ResolveFlags &resolveMode) const +{ + return QScriptEnginePrivate::property(engine->currentFrame, jscValue, index, resolveMode); +} + +inline JSC::JSValue QScriptValuePrivate::property(const JSC::UString &name, const QScriptValue::ResolveFlags &resolveMode) const +{ + JSC::ExecState *exec = engine->currentFrame; + return QScriptEnginePrivate::property(exec, jscValue, JSC::Identifier(exec, name), resolveMode); +} + +inline QScriptValue::PropertyFlags QScriptValuePrivate::propertyFlags( + const JSC::Identifier &id, const QScriptValue::ResolveFlags &mode) const +{ + return QScriptEnginePrivate::propertyFlags(engine->currentFrame, jscValue, id, mode); +} + +inline void QScriptValuePrivate::setProperty(const JSC::Identifier &id, const JSC::JSValue &value, + const QScriptValue::PropertyFlags &flags) +{ + QScriptEnginePrivate::setProperty(engine->currentFrame, jscValue, id, value, flags); +} + +inline void QScriptValuePrivate::setProperty(quint32 index, const JSC::JSValue &value, + const QScriptValue::PropertyFlags &flags) +{ + QScriptEnginePrivate::setProperty(engine->currentFrame, jscValue, index, value, flags); +} + +inline void QScriptValuePrivate::setProperty(const JSC::UString &name, const JSC::JSValue &value, + const QScriptValue::PropertyFlags &flags) +{ + JSC::ExecState *exec = engine->currentFrame; + QScriptEnginePrivate::setProperty(exec, jscValue, JSC::Identifier(exec, name), value, flags); } inline void* QScriptValuePrivate::operator new(size_t size, QScriptEnginePrivate *engine) @@ -522,7 +761,7 @@ inline void QScriptValuePrivate::operator delete(void *ptr) qFree(d); } -inline void QScriptValuePrivate::saveException(JSC::ExecState *exec, JSC::JSValue *val) +inline void QScriptEnginePrivate::saveException(JSC::ExecState *exec, JSC::JSValue *val) { if (exec) { *val = exec->exception(); @@ -532,7 +771,7 @@ inline void QScriptValuePrivate::saveException(JSC::ExecState *exec, JSC::JSValu } } -inline void QScriptValuePrivate::restoreException(JSC::ExecState *exec, JSC::JSValue val) +inline void QScriptEnginePrivate::restoreException(JSC::ExecState *exec, JSC::JSValue val) { if (exec && val) exec->setException(val); @@ -586,6 +825,231 @@ inline JSC::ExecState *QScriptEnginePrivate::globalExec() const return originalGlobalObject()->globalExec(); } +inline JSC::JSValue QScriptEnginePrivate::newArray(JSC::ExecState *exec, uint length) +{ + return JSC::constructEmptyArray(exec, length); +} + +inline JSC::JSValue QScriptEnginePrivate::newDate(JSC::ExecState *exec, qsreal value) +{ + JSC::JSValue val = JSC::jsNumber(exec, value); + JSC::ArgList args(&val, 1); + return JSC::constructDate(exec, args); +} + +inline JSC::JSValue QScriptEnginePrivate::newDate(JSC::ExecState *exec, const QDateTime &value) +{ + return newDate(exec, QScript::FromDateTime(value)); +} + +inline JSC::JSValue QScriptEnginePrivate::newObject() +{ + return new (currentFrame)QScriptObject(scriptObjectStructure); +} + +inline bool QScriptEnginePrivate::isObject(JSC::JSValue value) +{ + return value && value.isObject(); +} + +inline bool QScriptEnginePrivate::isArray(JSC::JSValue value) +{ + return isObject(value) && value.inherits(&JSC::JSArray::info); +} + +inline bool QScriptEnginePrivate::isDate(JSC::JSValue value) +{ + return isObject(value) && value.inherits(&JSC::DateInstance::info); +} + +inline bool QScriptEnginePrivate::isError(JSC::JSValue value) +{ + return isObject(value) && value.inherits(&JSC::ErrorInstance::info); +} + +inline bool QScriptEnginePrivate::isRegExp(JSC::JSValue value) +{ + return isObject(value) && value.inherits(&JSC::RegExpObject::info); +} + +inline bool QScriptEnginePrivate::isVariant(JSC::JSValue value) +{ + if (!isObject(value) || !value.inherits(&QScriptObject::info)) + return false; + QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(value)); + QScriptObjectDelegate *delegate = object->delegate(); + return (delegate && (delegate->type() == QScriptObjectDelegate::Variant)); +} + +inline bool QScriptEnginePrivate::isQObject(JSC::JSValue value) +{ +#ifndef QT_NO_QOBJECT + if (!isObject(value) || !value.inherits(&QScriptObject::info)) + return false; + QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(value)); + QScriptObjectDelegate *delegate = object->delegate(); + return (delegate && (delegate->type() == QScriptObjectDelegate::QtObject || + (delegate->type() == QScriptObjectDelegate::DeclarativeClassObject && + static_cast<QScript::DeclarativeObjectDelegate*>(delegate)->scriptClass()->isQObject()))); +#else + return false; +#endif +} + +inline bool QScriptEnginePrivate::isQMetaObject(JSC::JSValue value) +{ +#ifndef QT_NO_QOBJECT + return isObject(value) && JSC::asObject(value)->inherits(&QScript::QMetaObjectWrapperObject::info); +#else + return false; +#endif +} + +inline bool QScriptEnginePrivate::toBool(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + bool result = value.toBoolean(exec); + restoreException(exec, savedException); + return result; +} + +inline qsreal QScriptEnginePrivate::toInteger(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + qsreal result = value.toInteger(exec); + restoreException(exec, savedException); + return result; +} + +inline qsreal QScriptEnginePrivate::toNumber(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + qsreal result = value.toNumber(exec); + restoreException(exec, savedException); + return result; +} + +inline qint32 QScriptEnginePrivate::toInt32(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + qint32 result = value.toInt32(exec); + restoreException(exec, savedException); + return result; +} + +inline quint32 QScriptEnginePrivate::toUInt32(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + quint32 result = value.toUInt32(exec); + restoreException(exec, savedException); + return result; +} + +inline quint16 QScriptEnginePrivate::toUInt16(JSC::ExecState *exec, JSC::JSValue value) +{ + // ### no equivalent function in JSC + return QScript::ToUInt16(toNumber(exec, value)); +} + +inline JSC::UString QScriptEnginePrivate::toString(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + JSC::UString str = value.toString(exec); + if (exec && exec->hadException() && !str.size()) { + JSC::JSValue savedException2; + saveException(exec, &savedException2); + str = savedException2.toString(exec); + restoreException(exec, savedException2); + } + if (savedException) + restoreException(exec, savedException); + return str; +} + +inline QDateTime QScriptEnginePrivate::toDateTime(JSC::ExecState *, JSC::JSValue value) +{ + if (!isDate(value)) + return QDateTime(); + qsreal t = static_cast<JSC::DateInstance*>(JSC::asObject(value))->internalNumber(); + return QScript::ToDateTime(t, Qt::LocalTime); +} + +inline QObject *QScriptEnginePrivate::toQObject(JSC::ExecState *exec, JSC::JSValue value) +{ +#ifndef QT_NO_QOBJECT + if (isObject(value) && value.inherits(&QScriptObject::info)) { + QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(value)); + QScriptObjectDelegate *delegate = object->delegate(); + if (!delegate) + return 0; + if (delegate->type() == QScriptObjectDelegate::QtObject) + return static_cast<QScript::QObjectDelegate*>(delegate)->value(); + if (delegate->type() == QScriptObjectDelegate::DeclarativeClassObject) + return static_cast<QScript::DeclarativeObjectDelegate*>(delegate)->scriptClass()->toQObject(declarativeObject(value)); + if (delegate->type() == QScriptObjectDelegate::Variant) { + QVariant var = variantValue(value); + int type = var.userType(); + if ((type == QMetaType::QObjectStar) || (type == QMetaType::QWidgetStar)) + return *reinterpret_cast<QObject* const *>(var.constData()); + } + } +#endif + return 0; +} + +inline const QMetaObject *QScriptEnginePrivate::toQMetaObject(JSC::ExecState*, JSC::JSValue value) +{ +#ifndef QT_NO_QOBJECT + if (isQMetaObject(value)) + return static_cast<QScript::QMetaObjectWrapperObject*>(JSC::asObject(value))->value(); +#endif + return 0; +} + +inline QVariant &QScriptEnginePrivate::variantValue(JSC::JSValue value) +{ + Q_ASSERT(value.inherits(&QScriptObject::info)); + QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(value))->delegate(); + Q_ASSERT(delegate && (delegate->type() == QScriptObjectDelegate::Variant)); + return static_cast<QScript::QVariantDelegate*>(delegate)->value(); +} + +inline void QScriptEnginePrivate::setVariantValue(JSC::JSValue objectValue, const QVariant &value) +{ + Q_ASSERT(objectValue.inherits(&QScriptObject::info)); + QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(objectValue))->delegate(); + Q_ASSERT(delegate && (delegate->type() == QScriptObjectDelegate::Variant)); + static_cast<QScript::QVariantDelegate*>(delegate)->setValue(value); +} + +inline QScriptDeclarativeClass *QScriptEnginePrivate::declarativeClass(JSC::JSValue v) +{ + if (!QScriptEnginePrivate::isObject(v) || !v.inherits(&QScriptObject::info)) + return 0; + QScriptObject *scriptObject = static_cast<QScriptObject*>(JSC::asObject(v)); + QScriptObjectDelegate *delegate = scriptObject->delegate(); + if (!delegate || (delegate->type() != QScriptObjectDelegate::DeclarativeClassObject)) + return 0; + return static_cast<QScript::DeclarativeObjectDelegate*>(delegate)->scriptClass(); +} + +inline QScriptDeclarativeClass::Object *QScriptEnginePrivate::declarativeObject(JSC::JSValue v) +{ + if (!QScriptEnginePrivate::isObject(v) || !v.inherits(&QScriptObject::info)) + return 0; + QScriptObject *scriptObject = static_cast<QScriptObject*>(JSC::asObject(v)); + QScriptObjectDelegate *delegate = scriptObject->delegate(); + if (!delegate || (delegate->type() != QScriptObjectDelegate::DeclarativeClassObject)) + return 0; + return static_cast<QScript::DeclarativeObjectDelegate*>(delegate)->object(); +} + QT_END_NAMESPACE #endif diff --git a/src/script/api/qscriptengineagent.cpp b/src/script/api/qscriptengineagent.cpp index 3bd97eb..28905e8 100644 --- a/src/script/api/qscriptengineagent.cpp +++ b/src/script/api/qscriptengineagent.cpp @@ -151,14 +151,15 @@ void QScriptEngineAgentPrivate::exceptionCatch(const JSC::DebuggerCallFrame& fra engine->clearCurrentException(); } -void QScriptEngineAgentPrivate::atStatement(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineno, int column) +void QScriptEngineAgentPrivate::atStatement(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineno/*, int column*/) { QScript::UStringSourceProviderWithFeedback *source = engine->loadedScripts.value(sourceID); if (!source) { // QTBUG-6108: We don't have the source for this script, so ignore. return; } - column = source->columnNumberFromOffset(column); +// column = source->columnNumberFromOffset(column); + int column = 1; JSC::CallFrame *oldFrame = engine->currentFrame; int oldAgentLineNumber = engine->agentLineNumber; engine->currentFrame = frame.callFrame(); @@ -182,7 +183,7 @@ void QScriptEngineAgentPrivate::evaluateStop(const JSC::JSValue& returnValue, in } void QScriptEngineAgentPrivate::didReachBreakpoint(const JSC::DebuggerCallFrame& frame, - intptr_t sourceID, int lineno, int column) + intptr_t sourceID, int lineno/*, int column*/) { if (q_ptr->supportsExtension(QScriptEngineAgent::DebuggerInvocationRequest)) { QScript::UStringSourceProviderWithFeedback *source = engine->loadedScripts.value(sourceID); @@ -190,7 +191,8 @@ void QScriptEngineAgentPrivate::didReachBreakpoint(const JSC::DebuggerCallFrame& // QTBUG-6108: We don't have the source for this script, so ignore. return; } - column = source->columnNumberFromOffset(column); +// column = source->columnNumberFromOffset(column); + int column = 1; JSC::CallFrame *oldFrame = engine->currentFrame; int oldAgentLineNumber = engine->agentLineNumber; engine->currentFrame = frame.callFrame(); diff --git a/src/script/api/qscriptengineagent_p.h b/src/script/api/qscriptengineagent_p.h index 8354143..fd10e68 100644 --- a/src/script/api/qscriptengineagent_p.h +++ b/src/script/api/qscriptengineagent_p.h @@ -75,17 +75,18 @@ public: }; //exceptions - virtual void exception(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineno) + virtual void exception(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineno, bool hasHandler) { Q_UNUSED(frame); Q_UNUSED(sourceID); Q_UNUSED(lineno); + Q_UNUSED(hasHandler); }; virtual void exceptionThrow(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, bool hasHandler); virtual void exceptionCatch(const JSC::DebuggerCallFrame& frame, intptr_t sourceID); //statements - virtual void atStatement(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineno, int column); + virtual void atStatement(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineno/*, int column*/); virtual void callEvent(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineno) { Q_UNUSED(lineno); @@ -107,7 +108,7 @@ public: }; virtual void functionExit(const JSC::JSValue& returnValue, intptr_t sourceID); //others - virtual void didReachBreakpoint(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineno, int column); + virtual void didReachBreakpoint(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineno/*, int column*/); virtual void evaluateStart(intptr_t sourceID) { diff --git a/src/script/api/qscriptprogram.cpp b/src/script/api/qscriptprogram.cpp index c452052..8d4de11 100644 --- a/src/script/api/qscriptprogram.cpp +++ b/src/script/api/qscriptprogram.cpp @@ -63,7 +63,6 @@ QScriptProgramPrivate::QScriptProgramPrivate(const QString &src, QScriptProgramPrivate::~QScriptProgramPrivate() { - delete _executable; } QScriptProgramPrivate *QScriptProgramPrivate::get(const QScriptProgram &q) @@ -76,17 +75,17 @@ JSC::EvalExecutable *QScriptProgramPrivate::executable(JSC::ExecState *exec, { if (_executable) { if (eng == engine) - return _executable; - delete _executable; + return _executable.get(); + _executable = 0; } WTF::PassRefPtr<QScript::UStringSourceProviderWithFeedback> provider = QScript::UStringSourceProviderWithFeedback::create(sourceCode, fileName, firstLineNumber, eng); sourceId = provider->asID(); JSC::SourceCode source(provider, firstLineNumber); //after construction of SourceCode provider variable will be null. - _executable = new JSC::EvalExecutable(exec, source); + _executable = JSC::EvalExecutable::create(exec, source); engine = eng; isCompiled = false; - return _executable; + return _executable.get(); } /*! @@ -121,7 +120,7 @@ QScriptProgram::QScriptProgram(const QScriptProgram &other) */ QScriptProgram::~QScriptProgram() { - Q_D(QScriptProgram); + // Q_D(QScriptProgram); // if (d->engine && (d->ref == 1)) // d->engine->unregisterScriptProgram(d); } diff --git a/src/script/api/qscriptprogram_p.h b/src/script/api/qscriptprogram_p.h index 427ab34..95e75fd 100644 --- a/src/script/api/qscriptprogram_p.h +++ b/src/script/api/qscriptprogram_p.h @@ -37,6 +37,8 @@ #include <QtCore/qobjectdefs.h> +#include "RefPtr.h" + namespace JSC { class EvalExecutable; @@ -67,7 +69,7 @@ public: int firstLineNumber; QScriptEnginePrivate *engine; - JSC::EvalExecutable *_executable; + WTF::RefPtr<JSC::EvalExecutable> _executable; intptr_t sourceId; bool isCompiled; }; diff --git a/src/script/api/qscriptstring.cpp b/src/script/api/qscriptstring.cpp index 7978b61..8c7c30c 100644 --- a/src/script/api/qscriptstring.cpp +++ b/src/script/api/qscriptstring.cpp @@ -92,8 +92,12 @@ QScriptString::~QScriptString() d->ref.ref(); // avoid deletion break; case QScriptStringPrivate::HeapAllocated: - if (d->engine && (d->ref == 1)) + if (d->engine && (d->ref == 1)) { + // Make sure the identifier is removed from the correct engine. + QScript::APIShim shim(d->engine); + d->identifier = JSC::Identifier(); d->engine->unregisterScriptString(d); + } break; } } diff --git a/src/script/api/qscriptvalue.cpp b/src/script/api/qscriptvalue.cpp index 79d5dcb..1310c8c 100644 --- a/src/script/api/qscriptvalue.cpp +++ b/src/script/api/qscriptvalue.cpp @@ -29,33 +29,19 @@ #include "qscriptengine_p.h" #include "qscriptstring_p.h" -#include "JSArray.h" #include "JSGlobalObject.h" #include "JSImmediate.h" #include "JSObject.h" #include "JSValue.h" #include "JSFunction.h" -#include "DateInstance.h" -#include "ErrorInstance.h" -#include "RegExpObject.h" #include "Identifier.h" #include "Operations.h" #include "Arguments.h" -#include <QtCore/qdatetime.h> -#include <QtCore/qregexp.h> #include <QtCore/qvariant.h> #include <QtCore/qvarlengtharray.h> #include <QtCore/qnumeric.h> -#include "utils/qscriptdate_p.h" -#include "bridge/qscriptobject_p.h" -#include "bridge/qscriptclassobject_p.h" -#include "bridge/qscriptvariant_p.h" -#include "bridge/qscriptqobject_p.h" -#include "bridge/qscriptdeclarativeclass_p.h" -#include "bridge/qscriptdeclarativeobject_p.h" - /*! \since 4.3 \class QScriptValue @@ -180,248 +166,8 @@ \omitvalue ResolveFull Check the object's own properties first, then search the prototype chain, and finally search the scope chain. */ -// ### move - -#include <QtCore/qnumeric.h> -#include <math.h> - QT_BEGIN_NAMESPACE -namespace QScript -{ - -static const qsreal D32 = 4294967296.0; - -qint32 ToInt32(qsreal n) -{ - if (qIsNaN(n) || qIsInf(n) || (n == 0)) - return 0; - - qsreal sign = (n < 0) ? -1.0 : 1.0; - qsreal abs_n = fabs(n); - - n = ::fmod(sign * ::floor(abs_n), D32); - const double D31 = D32 / 2.0; - - if (sign == -1 && n < -D31) - n += D32; - - else if (sign != -1 && n >= D31) - n -= D32; - - return qint32 (n); -} - -quint32 ToUint32(qsreal n) -{ - if (qIsNaN(n) || qIsInf(n) || (n == 0)) - return 0; - - qsreal sign = (n < 0) ? -1.0 : 1.0; - qsreal abs_n = fabs(n); - - n = ::fmod(sign * ::floor(abs_n), D32); - - if (n < 0) - n += D32; - - return quint32 (n); -} - -quint16 ToUint16(qsreal n) -{ - static const qsreal D16 = 65536.0; - - if (qIsNaN(n) || qIsInf(n) || (n == 0)) - return 0; - - qsreal sign = (n < 0) ? -1.0 : 1.0; - qsreal abs_n = fabs(n); - - n = ::fmod(sign * ::floor(abs_n), D16); - - if (n < 0) - n += D16; - - return quint16 (n); -} - -qsreal ToInteger(qsreal n) -{ - if (qIsNaN(n)) - return 0; - - if (n == 0 || qIsInf(n)) - return n; - - int sign = n < 0 ? -1 : 1; - return sign * ::floor(::fabs(n)); -} - -} // namespace QScript - -QScriptValue QScriptValuePrivate::propertyHelper(const JSC::Identifier &id, int resolveMode) const -{ - JSC::JSValue result; - if (!(resolveMode & QScriptValue::ResolvePrototype)) { - // Look in the object's own properties - JSC::ExecState *exec = engine->currentFrame; - JSC::JSObject *object = JSC::asObject(jscValue); - JSC::PropertySlot slot(object); - if (object->getOwnPropertySlot(exec, id, slot)) - result = slot.getValue(exec, id); - } - if (!result && (resolveMode & QScriptValue::ResolveScope)) { - // ### check if it's a function object and look in the scope chain - QScriptValue scope = property(QString::fromLatin1("__qt_scope__"), QScriptValue::ResolveLocal); - if (scope.isObject()) - result = engine->scriptValueToJSCValue(QScriptValuePrivate::get(scope)->property(id, resolveMode)); - } - return engine->scriptValueFromJSCValue(result); -} - -QScriptValue QScriptValuePrivate::propertyHelper(quint32 index, int resolveMode) const -{ - JSC::JSValue result; - if (!(resolveMode & QScriptValue::ResolvePrototype)) { - // Look in the object's own properties - JSC::ExecState *exec = engine->currentFrame; - JSC::JSObject *object = JSC::asObject(jscValue); - JSC::PropertySlot slot(object); - if (object->getOwnPropertySlot(exec, index, slot)) - result = slot.getValue(exec, index); - } - return engine->scriptValueFromJSCValue(result); -} - -void QScriptValuePrivate::setProperty(const JSC::Identifier &id, const QScriptValue &value, - const QScriptValue::PropertyFlags &flags) -{ - QScriptEnginePrivate *valueEngine = QScriptValuePrivate::getEngine(value); - if (valueEngine && (valueEngine != engine)) { - qWarning("QScriptValue::setProperty(%s) failed: " - "cannot set value created in a different engine", - qPrintable(QString(id.ustring()))); - return; - } - JSC::ExecState *exec = engine->currentFrame; - JSC::JSValue jsValue = engine->scriptValueToJSCValue(value); - JSC::JSObject *thisObject = JSC::asObject(jscValue); - JSC::JSValue setter = thisObject->lookupSetter(exec, id); - JSC::JSValue getter = thisObject->lookupGetter(exec, id); - if ((flags & QScriptValue::PropertyGetter) || (flags & QScriptValue::PropertySetter)) { - if (!jsValue) { - // deleting getter/setter - if ((flags & QScriptValue::PropertyGetter) && (flags & QScriptValue::PropertySetter)) { - // deleting both: just delete the property - thisObject->deleteProperty(exec, id, /*checkDontDelete=*/false); - } else if (flags & QScriptValue::PropertyGetter) { - // preserve setter, if there is one - thisObject->deleteProperty(exec, id, /*checkDontDelete=*/false); - if (setter && setter.isObject()) - thisObject->defineSetter(exec, id, JSC::asObject(setter)); - } else { // flags & QScriptValue::PropertySetter - // preserve getter, if there is one - thisObject->deleteProperty(exec, id, /*checkDontDelete=*/false); - if (getter && getter.isObject()) - thisObject->defineGetter(exec, id, JSC::asObject(getter)); - } - } else { - if (jsValue.isObject()) { // ### should check if it has callData() - // defining getter/setter - if (id == exec->propertyNames().underscoreProto) { - qWarning("QScriptValue::setProperty() failed: " - "cannot set getter or setter of native property `__proto__'"); - } else { - if (flags & QScriptValue::PropertyGetter) - thisObject->defineGetter(exec, id, JSC::asObject(jsValue)); - if (flags & QScriptValue::PropertySetter) - thisObject->defineSetter(exec, id, JSC::asObject(jsValue)); - } - } else { - qWarning("QScriptValue::setProperty(): getter/setter must be a function"); - } - } - } else { - // setting the value - if (getter && getter.isObject() && !(setter && setter.isObject())) { - qWarning("QScriptValue::setProperty() failed: " - "property '%s' has a getter but no setter", - qPrintable(QString(id.ustring()))); - return; - } - if (!jsValue) { - // ### check if it's a getter/setter property - thisObject->deleteProperty(exec, id, /*checkDontDelete=*/false); - } else if (flags != QScriptValue::KeepExistingFlags) { - if (thisObject->hasOwnProperty(exec, id)) - thisObject->deleteProperty(exec, id, /*checkDontDelete=*/false); // ### hmmm - can't we just update the attributes? - unsigned attribs = 0; - if (flags & QScriptValue::ReadOnly) - attribs |= JSC::ReadOnly; - if (flags & QScriptValue::SkipInEnumeration) - attribs |= JSC::DontEnum; - if (flags & QScriptValue::Undeletable) - attribs |= JSC::DontDelete; - attribs |= flags & QScriptValue::UserRange; - thisObject->putWithAttributes(exec, id, jsValue, attribs); - } else { - JSC::PutPropertySlot slot; - thisObject->put(exec, id, jsValue, slot); - } - } -} - -QScriptValue::PropertyFlags QScriptValuePrivate::propertyFlags(const JSC::Identifier &id, - const QScriptValue::ResolveFlags &mode) const -{ - JSC::ExecState *exec = engine->currentFrame; - JSC::JSObject *object = JSC::asObject(jscValue); - unsigned attribs = 0; - JSC::PropertyDescriptor descriptor; - if (object->getOwnPropertyDescriptor(exec, id, descriptor)) - attribs = descriptor.attributes(); - else if (!object->getPropertyAttributes(exec, id, attribs)) { - if ((mode & QScriptValue::ResolvePrototype) && object->prototype() && object->prototype().isObject()) { - QScriptValue proto = engine->scriptValueFromJSCValue(object->prototype()); - return QScriptValuePrivate::get(proto)->propertyFlags(id, mode); - } - return 0; - } - QScriptValue::PropertyFlags result = 0; - if (attribs & JSC::ReadOnly) - result |= QScriptValue::ReadOnly; - if (attribs & JSC::DontEnum) - result |= QScriptValue::SkipInEnumeration; - if (attribs & JSC::DontDelete) - result |= QScriptValue::Undeletable; - //We cannot rely on attribs JSC::Setter/Getter because they are not necesserly set by JSC (bug?) - if (attribs & JSC::Getter || !object->lookupGetter(exec, id).isUndefinedOrNull()) - result |= QScriptValue::PropertyGetter; - if (attribs & JSC::Setter || !object->lookupSetter(exec, id).isUndefinedOrNull()) - result |= QScriptValue::PropertySetter; - if (attribs & QScript::QObjectMemberAttribute) - result |= QScriptValue::QObjectMember; - result |= QScriptValue::PropertyFlag(attribs & QScriptValue::UserRange); - return result; -} - -QVariant &QScriptValuePrivate::variantValue() const -{ - Q_ASSERT(jscValue.inherits(&QScriptObject::info)); - QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(jscValue))->delegate(); - Q_ASSERT(delegate && (delegate->type() == QScriptObjectDelegate::Variant)); - return static_cast<QScript::QVariantDelegate*>(delegate)->value(); -} - -void QScriptValuePrivate::setVariantValue(const QVariant &value) -{ - Q_ASSERT(jscValue.inherits(&QScriptObject::info)); - QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(jscValue))->delegate(); - Q_ASSERT(delegate && (delegate->type() == QScriptObjectDelegate::Variant)); - static_cast<QScript::QVariantDelegate*>(delegate)->setValue(value); -} - void QScriptValuePrivate::detachFromEngine() { if (isJSC()) @@ -707,9 +453,9 @@ QScriptValue &QScriptValue::operator=(const QScriptValue &other) bool QScriptValue::isError() const { Q_D(const QScriptValue); - if (!d || !d->isObject()) + if (!d || !d->isJSC()) return false; - return d->jscValue.inherits(&JSC::ErrorInstance::info); + return QScriptEnginePrivate::isError(d->jscValue); } /*! @@ -721,9 +467,9 @@ bool QScriptValue::isError() const bool QScriptValue::isArray() const { Q_D(const QScriptValue); - if (!d || !d->isObject()) + if (!d || !d->isJSC()) return false; - return d->jscValue.inherits(&JSC::JSArray::info); + return QScriptEnginePrivate::isArray(d->jscValue); } /*! @@ -735,9 +481,9 @@ bool QScriptValue::isArray() const bool QScriptValue::isDate() const { Q_D(const QScriptValue); - if (!d || !d->isObject()) + if (!d || !d->isJSC()) return false; - return d->jscValue.inherits(&JSC::DateInstance::info); + return QScriptEnginePrivate::isDate(d->jscValue); } /*! @@ -749,9 +495,9 @@ bool QScriptValue::isDate() const bool QScriptValue::isRegExp() const { Q_D(const QScriptValue); - if (!d || !d->isObject()) + if (!d || !d->isJSC()) return false; - return d->jscValue.inherits(&JSC::RegExpObject::info); + return QScriptEnginePrivate::isRegExp(d->jscValue); } /*! @@ -824,8 +570,10 @@ QScriptValue QScriptValue::scope() const Q_D(const QScriptValue); if (!d || !d->isObject()) return QScriptValue(); + QScript::APIShim shim(d->engine); // ### make hidden property - return d->property(QLatin1String("__qt_scope__"), QScriptValue::ResolveLocal); + JSC::JSValue result = d->property("__qt_scope__", QScriptValue::ResolveLocal); + return d->engine->scriptValueFromJSCValue(result); } /*! @@ -912,16 +660,17 @@ static Type type(const QScriptValue &v) return Object; } -QScriptValue ToPrimitive(const QScriptValue &object, JSC::PreferredPrimitiveType hint = JSC::NoPreference) +static QScriptValue ToPrimitive(const QScriptValue &object, JSC::PreferredPrimitiveType hint = JSC::NoPreference) { Q_ASSERT(object.isObject()); QScriptValuePrivate *pp = QScriptValuePrivate::get(object); Q_ASSERT(pp->engine != 0); + QScript::APIShim shim(pp->engine); JSC::ExecState *exec = pp->engine->currentFrame; JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); + QScriptEnginePrivate::saveException(exec, &savedException); JSC::JSValue result = JSC::asObject(pp->jscValue)->toPrimitive(exec, hint); - QScriptValuePrivate::restoreException(exec, savedException); + QScriptEnginePrivate::restoreException(exec, savedException); return pp->engine->scriptValueFromJSCValue(result); } @@ -1110,11 +859,12 @@ bool QScriptValue::equals(const QScriptValue &other) const if (!eng_p) eng_p = other.d_ptr->engine; if (eng_p) { + QScript::APIShim shim(eng_p); JSC::ExecState *exec = eng_p->currentFrame; JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); + QScriptEnginePrivate::saveException(exec, &savedException); bool result = JSC::JSValue::equal(exec, d->jscValue, other.d_ptr->jscValue); - QScriptValuePrivate::restoreException(exec, savedException); + QScriptEnginePrivate::restoreException(exec, savedException); return result; } } @@ -1160,18 +910,21 @@ bool QScriptValue::strictlyEquals(const QScriptValue &other) const if (d->type == QScriptValuePrivate::JavaScriptCore) { QScriptEnginePrivate *eng_p = d->engine ? d->engine : other.d_ptr->engine; if (eng_p) - return JSC::JSValue::strictEqual(d->jscValue, eng_p->scriptValueToJSCValue(other)); + return JSC::JSValue::strictEqual(eng_p->currentFrame, d->jscValue, eng_p->scriptValueToJSCValue(other)); } else if (other.d_ptr->type == QScriptValuePrivate::JavaScriptCore) { QScriptEnginePrivate *eng_p = other.d_ptr->engine ? other.d_ptr->engine : d->engine; if (eng_p) - return JSC::JSValue::strictEqual(eng_p->scriptValueToJSCValue(*this), other.d_ptr->jscValue); + return JSC::JSValue::strictEqual(eng_p->currentFrame, eng_p->scriptValueToJSCValue(*this), other.d_ptr->jscValue); } return false; } switch (d->type) { - case QScriptValuePrivate::JavaScriptCore: - return JSC::JSValue::strictEqual(d->jscValue, other.d_ptr->jscValue); + case QScriptValuePrivate::JavaScriptCore: { + QScriptEnginePrivate *eng_p = d->engine ? d->engine : other.d_ptr->engine; + JSC::ExecState *exec = eng_p ? eng_p->currentFrame : 0; + return JSC::JSValue::strictEqual(exec, d->jscValue, other.d_ptr->jscValue); + } case QScriptValuePrivate::Number: return (d->numberValue == other.d_ptr->numberValue); case QScriptValuePrivate::String: @@ -1199,22 +952,14 @@ QString QScriptValue::toString() const return QString(); switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { - JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); - JSC::UString str = d->jscValue.toString(exec); - if (exec && exec->hadException() && !str.size()) { - JSC::JSValue savedException2; - QScriptValuePrivate::saveException(exec, &savedException2); - str = savedException2.toString(exec); - QScriptValuePrivate::restoreException(exec, savedException2); - } - if (savedException) - QScriptValuePrivate::restoreException(exec, savedException); - return str; - } + if (d->engine) { + QScript::APIShim shim(d->engine); + return QScriptEnginePrivate::toString(d->engine->currentFrame, d->jscValue); + } else { + return QScriptEnginePrivate::toString(0, d->jscValue); + } } case QScriptValuePrivate::Number: - return JSC::UString::from(d->numberValue); + return QScript::ToString(d->numberValue); case QScriptValuePrivate::String: return d->stringValue; } @@ -1240,17 +985,17 @@ qsreal QScriptValue::toNumber() const return 0; switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { - JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); - qsreal result = d->jscValue.toNumber(exec); - QScriptValuePrivate::restoreException(exec, savedException); - return result; + if (d->engine) { + QScript::APIShim shim(d->engine); + return QScriptEnginePrivate::toNumber(d->engine->currentFrame, d->jscValue); + } else { + return QScriptEnginePrivate::toNumber(0, d->jscValue); + } } case QScriptValuePrivate::Number: return d->numberValue; case QScriptValuePrivate::String: - return ((JSC::UString)d->stringValue).toDouble(); + return QScript::ToNumber(d->stringValue); } return 0; } @@ -1267,17 +1012,17 @@ bool QScriptValue::toBoolean() const return false; switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { - JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); - bool result = d->jscValue.toBoolean(exec); - QScriptValuePrivate::restoreException(exec, savedException); - return result; + if (d->engine) { + QScript::APIShim shim(d->engine); + return QScriptEnginePrivate::toBool(d->engine->currentFrame, d->jscValue); + } else { + return QScriptEnginePrivate::toBool(0, d->jscValue); + } } case QScriptValuePrivate::Number: - return (d->numberValue != 0) && !qIsNaN(d->numberValue); + return QScript::ToBool(d->numberValue); case QScriptValuePrivate::String: - return (!d->stringValue.isEmpty()); + return QScript::ToBool(d->stringValue); } return false; } @@ -1303,17 +1048,17 @@ bool QScriptValue::toBool() const return false; switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { - JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); - bool result = d->jscValue.toBoolean(exec); - QScriptValuePrivate::restoreException(exec, savedException); - return result; + if (d->engine) { + QScript::APIShim shim(d->engine); + return QScriptEnginePrivate::toBool(d->engine->currentFrame, d->jscValue); + } else { + return QScriptEnginePrivate::toBool(0, d->jscValue); + } } case QScriptValuePrivate::Number: - return (d->numberValue != 0) && !qIsNaN(d->numberValue); + return QScript::ToBool(d->numberValue); case QScriptValuePrivate::String: - return (!d->stringValue.isEmpty()); + return QScript::ToBool(d->stringValue); } return false; } @@ -1337,17 +1082,17 @@ qint32 QScriptValue::toInt32() const return 0; switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { - JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); - qint32 result = d->jscValue.toInt32(exec); - QScriptValuePrivate::restoreException(exec, savedException); - return result; + if (d->engine) { + QScript::APIShim shim(d->engine); + return QScriptEnginePrivate::toInt32(d->engine->currentFrame, d->jscValue); + } else { + return QScriptEnginePrivate::toInt32(0, d->jscValue); + } } case QScriptValuePrivate::Number: return QScript::ToInt32(d->numberValue); case QScriptValuePrivate::String: - return QScript::ToInt32(((JSC::UString)d->stringValue).toDouble()); + return QScript::ToInt32(d->stringValue); } return 0; } @@ -1371,17 +1116,17 @@ quint32 QScriptValue::toUInt32() const return 0; switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { - JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); - quint32 result = d->jscValue.toUInt32(exec); - QScriptValuePrivate::restoreException(exec, savedException); - return result; + if (d->engine) { + QScript::APIShim shim(d->engine); + return QScriptEnginePrivate::toUInt32(d->engine->currentFrame, d->jscValue); + } else { + return QScriptEnginePrivate::toUInt32(0, d->jscValue); + } } case QScriptValuePrivate::Number: - return QScript::ToUint32(d->numberValue); + return QScript::ToUInt32(d->numberValue); case QScriptValuePrivate::String: - return QScript::ToUint32(((JSC::UString)d->stringValue).toDouble()); + return QScript::ToUInt32(d->stringValue); } return 0; } @@ -1405,13 +1150,17 @@ quint16 QScriptValue::toUInt16() const return 0; switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { - // ### no equivalent function in JSC - return QScript::ToUint16(toNumber()); + if (d->engine) { + QScript::APIShim shim(d->engine); + return QScriptEnginePrivate::toUInt16(d->engine->currentFrame, d->jscValue); + } else { + return QScriptEnginePrivate::toUInt16(0, d->jscValue); + } } case QScriptValuePrivate::Number: - return QScript::ToUint16(d->numberValue); + return QScript::ToUInt16(d->numberValue); case QScriptValuePrivate::String: - return QScript::ToUint16(((JSC::UString)d->stringValue).toDouble()); + return QScript::ToUInt16(d->stringValue); } return 0; } @@ -1435,17 +1184,17 @@ qsreal QScriptValue::toInteger() const return 0; switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { - JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); - qsreal result = d->jscValue.toInteger(exec); - QScriptValuePrivate::restoreException(exec, savedException); - return result; + if (d->engine) { + QScript::APIShim shim(d->engine); + return QScriptEnginePrivate::toInteger(d->engine->currentFrame, d->jscValue); + } else { + return QScriptEnginePrivate::toInteger(0, d->jscValue); + } } case QScriptValuePrivate::Number: return QScript::ToInteger(d->numberValue); case QScriptValuePrivate::String: - return QScript::ToInteger(((JSC::UString)d->stringValue).toDouble()); + return QScript::ToInteger(d->stringValue); } return 0; } @@ -1478,40 +1227,14 @@ QVariant QScriptValue::toVariant() const if (!d) return QVariant(); switch (d->type) { - case QScriptValuePrivate::JavaScriptCore: - if (isObject()) { - if (isVariant()) - return d->variantValue(); -#ifndef QT_NO_QOBJECT - else if (isQObject()) - return qVariantFromValue(toQObject()); -#endif - else if (isDate()) - return QVariant(toDateTime()); -#ifndef QT_NO_REGEXP - else if (isRegExp()) - return QVariant(toRegExp()); -#endif - else if (isArray()) - return QScriptEnginePrivate::variantListFromArray(*this); - else if (QScriptDeclarativeClass *dc = QScriptDeclarativeClass::scriptClass(*this)) - return dc->toVariant(QScriptDeclarativeClass::object(*this)); - // try to convert to primitive - JSC::ExecState *exec = d->engine->currentFrame; - JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); - JSC::JSValue prim = d->jscValue.toPrimitive(exec); - QScriptValuePrivate::restoreException(exec, savedException); - if (!prim.isObject()) - return d->engine->scriptValueFromJSCValue(prim).toVariant(); - } else if (isNumber()) { - return QVariant(toNumber()); - } else if (isString()) { - return QVariant(toString()); - } else if (isBool()) { - return QVariant(toBool()); + case QScriptValuePrivate::JavaScriptCore: { + if (d->engine) { + QScript::APIShim shim(d->engine); + return QScriptEnginePrivate::toVariant(d->engine->currentFrame, d->jscValue); + } else { + return QScriptEnginePrivate::toVariant(0, d->jscValue); } - return QVariant(); + } case QScriptValuePrivate::Number: return QVariant(d->numberValue); case QScriptValuePrivate::String: @@ -1543,10 +1266,9 @@ QScriptValue QScriptValue::toObject() const QDateTime QScriptValue::toDateTime() const { Q_D(const QScriptValue); - if (!isDate()) + if (!d || !d->engine) return QDateTime(); - qsreal t = static_cast<JSC::DateInstance*>(JSC::asObject(d->jscValue))->internalNumber(); - return QScript::ToDateTime(t, Qt::LocalTime); + return QScriptEnginePrivate::toDateTime(d->engine->currentFrame, d->jscValue); } #ifndef QT_NO_REGEXP @@ -1560,13 +1282,9 @@ QDateTime QScriptValue::toDateTime() const QRegExp QScriptValue::toRegExp() const { Q_D(const QScriptValue); - if (!isRegExp()) - return QRegExp(); - QString pattern = d->property(QLatin1String("source"), QScriptValue::ResolvePrototype).toString(); - Qt::CaseSensitivity kase = Qt::CaseSensitive; - if (d->property(QLatin1String("ignoreCase"), QScriptValue::ResolvePrototype).toBool()) - kase = Qt::CaseInsensitive; - return QRegExp(pattern, kase, QRegExp::RegExp2); + if (!d || !d->engine) + return QRegExp(); + return QScriptEnginePrivate::toRegExp(d->engine->currentFrame, d->jscValue); } #endif // QT_NO_REGEXP @@ -1583,19 +1301,9 @@ QRegExp QScriptValue::toRegExp() const QObject *QScriptValue::toQObject() const { Q_D(const QScriptValue); - if (isQObject()) { - QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(d->jscValue)); - QScriptObjectDelegate *delegate = object->delegate(); - if (delegate->type() == QScriptObjectDelegate::DeclarativeClassObject) - return static_cast<QScript::DeclarativeObjectDelegate*>(delegate)->scriptClass()->toQObject(QScriptDeclarativeClass::object(*this)); - return static_cast<QScript::QObjectDelegate*>(delegate)->value(); - } else if (isVariant()) { - QVariant var = toVariant(); - int type = var.userType(); - if ((type == QMetaType::QObjectStar) || (type == QMetaType::QWidgetStar)) - return *reinterpret_cast<QObject* const *>(var.constData()); - } - return 0; + if (!d || !d->engine) + return 0; + return QScriptEnginePrivate::toQObject(d->engine->currentFrame, d->jscValue); } /*! @@ -1607,9 +1315,9 @@ QObject *QScriptValue::toQObject() const const QMetaObject *QScriptValue::toQMetaObject() const { Q_D(const QScriptValue); - if (isQMetaObject()) - return static_cast<QScript::QMetaObjectWrapperObject*>(JSC::asObject(d->jscValue))->value(); - return 0; + if (!d || !d->engine) + return 0; + return QScriptEnginePrivate::toQMetaObject(d->engine->currentFrame, d->jscValue); } /*! @@ -1643,8 +1351,16 @@ void QScriptValue::setProperty(const QString &name, const QScriptValue &value, Q_D(QScriptValue); if (!d || !d->isObject()) return; - JSC::ExecState *exec = d->engine->currentFrame; - d->setProperty(JSC::Identifier(exec, name), value, flags); + QScript::APIShim shim(d->engine); + QScriptEnginePrivate *valueEngine = QScriptValuePrivate::getEngine(value); + if (valueEngine && (valueEngine != d->engine)) { + qWarning("QScriptValue::setProperty(%s) failed: " + "cannot set value created in a different engine", + qPrintable(name)); + return; + } + JSC::JSValue jsValue = d->engine->scriptValueToJSCValue(value); + d->setProperty(name, jsValue, flags); } /*! @@ -1668,7 +1384,8 @@ QScriptValue QScriptValue::property(const QString &name, Q_D(const QScriptValue); if (!d || !d->isObject()) return QScriptValue(); - return d->property(name, mode); + QScript::APIShim shim(d->engine); + return d->engine->scriptValueFromJSCValue(d->property(name, mode)); } /*! @@ -1690,7 +1407,7 @@ QScriptValue QScriptValue::property(quint32 arrayIndex, Q_D(const QScriptValue); if (!d || !d->isObject()) return QScriptValue(); - return d->property(arrayIndex, mode); + return d->engine->scriptValueFromJSCValue(d->property(arrayIndex, mode)); } /*! @@ -1717,33 +1434,8 @@ void QScriptValue::setProperty(quint32 arrayIndex, const QScriptValue &value, "cannot set value created in a different engine"); return; } - JSC::ExecState *exec = d->engine->currentFrame; - JSC::JSValue jscValue = d->engine->scriptValueToJSCValue(value); - if (!jscValue) { - JSC::asObject(d->jscValue)->deleteProperty(exec, arrayIndex, /*checkDontDelete=*/false); - } else { - if ((flags & QScriptValue::PropertyGetter) || (flags & QScriptValue::PropertySetter)) { - // fall back to string-based setProperty(), since there is no - // JSC::JSObject::defineGetter(unsigned) - d->setProperty(JSC::Identifier::from(exec, arrayIndex), value, flags); - } else { - if (flags != QScriptValue::KeepExistingFlags) { -// if (JSC::asObject(d->jscValue)->hasOwnProperty(exec, arrayIndex)) -// JSC::asObject(d->jscValue)->deleteProperty(exec, arrayIndex); - unsigned attribs = 0; - if (flags & QScriptValue::ReadOnly) - attribs |= JSC::ReadOnly; - if (flags & QScriptValue::SkipInEnumeration) - attribs |= JSC::DontEnum; - if (flags & QScriptValue::Undeletable) - attribs |= JSC::DontDelete; - attribs |= flags & QScriptValue::UserRange; - JSC::asObject(d->jscValue)->putWithAttributes(exec, arrayIndex, jscValue, attribs); - } else { - JSC::asObject(d->jscValue)->put(exec, arrayIndex, jscValue); - } - } - } + JSC::JSValue jsValue = d->engine->scriptValueToJSCValue(value); + d->setProperty(arrayIndex, jsValue, flags); } /*! @@ -1764,7 +1456,8 @@ QScriptValue QScriptValue::property(const QScriptString &name, Q_D(const QScriptValue); if (!d || !d->isObject() || !QScriptStringPrivate::isValid(name)) return QScriptValue(); - return d->property(name.d_ptr->identifier, mode); + QScript::APIShim shim(d->engine); + return d->engine->scriptValueFromJSCValue(d->property(name.d_ptr->identifier, mode)); } /*! @@ -1787,7 +1480,16 @@ void QScriptValue::setProperty(const QScriptString &name, Q_D(QScriptValue); if (!d || !d->isObject() || !QScriptStringPrivate::isValid(name)) return; - d->setProperty(name.d_ptr->identifier, value, flags); + QScriptEnginePrivate *valueEngine = QScriptValuePrivate::getEngine(value); + if (valueEngine && (valueEngine != d->engine)) { + qWarning("QScriptValue::setProperty(%s) failed: " + "cannot set value created in a different engine", + qPrintable(name.toString())); + return; + } + QScript::APIShim shim(d->engine); + JSC::JSValue jsValue = d->engine->scriptValueToJSCValue(value); + d->setProperty(name.d_ptr->identifier, jsValue, flags); } /*! @@ -1802,6 +1504,7 @@ QScriptValue::PropertyFlags QScriptValue::propertyFlags(const QString &name, Q_D(const QScriptValue); if (!d || !d->isObject()) return 0; + QScript::APIShim shim(d->engine); JSC::ExecState *exec = d->engine->currentFrame; return d->propertyFlags(JSC::Identifier(exec, name), mode); @@ -1851,8 +1554,9 @@ QScriptValue QScriptValue::call(const QScriptValue &thisObject, const QScriptValueList &args) { Q_D(const QScriptValue); - if (!d || !d->isJSC()) + if (!d || !d->isObject()) return QScriptValue(); + QScript::APIShim shim(d->engine); JSC::JSValue callee = d->jscValue; JSC::CallData callData; JSC::CallType callType = callee.getCallData(callData); @@ -1891,12 +1595,12 @@ QScriptValue QScriptValue::call(const QScriptValue &thisObject, JSC::ArgList jscArgs(argsVector.data(), argsVector.size()); JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); + QScriptEnginePrivate::saveException(exec, &savedException); JSC::JSValue result = JSC::call(exec, callee, callType, callData, jscThisObject, jscArgs); if (exec->hadException()) { result = exec->exception(); } else { - QScriptValuePrivate::restoreException(exec, savedException); + QScriptEnginePrivate::restoreException(exec, savedException); } return d->engine->scriptValueFromJSCValue(result); } @@ -1928,8 +1632,9 @@ QScriptValue QScriptValue::call(const QScriptValue &thisObject, const QScriptValue &arguments) { Q_D(QScriptValue); - if (!d || !d->isJSC()) + if (!d || !d->isObject()) return QScriptValue(); + QScript::APIShim shim(d->engine); JSC::JSValue callee = d->jscValue; JSC::CallData callData; JSC::CallType callType = callee.getCallData(callData); @@ -1971,12 +1676,12 @@ QScriptValue QScriptValue::call(const QScriptValue &thisObject, } JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); + QScriptEnginePrivate::saveException(exec, &savedException); JSC::JSValue result = JSC::call(exec, callee, callType, callData, jscThisObject, applyArgs); if (exec->hadException()) { result = exec->exception(); } else { - QScriptValuePrivate::restoreException(exec, savedException); + QScriptEnginePrivate::restoreException(exec, savedException); } return d->engine->scriptValueFromJSCValue(result); } @@ -2002,8 +1707,9 @@ QScriptValue QScriptValue::call(const QScriptValue &thisObject, QScriptValue QScriptValue::construct(const QScriptValueList &args) { Q_D(const QScriptValue); - if (!d || !d->isJSC()) + if (!d || !d->isObject()) return QScriptValue(); + QScript::APIShim shim(d->engine); JSC::JSValue callee = d->jscValue; JSC::ConstructData constructData; JSC::ConstructType constructType = callee.getConstructData(constructData); @@ -2023,12 +1729,12 @@ QScriptValue QScriptValue::construct(const QScriptValueList &args) JSC::ArgList jscArgs(argsVector.data(), argsVector.size()); JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); + QScriptEnginePrivate::saveException(exec, &savedException); JSC::JSObject *result = JSC::construct(exec, callee, constructType, constructData, jscArgs); if (exec->hadException()) { result = JSC::asObject(exec->exception()); } else { - QScriptValuePrivate::restoreException(exec, savedException); + QScriptEnginePrivate::restoreException(exec, savedException); } return d->engine->scriptValueFromJSCValue(result); } @@ -2051,8 +1757,9 @@ QScriptValue QScriptValue::construct(const QScriptValueList &args) QScriptValue QScriptValue::construct(const QScriptValue &arguments) { Q_D(QScriptValue); - if (!d || !d->isJSC()) + if (!d || !d->isObject()) return QScriptValue(); + QScript::APIShim shim(d->engine); JSC::JSValue callee = d->jscValue; JSC::ConstructData constructData; JSC::ConstructType constructType = callee.getConstructData(constructData); @@ -2082,13 +1789,13 @@ QScriptValue QScriptValue::construct(const QScriptValue &arguments) } JSC::JSValue savedException; - QScriptValuePrivate::saveException(exec, &savedException); + QScriptEnginePrivate::saveException(exec, &savedException); JSC::JSObject *result = JSC::construct(exec, callee, constructType, constructData, applyArgs); if (exec->hadException()) { if (exec->exception().isObject()) result = JSC::asObject(exec->exception()); } else { - QScriptValuePrivate::restoreException(exec, savedException); + QScriptEnginePrivate::restoreException(exec, savedException); } return d->engine->scriptValueFromJSCValue(result); } @@ -2237,11 +1944,9 @@ bool QScriptValue::isObject() const bool QScriptValue::isVariant() const { Q_D(const QScriptValue); - if (!d || !d->isJSC() || !d->jscValue.inherits(&QScriptObject::info)) + if (!d || !d->isJSC()) return false; - QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(d->jscValue)); - QScriptObjectDelegate *delegate = object->delegate(); - return (delegate && (delegate->type() == QScriptObjectDelegate::Variant)); + return QScriptEnginePrivate::isVariant(d->jscValue); } /*! @@ -2256,13 +1961,9 @@ bool QScriptValue::isVariant() const bool QScriptValue::isQObject() const { Q_D(const QScriptValue); - if (!d || !d->isJSC() || !d->jscValue.inherits(&QScriptObject::info)) + if (!d || !d->isJSC()) return false; - QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(d->jscValue)); - QScriptObjectDelegate *delegate = object->delegate(); - return (delegate && (delegate->type() == QScriptObjectDelegate::QtObject || - (delegate->type() == QScriptObjectDelegate::DeclarativeClassObject && - static_cast<QScript::DeclarativeObjectDelegate*>(delegate)->scriptClass()->isQObject()))); + return QScriptEnginePrivate::isQObject(d->jscValue); } /*! @@ -2274,9 +1975,9 @@ bool QScriptValue::isQObject() const bool QScriptValue::isQMetaObject() const { Q_D(const QScriptValue); - if (!d || !d->isObject()) + if (!d || !d->isJSC()) return false; - return JSC::asObject(d->jscValue)->inherits(&QScript::QMetaObjectWrapperObject::info); + return QScriptEnginePrivate::isQMetaObject(d->jscValue); } /*! @@ -2307,7 +2008,7 @@ QScriptValue QScriptValue::data() const return d->engine->scriptValueFromJSCValue(scriptObject->data()); } else { // ### make hidden property - return d->property(QLatin1String("__qt_data__"), QScriptValue::ResolveLocal); + return property(QLatin1String("__qt_data__"), QScriptValue::ResolveLocal); } } @@ -2318,6 +2019,8 @@ QScriptValue QScriptValue::data() const this function to set object-specific data that won't be directly accessible to scripts, but may be retrieved in C++ using the data() function. + + \sa QScriptEngine::reportAdditionalMemoryCost() */ void QScriptValue::setData(const QScriptValue &data) { diff --git a/src/script/api/qscriptvalue_p.h b/src/script/api/qscriptvalue_p.h index 7440d21..853c6c8 100644 --- a/src/script/api/qscriptvalue_p.h +++ b/src/script/api/qscriptvalue_p.h @@ -69,9 +69,6 @@ public: inline bool isJSC() const; inline bool isObject() const; - QVariant &variantValue() const; - void setVariantValue(const QVariant &value); - static inline QScriptValuePrivate *get(const QScriptValue &q) { return q.d_ptr.data(); @@ -89,15 +86,18 @@ public: return q.d_ptr->engine; } - inline QScriptValue property(const JSC::Identifier &id, int resolveMode) const; - QScriptValue propertyHelper(const JSC::Identifier &id, int resolveMode) const; - inline QScriptValue property(quint32 index, int resolveMode) const; - QScriptValue propertyHelper(quint32, int resolveMode) const; - inline QScriptValue property(const QString &, int resolveMode) const; - void setProperty(const JSC::Identifier &id, const QScriptValue &value, - const QScriptValue::PropertyFlags &flags); - QScriptValue::PropertyFlags propertyFlags( - const JSC::Identifier &id, const QScriptValue::ResolveFlags &mode) const; + inline JSC::JSValue property(const JSC::Identifier &id, + const QScriptValue::ResolveFlags &mode = QScriptValue::ResolvePrototype) const; + inline JSC::JSValue property(quint32 index, const QScriptValue::ResolveFlags &mode = QScriptValue::ResolvePrototype) const; + inline JSC::JSValue property(const JSC::UString &, const QScriptValue::ResolveFlags &mode = QScriptValue::ResolvePrototype) const; + inline void setProperty(const JSC::UString &name, const JSC::JSValue &value, + const QScriptValue::PropertyFlags &flags = QScriptValue::KeepExistingFlags); + inline void setProperty(const JSC::Identifier &id, const JSC::JSValue &value, + const QScriptValue::PropertyFlags &flags = QScriptValue::KeepExistingFlags); + inline void setProperty(quint32 index, const JSC::JSValue &value, + const QScriptValue::PropertyFlags &flags = QScriptValue::KeepExistingFlags); + inline QScriptValue::PropertyFlags propertyFlags( + const JSC::Identifier &id, const QScriptValue::ResolveFlags &mode = QScriptValue::ResolvePrototype) const; void detachFromEngine(); @@ -109,9 +109,6 @@ public: return -1; } - static inline void saveException(JSC::ExecState*, JSC::JSValue*); - static inline void restoreException(JSC::ExecState*, JSC::JSValue); - QScriptEnginePrivate *engine; Type type; JSC::JSValue jscValue; diff --git a/src/script/api/qscriptvalueiterator.cpp b/src/script/api/qscriptvalueiterator.cpp index 24d9754..ecda5fc 100644 --- a/src/script/api/qscriptvalueiterator.cpp +++ b/src/script/api/qscriptvalueiterator.cpp @@ -84,28 +84,50 @@ public: QScriptValueIteratorPrivate() : initialized(false) {} + + ~QScriptValueIteratorPrivate() + { + if (!initialized) + return; + QScriptEnginePrivate *eng_p = engine(); + if (!eng_p) + return; + QScript::APIShim shim(eng_p); + propertyNames.clear(); //destroying the identifiers need to be done under the APIShim guard + } + + QScriptValuePrivate *object() const + { + return QScriptValuePrivate::get(objectValue); + } + + QScriptEnginePrivate *engine() const + { + return QScriptEnginePrivate::get(objectValue.engine()); + } + void ensureInitialized() { if (initialized) return; - QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(object.engine()); + QScriptEnginePrivate *eng_p = engine(); + QScript::APIShim shim(eng_p); JSC::ExecState *exec = eng_p->globalExec(); JSC::PropertyNameArray propertyNamesArray(exec); - propertyNamesArray.setShouldCache(false); - JSC::asObject(QScriptValuePrivate::get(object)->jscValue)->getOwnPropertyNames(exec, propertyNamesArray, /*includeNonEnumerable=*/true); + JSC::asObject(object()->jscValue)->getOwnPropertyNames(exec, propertyNamesArray, JSC::IncludeDontEnumProperties); JSC::PropertyNameArray::const_iterator propertyNamesIt = propertyNamesArray.begin(); for(; propertyNamesIt != propertyNamesArray.end(); ++propertyNamesIt) { - propertyNames.append(propertyNamesIt->ustring()); + propertyNames.append(*propertyNamesIt); } it = propertyNames.begin(); initialized = true; } - QScriptValue object; - QLinkedList<JSC::UString> propertyNames; - QLinkedList<JSC::UString>::iterator it; - QLinkedList<JSC::UString>::iterator current; + QScriptValue objectValue; + QLinkedList<JSC::Identifier> propertyNames; + QLinkedList<JSC::Identifier>::iterator it; + QLinkedList<JSC::Identifier>::iterator current; bool initialized; }; @@ -119,7 +141,7 @@ QScriptValueIterator::QScriptValueIterator(const QScriptValue &object) { if (object.isObject()) { d_ptr.reset(new QScriptValueIteratorPrivate()); - d_ptr->object = object; + d_ptr->objectValue = object; } } @@ -240,9 +262,9 @@ void QScriptValueIterator::toBack() QString QScriptValueIterator::name() const { Q_D(const QScriptValueIterator); - if (!d || !d->initialized) + if (!d || !d->initialized || !d->engine()) return QString(); - return *d->current; + return d->current->ustring(); } /*! @@ -254,9 +276,9 @@ QString QScriptValueIterator::name() const QScriptString QScriptValueIterator::scriptName() const { Q_D(const QScriptValueIterator); - if (!d || !d->initialized) + if (!d || !d->initialized || !d->engine()) return QScriptString(); - return d->object.engine()->toStringHandle(name()); + return d->engine()->toStringHandle(*d->current); } /*! @@ -268,9 +290,11 @@ QScriptString QScriptValueIterator::scriptName() const QScriptValue QScriptValueIterator::value() const { Q_D(const QScriptValueIterator); - if (!d || !d->initialized) + if (!d || !d->initialized || !d->engine()) return QScriptValue(); - return d->object.property(name()); + QScript::APIShim shim(d->engine()); + JSC::JSValue jsValue = d->object()->property(*d->current); + return d->engine()->scriptValueFromJSCValue(jsValue); } /*! @@ -282,9 +306,11 @@ QScriptValue QScriptValueIterator::value() const void QScriptValueIterator::setValue(const QScriptValue &value) { Q_D(QScriptValueIterator); - if (!d || !d->initialized) + if (!d || !d->initialized || !d->engine()) return; - d->object.setProperty(name(), value); + QScript::APIShim shim(d->engine()); + JSC::JSValue jsValue = d->engine()->scriptValueToJSCValue(value); + d->object()->setProperty(*d->current, jsValue); } /*! @@ -296,9 +322,10 @@ void QScriptValueIterator::setValue(const QScriptValue &value) QScriptValue::PropertyFlags QScriptValueIterator::flags() const { Q_D(const QScriptValueIterator); - if (!d || !d->initialized) + if (!d || !d->initialized || !d->engine()) return 0; - return d->object.propertyFlags(name()); + QScript::APIShim shim(d->engine()); + return d->object()->propertyFlags(*d->current); } /*! @@ -310,9 +337,10 @@ QScriptValue::PropertyFlags QScriptValueIterator::flags() const void QScriptValueIterator::remove() { Q_D(QScriptValueIterator); - if (!d || !d->initialized) + if (!d || !d->initialized || !d->engine()) return; - d->object.setProperty(name(), QScriptValue()); + QScript::APIShim shim(d->engine()); + d->object()->setProperty(*d->current, JSC::JSValue()); d->propertyNames.erase(d->current); } @@ -326,7 +354,7 @@ QScriptValueIterator& QScriptValueIterator::operator=(QScriptValue &object) d_ptr.reset(); if (object.isObject()) { d_ptr.reset(new QScriptValueIteratorPrivate()); - d_ptr->object = object; + d_ptr->objectValue = object; } return *this; } |