qtdeclarative/qmljs_objects.cpp

977 lines
29 KiB
C++
Raw Normal View History

/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the V4VM module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
2012-04-16 19:23:25 +00:00
#include "qmljs_objects.h"
2012-05-07 14:05:05 +00:00
#include "qv4ir_p.h"
#include "qv4isel_p.h"
2012-05-14 15:12:25 +00:00
#include "qv4ecmaobjects_p.h"
#include "qv4mm.h"
#include <private/qqmljsengine_p.h>
#include <private/qqmljslexer_p.h>
#include <private/qqmljsparser_p.h>
#include <private/qqmljsast_p.h>
#include <qv4ir_p.h>
#include <qv4codegen_p.h>
#include "private/qlocale_tools_p.h"
#include <QtCore/qmath.h>
2012-05-07 14:05:05 +00:00
#include <QtCore/QDebug>
2012-04-16 19:23:25 +00:00
#include <cassert>
#include <typeinfo>
#include <iostream>
2012-04-16 19:23:25 +00:00
2012-05-04 13:28:04 +00:00
using namespace QQmlJS::VM;
//
// Object
//
2012-05-04 13:12:37 +00:00
Object::~Object()
{
}
void Object::__put__(ExecutionContext *ctx, const QString &name, const Value &value)
2012-05-14 14:03:10 +00:00
{
__put__(ctx, ctx->engine->identifier(name), value);
2012-05-14 14:03:10 +00:00
}
Value Object::getValue(ExecutionContext *ctx, PropertyDescriptor *p) const
{
if (p->isData())
return p->value;
if (!p->get)
return Value::undefinedValue();
return p->get->call(ctx, Value::fromObject(const_cast<Object *>(this)), 0, 0);
}
bool Object::inplaceBinOp(Value rhs, String *name, BinOp op, ExecutionContext *ctx)
{
bool hasProperty = false;
Value v = __get__(ctx, name, &hasProperty);
if (!hasProperty)
return false;
Value result = op(v, rhs, ctx);
__put__(ctx, name, result);
return true;
}
bool Object::inplaceBinOp(Value rhs, Value index, BinOp op, ExecutionContext *ctx)
{
String *name = index.toString(ctx);
assert(name);
return inplaceBinOp(rhs, name, op, ctx);
}
void Object::defineDefaultProperty(String *name, Value value)
{
if (!members)
members.reset(new PropertyTable());
PropertyDescriptor *pd = members->insert(name);
pd->type = PropertyDescriptor::Data;
pd->writable = PropertyDescriptor::Enabled;
pd->enumberable = PropertyDescriptor::Disabled;
pd->configurable = PropertyDescriptor::Enabled;
pd->value = value;
}
void Object::defineDefaultProperty(ExecutionContext *context, const QString &name, Value value)
{
defineDefaultProperty(context->engine->identifier(name), value);
}
void Object::defineDefaultProperty(ExecutionContext *context, const QString &name, Value (*code)(ExecutionContext *), int argumentCount)
{
Q_UNUSED(argumentCount);
String *s = context->engine->identifier(name);
FunctionObject* function = context->engine->newNativeFunction(context, s, code);
function->defineReadonlyProperty(context->engine, context->engine->id_length, Value::fromInt32(argumentCount));
defineDefaultProperty(s, Value::fromObject(function));
}
void Object::defineReadonlyProperty(ExecutionEngine *engine, const QString &name, Value value)
{
defineReadonlyProperty(engine, engine->identifier(name), value);
}
void Object::defineReadonlyProperty(ExecutionEngine *engine, String *name, Value value)
{
if (!members)
members.reset(new PropertyTable());
PropertyDescriptor *pd = members->insert(name);
pd->type = PropertyDescriptor::Data;
pd->writable = PropertyDescriptor::Disabled;
pd->enumberable = PropertyDescriptor::Disabled;
pd->configurable = PropertyDescriptor::Disabled;
pd->value = value;
}
void Object::getCollectables(QVector<Object *> &objects)
{
if (prototype)
objects.append(prototype);
if (members) {
for (PropertyTable::iterator it = members->begin(), eit = members->end(); it < eit; ++it) {
if ((*it) && (*it)->descriptor.isData())
if (Object *o = (*it)->descriptor.value.asObject())
objects.append(o);
}
}
array.getCollectables(objects);
}
// Section 8.12.1
PropertyDescriptor *Object::__getOwnProperty__(ExecutionContext *, String *name)
2012-04-16 19:23:25 +00:00
{
if (members)
return members->find(name);
2012-04-16 19:23:25 +00:00
return 0;
}
// Section 8.12.2
PropertyDescriptor *Object::__getPropertyDescriptor__(ExecutionContext *ctx, String *name)
2012-04-16 19:23:25 +00:00
{
Q_UNUSED(ctx);
Object *o = this;
while (o) {
if (o->members) {
if (PropertyDescriptor *p = o->members->find(name))
return p;
}
o = o->prototype;
}
2012-04-16 19:23:25 +00:00
return 0;
}
// Section 8.12.3
Value Object::__get__(ExecutionContext *ctx, String *name, bool *hasProperty)
2012-04-16 19:23:25 +00:00
{
if (name->isEqualTo(ctx->engine->id___proto__)) {
if (hasProperty)
*hasProperty = true;
return Value::fromObject(prototype);
}
2012-04-16 19:23:25 +00:00
if (PropertyDescriptor *p = __getPropertyDescriptor__(ctx, name)) {
if (hasProperty)
*hasProperty = true;
return getValue(ctx, p);
}
if (hasProperty)
*hasProperty = false;
return Value::undefinedValue();
2012-04-16 19:23:25 +00:00
}
// Section 8.12.4
bool Object::__canPut__(ExecutionContext *ctx, String *name)
2012-04-16 19:23:25 +00:00
{
if (PropertyDescriptor *p = __getOwnProperty__(ctx, name)) {
if (p->isAccessor())
return p->set != 0;
return p->isWritable();
}
if (! prototype)
2012-04-16 19:23:25 +00:00
return extensible;
if (PropertyDescriptor *p = prototype->__getPropertyDescriptor__(ctx, name)) {
if (p->isAccessor())
return p->set != 0;
if (!extensible)
return false;
return p->isWritable();
2012-04-16 19:23:25 +00:00
}
return extensible;
2012-04-16 19:23:25 +00:00
}
// Section 8.12.5
void Object::__put__(ExecutionContext *ctx, String *name, Value value)
2012-04-16 19:23:25 +00:00
{
// clause 1
if (!__canPut__(ctx, name))
goto reject;
2012-04-16 19:23:25 +00:00
if (!members)
members.reset(new PropertyTable());
{
// Clause 2
PropertyDescriptor *pd = __getOwnProperty__(ctx, name);
// Clause 3
if (pd && pd->isData()) {
// spec says to call [[DefineOwnProperty]] with { [[Value]]: value }
// ### to simplify and speed up we should expand the relevant parts here (clauses 6,7,9,10,12,13)
PropertyDescriptor desc = PropertyDescriptor::fromValue(value);
__defineOwnProperty__(ctx, name, &desc);
return;
}
// clause 4
if (!pd && prototype)
pd = prototype->__getPropertyDescriptor__(ctx, name);
// Clause 5
if (pd && pd->isAccessor()) {
assert(pd->set != 0);
Value args[1];
args[0] = value;
pd->set->call(ctx, Value::fromObject(this), args, 1);
return;
}
PropertyDescriptor *p = members->insert(name);
*p = PropertyDescriptor::fromValue(value);
p->configurable = PropertyDescriptor::Enabled;
p->enumberable = PropertyDescriptor::Enabled;
p->writable = PropertyDescriptor::Enabled;
return;
}
reject:
if (ctx->strictMode)
__qmljs_throw_type_error(ctx);
2012-04-16 19:23:25 +00:00
}
// Section 8.12.6
bool Object::__hasProperty__(const ExecutionContext *ctx, String *name) const
2012-04-16 19:23:25 +00:00
{
if (members && members->find(name) != 0)
return true;
2012-04-16 19:23:25 +00:00
return prototype ? prototype->__hasProperty__(ctx, name) : false;
2012-04-16 19:23:25 +00:00
}
// Section 8.12.7
bool Object::__delete__(ExecutionContext *ctx, String *name)
{
if (members) {
if (PropertyTableEntry *entry = members->findEntry(name)) {
if (entry->descriptor.isConfigurable()) {
members->remove(entry);
return true;
}
if (ctx->strictMode)
__qmljs_throw_type_error(ctx);
return false;
}
}
return true;
}
// Section 8.12.9
bool Object::__defineOwnProperty__(ExecutionContext *ctx, String *name, PropertyDescriptor *desc)
2012-05-20 17:59:47 +00:00
{
if (!members)
members.reset(new PropertyTable());
// Clause 1
PropertyDescriptor *current = __getOwnProperty__(ctx, name);
if (!current) {
// clause 3
if (!extensible)
goto reject;
// clause 4
PropertyDescriptor *pd = members->insert(name);
*pd = *desc;
pd->fullyPopulated();
return true;
}
// clause 5
if (desc->isEmpty())
return true;
// clause 6
if (desc->isSubset(current))
return true;
// clause 7
if (!current->isConfigurable()) {
if (desc->isConfigurable())
goto reject;
if (desc->enumberable != PropertyDescriptor::Undefined && desc->enumberable != current->enumberable)
goto reject;
}
// clause 8
if (desc->isGeneric())
goto accept;
// clause 9
if (current->isData() != desc->isData()) {
// 9a
if (!current->isConfigurable())
goto reject;
if (current->isData()) {
// 9b
current->type = PropertyDescriptor::Accessor;
current->writable = PropertyDescriptor::Undefined;
current->get = 0;
current->set = 0;
} else {
// 9c
current->type = PropertyDescriptor::Data;
current->writable = PropertyDescriptor::Disabled;
current->value = Value::undefinedValue();
}
} else if (current->isData() && desc->isData()) { // clause 10
if (!current->isConfigurable() && !current->isWritable()) {
if (desc->isWritable() || !current->value.sameValue(desc->value))
goto reject;
}
} else { // clause 10
assert(current->isAccessor() && desc->isAccessor());
if (!current->isConfigurable()) {
if ((desc->get && current->get != desc->get) ||
(desc->set && current->set != desc->set))
goto reject;
}
}
accept:
*current += *desc;
return true;
reject:
qDebug() << "___put__ rejected" << name->toQString();
if (ctx->strictMode)
__qmljs_throw_type_error(ctx);
return false;
2012-05-25 10:54:36 +00:00
}
2012-05-20 17:59:47 +00:00
bool Object::__defineOwnProperty__(ExecutionContext *ctx, const QString &name, PropertyDescriptor *desc)
{
return __defineOwnProperty__(ctx, ctx->engine->identifier(name), desc);
}
Value Object::call(ExecutionContext *context, Value , Value *, int)
{
context->throwTypeError();
return Value::undefinedValue();
}
String *ForEachIteratorObject::nextPropertyName()
{
PropertyTableEntry *p = 0;
while (1) {
if (!current)
return 0;
// ### index array data as well
++tableIndex;
if (!current->members || tableIndex > current->members->_propertyCount) {
current = current->prototype;
tableIndex = -1;
continue;
}
p = current->members->_properties[tableIndex];
// ### check that it's not a repeated attribute
if (p && p->descriptor.isEnumerable())
return p->name;
}
}
void ForEachIteratorObject::getCollectables(QVector<Object *> &objects)
{
Object::getCollectables(objects);
if (object)
objects.append(object);
if (current)
objects.append(current);
}
Value ArrayObject::__get__(ExecutionContext *ctx, String *name, bool *hasProperty)
2012-05-25 10:54:36 +00:00
{
if (name->isEqualTo(ctx->engine->id_length)) {
if (hasProperty)
*hasProperty = true;
return Value::fromDouble(array.length());
}
return Object::__get__(ctx, name, hasProperty);
2012-05-20 17:59:47 +00:00
}
bool ArrayObject::inplaceBinOp(Value rhs, Value index, BinOp op, ExecutionContext *ctx)
{
if (index.isNumber()) {
const quint32 idx = index.toUInt32(ctx);
Value v = array.at(idx);
v = op(v, rhs, ctx);
array.set(idx, v);
return true;
}
return Object::inplaceBinOp(rhs, index, op, ctx);
}
Function::~Function()
{
delete[] codeData;
}
bool FunctionObject::hasInstance(ExecutionContext *ctx, const Value &value)
2012-04-16 19:23:25 +00:00
{
2012-05-28 18:17:13 +00:00
if (! value.isObject()) {
ctx->throwTypeError();
return false;
}
Value o = __get__(ctx, ctx->engine->id_prototype);
2012-05-28 18:17:13 +00:00
if (! o.isObject()) {
ctx->throwTypeError();
return false;
}
Object *v = value.objectValue();
2012-05-28 18:17:13 +00:00
while (v) {
v = v->prototype;
if (! v)
break;
else if (o.objectValue() == v)
2012-05-28 18:17:13 +00:00
return true;
}
2012-04-16 19:23:25 +00:00
return false;
}
Value FunctionObject::construct(ExecutionContext *context, Value *args, int argc)
{
Object *obj = context->engine->newObject();
Value proto = __get__(context, context->engine->id_prototype);
if (proto.isObject())
obj->prototype = proto.objectValue();
ExecutionContext k;
ExecutionContext *ctx = needsActivation ? context->engine->newContext() : &k;
ctx->initCallContext(context, Value::fromObject(obj), this, args, argc);
Value result = construct(ctx);
ctx->leaveCallContext();
if (result.isObject())
return result;
return Value::fromObject(obj);
}
Value FunctionObject::call(ExecutionContext *context, Value thisObject, Value *args, int argc)
{
ExecutionContext k;
ExecutionContext *ctx = needsActivation ? context->engine->newContext() : &k;
ctx->initCallContext(context, thisObject, this, args, argc);
Value result = call(ctx);
ctx->leaveCallContext();
return result;
}
Value FunctionObject::call(ExecutionContext *ctx)
2012-04-16 19:23:25 +00:00
{
2012-05-07 14:05:05 +00:00
Q_UNUSED(ctx);
return Value::undefinedValue();
2012-05-07 14:05:05 +00:00
}
Value FunctionObject::construct(ExecutionContext *ctx)
2012-05-07 14:05:05 +00:00
{
return call(ctx);
2012-05-07 14:05:05 +00:00
}
2012-04-16 19:23:25 +00:00
ScriptFunction::ScriptFunction(ExecutionContext *scope, VM::Function *function)
2012-05-13 11:50:55 +00:00
: FunctionObject(scope)
2012-05-09 13:47:55 +00:00
, function(function)
2012-05-07 14:05:05 +00:00
{
assert(function);
assert(function->code);
// global function
if (!scope)
return;
if (!function->name.isEmpty())
name = scope->engine->identifier(function->name);
needsActivation = function->needsActivation();
usesArgumentsObject = function->usesArgumentsObject;
strictMode = function->isStrict;
2012-05-08 09:13:02 +00:00
formalParameterCount = function->formals.size();
if (formalParameterCount) {
formalParameterList = new String*[formalParameterCount];
for (unsigned int i = 0; i < formalParameterCount; ++i) {
formalParameterList[i] = scope->engine->identifier(function->formals.at(i));
2012-05-08 09:13:02 +00:00
}
2012-05-07 14:05:05 +00:00
}
2012-05-16 09:58:07 +00:00
varCount = function->locals.size();
if (varCount) {
varList = new String*[varCount];
for (unsigned int i = 0; i < varCount; ++i) {
varList[i] = scope->engine->identifier(function->locals.at(i));
2012-05-16 09:58:07 +00:00
}
}
2012-05-08 09:13:02 +00:00
}
ScriptFunction::~ScriptFunction()
{
delete[] formalParameterList;
2012-05-16 09:58:07 +00:00
delete[] varList;
2012-05-08 09:13:02 +00:00
}
Value ScriptFunction::call(VM::ExecutionContext *ctx)
2012-05-08 09:13:02 +00:00
{
assert(function->code);
return function->code(ctx, function->codeData);
2012-04-16 19:23:25 +00:00
}
Value EvalFunction::call(ExecutionContext *context, Value /*thisObject*/, Value *args, int argc)
{
if (argc < 1)
return Value::undefinedValue();
if (!args[0].isString())
return args[0];
// ### how to determine this correctly?
bool directCall = true;
const QString code = args[0].stringValue()->toQString();
QQmlJS::VM::Function *f = parseSource(context, QStringLiteral("eval code"), code, QQmlJS::Codegen::EvalCode);
if (!f)
return Value::undefinedValue();
bool strict = f->isStrict || context->strictMode;
ExecutionContext k, *ctx;
if (!directCall) {
qDebug() << "!direct";
// ###
} else if (strict) {
ctx = &k;
ctx->initCallContext(context, context->thisObject, this, args, argc);
} else {
// use the surrounding context
ctx = context;
}
// set the correct strict mode flag on the context
bool cstrict = ctx->strictMode;
ctx->strictMode = strict;
Value result = f->code(ctx, f->codeData);
ctx->strictMode = cstrict;
if (strict)
ctx->leaveCallContext();
return result;
}
EvalFunction::EvalFunction(ExecutionContext *scope)
: FunctionObject(scope)
{
name = scope->engine->newString(QLatin1String("eval"));
}
QQmlJS::VM::Function *EvalFunction::parseSource(QQmlJS::VM::ExecutionContext *ctx,
const QString &fileName, const QString &source,
QQmlJS::Codegen::Mode mode)
{
using namespace QQmlJS;
MemoryManager::GCBlocker gcBlocker(ctx->engine->memoryManager);
VM::ExecutionEngine *vm = ctx->engine;
IR::Module module;
VM::Function *globalCode = 0;
{
QQmlJS::Engine ee, *engine = &ee;
Lexer lexer(engine);
lexer.setCode(source, 1, false);
Parser parser(engine);
const bool parsed = parser.parseProgram();
VM::DiagnosticMessage *error = 0, **errIt = &error;
foreach (const QQmlJS::DiagnosticMessage &m, parser.diagnosticMessages()) {
if (m.isError()) {
*errIt = new VM::DiagnosticMessage;
(*errIt)->fileName = fileName;
(*errIt)->offset = m.loc.offset;
(*errIt)->length = m.loc.length;
(*errIt)->startLine = m.loc.startLine;
(*errIt)->startColumn = m.loc.startColumn;
(*errIt)->type = VM::DiagnosticMessage::Error;
(*errIt)->message = m.message;
errIt = &(*errIt)->next;
} else {
std::cerr << qPrintable(fileName) << ':' << m.loc.startLine << ':' << m.loc.startColumn
<< ": warning: " << qPrintable(m.message) << std::endl;
}
}
if (error)
ctx->throwSyntaxError(error);
if (parsed) {
using namespace AST;
Program *program = AST::cast<Program *>(parser.rootNode());
if (!program) {
// if parsing was successful, and we have no program, then
// we're done...:
return 0;
}
Codegen cg(ctx);
IR::Function *globalIRCode = cg(fileName, program, &module, mode);
QScopedPointer<EvalInstructionSelection> isel(ctx->engine->iselFactory->create(vm, &module));
if (globalIRCode)
globalCode = isel->vmFunction(globalIRCode);
}
if (! globalCode)
// ### should be a syntax error
__qmljs_throw_type_error(ctx);
}
return globalCode;
}
// parseInt [15.1.2.2]
ParseIntFunction::ParseIntFunction(ExecutionContext *scope)
: FunctionObject(scope)
{
name = scope->engine->newString(QLatin1String("parseInt"));
}
static inline int toInt(const QChar &qc, int R)
{
ushort c = qc.unicode();
int v = -1;
if (c >= '0' && c <= '9')
v = c - '0';
else if (c >= 'A' && c <= 'Z')
v = c - 'A' + 10;
else if (c >= 'a' && c <= 'z')
v = c - 'a' + 10;
if (v >= 0 && v < R)
return v;
else
return -1;
}
Value ParseIntFunction::call(ExecutionContext *context, Value thisObject, Value *args, int argc)
{
Q_UNUSED(thisObject);
Value string = (argc > 0) ? args[0] : Value::undefinedValue();
Value radix = (argc > 1) ? args[1] : Value::undefinedValue();
int R = radix.isUndefined() ? 0 : radix.toInt32(context);
// [15.1.2.2] step by step:
String *inputString = string.toString(context); // 1
QString trimmed = inputString->toQString().trimmed(); // 2
const QChar *pos = trimmed.constData();
const QChar *end = pos + trimmed.length();
int sign = 1; // 3
if (pos != end) {
if (*pos == QLatin1Char('-'))
sign = -1; // 4
if (*pos == QLatin1Char('-') || *pos == QLatin1Char('+'))
++pos; // 5
}
bool stripPrefix = true; // 7
if (R) { // 8
if (R < 2 || R > 36)
return Value::fromDouble(nan("")); // 8a
if (R != 16)
stripPrefix = false; // 8b
} else { // 9
R = 10; // 9a
}
if (stripPrefix) { // 10
if ((end - pos >= 2)
&& (pos[0] == QLatin1Char('0'))
&& (pos[1] == QLatin1Char('x') || pos[1] == QLatin1Char('X'))) { // 10a
pos += 2;
R = 16;
}
}
// 11: Z is progressively built below
// 13: this is handled by the toInt function
if (pos == end) // 12
return Value::fromDouble(nan(""));
int d = toInt(*pos++, R);
if (d == -1)
return Value::fromDouble(nan(""));
qint64 v = d;
while (pos != end) {
d = toInt(*pos++, R);
if (d == -1)
break;
v = v * R + d;
}
return Value::fromDouble(v * sign); // 15
}
// parseFloat [15.1.2.3]
ParseFloatFunction::ParseFloatFunction(ExecutionContext *scope)
: FunctionObject(scope)
{
name = scope->engine->newString(QLatin1String("parseFloat"));
}
Value ParseFloatFunction::call(ExecutionContext *context, Value thisObject, Value *args, int argc)
{
Q_UNUSED(context);
Q_UNUSED(thisObject);
Value string = (argc > 0) ? args[0] : Value::undefinedValue();
// [15.1.2.3] step by step:
String *inputString = string.toString(context); // 1
QString trimmed = inputString->toQString().trimmed(); // 2
// 4:
if (trimmed.startsWith(QLatin1String("Infinity"))
|| trimmed.startsWith(QLatin1String("+Infinity")))
return Value::fromDouble(INFINITY);
if (trimmed.startsWith("-Infinity"))
return Value::fromDouble(-INFINITY);
QByteArray ba = trimmed.toLatin1();
bool ok;
const char *begin = ba.constData();
const char *end = 0;
double d = qstrtod(begin, &end, &ok);
if (end - begin == 0)
return Value::fromDouble(nan("")); // 3
else
return Value::fromDouble(d);
}
/// isNaN [15.1.2.4]
IsNaNFunction::IsNaNFunction(ExecutionContext *scope)
: FunctionObject(scope)
{
name = scope->engine->newString(QLatin1String("isNaN"));
}
Value IsNaNFunction::call(ExecutionContext *context, Value /*thisObject*/, Value *args, int argc)
{
const Value &v = (argc > 0) ? args[0] : Value::undefinedValue();
if (v.integerCompatible())
return Value::fromBoolean(false);
double d = v.toNumber(context);
return Value::fromBoolean(std::isnan(d));
}
/// isFinite [15.1.2.5]
IsFiniteFunction::IsFiniteFunction(ExecutionContext *scope)
: FunctionObject(scope)
{
name = scope->engine->newString(QLatin1String("isFinite"));
}
Value IsFiniteFunction::call(ExecutionContext *context, Value /*thisObject*/, Value *args, int argc)
{
const Value &v = (argc > 0) ? args[0] : Value::undefinedValue();
if (v.integerCompatible())
return Value::fromBoolean(true);
double d = v.toNumber(context);
return Value::fromBoolean(std::isfinite(d));
}
Value RegExpObject::__get__(ExecutionContext *ctx, String *name, bool *hasProperty)
{
QString n = name->toQString();
Value v = Value::undefinedValue();
if (n == QLatin1String("source"))
v = Value::fromString(ctx, value.pattern());
else if (n == QLatin1String("global"))
v = Value::fromBoolean(global);
else if (n == QLatin1String("ignoreCase"))
v = Value::fromBoolean(value.patternOptions() & QRegularExpression::CaseInsensitiveOption);
else if (n == QLatin1String("multiline"))
v = Value::fromBoolean(value.patternOptions() & QRegularExpression::MultilineOption);
else if (n == QLatin1String("lastIndex"))
v = lastIndex;
if (v.type() != Value::Undefined_Type) {
if (hasProperty)
*hasProperty = true;
return v;
}
return Object::__get__(ctx, name, hasProperty);
}
Value ErrorObject::__get__(ExecutionContext *ctx, String *name, bool *hasProperty)
{
QString n = name->toQString();
if (n == QLatin1String("message")) {
if (hasProperty)
*hasProperty = true;
return value;
}
return Object::__get__(ctx, name, hasProperty);
}
void ErrorObject::setNameProperty(ExecutionContext *ctx)
{
defineDefaultProperty(ctx, QLatin1String("name"), Value::fromString(ctx, className()));
}
void ErrorObject::getCollectables(QVector<Object *> &objects)
{
Object::getCollectables(objects);
if (Object *o = value.asObject())
objects.append(o);
}
SyntaxErrorObject::SyntaxErrorObject(ExecutionContext *ctx, DiagnosticMessage *message)
: ErrorObject(ctx->argument(0))
, msg(message)
{
if (message)
value = Value::fromString(message->buildFullMessage(ctx));
setNameProperty(ctx);
}
ArgumentsObject::ArgumentsObject(ExecutionContext *context, int formalParameterCount, int actualParameterCount)
: context(context)
, currentIndex(-1)
{
defineDefaultProperty(context->engine->id_length, Value::fromInt32(actualParameterCount));
if (context->strictMode) {
for (uint i = 0; i < context->argumentCount; ++i)
Object::__put__(context, QString::number(i), context->arguments[i]);
FunctionObject *thrower = context->engine->newNativeFunction(context, 0, __qmljs_throw_type_error);
PropertyDescriptor pd = PropertyDescriptor::fromAccessor(thrower, thrower);
pd.configurable = PropertyDescriptor::Disabled;
pd.enumberable = PropertyDescriptor::Disabled;
__defineOwnProperty__(context, QStringLiteral("callee"), &pd);
__defineOwnProperty__(context, QStringLiteral("caller"), &pd);
} else {
FunctionObject *get = context->engine->newNativeFunction(context, 0, method_getArg);
FunctionObject *set = context->engine->newNativeFunction(context, 0, method_setArg);
PropertyDescriptor pd = PropertyDescriptor::fromAccessor(get, set);
pd.configurable = PropertyDescriptor::Enabled;
pd.enumberable = PropertyDescriptor::Enabled;
for (uint i = 0; i < formalParameterCount; ++i)
__defineOwnProperty__(context, QString::number(i), &pd);
for (uint i = formalParameterCount; i < context->argumentCount; ++i)
Object::__put__(context, QString::number(i), context->arguments[i]);
defineDefaultProperty(context, QStringLiteral("callee"), Value::fromObject(context->function));
}
}
Value ArgumentsObject::__get__(ExecutionContext *ctx, String *name, bool *hasProperty)
{
if (!ctx->strictMode) {
bool ok = false;
currentIndex = name->toQString().toInt(&ok);
if (!ok)
currentIndex = -1;
}
Value result = Object::__get__(ctx, name, hasProperty);
currentIndex = -1;
return result;
}
void ArgumentsObject::__put__(ExecutionContext *ctx, String *name, Value value)
{
if (!ctx->strictMode) {
bool ok = false;
currentIndex = name->toQString().toInt(&ok);
if (!ok)
currentIndex = -1;
}
Object::__put__(ctx, name, value);
currentIndex = -1;
}
Value ArgumentsObject::method_getArg(ExecutionContext *ctx)
{
Object *that = ctx->thisObject.asObject();
if (!that)
__qmljs_throw_type_error(ctx);
ArgumentsObject *args = that->asArgumentsObject();
if (!args)
__qmljs_throw_type_error(ctx);
assert(ctx != args->context);
assert(args->currentIndex >= 0 && args->currentIndex < (int)args->context->argumentCount);
return args->context->argument(args->currentIndex);
}
Value ArgumentsObject::method_setArg(ExecutionContext *ctx)
{
Object *that = ctx->thisObject.asObject();
if (!that)
__qmljs_throw_type_error(ctx);
ArgumentsObject *args = that->asArgumentsObject();
if (!args)
__qmljs_throw_type_error(ctx);
assert(ctx != args->context);
assert(args->currentIndex >= 0 && args->currentIndex < (int)args->context->argumentCount);
args->context->arguments[args->currentIndex] = ctx->arguments[0];
return Value::undefinedValue();
}
NativeFunction::NativeFunction(ExecutionContext *scope, String *name, Value (*code)(ExecutionContext *))
: FunctionObject(scope)
, code(code)
{
this->name = name;
}