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
33 changes: 33 additions & 0 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,37 @@ pub fn make_object(_: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultVa
Ok(object)
}


/// `Object.create( proto, [propertiesObject] )`
///
/// Creates a new object from the provided prototype.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.create
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
pub fn create_builtin(_: &Value, args: &[Value], interpreter: &mut Interpreter) -> ResultValue {
croraf marked this conversation as resolved.
Show resolved Hide resolved
let __proto__ = args.get(0).cloned().unwrap_or_else(Value::undefined);
croraf marked this conversation as resolved.
Show resolved Hide resolved
let properties = args.get(1).cloned().unwrap_or_else(Value::undefined);

if properties != Value::Undefined {
unimplemented!("propertiesObject argument of Object.create")
}
croraf marked this conversation as resolved.
Show resolved Hide resolved

match __proto__ {
Value::Object(_) | Value::Null => Ok(Value::new_object_from_prototype(
__proto__,
ObjectData::Ordinary,
)),
_ => interpreter.throw_type_error(format!(
"Object prototype may only be an Object or null: {}",
__proto__
)),
}
}

/// 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 +548,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, 2);
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
70 changes: 70 additions & 0 deletions boa/src/builtins/object/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
use crate::{exec::Interpreter, forward, realm::Realm};

#[test]
fn object_create_with_regular_object() {
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_create_with_undefined() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

let init = r#"
try {
const bar = Object.create();
} catch (err) {
err.message
}
"#;

let result = forward(&mut engine, init);
assert_eq!(
result,
"Object prototype may only be an Object or null: undefined"
);
}

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

let init = r#"
try {
const bar = Object.create(5);
} catch (err) {
err.message
}
"#;

let result = forward(&mut engine, init);
assert_eq!(result, "Object prototype may only be an Object or null: 5");
}

#[test]
#[ignore]
// to test on __proto__ somehow. __proto__ getter is not working as expected currently
fn object_create_with_function() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

let init = r#"
const x = function (){};
const bar = Object.create(5);
bar.__proto__
"#;

let result = forward(&mut engine, init);
assert_eq!(result, "...something on __proto__...");
}

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