From 648498dd839d88ea6ce9889eb71e16ac9a42e988 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 24 Aug 2009 10:34:57 +0200 Subject: Fix QScriptContext::argumentObjects for function called with QScriptValue::call They have the hostCallFrameFlag, but are function context, not context --- src/script/api/qscriptcontext.cpp | 9 ++++-- tests/auto/qscriptcontext/tst_qscriptcontext.cpp | 35 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/script/api/qscriptcontext.cpp b/src/script/api/qscriptcontext.cpp index 0b1ca33..6a85ede 100644 --- a/src/script/api/qscriptcontext.cpp +++ b/src/script/api/qscriptcontext.cpp @@ -305,8 +305,8 @@ QScriptValue QScriptContext::argumentsObject() const { JSC::CallFrame *frame = const_cast(QScriptEnginePrivate::frameForContext(this)); - if (frame == frame->lexicalGlobalObject()->globalExec() || frame->callerFrame()->hasHostCallFrameFlag()) { - // or context doesn't have arguments. return an empty object + if (frame == frame->lexicalGlobalObject()->globalExec()) { + // context doesn't have arguments. return an empty object return QScriptEnginePrivate::get(QScript::scriptEngineFromExec(frame))->newObject(); } @@ -316,6 +316,11 @@ QScriptValue QScriptContext::argumentsObject() const return QScript::scriptEngineFromExec(frame)->scriptValueFromJSCValue(result); } + if (frame->callerFrame()->hasHostCallFrameFlag()) { + // context doesn't have arguments. return an empty object + return QScriptEnginePrivate::get(QScript::scriptEngineFromExec(frame))->newObject(); + } + //for a native function if (!frame->optionalCalleeArguments()) { Q_ASSERT(frame->argumentCount() > 0); //we need at least 'this' otherwise we'll crash later diff --git a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp index a0c56ed..ef609e0 100644 --- a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp +++ b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp @@ -559,6 +559,11 @@ static QScriptValue custom_eval(QScriptContext *ctx, QScriptEngine *eng) return eng->evaluate(ctx->argumentsObject().property(0).toString(), ctx->argumentsObject().property(1).toString()); } +static QScriptValue custom_call(QScriptContext *ctx, QScriptEngine *) +{ + return ctx->argumentsObject().property(0).call(QScriptValue(), QScriptValueList() << ctx->argumentsObject().property(1)); +} + void tst_QScriptContext::backtrace_data() { QTest::addColumn("code"); @@ -681,7 +686,36 @@ void tst_QScriptContext::backtrace_data() QTest::newRow("two function") << source << expected; } + { + QString func("function foo(a, b) {\n" + " return bt(a);\n" + "}"); + + QString source = func + QString::fromLatin1("\n" + "custom_call(foo, 'hello');\n" + "var a = 1\n"); + + QStringList expected; + expected << "('hello') at -1" + << "foo(a = 'hello') at testfile:2" + << QString("(%1, 'hello') at -1").arg(func) + << "() at testfile:4"; + QTest::newRow("call") << source << expected; + } + + { + QString source = QString::fromLatin1("\n" + "custom_call(bt, 'hello_world');\n" + "var a = 1\n"); + + QStringList expected; + expected << "('hello_world') at -1" + << "(function () {\n [native code]\n}, 'hello_world') at -1" + << "() at testfile:2"; + + QTest::newRow("call native") << source << expected; + } } @@ -693,6 +727,7 @@ void tst_QScriptContext::backtrace() QScriptEngine eng; eng.globalObject().setProperty("bt", eng.newFunction(getBacktrace)); eng.globalObject().setProperty("custom_eval", eng.newFunction(custom_eval)); + eng.globalObject().setProperty("custom_call", eng.newFunction(custom_call)); QString fileName = "testfile"; QScriptValue ret = eng.evaluate(code, fileName); -- cgit v0.12