summaryrefslogtreecommitdiffstats
path: root/src/script
diff options
context:
space:
mode:
authorOlivier Goffart <ogoffart@trolltech.com>2009-07-23 14:21:30 (GMT)
committerOlivier Goffart <ogoffart@trolltech.com>2009-07-23 14:21:30 (GMT)
commit2ce65133026e1f291f7b6b312d832acb0a4045ae (patch)
tree39cc0fce5f89ac03daf7f10659cf2748d0c4dedc /src/script
parent268dfef13889b9a74e8bad3288791b62d20d0ff7 (diff)
downloadQt-2ce65133026e1f291f7b6b312d832acb0a4045ae.zip
Qt-2ce65133026e1f291f7b6b312d832acb0a4045ae.tar.gz
Qt-2ce65133026e1f291f7b6b312d832acb0a4045ae.tar.bz2
Implement QScriptString as a wraper around the JSC::Identifier
Diffstat (limited to 'src/script')
-rw-r--r--src/script/api/qscriptengine.cpp3
-rw-r--r--src/script/api/qscriptstring.cpp43
-rw-r--r--src/script/api/qscriptstring.h2
-rw-r--r--src/script/api/qscriptstring_p.h15
-rw-r--r--src/script/api/qscriptvalue.cpp139
-rw-r--r--src/script/api/qscriptvalue_p.h3
6 files changed, 114 insertions, 91 deletions
diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp
index 43f14fa..524a250 100644
--- a/src/script/api/qscriptengine.cpp
+++ b/src/script/api/qscriptengine.cpp
@@ -3386,8 +3386,9 @@ QScriptEngineAgent *QScriptEngine::agent() const
*/
QScriptString QScriptEngine::toStringHandle(const QString &str)
{
+ Q_D(QScriptEngine);
QScriptString ss;
- QScriptStringPrivate::init(ss, str);
+ QScriptStringPrivate::init(ss, this, JSC::Identifier(d->currentFrame, QScript::qtStringToJSCUString(str)));
return ss;
}
diff --git a/src/script/api/qscriptstring.cpp b/src/script/api/qscriptstring.cpp
index ce38d81..e8a5d7e 100644
--- a/src/script/api/qscriptstring.cpp
+++ b/src/script/api/qscriptstring.cpp
@@ -17,6 +17,11 @@
QT_BEGIN_NAMESPACE
+namespace QScript
+{
+ QString qtStringFromJSCUString(const JSC::UString &str);
+}
+
/*!
\since 4.4
\class QScriptString
@@ -58,11 +63,12 @@ QScriptStringPrivate::~QScriptStringPrivate()
/*!
\internal
*/
-void QScriptStringPrivate::init(QScriptString &q, const QString &value)
+void QScriptStringPrivate::init(QScriptString &q, QScriptEngine *engine, const JSC::Identifier &value)
{
Q_ASSERT(!q.isValid());
q.d_ptr = new QScriptStringPrivate();
- q.d_ptr->value = value;
+ q.d_ptr->identifier = value;
+ q.d_ptr->engine = engine;
q.d_ptr->ref.ref();
}
@@ -90,15 +96,6 @@ QScriptString::QScriptString(const QScriptString &other)
QScriptString::~QScriptString()
{
if (d_ptr && !d_ptr->ref.deref()) {
-// Q_ASSERT_X(false, Q_FUNC_INFO, "not implemented");
-#if 0
- if (isValid()) {
- d_ptr->engine->uninternString(d_ptr);
- } else {
- // the engine has already been deleted
- delete d_ptr;
- }
-#endif
delete d_ptr;
d_ptr = 0;
}
@@ -112,7 +109,6 @@ QScriptString &QScriptString::operator=(const QScriptString &other)
if (d_ptr == other.d_ptr)
return *this;
if (d_ptr && !d_ptr->ref.deref()) {
-// Q_ASSERT_X(false, Q_FUNC_INFO, "not implemented");
delete d_ptr;
}
d_ptr = other.d_ptr;
@@ -128,7 +124,7 @@ QScriptString &QScriptString::operator=(const QScriptString &other)
bool QScriptString::isValid() const
{
Q_D(const QScriptString);
- return (d != 0);
+ return (d && d->engine);
}
/*!
@@ -138,9 +134,15 @@ bool QScriptString::isValid() const
bool QScriptString::operator==(const QScriptString &other) const
{
Q_D(const QScriptString);
- // ### change back once proper string handles are implemented
- return toString() == other.toString();
-// return (d == other.d_func());
+ if (d == other.d_func())
+ return true;
+ if (!d || !other.d_func())
+ return false;
+ if (d->engine != other.d_func()->engine)
+ return false;
+ if (!d->engine)
+ return true;
+ return d->identifier == other.d_func()->identifier;
}
/*!
@@ -149,10 +151,7 @@ bool QScriptString::operator==(const QScriptString &other) const
*/
bool QScriptString::operator!=(const QScriptString &other) const
{
- Q_D(const QScriptString);
- // ### change back once proper string handles are implemented
- return toString() != other.toString();
-// return (d != other.d_func());
+ return !operator==(other);
}
/*!
@@ -164,9 +163,9 @@ bool QScriptString::operator!=(const QScriptString &other) const
QString QScriptString::toString() const
{
Q_D(const QScriptString);
- if (!d)
+ if (!d || !d->engine)
return QString();
- return d->value;
+ return QScript::qtStringFromJSCUString(d->identifier.ustring());
}
/*!
diff --git a/src/script/api/qscriptstring.h b/src/script/api/qscriptstring.h
index 7f854ab..9b2c4ce 100644
--- a/src/script/api/qscriptstring.h
+++ b/src/script/api/qscriptstring.h
@@ -44,7 +44,7 @@ public:
private:
QScriptStringPrivate *d_ptr;
-
+ friend class QScriptValue;
Q_DECLARE_PRIVATE(QScriptString)
};
diff --git a/src/script/api/qscriptstring_p.h b/src/script/api/qscriptstring_p.h
index 0f9ccd1..2940e63 100644
--- a/src/script/api/qscriptstring_p.h
+++ b/src/script/api/qscriptstring_p.h
@@ -28,20 +28,31 @@
#ifndef QT_NO_SCRIPT
#include <QtCore/qstring.h>
+#include <QtCore/qpointer.h>
+#include "qscriptengine.h"
+
+#include "Identifier.h"
+
QT_BEGIN_NAMESPACE
class QScriptString;
+class QScriptEngine;
class QScriptStringPrivate
{
public:
QScriptStringPrivate();
~QScriptStringPrivate();
- static void init(QScriptString &q, const QString &value);
+ static void init(QScriptString &q, QScriptEngine *engine, const JSC::Identifier &value);
QBasicAtomicInt ref;
- QString value;
+#ifndef QT_NO_QOBJECT
+ QPointer<QScriptEngine> engine;
+#else
+ void *engine;
+#endif
+ JSC::Identifier identifier;
};
QT_END_NAMESPACE
diff --git a/src/script/api/qscriptvalue.cpp b/src/script/api/qscriptvalue.cpp
index a6463d3..0fe6825 100644
--- a/src/script/api/qscriptvalue.cpp
+++ b/src/script/api/qscriptvalue.cpp
@@ -16,6 +16,7 @@
#include "qscriptvalue_p.h"
#include "qscriptengine.h"
#include "qscriptengine_p.h"
+#include "qscriptstring_p.h"
#include "JSArray.h"
#include "JSGlobalObject.h"
@@ -332,11 +333,17 @@ bool isFunction(JSC::JSValue value);
QScriptValue QScriptValuePrivate::property(const QString &name, int resolveMode) const
{
- Q_ASSERT(type == JSC);
QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine);
JSC::ExecState *exec = eng_p->currentFrame;
JSC::UString jscName = QScript::qtStringToJSCUString(name);
- JSC::Identifier id = JSC::Identifier(exec, jscName);
+ return property(JSC::Identifier(exec, jscName), resolveMode);
+}
+
+QScriptValue QScriptValuePrivate::property(const JSC::Identifier &id, int resolveMode) const
+{
+ Q_ASSERT(type == JSC);
+ QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine);
+ JSC::ExecState *exec = eng_p->currentFrame;
JSC::JSObject *object = jscValue.getObject();
JSC::PropertySlot slot(const_cast<JSC::JSObject*>(object));
JSC::JSValue result;
@@ -1698,47 +1705,7 @@ void QScriptValue::setProperty(const QString &name, const QScriptValue &value,
Q_D(QScriptValue);
if (!d || !d->isJSC() || !d->jscValue.isObject())
return;
- QScriptEngine *valueEngine = value.engine();
- if (valueEngine && (valueEngine != d->engine)) {
- qWarning("QScriptValue::setProperty(%s) failed: "
- "cannot set value created in a different engine",
- qPrintable(name));
- return;
- }
- QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(d->engine);
- JSC::JSValue jscValue = eng_p->scriptValueToJSCValue(value);
- JSC::UString jscName = QScript::qtStringToJSCUString(name);
- JSC::ExecState *exec = eng_p->currentFrame;
- JSC::Identifier id = JSC::Identifier(exec, jscName);
- if (!jscValue) {
- JSC::asObject(d->jscValue)->deleteProperty(exec, id);
- } else {
- if ((flags & QScriptValue::PropertyGetter) || (flags & QScriptValue::PropertySetter)) {
- if (jscValue.isObject()) {
- if (flags & QScriptValue::PropertyGetter)
- JSC::asObject(d->jscValue)->defineGetter(exec, id, JSC::asObject(jscValue));
- if (flags & QScriptValue::PropertySetter)
- JSC::asObject(d->jscValue)->defineSetter(exec, id, JSC::asObject(jscValue));
- }
- } else {
- if (flags != QScriptValue::KeepExistingFlags) {
- if (JSC::asObject(d->jscValue)->hasOwnProperty(exec, id))
- JSC::asObject(d->jscValue)->deleteProperty(exec, id);
- 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, id, jscValue, attribs);
- } else {
- JSC::PutPropertySlot slot;
- JSC::asObject(d->jscValue)->put(exec, id, jscValue, slot);
- }
- }
- }
+ setProperty(engine()->toStringHandle(name), value, flags);
}
/*!
@@ -1853,8 +1820,10 @@ void QScriptValue::setProperty(quint32 arrayIndex, const QScriptValue &value,
QScriptValue QScriptValue::property(const QScriptString &name,
const ResolveFlags &mode) const
{
- // ### simplified for now
- return property(name.toString(), mode);
+ Q_D(const QScriptValue);
+ if (!d || !d->isJSC() || !d->jscValue.isObject() || !name.isValid())
+ return QScriptValue();
+ return d->property(name.d_ptr->identifier, mode);
}
/*!
@@ -1874,8 +1843,49 @@ void QScriptValue::setProperty(const QScriptString &name,
const QScriptValue &value,
const PropertyFlags &flags)
{
- // ### simplified for now
- setProperty(name.toString(), value, flags);
+ Q_D(QScriptValue);
+ if (!d || !d->isJSC() || !d->jscValue.isObject() || !name.isValid())
+ return;
+ QScriptEngine *valueEngine = value.engine();
+ if (valueEngine && (valueEngine != d->engine)) {
+ qWarning("QScriptValue::setProperty(%s) failed: "
+ "cannot set value created in a different engine",
+ qPrintable(name.toString()));
+ return;
+ }
+ QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(d->engine);
+ JSC::ExecState *exec = eng_p->currentFrame;
+ JSC::JSValue jscValue = eng_p->scriptValueToJSCValue(value);
+ JSC::Identifier id = name.d_ptr->identifier;
+ if (!jscValue) {
+ JSC::asObject(d->jscValue)->deleteProperty(exec, id);
+ } else {
+ if ((flags & QScriptValue::PropertyGetter) || (flags & QScriptValue::PropertySetter)) {
+ if (jscValue.isObject()) {
+ if (flags & QScriptValue::PropertyGetter)
+ JSC::asObject(d->jscValue)->defineGetter(exec, id, JSC::asObject(jscValue));
+ if (flags & QScriptValue::PropertySetter)
+ JSC::asObject(d->jscValue)->defineSetter(exec, id, JSC::asObject(jscValue));
+ }
+ } else {
+ if (flags != QScriptValue::KeepExistingFlags) {
+ if (JSC::asObject(d->jscValue)->hasOwnProperty(exec, id))
+ JSC::asObject(d->jscValue)->deleteProperty(exec, id);
+ 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, id, jscValue, attribs);
+ } else {
+ JSC::PutPropertySlot slot;
+ JSC::asObject(d->jscValue)->put(exec, id, jscValue, slot);
+ }
+ }
+ }
}
/*!
@@ -1887,13 +1897,29 @@ void QScriptValue::setProperty(const QScriptString &name,
QScriptValue::PropertyFlags QScriptValue::propertyFlags(const QString &name,
const ResolveFlags &mode) const
{
+ if (!isObject())
+ return 0;
+ return propertyFlags(engine()->toStringHandle(name), mode);
+
+}
+
+/*!
+ \since 4.4
+
+ Returns the flags of the property with the given \a name, using the
+ given \a mode to resolve the property.
+
+ \sa property()
+*/
+QScriptValue::PropertyFlags QScriptValue::propertyFlags(const QScriptString &name,
+ const ResolveFlags &mode) const
+{
Q_D(const QScriptValue);
if (!isObject())
return 0;
QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(d->engine);
JSC::ExecState *exec = eng_p->currentFrame;
- JSC::UString jscName = QScript::qtStringToJSCUString(name);
- JSC::Identifier id = JSC::Identifier(exec, jscName);
+ JSC::Identifier id = name.d_ptr->identifier;
unsigned attribs = 0;
if (!JSC::asObject(d->jscValue)->getPropertyAttributes(exec, id, attribs))
return 0;
@@ -1916,21 +1942,6 @@ QScriptValue::PropertyFlags QScriptValue::propertyFlags(const QString &name,
}
/*!
- \since 4.4
-
- Returns the flags of the property with the given \a name, using the
- given \a mode to resolve the property.
-
- \sa property()
-*/
-QScriptValue::PropertyFlags QScriptValue::propertyFlags(const QScriptString &name,
- const ResolveFlags &mode) const
-{
- // ### simplified for now
- return propertyFlags(name.toString(), mode);
-}
-
-/*!
Calls this QScriptValue as a function, using \a thisObject as
the `this' object in the function call, and passing \a args
as arguments to the function. Returns the value returned from
diff --git a/src/script/api/qscriptvalue_p.h b/src/script/api/qscriptvalue_p.h
index 7b47e96..2d92e91 100644
--- a/src/script/api/qscriptvalue_p.h
+++ b/src/script/api/qscriptvalue_p.h
@@ -104,7 +104,8 @@ public:
static QScriptValuePrivate *get(const QScriptValue &q);
- QScriptValue property(const QString &name, int resolveMode) const;
+ QScriptValue property(const JSC::Identifier &id, int resolveMode) const;
+ QScriptValue property(const QString &, int resolveMode) const;
QScriptValue property(quint32 index, int resolveMode) const;
QScriptValue toPublic() {