LibJS: Implement most of the Reflect object

This commit is contained in:
Linus Groh 2020-05-01 11:06:27 +01:00 committed by Andreas Kling
parent 1ba2e6768d
commit 79b829637e
Notes: sideshowbarker 2024-07-19 07:08:14 +09:00
22 changed files with 877 additions and 54 deletions

View file

@ -1087,11 +1087,17 @@ PropertyName MemberExpression::computed_property_name(Interpreter& interpreter)
auto index = m_property->execute(interpreter);
if (interpreter.exception())
return {};
ASSERT(!index.is_empty());
// FIXME: What about non-integer numbers tho.
if (index.is_number() && index.to_i32() >= 0)
return PropertyName(index.to_i32());
return PropertyName(index.to_string());
if (!index.to_number().is_finite_number())
return PropertyName(index.to_string());
auto index_as_double = index.to_double();
if (index_as_double < 0 || (i32)index_as_double != index_as_double)
return PropertyName(index.to_string());
return PropertyName(index.to_i32());
}
String MemberExpression::to_string_approximation() const

View file

@ -208,6 +208,30 @@ Value Interpreter::call(Function& function, Value this_value, Optional<MarkedVal
return result;
}
Value Interpreter::construct(Function& function, Function& new_target, Optional<MarkedValueList> arguments)
{
auto& call_frame = push_call_frame();
call_frame.function_name = function.name();
if (arguments.has_value())
call_frame.arguments = arguments.value().values();
call_frame.environment = function.create_environment();
auto* new_object = Object::create_empty(*this, global_object());
auto prototype = new_target.get("prototype");
if (prototype.is_object())
new_object->set_prototype(&prototype.as_object());
call_frame.this_value = new_object;
auto result = function.construct(*this);
pop_call_frame();
if (exception())
return {};
if (result.is_object())
return result;
return new_object;
}
Value Interpreter::throw_exception(Exception* exception)
{
if (exception->value().is_object() && exception->value().as_object().is_error()) {

View file

@ -104,6 +104,7 @@ public:
void exit_scope(const ScopeNode&);
Value call(Function&, Value this_value = {}, Optional<MarkedValueList> arguments = {});
Value construct(Function&, Function& new_target, Optional<MarkedValueList> arguments = {});
CallFrame& push_call_frame()
{

View file

@ -39,6 +39,7 @@ OBJS = \
Runtime/ObjectPrototype.o \
Runtime/PrimitiveString.o \
Runtime/Reference.o \
Runtime/ReflectObject.o \
Runtime/ScriptFunction.o \
Runtime/Shape.o \
Runtime/StringConstructor.o \

View file

@ -49,6 +49,7 @@
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/ObjectConstructor.h>
#include <LibJS/Runtime/ObjectPrototype.h>
#include <LibJS/Runtime/ReflectObject.h>
#include <LibJS/Runtime/Shape.h>
#include <LibJS/Runtime/StringConstructor.h>
#include <LibJS/Runtime/StringPrototype.h>
@ -97,6 +98,7 @@ void GlobalObject::initialize()
put("globalThis", this, attr);
put("console", heap().allocate<ConsoleObject>(), attr);
put("Math", heap().allocate<MathObject>(), attr);
put("Reflect", heap().allocate<ReflectObject>(), attr);
add_constructor("Array", m_array_constructor, *m_array_prototype);
add_constructor("Boolean", m_boolean_constructor, *m_boolean_prototype);

View file

@ -108,7 +108,7 @@ Value Object::get_own_property(const Object& this_object, const FlyString& prope
return value_here;
}
Value Object::get_enumerable_own_properties(const Object& this_object, GetOwnPropertyMode kind) const
Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode kind, u8 attributes) const
{
auto* properties_array = Array::create(interpreter().global_object());
@ -152,7 +152,7 @@ Value Object::get_enumerable_own_properties(const Object& this_object, GetOwnPro
}
for (auto& it : this_object.shape().property_table_ordered()) {
if (it.value.attributes & Attribute::Enumerable) {
if (it.value.attributes & attributes) {
size_t offset = it.value.offset + property_index;
if (kind == GetOwnPropertyMode::Key) {
@ -171,13 +171,42 @@ Value Object::get_enumerable_own_properties(const Object& this_object, GetOwnPro
return properties_array;
}
Value Object::get_own_property_descriptor(const FlyString& property_name) const
{
auto metadata = shape().lookup(property_name);
if (!metadata.has_value())
return js_undefined();
auto value = get(property_name);
if (interpreter().exception())
return {};
auto* descriptor = Object::create_empty(interpreter(), interpreter().global_object());
descriptor->put("value", value.value_or(js_undefined()));
descriptor->put("writable", Value(!!(metadata.value().attributes & Attribute::Writable)));
descriptor->put("enumerable", Value(!!(metadata.value().attributes & Attribute::Enumerable)));
descriptor->put("configurable", Value(!!(metadata.value().attributes & Attribute::Configurable)));
return descriptor;
}
void Object::set_shape(Shape& new_shape)
{
m_storage.resize(new_shape.property_count());
m_shape = &new_shape;
}
bool Object::put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value value, PutOwnPropertyMode mode)
bool Object::define_property(const FlyString& property_name, const Object& descriptor, bool throw_exceptions)
{
auto value = descriptor.get("value");
u8 configurable = descriptor.get("configurable").value_or(Value(false)).to_boolean() * Attribute::Configurable;
u8 enumerable = descriptor.get("enumerable").value_or(Value(false)).to_boolean() * Attribute::Enumerable;
u8 writable = descriptor.get("writable").value_or(Value(false)).to_boolean() * Attribute::Writable;
u8 attributes = configurable | enumerable | writable;
dbg() << "Defining new property " << property_name << " with descriptor { " << configurable << ", " << enumerable << ", " << writable << ", attributes=" << attributes << " }";
return put_own_property(*this, property_name, attributes, value, PutOwnPropertyMode::DefineProperty, throw_exceptions);
}
bool Object::put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value value, PutOwnPropertyMode mode, bool throw_exceptions)
{
auto metadata = shape().lookup(property_name);
bool new_property = !metadata.has_value();
@ -195,7 +224,8 @@ bool Object::put_own_property(Object& this_object, const FlyString& property_nam
if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !(metadata.value().attributes & Attribute::Configurable) && attributes != metadata.value().attributes) {
dbg() << "Disallow reconfig of non-configurable property";
interpreter().throw_exception<TypeError>(String::format("Cannot redefine property '%s'", property_name.characters()));
if (throw_exceptions)
interpreter().throw_exception<TypeError>(String::format("Cannot redefine property '%s'", property_name.characters()));
return false;
}

View file

@ -46,6 +46,17 @@ public:
explicit Object(Object* prototype);
virtual ~Object();
enum class GetOwnPropertyMode {
Key,
Value,
KeyAndValue,
};
enum class PutOwnPropertyMode {
Put,
DefineProperty,
};
Shape& shape() { return *m_shape; }
const Shape& shape() const { return *m_shape; }
@ -60,20 +71,11 @@ public:
bool put(PropertyName, Value, u8 attributes = default_attributes);
Value get_own_property(const Object& this_object, const FlyString& property_name) const;
Value get_own_properties(const Object& this_object, GetOwnPropertyMode, u8 attributes = Attribute::Configurable | Attribute::Enumerable | Attribute::Writable) const;
Value get_own_property_descriptor(const FlyString& property_name) const;
enum class GetOwnPropertyMode {
Key,
Value,
KeyAndValue,
};
Value get_enumerable_own_properties(const Object& this_object, GetOwnPropertyMode) const;
enum class PutOwnPropertyMode {
Put,
DefineProperty,
};
bool put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value, PutOwnPropertyMode);
bool define_property(const FlyString& property_name, const Object& descriptor, bool throw_exceptions = true);
bool put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value, PutOwnPropertyMode, bool throw_exceptions = true);
bool put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)>, i32 length = 0, u8 attribute = default_attributes);
bool put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attribute = default_attributes);

View file

@ -108,47 +108,23 @@ Value ObjectConstructor::set_prototype_of(Interpreter& interpreter)
Value ObjectConstructor::get_own_property_descriptor(Interpreter& interpreter)
{
if (interpreter.argument_count() < 2)
return interpreter.throw_exception<TypeError>("Object.getOwnPropertyDescriptor() needs 2 arguments");
if (!interpreter.argument(0).is_object())
return interpreter.throw_exception<TypeError>("Object argument is not an object");
auto& object = interpreter.argument(0).as_object();
auto metadata = object.shape().lookup(interpreter.argument(1).to_string());
if (!metadata.has_value())
return js_undefined();
auto value = object.get(interpreter.argument(1).to_string()).value_or(js_undefined());
auto* object = interpreter.argument(0).to_object(interpreter.heap());
if (interpreter.exception())
return {};
auto* descriptor = Object::create_empty(interpreter, interpreter.global_object());
descriptor->put("configurable", Value(!!(metadata.value().attributes & Attribute::Configurable)));
descriptor->put("enumerable", Value(!!(metadata.value().attributes & Attribute::Enumerable)));
descriptor->put("writable", Value(!!(metadata.value().attributes & Attribute::Writable)));
descriptor->put("value", value);
return descriptor;
auto property_key = interpreter.argument(1).to_string();
return object->get_own_property_descriptor(property_key);
}
Value ObjectConstructor::define_property(Interpreter& interpreter)
{
if (interpreter.argument_count() < 3)
return interpreter.throw_exception<TypeError>("Object.defineProperty() needs 3 arguments");
if (!interpreter.argument(0).is_object())
return interpreter.throw_exception<TypeError>("Object argument is not an object");
if (!interpreter.argument(2).is_object())
return interpreter.throw_exception<TypeError>("Descriptor argument is not an object");
auto& object = interpreter.argument(0).as_object();
auto property_key = interpreter.argument(1).to_string();
auto& descriptor = interpreter.argument(2).as_object();
auto value = descriptor.get("value");
u8 configurable = descriptor.get("configurable").value_or(Value(false)).to_boolean() * Attribute::Configurable;
u8 enumerable = descriptor.get("enumerable").value_or(Value(false)).to_boolean() * Attribute::Enumerable;
u8 writable = descriptor.get("writable").value_or(Value(false)).to_boolean() * Attribute::Writable;
u8 attributes = configurable | enumerable | writable;
dbg() << "Defining new property " << interpreter.argument(1).to_string() << " with descriptor { " << configurable << ", " << enumerable << ", " << writable << ", attributes=" << attributes << " }";
object.put_own_property(object, interpreter.argument(1).to_string(), attributes, value, PutOwnPropertyMode::DefineProperty);
object.define_property(property_key, descriptor);
return &object;
}
@ -177,7 +153,7 @@ Value ObjectConstructor::keys(Interpreter& interpreter)
if (interpreter.exception())
return {};
return obj_arg->get_enumerable_own_properties(*obj_arg, GetOwnPropertyMode::Key);
return obj_arg->get_own_properties(*obj_arg, GetOwnPropertyMode::Key, Attribute::Enumerable);
}
Value ObjectConstructor::values(Interpreter& interpreter)
@ -189,7 +165,7 @@ Value ObjectConstructor::values(Interpreter& interpreter)
if (interpreter.exception())
return {};
return obj_arg->get_enumerable_own_properties(*obj_arg, GetOwnPropertyMode::Value);
return obj_arg->get_own_properties(*obj_arg, GetOwnPropertyMode::Value, Attribute::Enumerable);
}
Value ObjectConstructor::entries(Interpreter& interpreter)
@ -201,7 +177,7 @@ Value ObjectConstructor::entries(Interpreter& interpreter)
if (interpreter.exception())
return {};
return obj_arg->get_enumerable_own_properties(*obj_arg, GetOwnPropertyMode::KeyAndValue);
return obj_arg->get_own_properties(*obj_arg, GetOwnPropertyMode::KeyAndValue, Attribute::Enumerable);
}
}

View file

@ -0,0 +1,251 @@
/*
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Function.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Function.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/ReflectObject.h>
namespace JS {
static Object* get_target_object_from(Interpreter& interpreter, const String& name)
{
auto target = interpreter.argument(0);
if (!target.is_object()) {
interpreter.throw_exception<TypeError>(String::format("First argument of Reflect.%s() must be an object", name.characters()));
return nullptr;
}
return static_cast<Object*>(&target.as_object());
}
static Function* get_target_function_from(Interpreter& interpreter, const String& name)
{
auto target = interpreter.argument(0);
if (!target.is_object() || !target.as_object().is_function()) {
interpreter.throw_exception<TypeError>(String::format("First argument of Reflect.%s() must be a function", name.characters()));
return nullptr;
}
return static_cast<Function*>(&target.as_object());
}
static void prepare_arguments_list(Interpreter& interpreter, Value value, MarkedValueList* arguments)
{
if (!value.is_object()) {
interpreter.throw_exception<TypeError>("Arguments list must be an object");
return;
}
auto& arguments_list = value.as_object();
auto length_property = arguments_list.get("length");
if (interpreter.exception())
return;
auto length = length_property.to_size_t();
for (size_t i = 0; i < length; ++i) {
auto element = arguments_list.get(String::number(i));
if (interpreter.exception())
return;
arguments->append(element.value_or(js_undefined()));
}
}
ReflectObject::ReflectObject()
: Object(interpreter().global_object().object_prototype())
{
u8 attr = Attribute::Writable | Attribute::Configurable;
put_native_function("apply", apply, 3, attr);
put_native_function("construct", construct, 2, attr);
put_native_function("defineProperty", define_property, 3, attr);
put_native_function("deleteProperty", delete_property, 2, attr);
put_native_function("get", get, 2, attr);
put_native_function("getOwnPropertyDescriptor", get_own_property_descriptor, 2, attr);
put_native_function("getPrototypeOf", get_prototype_of, 1, attr);
put_native_function("has", has, 2, attr);
put_native_function("isExtensible", is_extensible, 1, attr);
put_native_function("ownKeys", own_keys, 1, attr);
put_native_function("preventExtensions", prevent_extensions, 1, attr);
put_native_function("set", set, 3, attr);
put_native_function("setPrototypeOf", set_prototype_of, 2, attr);
}
ReflectObject::~ReflectObject()
{
}
Value ReflectObject::apply(Interpreter& interpreter)
{
auto* target = get_target_function_from(interpreter, "apply");
if (!target)
return {};
auto this_arg = interpreter.argument(1);
MarkedValueList arguments(interpreter.heap());
prepare_arguments_list(interpreter, interpreter.argument(2), &arguments);
if (interpreter.exception())
return {};
return interpreter.call(*target, this_arg, move(arguments));
}
Value ReflectObject::construct(Interpreter& interpreter)
{
auto* target = get_target_function_from(interpreter, "construct");
if (!target)
return {};
MarkedValueList arguments(interpreter.heap());
prepare_arguments_list(interpreter, interpreter.argument(1), &arguments);
if (interpreter.exception())
return {};
auto* new_target = target;
if (interpreter.argument_count() > 2) {
auto new_target_value = interpreter.argument(2);
if (!new_target_value.is_object()
|| !new_target_value.as_object().is_function()
|| (new_target_value.as_object().is_native_function() && !static_cast<NativeFunction&>(new_target_value.as_object()).has_constructor())) {
interpreter.throw_exception<TypeError>("Optional third argument of Reflect.construct() must be a constructor");
return {};
}
new_target = static_cast<Function*>(&new_target_value.as_object());
}
return interpreter.construct(*target, *new_target, move(arguments));
}
Value ReflectObject::define_property(Interpreter& interpreter)
{
auto* target = get_target_object_from(interpreter, "defineProperty");
if (!target)
return {};
if (!interpreter.argument(2).is_object())
return interpreter.throw_exception<TypeError>("Descriptor argument is not an object");
auto property_key = interpreter.argument(1).to_string();
auto& descriptor = interpreter.argument(2).as_object();
auto success = target->define_property(property_key, descriptor, false);
return Value(success);
}
Value ReflectObject::delete_property(Interpreter& interpreter)
{
auto* target = get_target_object_from(interpreter, "deleteProperty");
if (!target)
return {};
auto property_key = interpreter.argument(1);
PropertyName property = PropertyName(property_key.to_string());
if (property_key.to_number().is_finite_number()) {
auto property_key_as_double = property_key.to_double();
if (property_key_as_double >= 0 && (i32)property_key_as_double == property_key_as_double)
property = PropertyName(property_key_as_double);
}
return target->delete_property(property);
}
Value ReflectObject::get(Interpreter& interpreter)
{
// FIXME: There's a third argument, receiver, for getters - use it once we have those.
auto* target = get_target_object_from(interpreter, "get");
if (!target)
return {};
auto property_key = interpreter.argument(1).to_string();
return target->get(property_key).value_or(js_undefined());
}
Value ReflectObject::get_own_property_descriptor(Interpreter& interpreter)
{
auto* target = get_target_object_from(interpreter, "getOwnPropertyDescriptor");
if (!target)
return {};
auto property_key = interpreter.argument(1).to_string();
return target->get_own_property_descriptor(property_key);
}
Value ReflectObject::get_prototype_of(Interpreter& interpreter)
{
auto* target = get_target_object_from(interpreter, "getPrototypeOf");
if (!target)
return {};
return target->prototype();
}
Value ReflectObject::has(Interpreter& interpreter)
{
auto* target = get_target_object_from(interpreter, "has");
if (!target)
return {};
auto property_key = interpreter.argument(1).to_string();
return Value(target->has_property(property_key));
}
Value ReflectObject::is_extensible(Interpreter&)
{
// FIXME: For this to be useful we need one of these:
// Object.seal(), Object.freeze(), Reflect.preventExtensions()
// For now we just return true, as that's always the case.
return Value(true);
}
Value ReflectObject::own_keys(Interpreter& interpreter)
{
auto* target = get_target_object_from(interpreter, "ownKeys");
if (!target)
return {};
return target->get_own_properties(*target, GetOwnPropertyMode::Key);
}
Value ReflectObject::prevent_extensions(Interpreter&)
{
// FIXME: Implement me :^)
ASSERT_NOT_REACHED();
}
Value ReflectObject::set(Interpreter& interpreter)
{
// FIXME: There's a fourth argument, receiver, for setters - use it once we have those.
auto* target = get_target_object_from(interpreter, "set");
if (!target)
return {};
auto property_key = interpreter.argument(1).to_string();
auto value = interpreter.argument(2);
return Value(target->put(property_key, value));
}
Value ReflectObject::set_prototype_of(Interpreter& interpreter)
{
auto* target = get_target_object_from(interpreter, "setPrototypeOf");
if (!target)
return {};
auto prototype_value = interpreter.argument(1);
if (!prototype_value.is_object() && !prototype_value.is_null()) {
interpreter.throw_exception<TypeError>("Prototype must be an object or null");
return {};
}
Object* prototype = nullptr;
if (!prototype_value.is_null())
prototype = const_cast<Object*>(&prototype_value.as_object());
target->set_prototype(prototype);
// FIXME: Needs to return false for prototype chain cycles and non-extensible objects (don't have those yet).
return Value(true);
}
}

View file

@ -0,0 +1,56 @@
/*
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibJS/Runtime/Object.h>
namespace JS {
class ReflectObject final : public Object {
public:
ReflectObject();
virtual ~ReflectObject() override;
private:
virtual const char* class_name() const override { return "ReflectObject"; }
static Value apply(Interpreter&);
static Value construct(Interpreter&);
static Value define_property(Interpreter&);
static Value delete_property(Interpreter&);
static Value get(Interpreter&);
static Value get_own_property_descriptor(Interpreter&);
static Value get_prototype_of(Interpreter&);
static Value has(Interpreter&);
static Value is_extensible(Interpreter&);
static Value own_keys(Interpreter&);
static Value prevent_extensions(Interpreter&);
static Value set(Interpreter&);
static Value set_prototype_of(Interpreter&);
};
}

View file

@ -38,6 +38,9 @@
#include <LibJS/Runtime/Value.h>
#include <math.h>
// 2 ** 53 - 1
#define MAX_ARRAY_LIKE_INDEX 9007199254740991.0
namespace JS {
bool Value::is_array() const
@ -190,7 +193,7 @@ size_t Value::to_size_t() const
auto number = to_number();
if (number.is_nan() || number.as_double() <= 0)
return 0;
return min(number.to_i32(), (i32)pow(2, 53) - 1);
return min((double)number.to_i32(), MAX_ARRAY_LIKE_INDEX);
}
Value greater_than(Interpreter&, Value lhs, Value rhs)

View file

@ -0,0 +1,37 @@
load("test-common.js");
try {
assert(Reflect.apply.length === 3);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.apply(value);
}, {
error: TypeError,
message: "First argument of Reflect.apply() must be a function"
});
assertThrowsError(() => {
Reflect.apply(() => {}, undefined, value);
}, {
error: TypeError,
message: "Arguments list must be an object"
});
});
assert(Reflect.apply(String.prototype.charAt, "foo", [0]) === "f");
assert(Reflect.apply(Array.prototype.indexOf, ["hello", 123, "foo", "bar"], ["foo"]) === 2);
function Foo(foo) {
this.foo = foo;
}
var o = {};
assert(o.foo === undefined);
assert(Reflect.apply(Foo, o, ["bar"]) === undefined);
assert(o.foo === "bar");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,54 @@
load("test-common.js");
try {
assert(Reflect.construct.length === 2);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.construct(value);
}, {
error: TypeError,
message: "First argument of Reflect.construct() must be a function"
});
assertThrowsError(() => {
Reflect.construct(() => {}, value);
}, {
error: TypeError,
message: "Arguments list must be an object"
});
assertThrowsError(() => {
Reflect.construct(() => {}, [], value);
}, {
error: TypeError,
message: "Optional third argument of Reflect.construct() must be a constructor"
});
});
var a = Reflect.construct(Array, [5]);
assert(a instanceof Array);
assert(a.length === 5);
var s = Reflect.construct(String, [123]);
assert(s instanceof String);
assert(s.length === 3);
assert(s.toString() === "123");
function Foo() {
this.name = "foo";
}
function Bar() {
this.name = "bar";
}
var o = Reflect.construct(Foo, [], Bar);
assert(o.name === "foo");
assert(o instanceof Foo === false);
assert(o instanceof Bar === true);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,59 @@
load("test-common.js");
try {
assert(Reflect.defineProperty.length === 3);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.defineProperty(value);
}, {
error: TypeError,
message: "First argument of Reflect.defineProperty() must be an object"
});
assertThrowsError(() => {
Reflect.defineProperty({}, "foo", value);
}, {
error: TypeError,
message: "Descriptor argument is not an object"
});
});
var o = {};
assert(Reflect.defineProperty(o, "foo", { value: 1, writable: false, enumerable: false }) === true);
assert(o.foo === 1);
o.foo = 2;
assert(o.foo === 1);
assert(Reflect.defineProperty(o, "bar", { value: "hi", writable: true, enumerable: true }) === true);
assert(o.bar === "hi");
o.bar = "ho";
assert(o.bar === "ho");
assert(Reflect.defineProperty(o, "bar", { value: "xx", enumerable: false }) === false);
var d = Reflect.getOwnPropertyDescriptor(o, "foo");
assert(d.configurable === false);
assert(d.enumerable === false);
assert(d.writable === false);
assert(d.value === 1);
d = Reflect.getOwnPropertyDescriptor(o, "bar");
assert(d.configurable === false);
assert(d.enumerable === true);
assert(d.writable === true);
assert(d.value === "ho");
assert(Reflect.defineProperty(o, "baz", { value: 9, configurable: true, writable: false }) === true);
assert(Reflect.defineProperty(o, "baz", { configurable: true, writable: true }) === true);
d = Reflect.getOwnPropertyDescriptor(o, "baz");
assert(d.configurable === true);
assert(d.writable === true);
assert(d.value === 9);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,47 @@
load("test-common.js");
try {
assert(Reflect.deleteProperty.length === 2);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.deleteProperty(value);
}, {
error: TypeError,
message: "First argument of Reflect.deleteProperty() must be an object"
});
});
assert(Reflect.deleteProperty({}) === true);
assert(Reflect.deleteProperty({}, "foo") === true);
var o = { foo: 1 };
assert(o.foo === 1);
assert(Reflect.deleteProperty(o, "foo") === true);
assert(o.foo === undefined);
assert(Reflect.deleteProperty(o, "foo") === true);
assert(o.foo === undefined);
Object.defineProperty(o, "bar", { value: 2, configurable: true, writable: false });
assert(Reflect.deleteProperty(o, "bar") === true);
assert(o.bar === undefined);
Object.defineProperty(o, "baz", { value: 3, configurable: false, writable: true });
assert(Reflect.deleteProperty(o, "baz") === false);
assert(o.baz === 3);
var a = [1, 2, 3];
assert(a.length === 3);
assert(a[0] === 1);
assert(a[1] === 2);
assert(a[2] === 3);
assert(Reflect.deleteProperty(a, 1) === true);
assert(a.length === 3);
assert(a[0] === 1);
assert(a[1] === undefined);
assert(a[2] === 3);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,39 @@
load("test-common.js");
try {
assert(Reflect.get.length === 2);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.get(value);
}, {
error: TypeError,
message: "First argument of Reflect.get() must be an object"
});
});
assert(Reflect.get({}) === undefined);
assert(Reflect.get({ undefined: 1 }) === 1);
assert(Reflect.get({ foo: 1 }) === undefined);
assert(Reflect.get({ foo: 1 }, "foo") === 1);
assert(Reflect.get([]) === undefined);
assert(Reflect.get([1, 2, 3]) === undefined);
assert(Reflect.get([1, 2, 3], "0") === 1);
assert(Reflect.get([1, 2, 3], 0) === 1);
assert(Reflect.get([1, 2, 3], 1) === 2);
assert(Reflect.get([1, 2, 3], 2) === 3);
assert(Reflect.get([1, 2, 3], 4) === undefined);
assert(Reflect.get(new String()) === undefined);
assert(Reflect.get(new String(), 0) === undefined);
assert(Reflect.get(new String("foo"), "0") === "f");
assert(Reflect.get(new String("foo"), 0) === "f");
assert(Reflect.get(new String("foo"), 1) === "o");
assert(Reflect.get(new String("foo"), 2) === "o");
assert(Reflect.get(new String("foo"), 3) === undefined);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,35 @@
load("test-common.js");
try {
assert(Reflect.getOwnPropertyDescriptor.length === 2);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.getOwnPropertyDescriptor(value);
}, {
error: TypeError,
message: "First argument of Reflect.getOwnPropertyDescriptor() must be an object"
});
});
assert(Reflect.getOwnPropertyDescriptor({}) === undefined);
assert(Reflect.getOwnPropertyDescriptor({}, "foo") === undefined);
var o = { foo: "bar" };
var d = Reflect.getOwnPropertyDescriptor(o, "foo");
assert(d.value === "bar");
assert(d.writable === true);
assert(d.enumerable === true);
assert(d.configurable === true);
var a = [];
d = Reflect.getOwnPropertyDescriptor(a, "length");
assert(d.value === 0);
assert(d.writable === true);
assert(d.enumerable === false);
assert(d.configurable === false);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,26 @@
load("test-common.js");
try {
assert(Reflect.getPrototypeOf.length === 1);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.getPrototypeOf(value);
}, {
error: TypeError,
message: "First argument of Reflect.getPrototypeOf() must be an object"
});
});
assert(Reflect.getPrototypeOf({}) === Object.prototype);
assert(Reflect.getPrototypeOf([]) === Array.prototype);
assert(Reflect.getPrototypeOf(new String()) === String.prototype);
var o = {};
Reflect.setPrototypeOf(o, { foo: "bar" });
assert(Reflect.getPrototypeOf(o).foo === "bar");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,42 @@
load("test-common.js");
try {
assert(Reflect.has.length === 2);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.has(value);
}, {
error: TypeError,
message: "First argument of Reflect.has() must be an object"
});
});
assert(Reflect.has({}) === false);
assert(Reflect.has({ undefined }) === true);
assert(Reflect.has({ 0: "1" }, "0") === true);
assert(Reflect.has({ foo: "bar" }, "foo") === true);
assert(Reflect.has({ bar: "baz" }, "foo") === false);
assert(Reflect.has({}, "toString") === true);
assert(Reflect.has([]) === false);
assert(Reflect.has([], 0) === false);
assert(Reflect.has([1, 2, 3], "0") === true);
assert(Reflect.has([1, 2, 3], 0) === true);
assert(Reflect.has([1, 2, 3], 1) === true);
assert(Reflect.has([1, 2, 3], 2) === true);
assert(Reflect.has([1, 2, 3], 3) === false);
assert(Reflect.has([], "pop") === true);
assert(Reflect.has(new String()) === false);
assert(Reflect.has(new String("foo"), "0") === true);
assert(Reflect.has(new String("foo"), 0) === true);
assert(Reflect.has(new String("foo"), 1) === true);
assert(Reflect.has(new String("foo"), 2) === true);
assert(Reflect.has(new String("foo"), 3) === false);
assert(Reflect.has(new String("foo"), "charAt") === true);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,39 @@
load("test-common.js");
try {
assert(Reflect.ownKeys.length === 1);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.ownKeys(value);
}, {
error: TypeError,
message: "First argument of Reflect.ownKeys() must be an object"
});
});
var objectOwnKeys = Reflect.ownKeys({});
assert(objectOwnKeys.length === 0);
objectOwnKeys = Reflect.ownKeys({ foo: "bar", bar: "baz", 0: 42 });
assert(objectOwnKeys.length === 3);
assert(objectOwnKeys[0] === "0");
assert(objectOwnKeys[1] === "foo");
assert(objectOwnKeys[2] === "bar");
var arrayOwnKeys = Reflect.ownKeys([]);
assert(arrayOwnKeys.length === 1);
assert(arrayOwnKeys[0] === "length");
arrayOwnKeys = Reflect.ownKeys(["foo", [], 123, undefined]);
assert(arrayOwnKeys.length === 5);
assert(arrayOwnKeys[0] === "0");
assert(arrayOwnKeys[1] === "1");
assert(arrayOwnKeys[2] === "2");
assert(arrayOwnKeys[3] === "3");
assert(arrayOwnKeys[4] === "length");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,54 @@
load("test-common.js");
try {
assert(Reflect.set.length === 3);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.set(value);
}, {
error: TypeError,
message: "First argument of Reflect.set() must be an object"
});
});
assert(Reflect.set({}) === true);
assert(Reflect.set({}, "foo") === true);
assert(Reflect.set({}, "foo", "bar") === true);
var o = {};
assert(o.foo === undefined);
assert(Reflect.set(o, "foo", 1) === true);
assert(o.foo === 1);
assert(Reflect.set(o, "foo", 2) === true);
assert(o.foo === 2);
Object.defineProperty(o, "bar", { value: 2, configurable: true, writable: false });
assert(Reflect.set(o, "bar") === false);
assert(o.bar === 2);
Object.defineProperty(o, "baz", { value: 3, configurable: false, writable: true });
assert(Reflect.set(o, "baz") === true);
assert(o.baz === undefined);
var a = [];
assert(a.length === 0);
assert(Reflect.set(a, "0") === true);
assert(a.length === 1);
assert(a[0] === undefined);
assert(Reflect.set(a, 1, "foo") === true);
assert(a.length === 2);
assert(a[0] === undefined);
assert(a[1] === "foo");
assert(Reflect.set(a, 4, "bar") === true);
assert(a.length === 5);
assert(a[0] === undefined);
assert(a[1] === "foo");
assert(a[2] === undefined);
assert(a[3] === undefined);
assert(a[4] === "bar");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,39 @@
load("test-common.js");
try {
assert(Reflect.setPrototypeOf.length === 2);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.setPrototypeOf(value);
}, {
error: TypeError,
message: "First argument of Reflect.setPrototypeOf() must be an object"
});
if (value === null)
return;
assertThrowsError(() => {
Reflect.setPrototypeOf({}, value);
}, {
error: TypeError,
message: "Prototype must be an object or null"
});
});
assert(Reflect.setPrototypeOf({}, null) === true);
assert(Reflect.setPrototypeOf({}, {}) === true);
assert(Reflect.setPrototypeOf({}, Object.prototype) === true);
assert(Reflect.setPrototypeOf({}, Array.prototype) === true);
assert(Reflect.setPrototypeOf({}, String.prototype) === true);
assert(Reflect.setPrototypeOf({}, Reflect.getPrototypeOf({})) === true);
var o = {};
assert(o.foo === undefined);
assert(Reflect.setPrototypeOf(o, { foo: "bar" }) === true);
assert(o.foo === "bar");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}