https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toobject
(ctx: &Ctx<'js>, value: Value<'js>)
| 628 | |
| 629 | // https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toobject |
| 630 | fn to_object<'js>(ctx: &Ctx<'js>, value: Value<'js>) -> Result<Object<'js>> { |
| 631 | let base_primordials = BasePrimordials::get(ctx)?; |
| 632 | |
| 633 | match value.type_of() { |
| 634 | // Return a new Boolean object whose [[BooleanData]] internal slot is set to argument |
| 635 | Type::Bool => base_primordials.constructor_bool.construct((value,))?, |
| 636 | // Return a new Number object whose [[NumberData]] internal slot is set to argument |
| 637 | Type::Int | Type::Float => base_primordials.constructor_number.construct((value,))?, |
| 638 | // Return a new String object whose [[StringData]] internal slot is set to argument |
| 639 | Type::String => base_primordials.constructor_string.construct((value,))?, |
| 640 | // Return a new Symbol object whose [[SymbolData]] internal slot is set to argument |
| 641 | // `new Symbol` is invalid but we can use `Object(symbol) |
| 642 | Type::Symbol => base_primordials.constructor_object.call((value,))?, |
| 643 | // Return a new BigInt object whose [[BigIntData]] internal slot is set to argument |
| 644 | // `new BigInt` is invalid but we can use `Object(bigInt) |
| 645 | Type::BigInt => base_primordials.constructor_object.call((value,))?, |
| 646 | // Return argument |
| 647 | typ if typ.interpretable_as(Type::Object) => Ok(value.into_object().unwrap()), |
| 648 | // Throw a TypeError exception. |
| 649 | typ => Err(Exception::throw_type( |
| 650 | ctx, |
| 651 | &format!("{typ} cannot be converted to an object"), |
| 652 | )), |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | #[cfg(test)] |
| 657 | mod tests { |