Skip to content

Commit

Permalink
Update [[HostDefined]] example.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnyob committed Nov 7, 2023
1 parent 673f062 commit d5f18f8
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions boa_examples/src/bin/host_defined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ fn main() -> Result<(), JsError> {

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

{
assert!(realm.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 host_defined = realm.host_defined();
let Some(host_defined) = host_defined.get::<CustomHostDefinedStruct>() else {
return Err(JsNativeError::typ()
.with_message("Realm does not have HostDefined field")
.into());
Expand All @@ -53,15 +54,15 @@ fn main() -> Result<(), JsError> {

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

{
assert!(realm.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>()
else {
let host_defined = realm.host_defined();
let Some(host_defined) = host_defined.get::<AnotherCustomHostDefinedStruct>() else {
return Err(JsNativeError::typ()
.with_message("Realm does not have HostDefined field")
.into());
Expand All @@ -73,7 +74,7 @@ fn main() -> Result<(), JsError> {

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

Expand All @@ -86,8 +87,8 @@ 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 Some(mut host_defined) = host_defined.get_mut::<CustomHostDefinedStruct>() else {
let mut host_defined = context.realm().host_defined_mut();
let Some(host_defined) = host_defined.get_mut::<CustomHostDefinedStruct>() else {
return Err(JsNativeError::typ()
.with_message("Realm does not have HostDefined field")
.into());
Expand Down Expand Up @@ -122,7 +123,8 @@ fn main() -> Result<(), JsError> {
",
))?;

let Some(host_defined) = realm.host_defined().get::<CustomHostDefinedStruct>() else {
let host_defined = realm.host_defined();
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 d5f18f8

Please sign in to comment.