Skip to content

Commit 99beacc

Browse files
fix(bindgen): more gracefully handle missing type data
1 parent 0ca2717 commit 99beacc

1 file changed

Lines changed: 46 additions & 42 deletions

File tree

crates/js-component-bindgen/src/transpile_bindgen.rs

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4792,42 +4792,37 @@ pub fn gen_flat_lift_fn_js_expr(
47924792
let component_idx = table_ty.unwrap_concrete_instance().as_u32();
47934793
let resource_idx = table_ty.unwrap_concrete_ty();
47944794

4795-
let resource_typedef = intrinsic_mgr
4795+
if let Some(resource_typedef) = intrinsic_mgr
47964796
.exports_resource_index_types
47974797
.get(&resource_idx)
4798-
.expect("unexpectedly missing owned resource type in lookup");
4799-
4800-
let (resource_class_name, create_resource_fn_js) = match intrinsic_mgr
4801-
.resource_exports
4802-
.get(resource_typedef)
4798+
&& let Some(ResourceTable { imported, data }) =
4799+
intrinsic_mgr.resource_exports.get(resource_typedef)
48034800
{
4804-
None => ("null".into(), "() => null".into()),
4805-
Some(ResourceTable { imported, data }) => {
4806-
assert!(
4807-
!imported,
4808-
"imported resources cannot be owned and returned via lifting"
4809-
);
4801+
assert!(
4802+
!imported,
4803+
"imported resources cannot be owned and returned via lifting"
4804+
);
48104805

4811-
match data {
4812-
ResourceData::Guest { .. } => {
4813-
unimplemented!(
4814-
"owned resources created by guests should must have host-side data"
4815-
)
4816-
}
4817-
ResourceData::Host {
4818-
tid,
4819-
local_name,
4820-
dtor_name,
4821-
..
4822-
} => {
4823-
let empty_func = JsHelperIntrinsic::EmptyFunc.name();
4824-
let symbol_resource_handle = Intrinsic::SymbolResourceHandle.name();
4825-
let symbol_dispose = Intrinsic::SymbolDispose.name();
4826-
let rsc_table_remove = ResourceIntrinsic::ResourceTableRemove.name();
4827-
let tid = tid.as_u32();
4828-
let rsc_flag = ResourceIntrinsic::ResourceTableFlag.name();
4829-
4830-
let dtor_setup_js = dtor_name.as_ref().map(|dtor|
4806+
let (resource_class_name, create_resource_fn_js) = match data {
4807+
ResourceData::Guest { .. } => {
4808+
unimplemented!(
4809+
"owned resources created by guests should must have host-side data"
4810+
)
4811+
}
4812+
ResourceData::Host {
4813+
tid,
4814+
local_name,
4815+
dtor_name,
4816+
..
4817+
} => {
4818+
let empty_func = JsHelperIntrinsic::EmptyFunc.name();
4819+
let symbol_resource_handle = Intrinsic::SymbolResourceHandle.name();
4820+
let symbol_dispose = Intrinsic::SymbolDispose.name();
4821+
let rsc_table_remove = ResourceIntrinsic::ResourceTableRemove.name();
4822+
let tid = tid.as_u32();
4823+
let rsc_flag = ResourceIntrinsic::ResourceTableFlag.name();
4824+
4825+
let dtor_setup_js = dtor_name.as_ref().map(|dtor|
48314826
format!(
48324827
r#"
48334828
Object.defineProperty(
@@ -4848,8 +4843,8 @@ pub fn gen_flat_lift_fn_js_expr(
48484843
)
48494844
).unwrap_or_default();
48504845

4851-
let create_resource_fn_js = format!(
4852-
r#"
4846+
let create_resource_fn_js = format!(
4847+
r#"
48534848
(handle) => {{
48544849
const resourceObj = Object.create({local_name}.prototype);
48554850
Object.defineProperty(resourceObj, {symbol_resource_handle}, {{
@@ -4861,22 +4856,31 @@ pub fn gen_flat_lift_fn_js_expr(
48614856
return resourceObj;
48624857
}}
48634858
"#
4864-
);
4859+
);
48654860

4866-
(local_name.to_string(), create_resource_fn_js)
4867-
}
4861+
(local_name.to_string(), create_resource_fn_js)
48684862
}
4869-
}
4870-
};
4863+
};
48714864

4872-
format!(
4873-
r#"{f}({{
4865+
format!(
4866+
r#"{f}({{
48744867
componentIdx: {component_idx},
48754868
className: {resource_class_name},
48764869
createResourceFn: {create_resource_fn_js},
48774870
}})
48784871
"#,
4879-
)
4872+
)
4873+
} else {
4874+
// In this case we couldn't find required type information
4875+
format!(
4876+
r#"{f}({{
4877+
componentIdx: {component_idx},
4878+
className: null,
4879+
createResourceFn: () => {{ throw new Error('invalid/missing resource type data'); }},
4880+
}})
4881+
"#,
4882+
)
4883+
}
48804884
}
48814885

48824886
InterfaceType::Borrow(ty_idx) => {

0 commit comments

Comments
 (0)