示例#1
0
QString Value::toQStringNoThrow() const
{
    switch (type()) {
    case Value::Empty_Type:
        Q_ASSERT(!"empty Value encountered");
    case Value::Undefined_Type:
        return QStringLiteral("undefined");
    case Value::Null_Type:
        return QStringLiteral("null");
    case Value::Boolean_Type:
        if (booleanValue())
            return QStringLiteral("true");
        else
            return QStringLiteral("false");
    case Value::Managed_Type:
        if (isString())
            return stringValue()->toQString();
        {
            ExecutionContext *ctx = objectValue()->internalClass()->engine->currentContext();
            Scope scope(ctx);
            ScopedValue ex(scope);
            bool caughtException = false;
            ScopedValue prim(scope, RuntimeHelpers::toPrimitive(ValueRef::fromRawValue(this), STRING_HINT));
            if (scope.hasException()) {
                ex = ctx->catchException();
                caughtException = true;
            } else if (prim->isPrimitive()) {
                    return prim->toQStringNoThrow();
            }
            // Can't nest try/catch due to CXX ABI limitations for foreign exception nesting.
            if (caughtException) {
                ScopedValue prim(scope, RuntimeHelpers::toPrimitive(ex, STRING_HINT));
                if (scope.hasException()) {
                    ex = ctx->catchException();
                } else if (prim->isPrimitive()) {
                    return prim->toQStringNoThrow();
                }
            }
            return QString();
        }
    case Value::Integer_Type: {
        QString str;
        RuntimeHelpers::numberToString(&str, (double)int_32, 10);
        return str;
    }
    default: { // double
        QString str;
        RuntimeHelpers::numberToString(&str, doubleValue(), 10);
        return str;
    }
    } // switch
}
示例#2
0
/*!
  Attempts to delete this object's property of the given \a name.
  Returns true if the property was deleted, otherwise returns false.

  The behavior of this function is consistent with the JavaScript
  delete operator. In particular:

  \list
  \li Non-configurable properties cannot be deleted.
  \li This function will return true even if this object doesn't
     have a property of the given \a name (i.e., non-existent
     properties are "trivially deletable").
  \li If this object doesn't have an own property of the given
     \a name, but an object in the prototype() chain does, the
     prototype object's property is not deleted, and this function
     returns true.
  \endlist

  \sa setProperty(), hasOwnProperty()
*/
bool QJSValue::deleteProperty(const QString &name)
{
    ExecutionEngine *engine = d->engine;
    ExecutionContext *ctx = engine->currentContext();
    Scope scope(engine);
    ScopedObject o(scope, d->value.asObject());
    if (!o)
        return false;

    ScopedString s(scope, engine->newString(name));
    bool b = o->deleteProperty(s);
    if (scope.hasException())
        ctx->catchException();
    return b;
}