Exemplo n.º 1
0
QScriptValueImpl Number::method_toString(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *classInfo)
{
    QScriptValueImpl self = context->thisObject();
    if (self.classInfo() != classInfo)
        return context->throwError(QScriptContext::TypeError,
                                   QLatin1String("Number.prototype.toString"));

    QScriptValueImpl arg = context->argument(0);
    if (!arg.isUndefined()) {
        int radix = arg.toInt32();
        if (radix < 2 || radix > 36)
            return context->throwError(QString::fromLatin1("Number.prototype.toString: %0 is not a valid radix")
                                       .arg(radix));
        if (radix != 10) {
            QString str;
            qsreal num = self.internalValue().toInteger();
            do {
                char c = (char)::fmod(num, radix);
                if (c < 10)
                    c += '0';
                else
                    c = c - 10 + 'a';
                str.prepend(QLatin1Char(c));
                num = ::floor(num / radix);
            } while (num != 0);
            return QScriptValueImpl(eng, str);
        }
    }
    QString str = self.internalValue().toString();
    return (QScriptValueImpl(eng, str));
}
QScriptValueImpl Function::method_apply(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *)
{
    if (! context->thisObject().isFunction()) {
        return context->throwError(QScriptContext::TypeError,
                                   QLatin1String("Function.prototype.apply"));
    }

    QScriptValueImpl thisObject = context->argument(0).toObject();
    if (! (thisObject.isValid () && thisObject.isObject()))
        thisObject = eng->globalObject();

    QScriptValueImplList args;
    QScriptValueImpl undefined = eng->undefinedValue();

    QScriptValueImpl arg = context->argument(1);

    if (Ecma::Array::Instance *arr = eng->arrayConstructor->get(arg)) {
        QScript::Array actuals = arr->value;

        for (quint32 i = 0; i < actuals.count(); ++i) {
            QScriptValueImpl a = actuals.at(i);
            if (! a.isValid())
                args << undefined;
            else
                args << a;
        }
    } else if (arg.classInfo() == eng->m_class_arguments) {
        QScript::ArgumentsObjectData *arguments;
        arguments = static_cast<QScript::ArgumentsObjectData*> (arg.objectData());
        QScriptObject *activation = arguments->activation.objectValue();
        for (uint i = 0; i < arguments->length; ++i)
            args << activation->m_objects[i];
    } else if (!(arg.isUndefined() || arg.isNull())) {
        return context->throwError(QScriptContext::TypeError,
                                   QLatin1String("Function.prototype.apply: second argument is not an array"));
    }

    return context->thisObject().call(thisObject, args);
}
Exemplo n.º 3
0
void RegExp::execute(QScriptContextPrivate *context)
{
#ifndef Q_SCRIPT_NO_EVENT_NOTIFY
    engine()->notifyFunctionEntry(context);
#endif
    QString P;
    int F;
    QScriptValueImpl pattern = context->argument(0);
    QScriptValueImpl flags = context->argument(1);
    if (!context->isCalledAsConstructor()) {
        if ((pattern.classInfo() == classInfo()) && flags.isUndefined()) {
            context->m_result = pattern;
            goto Lout;
        }
    }
    if (pattern.classInfo() == classInfo()) {
        if (!flags.isUndefined()) {
            context->throwTypeError(QString::fromLatin1("cannot specify flags when creating a copy of a RegExp"));
            goto Lout;
        }
        Instance *data = Instance::get(pattern, classInfo());
#ifndef QT_NO_REGEXP
        P = data->value.pattern();
#else
        P = data->pattern;
#endif
        F = data->flags;
    } else {
        if (!pattern.isUndefined())
            P = pattern.toString();
        F = 0;
        if (!flags.isUndefined()) {
            QString flagsStr = flags.toString();
            for (int i = 0; i < flagsStr.length(); ++i) {
                int bitflag = flagFromChar(flagsStr.at(i));
                if (bitflag == 0) {
                    context->throwError(
                        QScriptContext::SyntaxError,
                        QString::fromUtf8("invalid regular expression flag '%0'")
                        .arg(flagsStr.at(i)));
                    goto Lout;
                }
                F |= bitflag;
            }
        }
    }
    if (context->isCalledAsConstructor()) {
        QScriptValueImpl &object = context->m_thisObject;
        object.setClassInfo(classInfo());
        object.setPrototype(publicPrototype);
#ifndef QT_NO_REGEXP
        initRegExp(&object, toRegExp(P, F), F);
#else
        initRegExp(&object, P, F);
#endif
    } else {
        newRegExp(&context->m_result, P, F);
    }
 Lout: ;
#ifndef Q_SCRIPT_NO_EVENT_NOTIFY
    engine()->notifyFunctionExit(context);
#endif
}