summaryrefslogtreecommitdiffstats
path: root/src/script/api
diff options
context:
space:
mode:
authorOlivier Goffart <ogoffart@trolltech.com>2009-07-30 19:50:17 (GMT)
committerOlivier Goffart <ogoffart@trolltech.com>2009-07-31 14:11:29 (GMT)
commitbb1e71ac344b184d2ab13cd0ed7188eebb34aaf1 (patch)
treee2481d6ab859207c3cef2dd2822f9e0cb65da5cd /src/script/api
parentf6713c0e69d2b2b20da00e9a9a4e23a8f4f85c3d (diff)
downloadQt-bb1e71ac344b184d2ab13cd0ed7188eebb34aaf1.zip
Qt-bb1e71ac344b184d2ab13cd0ed7188eebb34aaf1.tar.gz
Qt-bb1e71ac344b184d2ab13cd0ed7188eebb34aaf1.tar.bz2
Small Refactoring of QScript
- Create a scope (activation object) for the native constructor in QScriptClass - put the isCalledasConstructor in the activation object (so i can clean up the QScriptContext - Remove the code duplication in all native functions. Aknoweldged-by: Kent
Diffstat (limited to 'src/script/api')
-rw-r--r--src/script/api/qscriptcontext.cpp18
-rw-r--r--src/script/api/qscriptcontext_p.h1
-rw-r--r--src/script/api/qscriptengine.cpp20
-rw-r--r--src/script/api/qscriptengine_p.h18
4 files changed, 52 insertions, 5 deletions
diff --git a/src/script/api/qscriptcontext.cpp b/src/script/api/qscriptcontext.cpp
index 15e4efe..4bdcc6a 100644
--- a/src/script/api/qscriptcontext.cpp
+++ b/src/script/api/qscriptcontext.cpp
@@ -168,7 +168,6 @@ JSC::UString qtStringToJSCUString(const QString &);
}
QScriptContextPrivate::QScriptContextPrivate()
- : calledAsConstructor(false)
{
}
@@ -281,8 +280,6 @@ QScriptValue QScriptContext::throwError(const QString &text)
*/
QScriptContext::~QScriptContext()
{
- delete d_ptr;
- d_ptr = 0;
}
/*!
@@ -366,7 +363,20 @@ QScriptValue QScriptContext::argumentsObject() const
bool QScriptContext::isCalledAsConstructor() const
{
Q_D(const QScriptContext);
- return d->calledAsConstructor;
+ //look up for the QScriptActivationObject and its calledAsConstructor flag.
+ JSC::ScopeChainNode *node = d->frame->scopeChain();
+ JSC::ScopeChainIterator it(node);
+ for (it = node->begin(); it != node->end(); ++it) {
+ if (!(*it)->isVariableObject()) {
+ if ((*it)->inherits(&QScript::QScriptActivationObject::info)) {
+ return static_cast<QScript::QScriptActivationObject *>(*it)->d_ptr()->calledAsConstructor;
+ }
+ //not a native function
+ //### we have no way to know if is is or not a constructor
+ return false;
+ }
+ }
+ return false;
}
/*!
diff --git a/src/script/api/qscriptcontext_p.h b/src/script/api/qscriptcontext_p.h
index 43efd06..25104a1 100644
--- a/src/script/api/qscriptcontext_p.h
+++ b/src/script/api/qscriptcontext_p.h
@@ -84,7 +84,6 @@ public:
static QScriptContextPrivate *get(QScriptContext *q);
JSC::ExecState *frame;
- bool calledAsConstructor;
QScriptEnginePrivate *engine;
};
diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp
index 6bf7354..4fe8f76 100644
--- a/src/script/api/qscriptengine.cpp
+++ b/src/script/api/qscriptengine.cpp
@@ -871,6 +871,26 @@ JSC::JSValue stringProtoFuncArg(JSC::ExecState *exec, JSC::JSObject*, JSC::JSVal
return JSC::jsString(exec, qtStringToJSCUString(result));
}
+
+QScriptPushScopeHelper::QScriptPushScopeHelper(JSC::CallFrame *exec, bool calledAsConstructor)
+{
+ engine = scriptEngineFromExec(exec);
+ previousFrame = engine->currentFrame;
+ engine->currentFrame = exec;
+ QScriptActivationObject *scope = new (exec) QScriptActivationObject(exec);
+ scope->d_ptr()->calledAsConstructor = calledAsConstructor;
+ exec->setScopeChain(exec->scopeChain()->copy()->push(scope));
+}
+
+QScriptPushScopeHelper::~QScriptPushScopeHelper()
+{
+ JSC::CallFrame *exec = engine->currentFrame;
+ exec->setScopeChain(exec->scopeChain()->pop());
+ exec->scopeChain()->deref();
+ engine->currentFrame = previousFrame;
+ engine->releaseContextForFrame(exec);
+}
+
} // namespace QScript
namespace JSC {
diff --git a/src/script/api/qscriptengine_p.h b/src/script/api/qscriptengine_p.h
index bc6acfa..ced49bb 100644
--- a/src/script/api/qscriptengine_p.h
+++ b/src/script/api/qscriptengine_p.h
@@ -234,6 +234,24 @@ public:
#endif
};
+namespace QScript
+{
+/*! \internal
+ Helper class to create QScriptActivationObject.
+ To be used on the stack
+*/
+class QScriptPushScopeHelper
+{
+ public:
+ QScriptPushScopeHelper(JSC::CallFrame *newFrame, bool calledAsConstructor = false);
+ ~QScriptPushScopeHelper();
+ private:
+ QScriptEnginePrivate *engine;
+ JSC::CallFrame *previousFrame;
+};
+} // namespace QScript
+
+
QT_END_NAMESPACE
#endif // QT_NO_SCRIPT