Skip to content

Commit 2aa4087

Browse files
fix(bindgen): option, result, flag lowers
1 parent 508e707 commit 2aa4087

1 file changed

Lines changed: 35 additions & 23 deletions

File tree

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

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

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ impl LowerIntrinsic {
583583
let lower_flat_string_utf8_fn = self.name();
584584
let utf8_encode_fn = Intrinsic::String(StringIntrinsic::Utf8Encode).name();
585585

586-
output.push_str(&format!("
586+
output.push_str(&format!(r#"
587587
function {lower_flat_string_utf8_fn}(ctx) {{
588588
{debug_log_fn}('[{lower_flat_string_utf8_fn}()] args', ctx);
589589
if (!ctx.realloc) {{ throw new Error('missing realloc during flat string lower'); }}
@@ -597,7 +597,7 @@ impl LowerIntrinsic {
597597
598598
ctx.storagePtr += len;
599599
}}
600-
"));
600+
"#));
601601
}
602602

603603
Self::LowerFlatRecord => {
@@ -663,7 +663,7 @@ impl LowerIntrinsic {
663663
ctx.vals = [val];
664664
if (lowerFn) {{ lowerFn(ctx); }}
665665
666-
const bytesWritten = ctx.storagePtr - payloadOffsetPtr;
666+
let bytesWritten = ctx.storagePtr - payloadOffsetPtr;
667667
668668
const rem = ctx.storagePtr % align32;
669669
if (rem !== 0) {{
@@ -760,20 +760,21 @@ impl LowerIntrinsic {
760760
let debug_log_fn = Intrinsic::DebugLog.name();
761761
let lower_flat_flags_fn = self.name();
762762

763-
output.push_str(&format!("
763+
output.push_str(&format!(r#"
764764
function {lower_flat_flags_fn}(meta) {{
765765
const {{ names, size32, align32, intSizeBytes }} = meta;
766-
const nameLookup = Object.fromEntries(
767-
names.entries().map(([idx, n]) => [n, idx])
768-
);
769766
770767
return function {lower_flat_flags_fn}Inner(ctx) {{
771768
{debug_log_fn}('[{lower_flat_flags_fn}()] args', {{ ctx }});
772769
if (ctx.vals.length !== 1) {{ throw new Error('unexpected number of vals'); }}
773770
774-
const {{ tag }} = ctx.vals[0];
775-
const nameIdx = nameLookup[tag];
776-
const flagValue = 1 << nameIdx;
771+
let flagObj = ctx.vals[0];
772+
let flagValue = 0;
773+
for (const [idx, name] of names.entries()) {{
774+
if (flagObj[name] === true) {{
775+
flagValue |= 1 << idx;
776+
}}
777+
}}
777778
778779
const rem = ctx.storagePtr % align32;
779780
if (rem !== 0) {{ ctx.storagePtr += (align32 - rem); }}
@@ -792,7 +793,7 @@ impl LowerIntrinsic {
792793
ctx.storagePtr += intSizeBytes;
793794
}}
794795
}}
795-
"));
796+
"#));
796797
}
797798

798799
Self::LowerFlatEnum => {
@@ -801,14 +802,23 @@ impl LowerIntrinsic {
801802
let lower_variant_fn = Self::LowerFlatVariant.name();
802803

803804
output.push_str(&format!(
804-
"
805+
r#"
805806
function {lower_flat_enum_fn}(lowerMetas) {{
806807
return function {lower_flat_enum_fn}Inner(ctx) {{
807808
{debug_log_fn}('[{lower_flat_enum_fn}()] args', {{ ctx }});
809+
810+
const v = ctx.vals[0];
811+
const isNotEnumObject = typeof v !== 'object'
812+
|| Object.keys(v).length !== 2
813+
|| !('tag' in v);
814+
if (isNotEnumObject) {{
815+
ctx.vals[0] = {{ tag: v }};
816+
}}
817+
808818
{lower_variant_fn}(lowerMetas)(ctx);
809819
}}
810820
}}
811-
"
821+
"#
812822
));
813823
}
814824

@@ -830,9 +840,10 @@ impl LowerIntrinsic {
830840
const isNotOptionObject = typeof v !== 'object'
831841
|| Object.keys(v).length !== 2
832842
|| !('tag' in v)
833-
|| !('value' in v);
843+
|| !(v.tag === 'some' || v.tag === 'none')
844+
|| !('val' in v);
834845
if (isNotOptionObject) {{
835-
ctx.vals[0] = {{ tag: v === null ? 'none' : 'some', val: v }};
846+
ctx.vals[0] = {{ tag: 'some', val: v }};
836847
}}
837848
}}
838849
@@ -853,14 +864,15 @@ impl LowerIntrinsic {
853864
return function {lower_flat_result_fn}Inner(ctx) {{
854865
{debug_log_fn}('[{lower_flat_result_fn}()] args', {{ lowerMetas }});
855866
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-
}}
867+
const v = ctx.vals[0];
868+
const isNotResultObject = typeof v !== 'object'
869+
|| Object.keys(v).length !== 2
870+
|| !('tag' in v)
871+
|| !('ok' === v.tag || 'err' === v.tag)
872+
|| !('val' in v);
873+
if (isNotResultObject) {{
874+
ctx.vals[0] = {{ tag: 'ok', val: v }};
875+
}}
864876
865877
{lower_variant_fn}(lowerMetas)(ctx);
866878
}};

0 commit comments

Comments
 (0)