Skip to content

Commit 17485b2

Browse files
fix(bindgen): implementation of flat lower own
1 parent d3932bd commit 17485b2

3 files changed

Lines changed: 317 additions & 21 deletions

File tree

crates/js-component-bindgen/src/intrinsics/lower.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -924,17 +924,31 @@ impl LowerIntrinsic {
924924
));
925925
}
926926

927-
// TODO: implement lower flat own
928927
Self::LowerFlatOwn => {
929928
let debug_log_fn = Intrinsic::DebugLog.name();
930929
let lower_flat_own_fn = self.name();
930+
931931
output.push_str(&format!(
932-
"
933-
function {lower_flat_own_fn}(ctx) {{
934-
{debug_log_fn}('[{lower_flat_own_fn}()] args', {{ ctx }});
935-
throw new Error('flat lower for owned resources not yet implemented!');
936-
}}
937-
"
932+
r#"
933+
function {lower_flat_own_fn}(meta) {{
934+
const {{ lowerFn, componentIdx }} = meta;
935+
936+
return function {lower_flat_own_fn}Inner(ctx) {{
937+
{debug_log_fn}('[{lower_flat_own_fn}()] args', {{ ctx }});
938+
const {{ createFn }} = ctx;
939+
940+
if (ctx.componentIdx !== componentIdx) {{
941+
throw new Error(`component index mismatch (expected [${{componentIdx}}], lift called from [${{ctx.componentIdx}}])`);
942+
}}
943+
944+
console.log("PARAMS?", [...arguments]);
945+
946+
const obj = ctx.vals[0];
947+
if (obj === undefined || obj === null) {{ throw new Error('missing resource'); }}
948+
lowerFn(obj);
949+
}};
950+
}}
951+
"#
938952
));
939953
}
940954

crates/js-component-bindgen/src/intrinsics/resource.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ pub enum ResourceIntrinsic {
6161
ResourceTransferBorrowValidLifting,
6262
ResourceTransferOwn,
6363
CurResourceBorrows,
64+
65+
/// Monotonically increasing ID that is used for lowered resource reps
66+
LoweredResourceRepId,
6467
}
6568

6669
impl ResourceIntrinsic {
@@ -83,6 +86,7 @@ impl ResourceIntrinsic {
8386
Self::ResourceTransferBorrowValidLifting.name(),
8487
Self::ResourceTransferOwn.name(),
8588
Self::CurResourceBorrows.name(),
89+
Self::LoweredResourceRepId.name(),
8690
]
8791
}
8892

@@ -100,6 +104,7 @@ impl ResourceIntrinsic {
100104
Self::ResourceTransferBorrowValidLifting => "resourceTransferBorrowValidLifting",
101105
Self::ResourceTransferOwn => "resourceTransferOwn",
102106
Self::CurResourceBorrows => "curResourceBorrows",
107+
Self::LoweredResourceRepId => "LOWERED_RESOURCE_REP",
103108
}
104109
}
105110

@@ -252,6 +257,11 @@ impl ResourceIntrinsic {
252257
let name = self.name();
253258
output.push_str(&format!("let {name} = [];"));
254259
}
260+
261+
Self::LoweredResourceRepId => {
262+
let name = self.name();
263+
output.push_str(&format!("const {name} = 1;"));
264+
}
255265
}
256266
}
257267
}

0 commit comments

Comments
 (0)