Skip to content

Commit

Permalink
Fixed name and length properties in global objects
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed May 23, 2020
1 parent d01d61a commit 70f2ef4
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ pub fn create(global: &Value) -> Value {
make_builtin_fn!(slice, named "slice", with length 2, of prototype);
make_builtin_fn!(some, named "some", with length 2, of prototype);

let array = make_constructor_fn(make_array, global, prototype, true);
let array = make_constructor_fn("Array", 1, make_array, global, prototype, true);

// Static Methods
make_builtin_fn!(is_array, named "isArray", with length 1, of array);
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn create(global: &Value) -> Value {
make_builtin_fn!(to_string, named "toString", with length 1, of prototype);
make_builtin_fn!(value_of, named "valueOf", of prototype);

make_constructor_fn(make_bigint, global, prototype, false)
make_constructor_fn("BigInt", 1, make_bigint, global, prototype, false)
}

/// Initialise the `BigInt` object on the global object.
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn create(global: &Value) -> Value {
make_builtin_fn!(to_string, named "toString", of prototype);
make_builtin_fn!(value_of, named "valueOf", of prototype);

make_constructor_fn(construct_boolean, global, prototype, true)
make_constructor_fn("Boolean", 1, construct_boolean, global, prototype, true)
}

/// Initialise the `Boolean` object on the global object.
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn create(global: &Value) -> Value {
prototype.set_field_slice("name", Value::from("Error"));
make_builtin_fn!(to_string, named "toString", of prototype);

make_constructor_fn(make_error, global, prototype, true)
make_constructor_fn("Error", 1, make_error, global, prototype, true)
}

/// Initialise the global object with the `Error` object.
Expand Down
30 changes: 18 additions & 12 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl Function {
}
}
} else {
let name = this_obj.get_field_slice("name").to_string();
let name = this.get_field_slice("name").to_string();
panic!("TypeError: {} is not a constructor", name);
}
}
Expand Down Expand Up @@ -338,16 +338,6 @@ impl Debug for Function {
}
}

/// Function Prototype.
///
/// <https://tc39.es/ecma262/#sec-properties-of-the-function-prototype-object>
pub fn create_function_prototype() {
let mut function_prototype: Object = Object::default();
// Set Kind to function (for historical & compatibility reasons)
// <https://tc39.es/ecma262/#sec-properties-of-the-function-prototype-object>
function_prototype.kind = ObjectKind::Function;
}

/// Arguments.
///
/// <https://tc39.es/ecma262/#sec-createunmappedargumentsobject>
Expand Down Expand Up @@ -388,14 +378,16 @@ pub fn make_function(this: &mut Value, _: &[Value], _: &mut Interpreter) -> Resu
pub fn create(global: &Value) -> Value {
let prototype = Value::new_object(Some(global));

make_constructor_fn(make_function, global, prototype, true)
make_constructor_fn("Function", 1, make_function, global, prototype, true)
}

/// Creates a new constructor function
///
/// This utility function handling linking the new Constructor to the prototype.
/// So far this is only used by internal functions
pub fn make_constructor_fn(
name: &str,
length: i32,
body: NativeFunctionData,
global: &Value,
proto: Value,
Expand All @@ -422,6 +414,20 @@ pub fn make_constructor_fn(
proto.set_field_slice("constructor", constructor_val.clone());
constructor_val.set_field_slice(PROTOTYPE, proto);

let length = Property::new()
.value(Value::from(length))
.writable(false)
.configurable(false)
.enumerable(false);
constructor_val.set_property_slice("length", length);

let name = Property::new()
.value(Value::from(name))
.writable(false)
.configurable(false)
.enumerable(false);
constructor_val.set_property_slice("name", name);

constructor_val
}

Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ pub fn create(global: &Value) -> Value {
make_builtin_fn!(to_string, named "toString", with length 1, of prototype);
make_builtin_fn!(value_of, named "valueOf", of prototype);

make_constructor_fn(make_number, global, prototype, true)
make_constructor_fn("Number", 1, make_number, global, prototype, true)
}

/// Initialise the `Number` object on the global object.
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ pub fn create(global: &Value) -> Value {
make_builtin_fn!(has_own_property, named "hasOwnProperty", of prototype);
make_builtin_fn!(to_string, named "toString", of prototype);

let object = make_constructor_fn(make_object, global, prototype, true);
let object = make_constructor_fn("Object", 1, make_object, global, prototype, true);

object.set_field_slice("length", Value::from(1));
make_builtin_fn!(set_prototype_of, named "setPrototypeOf", with length 2, of object);
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ pub fn create(global: &Value) -> Value {
make_builtin_fn!(get_sticky, named "sticky", of prototype);
make_builtin_fn!(get_unicode, named "unicode", of prototype);

make_constructor_fn(make_regexp, global, prototype, true)
make_constructor_fn("RegExp", 1, make_regexp, global, prototype, true)
}

/// Initialise the `RegExp` object on the global object.
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ pub fn create(global: &Value) -> Value {
make_builtin_fn!(match_all, named "matchAll", with length 1, of prototype);
make_builtin_fn!(replace, named "replace", with length 2, of prototype);

make_constructor_fn(make_string, global, prototype, true)
make_constructor_fn("String", 1, make_string, global, prototype, true)
}

/// Initialise the `String` object on the global object.
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/symbol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn create(global: &Value) -> Value {
let prototype = Value::new_object(Some(global));
make_builtin_fn!(to_string, named "toString", of prototype);

make_constructor_fn(call_symbol, global, prototype, false)
make_constructor_fn("Symbol", 1, call_symbol, global, prototype, false)
}

/// Initialise the `Symbol` object on the global object.
Expand Down

0 comments on commit 70f2ef4

Please sign in to comment.