Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[builtins - object] Object.create #543

Merged
merged 12 commits into from
Jul 8, 2020
12 changes: 12 additions & 0 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ pub fn make_object(_: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultVa
Ok(object)
}

/// Creates a new object from the provided prototype
///
/// https://tc39.es/ecma262/#sec-object.create
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
croraf marked this conversation as resolved.
Show resolved Hide resolved
pub fn create_builtin(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
let __proto__ = args.get(0).cloned().unwrap_or_else(Value::undefined);
croraf marked this conversation as resolved.
Show resolved Hide resolved
let new_object = Value::new_object_from_prototype(__proto__, ObjectData::Ordinary);
Ok(new_object.into())
croraf marked this conversation as resolved.
Show resolved Hide resolved
}

/// Uses the SameValue algorithm to check equality of objects
pub fn is(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
let x = args.get(0).cloned().unwrap_or_else(Value::undefined);
Expand Down Expand Up @@ -517,6 +527,8 @@ pub fn create(global: &Value) -> Value {

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

// static methods of the builtin Object
make_builtin_fn(create_builtin, "create", &object, 1);
make_builtin_fn(set_prototype_of, "setPrototypeOf", &object, 2);
make_builtin_fn(get_prototype_of, "getPrototypeOf", &object, 1);
make_builtin_fn(define_property, "defineProperty", &object, 3);
Expand Down
16 changes: 16 additions & 0 deletions boa/src/builtins/object/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
use crate::{exec::Interpreter, forward, realm::Realm};

#[test]
fn object_create() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

let init = r#"
const foo = { a: 5 };
const bar = Object.create(foo);
"#;

forward(&mut engine, init);

assert_eq!(forward(&mut engine, "bar.a"), "5");
assert_eq!(forward(&mut engine, "Object.create.length"), "1");
}

#[test]
fn object_is() {
let realm = Realm::create();
Expand Down