Skip to content

Commit

Permalink
Update [[HostDefined]] example.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnyob committed Oct 9, 2023
1 parent 127a7f5 commit 52e4428
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions boa_examples/src/bin/host_defined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,51 +32,54 @@ fn main() -> Result<(), JsError> {
// Get the realm from the context.
let realm = context.realm().clone();

// Get the [[HostDefined]] field from the realm.
let mut host_defined = realm.host_defined().borrow_mut();

// Insert a default CustomHostDefinedStruct.
realm
.host_defined()
.insert_default::<CustomHostDefinedStruct>();
host_defined.insert_default::<CustomHostDefinedStruct>();

{
assert!(realm.host_defined().has::<CustomHostDefinedStruct>());
assert!(host_defined.has::<CustomHostDefinedStruct>());

// Get the [[HostDefined]] field from the realm and downcast it to our concrete type.
let Some(host_defined) = realm.host_defined().get::<CustomHostDefinedStruct>() else {
let Some(custom_host_defined_struct) = host_defined.get::<CustomHostDefinedStruct>() else {
return Err(JsNativeError::typ()
.with_message("Realm does not have HostDefined field")
.into());
};

// Assert that the [[HostDefined]] field is in it's initial state.
assert_eq!(host_defined.counter, 0);
assert_eq!(custom_host_defined_struct.counter, 0);
}

// Insert another struct with state into [[HostDefined]] field.
realm
.host_defined()
.insert(AnotherCustomHostDefinedStruct::new(10));
host_defined.insert(AnotherCustomHostDefinedStruct::new(10));

{
assert!(realm.host_defined().has::<AnotherCustomHostDefinedStruct>());
assert!(host_defined.has::<AnotherCustomHostDefinedStruct>());

// Get the [[HostDefined]] field from the realm and downcast it to our concrete type.
let Some(host_defined) = realm.host_defined().get::<AnotherCustomHostDefinedStruct>()
let Some(another_custom_host_defined_struct) =
host_defined.get::<AnotherCustomHostDefinedStruct>()
else {
return Err(JsNativeError::typ()
.with_message("Realm does not have HostDefined field")
.into());
};

// Assert that the [[HostDefined]] field is in it's initial state.
assert_eq!(host_defined.counter, 10);
assert_eq!(another_custom_host_defined_struct.counter, 10);
}

// Remove a type from the [[HostDefined]] field.
assert!(realm
.host_defined()
assert!(host_defined
.remove::<AnotherCustomHostDefinedStruct>()
.is_some());

// We need to explicitly drop the borrow of the [[HostDefined]] field
// to avoid a double borrow when running the JavaScript code.
drop(host_defined);

// Create and register function for getting and setting the realm value.
//
// The funtion lives in the context's realm and has access to the host-defined field.
Expand All @@ -86,7 +89,7 @@ fn main() -> Result<(), JsError> {
NativeFunction::from_fn_ptr(|_, args, context| {
let value: usize = args.get_or_undefined(0).try_js_into(context)?;

let host_defined = context.realm().host_defined();
let host_defined = context.realm().host_defined().borrow_mut();
let Some(mut host_defined) = host_defined.get_mut::<CustomHostDefinedStruct>() else {
return Err(JsNativeError::typ()
.with_message("Realm does not have HostDefined field")
Expand All @@ -103,7 +106,7 @@ fn main() -> Result<(), JsError> {
js_string!("getRealmValue"),
0,
NativeFunction::from_fn_ptr(|_, _, context| {
let host_defined = context.realm().host_defined();
let host_defined = context.realm().host_defined().borrow_mut();
let Some(host_defined) = host_defined.get::<CustomHostDefinedStruct>() else {
return Err(JsNativeError::typ()
.with_message("Realm does not have HostDefined field")
Expand All @@ -122,7 +125,10 @@ fn main() -> Result<(), JsError> {
",
))?;

let Some(host_defined) = realm.host_defined().get::<CustomHostDefinedStruct>() else {
// Get the [[HostDefined]] field from the realm
let host_defined = realm.host_defined().borrow();

let Some(host_defined) = host_defined.get::<CustomHostDefinedStruct>() else {
return Err(JsNativeError::typ()
.with_message("Realm does not have HostDefined field")
.into());
Expand Down

0 comments on commit 52e4428

Please sign in to comment.