MCPcopy Create free account
hub / github.com/awslabs/llrt / to_object

Function to_object

modules/llrt_stream_web/src/readable/iterator.rs:630–654  ·  view source on GitHub ↗

https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toobject

(ctx: &Ctx<'js>, value: Value<'js>)

Source from the content-addressed store, hash-verified

628
629// https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toobject
630fn 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)]
657mod tests {

Callers 2

get_vFunction · 0.85
test_to_objectFunction · 0.85

Calls 2

getFunction · 0.85
callMethod · 0.45

Tested by 1

test_to_objectFunction · 0.68