Skip to content

Commit 72974e0

Browse files
fix(bindgen): async stream option & result lowering
1 parent 448b5d8 commit 72974e0

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

  • crates/js-component-bindgen/src/intrinsics

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -471,21 +471,21 @@ impl LowerIntrinsic {
471471
let require_valid_numeric_primitive_fn =
472472
Intrinsic::Conversion(ConversionIntrinsic::RequireValidNumericPrimitive).name();
473473

474-
output.push_str(&format!("
474+
output.push_str(&format!(r#"
475475
function {lower_flat_f32_fn}(ctx) {{
476476
{debug_log_fn}('[{lower_flat_f32_fn}()] args', {{ ctx }});
477477
478478
if (ctx.vals.length !== 1) {{ throw new Error('unexpected number of vals'); }}
479479
480-
const rem = ctx.storagePtr % 8;
481-
if (rem !== 0) {{ ctx.storagePtr += (8 - rem); }}
480+
const rem = ctx.storagePtr % 4;
481+
if (rem !== 0) {{ ctx.storagePtr += (4 - rem); }}
482482
483483
{require_valid_numeric_primitive_fn}.bind('f32', ctx.vals[0]);
484484
new DataView(ctx.memory.buffer).setFloat32(ctx.storagePtr, ctx.vals[0], true);
485485
486-
ctx.storagePtr += 8;
486+
ctx.storagePtr += 4;
487487
}}
488-
"));
488+
"#));
489489
}
490490

491491
Self::LowerFlatFloat64 => {
@@ -659,14 +659,6 @@ impl LowerIntrinsic {
659659
}}
660660
661661
const payloadOffsetPtr = originalPtr + payloadOffset32;
662-
console.log("ABOUT TO LOWER?", {{
663-
payloadOffsetPtr,
664-
originalPtr,
665-
afterDiscrimStoragePtr: ctx.storagePtr,
666-
lowerFn,
667-
val,
668-
payloadMem: new Uint8Array(ctx.memory.buffer, payloadOffsetPtr, size32),
669-
}});
670662
ctx.storagePtr = payloadOffsetPtr;
671663
ctx.vals = [val];
672664
if (lowerFn) {{ lowerFn(ctx); }}
@@ -828,8 +820,22 @@ impl LowerIntrinsic {
828820
output.push_str(&format!(
829821
"
830822
function {lower_flat_option_fn}(lowerMetas) {{
831-
function {lower_flat_option_fn}Inner(ctx) {{
823+
return function {lower_flat_option_fn}Inner(ctx) {{
832824
{debug_log_fn}('[{lower_flat_option_fn}()] args', {{ ctx }});
825+
826+
const v = ctx.vals[0];
827+
if (v === null) {{
828+
ctx.vals[0] = {{ tag: 'none' }};
829+
}} else {{
830+
const isNotOptionObject = typeof v !== 'object'
831+
|| Object.keys(v).length !== 2
832+
|| !('tag' in v)
833+
|| !('value' in v);
834+
if (isNotOptionObject) {{
835+
ctx.vals[0] = {{ tag: v === null ? 'none' : 'some', val: v }};
836+
}}
837+
}}
838+
833839
{lower_variant_fn}(lowerMetas)(ctx);
834840
}}
835841
}}
@@ -846,6 +852,16 @@ impl LowerIntrinsic {
846852
function {lower_flat_result_fn}(lowerMetas) {{
847853
return function {lower_flat_result_fn}Inner(ctx) {{
848854
{debug_log_fn}('[{lower_flat_result_fn}()] args', {{ lowerMetas }});
855+
856+
const v = ctx.vals[0];
857+
const isNotResultObject = typeof v !== 'object'
858+
|| Object.keys(v).length !== 2
859+
|| !('ok' in v || 'err' in v)
860+
|| !('value' in v);
861+
if (isNotResultObject) {{
862+
ctx.vals[0] = {{ tag: 'ok', val: v }};
863+
}}
864+
849865
{lower_variant_fn}(lowerMetas)(ctx);
850866
}};
851867
}}

0 commit comments

Comments
 (0)