summaryrefslogtreecommitdiffstats
path: root/src/script/bridge/qscriptqobject.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/script/bridge/qscriptqobject.cpp')
-rw-r--r--src/script/bridge/qscriptqobject.cpp122
1 files changed, 73 insertions, 49 deletions
diff --git a/src/script/bridge/qscriptqobject.cpp b/src/script/bridge/qscriptqobject.cpp
index 91636da..6c401f8 100644
--- a/src/script/bridge/qscriptqobject.cpp
+++ b/src/script/bridge/qscriptqobject.cpp
@@ -163,10 +163,40 @@ static bool isEnumerableMetaProperty(const QMetaProperty &prop,
&& (mo->indexOfProperty(prop.name()) == index);
}
-static inline QByteArray methodName(const QMetaMethod &method)
+/*! \internal
+ Calculates the length of the name of the given \a method by looking
+ for the first '(' character.
+*/
+static inline int methodNameLength(const QMetaMethod &method)
{
- QByteArray signature = method.signature();
- return signature.left(signature.indexOf('('));
+ const char *signature = method.signature();
+ const char *s = signature;
+ while (*s && (*s != '('))
+ ++s;
+ return s - signature;
+}
+
+/*! \internal
+ Makes a deep copy of the first \a nameLength characters of the given
+ method \a signature and returns the copy.
+*/
+static inline QByteArray methodName(const char *signature, int nameLength)
+{
+ return QByteArray(signature, nameLength);
+}
+
+/*! \internal
+
+ Returns true if the name of the given \a method is the same as that
+ specified by the (signature, nameLength) pair, otherwise returns
+ false.
+*/
+static inline bool methodNameEquals(const QMetaMethod &method,
+ const char *signature, int nameLength)
+{
+ const char *otherSignature = method.signature();
+ return !qstrncmp(otherSignature, signature, nameLength)
+ && (otherSignature[nameLength] == '(');
}
static QVariant variantFromValue(JSC::ExecState *exec, int targetType, JSC::JSValue value)
@@ -310,25 +340,16 @@ QList<int> QScript::QtFunction::overloadedIndexes() const
if (!maybeOverloaded())
return QList<int>();
QList<int> result;
- QString name = functionName();
const QMetaObject *meta = metaObject();
+ QMetaMethod method = meta->method(initialIndex());
+ int nameLength = methodNameLength(method);
for (int index = mostGeneralMethod() - 1; index >= 0; --index) {
- QString otherName = QString::fromLatin1(methodName(meta->method(index)));
- if (otherName == name)
+ if (methodNameEquals(meta->method(index), method.signature(), nameLength))
result.append(index);
}
return result;
}
-QString QtFunction::functionName() const
-{
- const QMetaObject *meta = metaObject();
- if (!meta)
- return QString();
- QMetaMethod method = meta->method(initialIndex());
- return QLatin1String(methodName(method));
-}
-
class QScriptMetaType
{
public:
@@ -415,8 +436,8 @@ class QScriptMetaMethod
public:
inline QScriptMetaMethod()
{ }
- inline QScriptMetaMethod(const QByteArray &name, const QVector<QScriptMetaType> &types)
- : m_name(name), m_types(types), m_firstUnresolvedIndex(-1)
+ inline QScriptMetaMethod(const QVector<QScriptMetaType> &types)
+ : m_types(types), m_firstUnresolvedIndex(-1)
{
QVector<QScriptMetaType>::const_iterator it;
for (it = m_types.constBegin(); it != m_types.constEnd(); ++it) {
@@ -429,9 +450,6 @@ public:
inline bool isValid() const
{ return !m_types.isEmpty(); }
- QByteArray name() const
- { return m_name; }
-
inline QScriptMetaType returnType() const
{ return m_types.at(0); }
@@ -460,7 +478,6 @@ public:
{ return m_types; }
private:
- QByteArray m_name;
QVector<QScriptMetaType> m_types;
int m_firstUnresolvedIndex;
};
@@ -497,7 +514,6 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c
const QMetaObject *meta, int initialIndex,
bool maybeOverloaded)
{
- QByteArray funName;
QScriptMetaMethod chosenMethod;
int chosenIndex = -1;
QVarLengthArray<QVariant, 9> args;
@@ -506,15 +522,18 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c
QVector<int> tooFewArgs;
QVector<int> conversionFailed;
int index;
+ int nameLength = 0;
+ const char *initialMethodSignature = 0;
exec->clearException();
QScriptEnginePrivate *engine = QScript::scriptEngineFromExec(exec);
for (index = initialIndex; index >= 0; --index) {
QMetaMethod method = metaMethod(meta, callType, index);
- if (index == initialIndex)
- funName = methodName(method);
- else {
- if (methodName(method) != funName)
+ if (index == initialIndex) {
+ initialMethodSignature = method.signature();
+ nameLength = methodNameLength(method);
+ } else {
+ if (!methodNameEquals(method, initialMethodSignature, nameLength))
continue;
}
@@ -555,7 +574,7 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c
}
}
- QScriptMetaMethod mtd = QScriptMetaMethod(methodName(method), types);
+ QScriptMetaMethod mtd = QScriptMetaMethod(types);
if (int(scriptArgs.size()) < mtd.argumentCount()) {
tooFewArgs.append(index);
@@ -830,9 +849,10 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c
//#ifndef Q_SCRIPT_NO_EVENT_NOTIFY
// engine->notifyFunctionEntry(context);
//#endif
+ QString funName = QString::fromLatin1(methodName(initialMethodSignature, nameLength));
if (!conversionFailed.isEmpty()) {
QString message = QString::fromLatin1("incompatible type of argument(s) in call to %0(); candidates were\n")
- .arg(QLatin1String(funName));
+ .arg(funName);
for (int i = 0; i < conversionFailed.size(); ++i) {
if (i > 0)
message += QLatin1String("\n");
@@ -847,7 +867,7 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c
QScriptMetaType unresolvedType = argsInstance.method.type(unresolvedIndex);
QString unresolvedTypeName = QString::fromLatin1(unresolvedType.name());
QString message = QString::fromLatin1("cannot call %0(): ")
- .arg(QString::fromLatin1(funName));
+ .arg(funName);
if (unresolvedIndex > 0) {
message.append(QString::fromLatin1("argument %0 has unknown type `%1'").
arg(unresolvedIndex).arg(unresolvedTypeName));
@@ -859,7 +879,7 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c
result = JSC::throwError(exec, JSC::TypeError, message);
} else {
QString message = QString::fromLatin1("too few arguments in call to %0(); candidates are\n")
- .arg(QLatin1String(funName));
+ .arg(funName);
for (int i = 0; i < tooFewArgs.size(); ++i) {
if (i > 0)
message += QLatin1String("\n");
@@ -875,6 +895,7 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c
&& (metaArgs.args.count() == candidates.at(1).args.count())
&& (metaArgs.matchDistance == candidates.at(1).matchDistance)) {
// ambiguous call
+ QByteArray funName = methodName(initialMethodSignature, nameLength);
QString message = QString::fromLatin1("ambiguous call of overloaded function %0(); candidates were\n")
.arg(QLatin1String(funName));
for (int i = 0; i < candidates.size(); ++i) {
@@ -1036,14 +1057,7 @@ JSC::JSValue JSC_HOST_CALL QtPropertyFunction::call(
if (!callee->inherits(&QtPropertyFunction::info))
return throwError(exec, JSC::TypeError, "callee is not a QtPropertyFunction object");
QtPropertyFunction *qfun = static_cast<QtPropertyFunction*>(callee);
- QScriptEnginePrivate *eng_p = scriptEngineFromExec(exec);
- JSC::ExecState *previousFrame = eng_p->currentFrame;
- eng_p->currentFrame = exec;
- eng_p->pushContext(exec, thisValue, args, callee);
- JSC::JSValue result = qfun->execute(eng_p->currentFrame, thisValue, args);
- eng_p->popContext();
- eng_p->currentFrame = previousFrame;
- return result;
+ return qfun->execute(exec, thisValue, args);
}
JSC::JSValue QtPropertyFunction::execute(JSC::ExecState *exec,
@@ -1053,12 +1067,15 @@ JSC::JSValue QtPropertyFunction::execute(JSC::ExecState *exec,
JSC::JSValue result = JSC::jsUndefined();
QScriptEnginePrivate *engine = scriptEngineFromExec(exec);
- thisValue = engine->toUsableValue(thisValue);
- QObject *qobject = QScriptEnginePrivate::toQObject(exec, thisValue);
+ JSC::ExecState *previousFrame = engine->currentFrame;
+ engine->currentFrame = exec;
+
+ JSC::JSValue qobjectValue = engine->toUsableValue(thisValue);
+ QObject *qobject = QScriptEnginePrivate::toQObject(exec, qobjectValue);
while ((!qobject || (qobject->metaObject() != data->meta))
- && JSC::asObject(thisValue)->prototype().isObject()) {
- thisValue = JSC::asObject(thisValue)->prototype();
- qobject = QScriptEnginePrivate::toQObject(exec, thisValue);
+ && JSC::asObject(qobjectValue)->prototype().isObject()) {
+ qobjectValue = JSC::asObject(qobjectValue)->prototype();
+ qobject = QScriptEnginePrivate::toQObject(exec, qobjectValue);
}
Q_ASSERT_X(qobject, Q_FUNC_INFO, "this-object must be a QObject");
@@ -1070,14 +1087,17 @@ JSC::JSValue QtPropertyFunction::execute(JSC::ExecState *exec,
QScriptable *scriptable = scriptableFromQObject(qobject);
QScriptEngine *oldEngine = 0;
if (scriptable) {
+ engine->pushContext(exec, thisValue, args, this);
oldEngine = QScriptablePrivate::get(scriptable)->engine;
QScriptablePrivate::get(scriptable)->engine = QScriptEnginePrivate::get(engine);
}
QVariant v = prop.read(qobject);
- if (scriptable)
+ if (scriptable) {
QScriptablePrivate::get(scriptable)->engine = oldEngine;
+ engine->popContext();
+ }
result = QScriptEnginePrivate::jscValueFromVariant(exec, v);
}
@@ -1097,17 +1117,21 @@ JSC::JSValue QtPropertyFunction::execute(JSC::ExecState *exec,
QScriptable *scriptable = scriptableFromQObject(qobject);
QScriptEngine *oldEngine = 0;
if (scriptable) {
+ engine->pushContext(exec, thisValue, args, this);
oldEngine = QScriptablePrivate::get(scriptable)->engine;
QScriptablePrivate::get(scriptable)->engine = QScriptEnginePrivate::get(engine);
}
prop.write(qobject, v);
- if (scriptable)
+ if (scriptable) {
QScriptablePrivate::get(scriptable)->engine = oldEngine;
+ engine->popContext();
+ }
result = arg;
}
+ engine->currentFrame = previousFrame;
return result;
}
@@ -1240,7 +1264,7 @@ bool QObjectDelegate::getOwnPropertySlot(QScriptObject *object, JSC::ExecState *
for (index = meta->methodCount() - 1; index >= offset; --index) {
QMetaMethod method = meta->method(index);
if (hasMethodAccess(method, index, opt)
- && (methodName(method) == name)) {
+ && methodNameEquals(method, name.constData(), name.length())) {
QtFunction *fun = new (exec)QtFunction(
object, index, /*maybeOverloaded=*/true,
&exec->globalData(), eng->originalGlobalObject()->functionStructure(),
@@ -1372,7 +1396,7 @@ bool QObjectDelegate::getOwnPropertyDescriptor(QScriptObject *object, JSC::ExecS
for (index = meta->methodCount() - 1; index >= offset; --index) {
QMetaMethod method = meta->method(index);
if (hasMethodAccess(method, index, opt)
- && (methodName(method) == name)) {
+ && methodNameEquals(method, name.constData(), name.length())) {
QtFunction *fun = new (exec)QtFunction(
object, index, /*maybeOverloaded=*/true,
&exec->globalData(), eng->originalGlobalObject()->functionStructure(),
@@ -1486,7 +1510,7 @@ void QObjectDelegate::put(QScriptObject *object, JSC::ExecState* exec,
for (index = meta->methodCount() - 1; index >= offset; --index) {
QMetaMethod method = meta->method(index);
if (hasMethodAccess(method, index, opt)
- && (methodName(method) == name)) {
+ && methodNameEquals(method, name.constData(), name.length())) {
data->cachedMembers.insert(name, value);
return;
}
@@ -1605,7 +1629,7 @@ bool QObjectDelegate::getPropertyAttributes(const QScriptObject *object,
for (index = meta->methodCount() - 1; index >= offset; --index) {
QMetaMethod method = meta->method(index);
if (hasMethodAccess(method, index, opt)
- && (methodName(method) == name)) {
+ && methodNameEquals(method, name.constData(), name.length())) {
attributes = QObjectMemberAttribute;
if (opt & QScriptEngine::SkipMethodsInEnumeration)
attributes |= JSC::DontEnum;