Beispiel #1
0
void propertyBindingFromScriptValue(const QScriptValue& object, PropertyBinding& value) {
    QScriptValue avatar = object.property("avatar");
    QScriptValue entity = object.property("entity");

    if (avatar.isValid() && !avatar.isNull()) {
        value.avatar = avatar.toVariant().toString();
    } else if (entity.isValid() && !entity.isNull()) {
        value.entity = entity.toVariant().toUuid();
    }
}
Beispiel #2
0
    QString to_source_f<QScriptValue>::operator()( QScriptValue const & x ) const
    {
	if( x.isUndefined() ) return "undefined";
	if( x.isNull() ) return "null";
	if(0) qDebug() << "to_source_f<QScriptValue>("<<x.toString()<<")";

#define TODO(A,B,C)
#define DO(IS,T,TO) \
	if( x.IS() ) {\
	    if(0) qDebug() << "to_source_f<QScriptValue>(is-a: "<<# IS<<")"; \
	    return toSource<T>( x.TO() );\
	}


	DO(isVariant,QVariant,toVariant); // must come before the others!
	DO(isBoolean,bool,toBoolean);
	DO(isNumber,qreal,toNumber);
	DO(isString,QString,toString);
	TODO(isRegExp,QRegExp,toRegExp);
	if( x.isArray() || x.isObject() )
	{
	    if(0) qDebug() << "to_source_f<QScriptValue>(is-a: array or object)";
	    return to_source_f_object()( x );
	}

#undef DO
#undef TODO
	return QString("undefined");
    }
Beispiel #3
0
/* Check for uncaught exceptions. Return true and print stuff if
 * there were any.
 */
bool
JavaScriptExtension::_hasError(QScriptValue evalReturnValue) const
{
    if (this->_engine.isNull())
    {
        qCritical() << "JavaScriptExtension::_hasError(): "
		    << "Called without engine!";
        return true;
    }
    if (! this->_engine.data()->hasUncaughtException())
        return false;

    QString retvalstr = (evalReturnValue.isNull()
			 ? "In"
			 : evalReturnValue.toString() + " in");

    qWarning() << "JavaScriptExtension::_hasError():" << retvalstr
               << "source for" << this->_definition->id()
               << "line" << this->_engine.data()->uncaughtExceptionLineNumber();
    qWarning() << "  " << this->_engine.data()->uncaughtException().toString();
    qWarning() << "Trace follows:";
    foreach(QString line, this->_engine.data()->uncaughtExceptionBacktrace())
        qWarning() << "  |" << line;

    return true;
}
Beispiel #4
0
QScriptDebuggerValue::QScriptDebuggerValue(const QScriptValue &value)
    : d_ptr(0)
{
    if (value.isValid()) {
        d_ptr = new QScriptDebuggerValuePrivate;
        if (value.isUndefined())
            d_ptr->type = UndefinedValue;
        else if (value.isNull())
            d_ptr->type = NullValue;
        else if (value.isNumber()) {
            d_ptr->type = NumberValue;
            d_ptr->numberValue = value.toNumber();
        } else if (value.isBoolean()) {
            d_ptr->type = BooleanValue;
            d_ptr->booleanValue = value.toBoolean();
        } else if (value.isString()) {
            d_ptr->type = StringValue;
            d_ptr->stringValue = new QString(value.toString());
        } else {
            Q_ASSERT(value.isObject());
            d_ptr->type = ObjectValue;
            d_ptr->objectId = value.objectId();
        }
        d_ptr->ref.ref();
    }
}
Beispiel #5
0
void fromScriptValueEntityReference(const QScriptValue &obj, EntityReference &s)
{
    if (obj.isString())
        s.ref = obj.toString();
    else
    {
        if (!obj.property("ref").isValid())
            LogError("Can't convert QScriptValue to EntityReference! QScriptValue does not contain ref attribute!");
        else
        {
            QScriptValue ref = obj.property("ref");
            if (ref.isNull())
                s.ref = ""; // Empty the reference
            else if (ref.isString())
                s.ref = ref.toString();
            else if (ref.isQObject())
            {
                // If the object is an Entity, call EntityReference::Set() with it
                Entity* entity = dynamic_cast<Entity*>(ref.toQObject());
                s.Set(entity);
            }
            else if (ref.isNumber() || ref.isVariant())
                s.ref = QString::number(ref.toInt32());
            else
                LogError("Can't convert QScriptValue to EntityReference! Ref attribute is not null, string, a number, or an entity");
        }
    }
}
Beispiel #6
0
QString KateTemplateScript::invoke(KateView* view, const QString& functionName, const QString &srcText) {

  if(!setView(view))
    return QString();

  clearExceptions();
  QScriptValue myFunction = function(functionName);
  if(!myFunction.isValid()) {
    return QString();
  }

  QScriptValueList arguments;
  arguments << QScriptValue(m_engine, srcText);

  QScriptValue result = myFunction.call(QScriptValue(), arguments);
  
  if(m_engine->hasUncaughtException()) {
    displayBacktrace(result, "Error while calling helper function");
    return QString();
  }
 
  
  if (result.isNull()) {
    return QString();
  }

  return result.toString();
}
Beispiel #7
0
SkinContext::SkinContext(UserSettingsPointer pConfig,
                         const QString& xmlPath)
        : m_xmlPath(xmlPath),
          m_pConfig(pConfig),
          m_pScriptEngine(new QScriptEngine()),
          m_pScriptDebugger(new QScriptEngineDebugger()),
          m_pSvgCache(new QHash<QString, QDomElement>()),
          m_pSingletons(new SingletonMap()) {
    enableDebugger(true);
    // the extensions are imported once and will be passed to the children
    // global object as properties of the parent's global object.
    importScriptExtension("console");
    importScriptExtension("svg");
    m_pScriptEngine->installTranslatorFunctions();

    // Retrieving hooks pattern from script extension
    QScriptValue global = m_pScriptEngine->globalObject();
    QScriptValue svg = global.property("svg");
    QScriptValue hooksPattern = svg.property("getHooksPattern").call(svg);
    if (!hooksPattern.isNull()) {
        m_hookRx.setPattern(hooksPattern.toString());
    }

    m_scaleFactor = 1.0;
}
Beispiel #8
0
QScriptValue Plugin::setting(const QString& name, const QScriptValue& defaultValue) {
	mv::Settings settings;
	settings.beginGroup("plugins/" + plugin_->id());
	QVariant v = settings.value(name);
	if (v.isNull() && !defaultValue.isNull()) {
		return defaultValue;
	}
	return mv::scriptutil::variantToScriptValue(v);
}
Beispiel #9
0
void quuidFromScriptValue(const QScriptValue& object, QUuid& uuid) {
    if (object.isNull()) {
        uuid = QUuid();
        return;
    }
    QString uuidAsString = object.toVariant().toString();
    QUuid fromString(uuidAsString);
    uuid = fromString;
}
static JSAgentWatchData fromScriptValue(const QString &expression,
                                        const QScriptValue &value)
{
    static const QString arrayStr = QCoreApplication::translate
            ("Debugger::JSAgentWatchData", "[Array of length %1]");
    static const QString undefinedStr = QCoreApplication::translate
            ("Debugger::JSAgentWatchData", "<undefined>");

    JSAgentWatchData data;
    data.exp = expression.toUtf8();
    data.name = data.exp;
    data.hasChildren = false;
    data.value = value.toString().toUtf8();
    data.objectId = value.objectId();
    if (value.isArray()) {
        data.type = "Array";
        data.value = arrayStr.arg(value.property(QLatin1String("length")).toString()).toUtf8();
        data.hasChildren = true;
    } else if (value.isBool()) {
        data.type = "Bool";
        // data.value = value.toBool() ? "true" : "false";
    } else if (value.isDate()) {
        data.type = "Date";
        data.value = value.toDateTime().toString().toUtf8();
    } else if (value.isError()) {
        data.type = "Error";
    } else if (value.isFunction()) {
        data.type = "Function";
    } else if (value.isUndefined()) {
        data.type = undefinedStr.toUtf8();
    } else if (value.isNumber()) {
        data.type = "Number";
    } else if (value.isRegExp()) {
        data.type = "RegExp";
    } else if (value.isString()) {
        data.type = "String";
    } else if (value.isVariant()) {
        data.type = "Variant";
    } else if (value.isQObject()) {
        const QObject *obj = value.toQObject();
        data.type = "Object";
        data.value += '[';
        data.value += obj->metaObject()->className();
        data.value += ']';
        data.hasChildren = true;
    } else if (value.isObject()) {
        data.type = "Object";
        data.hasChildren = true;
        data.value = "[Object]";
    } else if (value.isNull()) {
        data.type = "<null>";
    } else {
        data.type = "<unknown>";
    }
    return data;
}
Beispiel #11
0
/**
 * Executes the command in the command text box.
 */
void dlgScriptConsole::executeCommand() {
  QString cmd = doc->text(); //txtCommand->toPlainText();
  QScriptValue result = engine->evaluate(cmd);
  
  if( !(result.isUndefined()) && !(result.isNull()) ) {
    lblError->setText(result.toString());
  } else {
    lblError->setText("Command executed successfully.");
  }
}
Beispiel #12
0
    bool IniFile::equals(const QScriptValue &other) const
    {
        if(other.isUndefined() || other.isNull())
            return false;

        QObject *object = other.toQObject();
        if(IniFile *otherIniFile = qobject_cast<IniFile*>(object))
            return (otherIniFile == this);

        return false;
    }
Beispiel #13
0
bool File::equals(const QScriptValue &other) const
{
    if(other.isUndefined() || other.isNull())
        return false;

    QObject *object = other.toQObject();
    if(File *otherFile = qobject_cast<File*>(object))
        return (otherFile == this || &otherFile->mFile == &mFile);

    return false;
}
Beispiel #14
0
bool RawData::equals(const QScriptValue &other) const
{
    if(other.isUndefined() || other.isNull())
        return false;

    QObject *object = other.toQObject();
    if(RawData *otherRawData = qobject_cast<RawData*>(object))
        return (otherRawData == this || otherRawData->mByteArray == mByteArray);

    return false;
}
Beispiel #15
0
	bool Size::equals(const QScriptValue &other) const
	{
		if(other.isUndefined() || other.isNull())
			return false;
		
		QObject *object = other.toQObject();
		if(Size *otherSize = qobject_cast<Size*>(object))
			return (otherSize == this || otherSize->mSize == mSize);
			
		return false;
	}
Beispiel #16
0
	bool ProcessHandle::equals(const QScriptValue &other) const
	{
		if(other.isUndefined() || other.isNull())
			return false;
		
		QObject *object = other.toQObject();
		if(ProcessHandle *otherProcess = qobject_cast<ProcessHandle*>(object))
			return (otherProcess == this || otherProcess->mProcessId == mProcessId);
			
		return false;
	}
Beispiel #17
0
	bool Color::equals(const QScriptValue &other) const
	{
		if(other.isUndefined() || other.isNull())
			return false;
		
		QObject *object = other.toQObject();
		if(Color *otherColor = qobject_cast<Color*>(object))
			return (otherColor == this || otherColor->mColor == mColor);
			
		return false;
	}
Beispiel #18
0
	bool Point::equals(const QScriptValue &other) const
	{
		if(other.isUndefined() || other.isNull())
			return false;
		
		QObject *object = other.toQObject();
		if(Point *otherPoint = qobject_cast<Point*>(object))
			return (otherPoint == this || otherPoint->mPoint == mPoint);
			
		return false;
	}
Beispiel #19
0
void BillboardOverlay::setProperties(const QScriptValue &properties) {
    Base3DOverlay::setProperties(properties);

    QScriptValue urlValue = properties.property("url");
    if (urlValue.isValid()) {
        QString newURL = urlValue.toVariant().toString();
        if (newURL != _url) {
            setBillboardURL(newURL);
        }
    }

    QScriptValue subImageBounds = properties.property("subImage");
    if (subImageBounds.isValid()) {
        if (subImageBounds.isNull()) {
            _fromImage = QRect();
        } else {
            QRect oldSubImageRect = _fromImage;
            QRect subImageRect = _fromImage;
            if (subImageBounds.property("x").isValid()) {
                subImageRect.setX(subImageBounds.property("x").toVariant().toInt());
            } else {
                subImageRect.setX(oldSubImageRect.x());
            }
            if (subImageBounds.property("y").isValid()) {
                subImageRect.setY(subImageBounds.property("y").toVariant().toInt());
            } else {
                subImageRect.setY(oldSubImageRect.y());
            }
            if (subImageBounds.property("width").isValid()) {
                subImageRect.setWidth(subImageBounds.property("width").toVariant().toInt());
            } else {
                subImageRect.setWidth(oldSubImageRect.width());
            }
            if (subImageBounds.property("height").isValid()) {
                subImageRect.setHeight(subImageBounds.property("height").toVariant().toInt());
            } else {
                subImageRect.setHeight(oldSubImageRect.height());
            }
            setClipFromSource(subImageRect);
        }
    }

    QScriptValue scaleValue = properties.property("scale");
    if (scaleValue.isValid()) {
        _scale = scaleValue.toVariant().toFloat();
    }

    QScriptValue isFacingAvatarValue = properties.property("isFacingAvatar");
    if (isFacingAvatarValue.isValid()) {
        _isFacingAvatar = isFacingAvatarValue.toVariant().toBool();
    }
}
Beispiel #20
0
void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::FunctionSignature getter,
                                        QScriptEngine::FunctionSignature setter, QScriptValue object) {
    QScriptValue setterFunction = newFunction(setter, 1);
    QScriptValue getterFunction = newFunction(getter);

    if (!object.isNull()) {
        object.setProperty(name, setterFunction, QScriptValue::PropertySetter);
        object.setProperty(name, getterFunction, QScriptValue::PropertyGetter);
    } else {
        globalObject().setProperty(name, setterFunction, QScriptValue::PropertySetter);
        globalObject().setProperty(name, getterFunction, QScriptValue::PropertyGetter);
    }
}
Beispiel #21
0
QScriptValue UtilitiesExtension::js_canonicalArchitecture(QScriptContext *context,
                                                          QScriptEngine *engine)
{
    const QScriptValue value = context->argument(0);
    if (value.isUndefined() || value.isNull())
        return value;

    if (context->argumentCount() == 1 && value.isString())
        return engine->toScriptValue(canonicalArchitecture(value.toString()));

    return context->throwError(QScriptContext::SyntaxError,
        QStringLiteral("canonicalArchitecture expects one argument of type string"));
}
Beispiel #22
0
QScriptValue FileInfoExtension::js_joinPaths(QScriptContext *context, QScriptEngine *engine)
{
    Q_UNUSED(engine);
    QStringList paths;
    for (int i = 0; i < context->argumentCount(); ++i) {
        const QScriptValue value = context->argument(i);
        if (!value.isUndefined() && !value.isNull()) {
            const QString arg = value.toString();
            if (!arg.isEmpty())
                paths.append(arg);
        }
    }
    return paths.join(QLatin1Char('/')).replace(QRegularExpression(QLatin1String("/{2,}")),
                                                QLatin1String("/"));
}
Beispiel #23
0
QScriptValue QNLoptPrototype :: testCallback(QScriptValue callback)
{
  using namespace std::placeholders;
  auto obj = qscriptvalue_cast<QNLopt*>(thisObject());
  if (!obj) {
    context()->throwError (QString (tr ("Cannot instanciate QNLopt")));
    return QScriptValue();
  }
  auto act = context()->activationObject();
  if (!callback.isNull() && callback.isFunction()) {
    auto f1 = std::bind(testF, callback, act, _1, _2);
    obj->testCallback(f1);
  }
  return QScriptValue();
}
Beispiel #24
0
QScriptValue QNLoptPrototype :: setMinObjective (QScriptValue callback, QScriptValue fData)
{
  using namespace std::placeholders;
  auto obj = qscriptvalue_cast<QNLopt*>(thisObject());
  if (!obj) {
    context()->throwError (QString (tr ("Cannot instanciate QNLopt")));
    return QScriptValue();
  }
  auto act = context()->activationObject();
  if (!callback.isNull() && callback.isFunction()) {
    auto f1 = std::bind(f, callback, act, _1, _2, _3, _4);
    obj->setMinObjective(f1, nullptr);
  }
  
  return QScriptValue();
}
QScriptValue QGSLLinearRegressionPrototype :: linearEst(double x, double c0, double c1, double cov00, double cov01, double cov11, QScriptValue callback)
{
   auto l = qscriptvalue_cast<QGSLLinearRegression*>(thisObject());
   if (!l) {
      context()->throwError (QString (tr ("Cannot instanciate QGSLLinearRegression")));
   }

   double y, yErr;
   auto retval = l->linearEst(x, c0, c1, cov00, cov01, cov11, y, yErr);
   if (!callback.isNull() && callback.isFunction()) {
      auto act = context()->activationObject();
      auto args = QScriptValueList() << y << yErr;
      callback.call(act, args);
   }
   return engine()->toScriptValue(retval);
}
Beispiel #26
0
void Image3DOverlay::setProperties(const QScriptValue &properties) {
    Billboard3DOverlay::setProperties(properties);

    QScriptValue urlValue = properties.property("url");
    if (urlValue.isValid()) {
        QString newURL = urlValue.toVariant().toString();
        if (newURL != _url) {
            setURL(newURL);
        }
    }

    QScriptValue subImageBounds = properties.property("subImage");
    if (subImageBounds.isValid()) {
        if (subImageBounds.isNull()) {
            _fromImage = QRect();
        } else {
            QRect oldSubImageRect = _fromImage;
            QRect subImageRect = _fromImage;
            if (subImageBounds.property("x").isValid()) {
                subImageRect.setX(subImageBounds.property("x").toVariant().toInt());
            } else {
                subImageRect.setX(oldSubImageRect.x());
            }
            if (subImageBounds.property("y").isValid()) {
                subImageRect.setY(subImageBounds.property("y").toVariant().toInt());
            } else {
                subImageRect.setY(oldSubImageRect.y());
            }
            if (subImageBounds.property("width").isValid()) {
                subImageRect.setWidth(subImageBounds.property("width").toVariant().toInt());
            } else {
                subImageRect.setWidth(oldSubImageRect.width());
            }
            if (subImageBounds.property("height").isValid()) {
                subImageRect.setHeight(subImageBounds.property("height").toVariant().toInt());
            } else {
                subImageRect.setHeight(oldSubImageRect.height());
            }
            setClipFromSource(subImageRect);
        }
    }

    QScriptValue emissiveValue = properties.property("emissive");
    if (emissiveValue.isValid()) {
        _emissive = emissiveValue.toBool();
    }
}
Beispiel #27
0
    QString to_source_f_object::operator()( QScriptValue const & x ) const
    {
	if( ! x.isObject() ) return QString("undefined"); // should we return an empty string?
	if( x.isNull() ) return QString("null");
	if( x.isFunction() ) return x.toString(); // QString("('[toSource() cannot handle functions]',null)");
	if( scriptValList().contains(x) )
	{
	    return QString("('[toSource() skipping circular object reference!]',null)");
	}
	scriptValList().append(x);
	QScriptValueIterator it( x );
	QByteArray ba;
	QBuffer buf(&ba);
	// Note that we use QBuffer, instead of QDataStream because
	// writing to a QDataStream serializes in a custom binary format. :(
	// FIXME: replace this with QTextStream.
	buf.open(QIODevice::WriteOnly);
	bool isAr = x.isArray();
	char const * opener = (isAr ? "[" : "{");
	char const * closer = (isAr ? "]" : "}");
	char const * sep = ",";
	buf.write(opener);
	while( it.hasNext() )
	{
	    it.next();
	    if(0) qDebug() << "to_source_object_f: child:"<<it.name();
	    QString sub;
	    if( isAr )
	    {
		sub = toSource( it.value() );
	    }
	    else
	    {
		sub = QString("%1:%2").
		    arg(it.name()).
		    arg( toSource( it.value() ) );
	    }
	    buf.write( sub.toAscii().constData(), sub.size() );
	    if( it.hasNext() ) buf.write(sep);
	}
	buf.write(closer);
	buf.close();
	scriptValList().removeAll(x);
	QString ret(ba);
	return ret;
    }
Beispiel #28
0
QScriptValue UtilitiesExtension::js_canonicalTargetArchitecture(QScriptContext *context,
                                                                QScriptEngine *engine)
{
    const QScriptValue arch = context->argument(0);
    if (arch.isUndefined() || arch.isNull())
        return arch;

    const QScriptValue vendor = context->argument(1);
    const QScriptValue system = context->argument(2);
    const QScriptValue abi = context->argument(3);

    if (!arch.isString() || !vendor.isString() || !system.isString() || !abi.isString())
        return context->throwError(QScriptContext::SyntaxError,
            QStringLiteral("canonicalTargetArchitecture expects 1 to 4 arguments of type string"));

    return engine->toScriptValue(canonicalTargetArchitecture(arch.toString(), vendor.toString(),
                                                             system.toString(), abi.toString()));
}
QScriptValue QGSLLinearRegressionPrototype :: wmul (QVariantList const &xs, QVariantList const &ws, QVariantList const &ys, QScriptValue callback)
{
   auto l = qscriptvalue_cast<QGSLLinearRegression*>(thisObject());
   if (!l) {
      context()->throwError (QString (tr ("Cannot instanciate QGSLLinearRegression")));
   }
   double c1, cov11, sumsq;
   auto retval = l->wmul(xs, ws, ys, c1, cov11, sumsq);
   Q_UNUSED(retval);
   auto act = context()->activationObject();
   act.setProperty ("c1", c1);
   if (!callback.isNull() && callback.isFunction()) {
      auto args = QScriptValueList() << c1 << cov11 << sumsq;
      callback.call (act, args);
   }

   return engine()->evaluate("(function(x){return(c1*x);})");
}
void JavascriptInstance::GetObjectInformation(const QScriptValue &object, QSet<qint64> &ids, uint &valueCount, uint &objectCount, uint &nullCount, uint &numberCount, 
    uint &boolCount, uint &stringCount, uint &arrayCount, uint &funcCount, uint &qobjCount, uint &qobjMethodCount)
{
    if (!ids.contains(object.objectId()))       
        ids << object.objectId();
    
    QScriptValueIterator iter(object);
    while(iter.hasNext()) 
    {
        iter.next();
        QScriptValue v = iter.value();

        if (ids.contains(v.objectId()))
            continue;
        ids << v.objectId();
        
        valueCount++;
        if (v.isNull())
            nullCount++;

        if (v.isNumber())
            numberCount++;
        else if (v.isBool())
            boolCount++;
        else if (v.isString())
            stringCount++;
        else if (v.isArray())
            arrayCount++;
        else if (v.isFunction())
            funcCount++;
        else if (v.isQObject())
            qobjCount++;
        
        if (v.isObject())
            objectCount++;

        if (v.isQMetaObject())
            qobjMethodCount += v.toQMetaObject()->methodCount();
        
        // Recurse
        if ((v.isObject() || v.isArray()) && !v.isFunction() && !v.isString() && !v.isNumber() && !v.isBool() && !v.isQObject() && !v.isQMetaObject())
            GetObjectInformation(v, ids, valueCount, objectCount, nullCount, numberCount, boolCount, stringCount, arrayCount, funcCount, qobjCount, qobjMethodCount);
    }
}