diff --git a/example/js/lib/api/FixedDecimalFormatterOptions.mjs b/example/js/lib/api/FixedDecimalFormatterOptions.mjs index 4ed7c60d4..16252cf16 100644 --- a/example/js/lib/api/FixedDecimalFormatterOptions.mjs +++ b/example/js/lib/api/FixedDecimalFormatterOptions.mjs @@ -24,13 +24,13 @@ export class FixedDecimalFormatterOptions { if ("groupingStrategy" in struct_obj) { this.#groupingStrategy = struct_obj.groupingStrategy; } else { - throw new Error("Missing required type groupingStrategy."); + throw new Error("Missing required field groupingStrategy."); } if ("someOtherConfig" in struct_obj) { this.#someOtherConfig = struct_obj.someOtherConfig; } else { - throw new Error("Missing required type someOtherConfig."); + throw new Error("Missing required field someOtherConfig."); } } @@ -65,11 +65,17 @@ export class FixedDecimalFormatterOptions { // and passes it down to individual fields containing the borrow. // This method does not attempt to handle any dependencies between lifetimes, the caller // should handle this when constructing edge arrays. - #fromFFI(ptr) { + static _fromFFI(internalConstructor, ptr) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("FixedDecimalFormatterOptions._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const groupingStrategyDeref = diplomatRuntime.enumDiscriminant(wasm, ptr); - this.#groupingStrategy = new FixedDecimalGroupingStrategy(diplomatRuntime.internalConstructor, groupingStrategyDeref); + structObj.groupingStrategy = new FixedDecimalGroupingStrategy(diplomatRuntime.internalConstructor, groupingStrategyDeref); const someOtherConfigDeref = (new Uint8Array(wasm.memory.buffer, ptr + 4, 1))[0] === 1; - this.#someOtherConfig = someOtherConfigDeref; + structObj.someOtherConfig = someOtherConfigDeref; + + return new FixedDecimalFormatterOptions(structObj); } static default_() { @@ -78,7 +84,7 @@ export class FixedDecimalFormatterOptions { const result = wasm.icu4x_FixedDecimalFormatterOptions_default_mv1(diplomatReceive.buffer); try { - return new FixedDecimalFormatterOptions(diplomatRuntime.internalConstructor, diplomatReceive.buffer); + return FixedDecimalFormatterOptions._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); } finally { diff --git a/feature_tests/js/api/BigStructWithStuff.mjs b/feature_tests/js/api/BigStructWithStuff.mjs index 7a474961a..a8bfe6fdd 100644 --- a/feature_tests/js/api/BigStructWithStuff.mjs +++ b/feature_tests/js/api/BigStructWithStuff.mjs @@ -51,31 +51,31 @@ export class BigStructWithStuff { if ("first" in struct_obj) { this.#first = struct_obj.first; } else { - throw new Error("Missing required type first."); + throw new Error("Missing required field first."); } if ("second" in struct_obj) { this.#second = struct_obj.second; } else { - throw new Error("Missing required type second."); + throw new Error("Missing required field second."); } if ("third" in struct_obj) { this.#third = struct_obj.third; } else { - throw new Error("Missing required type third."); + throw new Error("Missing required field third."); } if ("fourth" in struct_obj) { this.#fourth = struct_obj.fourth; } else { - throw new Error("Missing required type fourth."); + throw new Error("Missing required field fourth."); } if ("fifth" in struct_obj) { this.#fifth = struct_obj.fifth; } else { - throw new Error("Missing required type fifth."); + throw new Error("Missing required field fifth."); } } @@ -108,17 +108,23 @@ export class BigStructWithStuff { // and passes it down to individual fields containing the borrow. // This method does not attempt to handle any dependencies between lifetimes, the caller // should handle this when constructing edge arrays. - #fromFFI(ptr) { + static _fromFFI(internalConstructor, ptr) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("BigStructWithStuff._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const firstDeref = (new Uint8Array(wasm.memory.buffer, ptr, 1))[0]; - this.#first = firstDeref; + structObj.first = firstDeref; const secondDeref = (new Uint16Array(wasm.memory.buffer, ptr + 2, 1))[0]; - this.#second = secondDeref; + structObj.second = secondDeref; const thirdDeref = (new Uint16Array(wasm.memory.buffer, ptr + 4, 1))[0]; - this.#third = thirdDeref; + structObj.third = thirdDeref; const fourthDeref = ptr + 8; - this.#fourth = new ScalarPairWithPadding(diplomatRuntime.internalConstructor, fourthDeref); + structObj.fourth = ScalarPairWithPadding._fromFFI(diplomatRuntime.internalConstructor, fourthDeref); const fifthDeref = (new Uint8Array(wasm.memory.buffer, ptr + 16, 1))[0]; - this.#fifth = fifthDeref; + structObj.fifth = fifthDeref; + + return new BigStructWithStuff(structObj); } assertValue(extraVal) { diff --git a/feature_tests/js/api/BorrowedFields.mjs b/feature_tests/js/api/BorrowedFields.mjs index 4a7d14d61..24546aeab 100644 --- a/feature_tests/js/api/BorrowedFields.mjs +++ b/feature_tests/js/api/BorrowedFields.mjs @@ -32,19 +32,19 @@ export class BorrowedFields { if ("a" in struct_obj) { this.#a = struct_obj.a; } else { - throw new Error("Missing required type a."); + throw new Error("Missing required field a."); } if ("b" in struct_obj) { this.#b = struct_obj.b; } else { - throw new Error("Missing required type b."); + throw new Error("Missing required field b."); } if ("c" in struct_obj) { this.#c = struct_obj.c; } else { - throw new Error("Missing required type c."); + throw new Error("Missing required field c."); } } @@ -73,13 +73,19 @@ export class BorrowedFields { diplomatRuntime.CleanupArena.maybeCreateWith(functionCleanupArena, ...appendArrayMap['aAppendArray']).alloc(diplomatRuntime.DiplomatBuf.str8(wasm, this.#c)).writePtrLenToArrayBuffer(arrayBuffer, offset + 16); } - #fromFFI(ptr, aEdges) { + static _fromFFI(internalConstructor, ptr, aEdges) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("BorrowedFields._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const aDeref = ptr; - this.#a = new diplomatRuntime.DiplomatSliceStr(wasm, aDeref, "string16", aEdges); + structObj.a = new diplomatRuntime.DiplomatSliceStr(wasm, aDeref, "string16", aEdges); const bDeref = ptr + 8; - this.#b = new diplomatRuntime.DiplomatSliceStr(wasm, bDeref, "string8", aEdges); + structObj.b = new diplomatRuntime.DiplomatSliceStr(wasm, bDeref, "string8", aEdges); const cDeref = ptr + 16; - this.#c = new diplomatRuntime.DiplomatSliceStr(wasm, cDeref, "string8", aEdges); + structObj.c = new diplomatRuntime.DiplomatSliceStr(wasm, cDeref, "string8", aEdges); + + return new BorrowedFields(structObj); } // Return all fields corresponding to lifetime `'a` @@ -105,7 +111,7 @@ export class BorrowedFields { const result = wasm.BorrowedFields_from_bar_and_strings(diplomatReceive.buffer, bar.ffiValue, ...dstr16Slice.splat(), ...utf8StrSlice.splat()); try { - return new BorrowedFields(diplomatRuntime.internalConstructor, diplomatReceive.buffer, xEdges); + return BorrowedFields._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer, xEdges); } finally { diff --git a/feature_tests/js/api/BorrowedFieldsReturning.mjs b/feature_tests/js/api/BorrowedFieldsReturning.mjs index d66addffa..1bd1f27fc 100644 --- a/feature_tests/js/api/BorrowedFieldsReturning.mjs +++ b/feature_tests/js/api/BorrowedFieldsReturning.mjs @@ -15,7 +15,7 @@ export class BorrowedFieldsReturning { if ("bytes" in struct_obj) { this.#bytes = struct_obj.bytes; } else { - throw new Error("Missing required type bytes."); + throw new Error("Missing required field bytes."); } } @@ -42,9 +42,15 @@ export class BorrowedFieldsReturning { diplomatRuntime.CleanupArena.maybeCreateWith(functionCleanupArena, ...appendArrayMap['aAppendArray']).alloc(diplomatRuntime.DiplomatBuf.str8(wasm, this.#bytes)).writePtrLenToArrayBuffer(arrayBuffer, offset + 0); } - #fromFFI(ptr, aEdges) { + static _fromFFI(internalConstructor, ptr, aEdges) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("BorrowedFieldsReturning._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const bytesDeref = ptr; - this.#bytes = new diplomatRuntime.DiplomatSliceStr(wasm, bytesDeref, "string8", aEdges); + structObj.bytes = new diplomatRuntime.DiplomatSliceStr(wasm, bytesDeref, "string8", aEdges); + + return new BorrowedFieldsReturning(structObj); } // Return all fields corresponding to lifetime `'a` diff --git a/feature_tests/js/api/BorrowedFieldsWithBounds.mjs b/feature_tests/js/api/BorrowedFieldsWithBounds.mjs index a2f956875..44a4fa4cd 100644 --- a/feature_tests/js/api/BorrowedFieldsWithBounds.mjs +++ b/feature_tests/js/api/BorrowedFieldsWithBounds.mjs @@ -32,19 +32,19 @@ export class BorrowedFieldsWithBounds { if ("fieldA" in struct_obj) { this.#fieldA = struct_obj.fieldA; } else { - throw new Error("Missing required type fieldA."); + throw new Error("Missing required field fieldA."); } if ("fieldB" in struct_obj) { this.#fieldB = struct_obj.fieldB; } else { - throw new Error("Missing required type fieldB."); + throw new Error("Missing required field fieldB."); } if ("fieldC" in struct_obj) { this.#fieldC = struct_obj.fieldC; } else { - throw new Error("Missing required type fieldC."); + throw new Error("Missing required field fieldC."); } } @@ -75,13 +75,19 @@ export class BorrowedFieldsWithBounds { diplomatRuntime.CleanupArena.maybeCreateWith(functionCleanupArena, ...appendArrayMap['cAppendArray']).alloc(diplomatRuntime.DiplomatBuf.str8(wasm, this.#fieldC)).writePtrLenToArrayBuffer(arrayBuffer, offset + 16); } - #fromFFI(ptr, aEdges, bEdges, cEdges) { + static _fromFFI(internalConstructor, ptr, aEdges, bEdges, cEdges) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("BorrowedFieldsWithBounds._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const fieldADeref = ptr; - this.#fieldA = new diplomatRuntime.DiplomatSliceStr(wasm, fieldADeref, "string16", aEdges); + structObj.fieldA = new diplomatRuntime.DiplomatSliceStr(wasm, fieldADeref, "string16", aEdges); const fieldBDeref = ptr + 8; - this.#fieldB = new diplomatRuntime.DiplomatSliceStr(wasm, fieldBDeref, "string8", bEdges); + structObj.fieldB = new diplomatRuntime.DiplomatSliceStr(wasm, fieldBDeref, "string8", bEdges); const fieldCDeref = ptr + 16; - this.#fieldC = new diplomatRuntime.DiplomatSliceStr(wasm, fieldCDeref, "string8", cEdges); + structObj.fieldC = new diplomatRuntime.DiplomatSliceStr(wasm, fieldCDeref, "string8", cEdges); + + return new BorrowedFieldsWithBounds(structObj); } // Return all fields corresponding to lifetime `'a` @@ -131,7 +137,7 @@ export class BorrowedFieldsWithBounds { const result = wasm.BorrowedFieldsWithBounds_from_foo_and_strings(diplomatReceive.buffer, foo.ffiValue, ...dstr16XSlice.splat(), ...utf8StrZSlice.splat()); try { - return new BorrowedFieldsWithBounds(diplomatRuntime.internalConstructor, diplomatReceive.buffer, xEdges, yEdges, zEdges); + return BorrowedFieldsWithBounds._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer, xEdges, yEdges, zEdges); } finally { diff --git a/feature_tests/js/api/CyclicStructA.mjs b/feature_tests/js/api/CyclicStructA.mjs index 161b88a43..0543e1dba 100644 --- a/feature_tests/js/api/CyclicStructA.mjs +++ b/feature_tests/js/api/CyclicStructA.mjs @@ -16,7 +16,7 @@ export class CyclicStructA { if ("a" in struct_obj) { this.#a = struct_obj.a; } else { - throw new Error("Missing required type a."); + throw new Error("Missing required field a."); } } @@ -45,9 +45,15 @@ export class CyclicStructA { // and passes it down to individual fields containing the borrow. // This method does not attempt to handle any dependencies between lifetimes, the caller // should handle this when constructing edge arrays. - #fromFFI(ptr) { + static _fromFFI(internalConstructor, ptr) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("CyclicStructA._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const aDeref = ptr; - this.#a = new CyclicStructB(diplomatRuntime.internalConstructor, aDeref); + structObj.a = CyclicStructB._fromFFI(diplomatRuntime.internalConstructor, aDeref); + + return new CyclicStructA(structObj); } static getB() { @@ -56,7 +62,7 @@ export class CyclicStructA { const result = wasm.CyclicStructA_get_b(diplomatReceive.buffer); try { - return new CyclicStructB(diplomatRuntime.internalConstructor, diplomatReceive.buffer); + return CyclicStructB._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); } finally { diff --git a/feature_tests/js/api/CyclicStructB.mjs b/feature_tests/js/api/CyclicStructB.mjs index c3e425741..feb577e58 100644 --- a/feature_tests/js/api/CyclicStructB.mjs +++ b/feature_tests/js/api/CyclicStructB.mjs @@ -16,7 +16,7 @@ export class CyclicStructB { if ("field" in struct_obj) { this.#field = struct_obj.field; } else { - throw new Error("Missing required type field."); + throw new Error("Missing required field field."); } } @@ -45,9 +45,15 @@ export class CyclicStructB { // and passes it down to individual fields containing the borrow. // This method does not attempt to handle any dependencies between lifetimes, the caller // should handle this when constructing edge arrays. - #fromFFI(ptr) { + static _fromFFI(internalConstructor, ptr) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("CyclicStructB._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const fieldDeref = (new Uint8Array(wasm.memory.buffer, ptr, 1))[0]; - this.#field = fieldDeref; + structObj.field = fieldDeref; + + return new CyclicStructB(structObj); } static getA() { @@ -56,7 +62,7 @@ export class CyclicStructB { const result = wasm.CyclicStructB_get_a(diplomatReceive.buffer); try { - return new CyclicStructA(diplomatRuntime.internalConstructor, diplomatReceive.buffer); + return CyclicStructA._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); } finally { diff --git a/feature_tests/js/api/ErrorStruct.mjs b/feature_tests/js/api/ErrorStruct.mjs index 10196f475..49de5f33b 100644 --- a/feature_tests/js/api/ErrorStruct.mjs +++ b/feature_tests/js/api/ErrorStruct.mjs @@ -23,13 +23,13 @@ export class ErrorStruct { if ("i" in struct_obj) { this.#i = struct_obj.i; } else { - throw new Error("Missing required type i."); + throw new Error("Missing required field i."); } if ("j" in struct_obj) { this.#j = struct_obj.j; } else { - throw new Error("Missing required type j."); + throw new Error("Missing required field j."); } } @@ -59,10 +59,16 @@ export class ErrorStruct { // and passes it down to individual fields containing the borrow. // This method does not attempt to handle any dependencies between lifetimes, the caller // should handle this when constructing edge arrays. - #fromFFI(ptr) { + static _fromFFI(internalConstructor, ptr) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("ErrorStruct._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const iDeref = (new Int32Array(wasm.memory.buffer, ptr, 1))[0]; - this.#i = iDeref; + structObj.i = iDeref; const jDeref = (new Int32Array(wasm.memory.buffer, ptr + 4, 1))[0]; - this.#j = jDeref; + structObj.j = jDeref; + + return new ErrorStruct(structObj); } } \ No newline at end of file diff --git a/feature_tests/js/api/Foo.mjs b/feature_tests/js/api/Foo.mjs index 78f75ffac..a8be17a9b 100644 --- a/feature_tests/js/api/Foo.mjs +++ b/feature_tests/js/api/Foo.mjs @@ -84,7 +84,7 @@ export class Foo { const result = wasm.Foo_as_returning(diplomatReceive.buffer, this.ffiValue); try { - return new BorrowedFieldsReturning(diplomatRuntime.internalConstructor, diplomatReceive.buffer, aEdges); + return BorrowedFieldsReturning._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer, aEdges); } finally { diff --git a/feature_tests/js/api/ImportedStruct.mjs b/feature_tests/js/api/ImportedStruct.mjs index a9b6cfa99..da38048cc 100644 --- a/feature_tests/js/api/ImportedStruct.mjs +++ b/feature_tests/js/api/ImportedStruct.mjs @@ -24,13 +24,13 @@ export class ImportedStruct { if ("foo" in struct_obj) { this.#foo = struct_obj.foo; } else { - throw new Error("Missing required type foo."); + throw new Error("Missing required field foo."); } if ("count" in struct_obj) { this.#count = struct_obj.count; } else { - throw new Error("Missing required type count."); + throw new Error("Missing required field count."); } } @@ -65,10 +65,16 @@ export class ImportedStruct { // and passes it down to individual fields containing the borrow. // This method does not attempt to handle any dependencies between lifetimes, the caller // should handle this when constructing edge arrays. - #fromFFI(ptr) { + static _fromFFI(internalConstructor, ptr) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("ImportedStruct._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const fooDeref = diplomatRuntime.enumDiscriminant(wasm, ptr); - this.#foo = new UnimportedEnum(diplomatRuntime.internalConstructor, fooDeref); + structObj.foo = new UnimportedEnum(diplomatRuntime.internalConstructor, fooDeref); const countDeref = (new Uint8Array(wasm.memory.buffer, ptr + 4, 1))[0]; - this.#count = countDeref; + structObj.count = countDeref; + + return new ImportedStruct(structObj); } } \ No newline at end of file diff --git a/feature_tests/js/api/MyStruct.mjs b/feature_tests/js/api/MyStruct.mjs index 892c70e09..07edfd3e7 100644 --- a/feature_tests/js/api/MyStruct.mjs +++ b/feature_tests/js/api/MyStruct.mjs @@ -65,43 +65,43 @@ export class MyStruct { if ("a" in struct_obj) { this.#a = struct_obj.a; } else { - throw new Error("Missing required type a."); + throw new Error("Missing required field a."); } if ("b" in struct_obj) { this.#b = struct_obj.b; } else { - throw new Error("Missing required type b."); + throw new Error("Missing required field b."); } if ("c" in struct_obj) { this.#c = struct_obj.c; } else { - throw new Error("Missing required type c."); + throw new Error("Missing required field c."); } if ("d" in struct_obj) { this.#d = struct_obj.d; } else { - throw new Error("Missing required type d."); + throw new Error("Missing required field d."); } if ("e" in struct_obj) { this.#e = struct_obj.e; } else { - throw new Error("Missing required type e."); + throw new Error("Missing required field e."); } if ("f" in struct_obj) { this.#f = struct_obj.f; } else { - throw new Error("Missing required type f."); + throw new Error("Missing required field f."); } if ("g" in struct_obj) { this.#g = struct_obj.g; } else { - throw new Error("Missing required type g."); + throw new Error("Missing required field g."); } } @@ -136,21 +136,27 @@ export class MyStruct { // and passes it down to individual fields containing the borrow. // This method does not attempt to handle any dependencies between lifetimes, the caller // should handle this when constructing edge arrays. - #fromFFI(ptr) { + static _fromFFI(internalConstructor, ptr) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("MyStruct._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const aDeref = (new Uint8Array(wasm.memory.buffer, ptr, 1))[0]; - this.#a = aDeref; + structObj.a = aDeref; const bDeref = (new Uint8Array(wasm.memory.buffer, ptr + 1, 1))[0] === 1; - this.#b = bDeref; + structObj.b = bDeref; const cDeref = (new Uint8Array(wasm.memory.buffer, ptr + 2, 1))[0]; - this.#c = cDeref; + structObj.c = cDeref; const dDeref = (new BigUint64Array(wasm.memory.buffer, ptr + 8, 1))[0]; - this.#d = dDeref; + structObj.d = dDeref; const eDeref = (new Int32Array(wasm.memory.buffer, ptr + 16, 1))[0]; - this.#e = eDeref; + structObj.e = eDeref; const fDeref = (new Uint32Array(wasm.memory.buffer, ptr + 20, 1))[0]; - this.#f = fDeref; + structObj.f = fDeref; const gDeref = diplomatRuntime.enumDiscriminant(wasm, ptr + 24); - this.#g = new MyEnum(diplomatRuntime.internalConstructor, gDeref); + structObj.g = new MyEnum(diplomatRuntime.internalConstructor, gDeref); + + return new MyStruct(structObj); } static new_() { @@ -159,7 +165,7 @@ export class MyStruct { const result = wasm.MyStruct_new(diplomatReceive.buffer); try { - return new MyStruct(diplomatRuntime.internalConstructor, diplomatReceive.buffer); + return MyStruct._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); } finally { @@ -186,7 +192,7 @@ export class MyStruct { try { if (result !== 1) { - const cause = new MyZst(diplomatRuntime.internalConstructor); + const cause = MyZst._fromFFI(diplomatRuntime.internalConstructor); throw new globalThis.Error('MyZst', { cause }); } @@ -200,7 +206,7 @@ export class MyStruct { try { if (result !== 1) { - const cause = new MyZst(diplomatRuntime.internalConstructor); + const cause = MyZst._fromFFI(diplomatRuntime.internalConstructor); throw new globalThis.Error('MyZst', { cause }); } diff --git a/feature_tests/js/api/NestedBorrowedFields.mjs b/feature_tests/js/api/NestedBorrowedFields.mjs index 82694ad5d..a00df4f4e 100644 --- a/feature_tests/js/api/NestedBorrowedFields.mjs +++ b/feature_tests/js/api/NestedBorrowedFields.mjs @@ -35,19 +35,19 @@ export class NestedBorrowedFields { if ("fields" in struct_obj) { this.#fields = struct_obj.fields; } else { - throw new Error("Missing required type fields."); + throw new Error("Missing required field fields."); } if ("bounds" in struct_obj) { this.#bounds = struct_obj.bounds; } else { - throw new Error("Missing required type bounds."); + throw new Error("Missing required field bounds."); } if ("bounds2" in struct_obj) { this.#bounds2 = struct_obj.bounds2; } else { - throw new Error("Missing required type bounds2."); + throw new Error("Missing required field bounds2."); } } @@ -78,13 +78,19 @@ export class NestedBorrowedFields { this.#bounds2._writeToArrayBuffer(arrayBuffer, offset + 48, functionCleanupArena, {aAppendArray: [...zAppendArray],bAppendArray: [...zAppendArray],cAppendArray: [...zAppendArray],}); } - #fromFFI(ptr, xEdges, yEdges, zEdges) { + static _fromFFI(internalConstructor, ptr, xEdges, yEdges, zEdges) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("NestedBorrowedFields._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const fieldsDeref = ptr; - this.#fields = new BorrowedFields(diplomatRuntime.internalConstructor, fieldsDeref, xEdges); + structObj.fields = BorrowedFields._fromFFI(diplomatRuntime.internalConstructor, fieldsDeref, xEdges); const boundsDeref = ptr + 24; - this.#bounds = new BorrowedFieldsWithBounds(diplomatRuntime.internalConstructor, boundsDeref, xEdges, yEdges, yEdges); + structObj.bounds = BorrowedFieldsWithBounds._fromFFI(diplomatRuntime.internalConstructor, boundsDeref, xEdges, yEdges, yEdges); const bounds2Deref = ptr + 48; - this.#bounds2 = new BorrowedFieldsWithBounds(diplomatRuntime.internalConstructor, bounds2Deref, zEdges, zEdges, zEdges); + structObj.bounds2 = BorrowedFieldsWithBounds._fromFFI(diplomatRuntime.internalConstructor, bounds2Deref, zEdges, zEdges, zEdges); + + return new NestedBorrowedFields(structObj); } // Return all fields corresponding to lifetime `'x` @@ -138,7 +144,7 @@ export class NestedBorrowedFields { const result = wasm.NestedBorrowedFields_from_bar_and_foo_and_strings(diplomatReceive.buffer, bar.ffiValue, foo.ffiValue, ...dstr16XSlice.splat(), ...dstr16ZSlice.splat(), ...utf8StrYSlice.splat(), ...utf8StrZSlice.splat()); try { - return new NestedBorrowedFields(diplomatRuntime.internalConstructor, diplomatReceive.buffer, xEdges, yEdges, zEdges); + return NestedBorrowedFields._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer, xEdges, yEdges, zEdges); } finally { diff --git a/feature_tests/js/api/Opaque.mjs b/feature_tests/js/api/Opaque.mjs index 708f5bc1d..663118c27 100644 --- a/feature_tests/js/api/Opaque.mjs +++ b/feature_tests/js/api/Opaque.mjs @@ -117,7 +117,7 @@ export class Opaque { const result = wasm.Opaque_returns_imported(diplomatReceive.buffer); try { - return new ImportedStruct(diplomatRuntime.internalConstructor, diplomatReceive.buffer); + return ImportedStruct._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); } finally { diff --git a/feature_tests/js/api/OptionInputStruct.mjs b/feature_tests/js/api/OptionInputStruct.mjs index 9bab8234a..94ad38110 100644 --- a/feature_tests/js/api/OptionInputStruct.mjs +++ b/feature_tests/js/api/OptionInputStruct.mjs @@ -32,19 +32,19 @@ export class OptionInputStruct { if ("a" in struct_obj) { this.#a = struct_obj.a; } else { - throw new Error("Missing required type a."); + throw new Error("Missing required field a."); } if ("b" in struct_obj) { this.#b = struct_obj.b; } else { - throw new Error("Missing required type b."); + throw new Error("Missing required field b."); } if ("c" in struct_obj) { this.#c = struct_obj.c; } else { - throw new Error("Missing required type c."); + throw new Error("Missing required field c."); } } @@ -75,12 +75,18 @@ export class OptionInputStruct { // and passes it down to individual fields containing the borrow. // This method does not attempt to handle any dependencies between lifetimes, the caller // should handle this when constructing edge arrays. - #fromFFI(ptr) { + static _fromFFI(internalConstructor, ptr) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("OptionInputStruct._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const aDeref = ptr; - this.#a = diplomatRuntime.readOption(wasm, aDeref, 1, (wasm, offset) => { const deref = (new Uint8Array(wasm.memory.buffer, offset, 1))[0]; return deref }); + structObj.a = diplomatRuntime.readOption(wasm, aDeref, 1, (wasm, offset) => { const deref = (new Uint8Array(wasm.memory.buffer, offset, 1))[0]; return deref }); const bDeref = ptr + 4; - this.#b = diplomatRuntime.readOption(wasm, bDeref, 4, (wasm, offset) => { const deref = (new Uint32Array(wasm.memory.buffer, offset, 1))[0]; return deref }); + structObj.b = diplomatRuntime.readOption(wasm, bDeref, 4, (wasm, offset) => { const deref = (new Uint32Array(wasm.memory.buffer, offset, 1))[0]; return deref }); const cDeref = ptr + 12; - this.#c = diplomatRuntime.readOption(wasm, cDeref, 4, (wasm, offset) => { const deref = diplomatRuntime.enumDiscriminant(wasm, offset); return new OptionEnum(diplomatRuntime.internalConstructor, deref) }); + structObj.c = diplomatRuntime.readOption(wasm, cDeref, 4, (wasm, offset) => { const deref = diplomatRuntime.enumDiscriminant(wasm, offset); return new OptionEnum(diplomatRuntime.internalConstructor, deref) }); + + return new OptionInputStruct(structObj); } } \ No newline at end of file diff --git a/feature_tests/js/api/OptionOpaque.mjs b/feature_tests/js/api/OptionOpaque.mjs index d099db5c4..f6065439f 100644 --- a/feature_tests/js/api/OptionOpaque.mjs +++ b/feature_tests/js/api/OptionOpaque.mjs @@ -65,7 +65,7 @@ export class OptionOpaque { if (!diplomatReceive.resultFlag) { return null; } - return new OptionStruct(diplomatRuntime.internalConstructor, diplomatReceive.buffer); + return OptionStruct._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); } finally { @@ -147,7 +147,7 @@ export class OptionOpaque { const result = wasm.OptionOpaque_new_struct(diplomatReceive.buffer); try { - return new OptionStruct(diplomatRuntime.internalConstructor, diplomatReceive.buffer); + return OptionStruct._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); } finally { @@ -161,7 +161,7 @@ export class OptionOpaque { const result = wasm.OptionOpaque_new_struct_nones(diplomatReceive.buffer); try { - return new OptionStruct(diplomatRuntime.internalConstructor, diplomatReceive.buffer); + return OptionStruct._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); } finally { @@ -231,7 +231,7 @@ export class OptionOpaque { if (!diplomatReceive.resultFlag) { return null; } - return new OptionInputStruct(diplomatRuntime.internalConstructor, diplomatReceive.buffer); + return OptionInputStruct._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); } finally { @@ -247,7 +247,7 @@ export class OptionOpaque { const result = wasm.OptionOpaque_returns_option_input_struct(diplomatReceive.buffer); try { - return new OptionInputStruct(diplomatRuntime.internalConstructor, diplomatReceive.buffer); + return OptionInputStruct._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); } finally { diff --git a/feature_tests/js/api/OptionStruct.mjs b/feature_tests/js/api/OptionStruct.mjs index d04b6cae1..5ffbca10e 100644 --- a/feature_tests/js/api/OptionStruct.mjs +++ b/feature_tests/js/api/OptionStruct.mjs @@ -36,25 +36,25 @@ export class OptionStruct { if ("a" in struct_obj) { this.#a = struct_obj.a; } else { - throw new Error("Missing required type a."); + throw new Error("Missing required field a."); } if ("b" in struct_obj) { this.#b = struct_obj.b; } else { - throw new Error("Missing required type b."); + throw new Error("Missing required field b."); } if ("c" in struct_obj) { this.#c = struct_obj.c; } else { - throw new Error("Missing required type c."); + throw new Error("Missing required field c."); } if ("d" in struct_obj) { this.#d = struct_obj.d; } else { - throw new Error("Missing required type d."); + throw new Error("Missing required field d."); } } @@ -86,14 +86,20 @@ export class OptionStruct { // and passes it down to individual fields containing the borrow. // This method does not attempt to handle any dependencies between lifetimes, the caller // should handle this when constructing edge arrays. - #fromFFI(ptr) { + static _fromFFI(internalConstructor, ptr) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("OptionStruct._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const aDeref = diplomatRuntime.ptrRead(wasm, ptr); - this.#a = aDeref === 0 ? null : new OptionOpaque(diplomatRuntime.internalConstructor, aDeref, []); + structObj.a = aDeref === 0 ? null : new OptionOpaque(diplomatRuntime.internalConstructor, aDeref, []); const bDeref = diplomatRuntime.ptrRead(wasm, ptr + 4); - this.#b = bDeref === 0 ? null : new OptionOpaqueChar(diplomatRuntime.internalConstructor, bDeref, []); + structObj.b = bDeref === 0 ? null : new OptionOpaqueChar(diplomatRuntime.internalConstructor, bDeref, []); const cDeref = (new Uint32Array(wasm.memory.buffer, ptr + 8, 1))[0]; - this.#c = cDeref; + structObj.c = cDeref; const dDeref = diplomatRuntime.ptrRead(wasm, ptr + 12); - this.#d = dDeref === 0 ? null : new OptionOpaque(diplomatRuntime.internalConstructor, dDeref, []); + structObj.d = dDeref === 0 ? null : new OptionOpaque(diplomatRuntime.internalConstructor, dDeref, []); + + return new OptionStruct(structObj); } } \ No newline at end of file diff --git a/feature_tests/js/api/ResultOpaque.mjs b/feature_tests/js/api/ResultOpaque.mjs index 0d149ac28..0753c6c2a 100644 --- a/feature_tests/js/api/ResultOpaque.mjs +++ b/feature_tests/js/api/ResultOpaque.mjs @@ -113,7 +113,7 @@ export class ResultOpaque { try { if (!diplomatReceive.resultFlag) { - const cause = new ErrorStruct(diplomatRuntime.internalConstructor, diplomatReceive.buffer); + const cause = ErrorStruct._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); throw new globalThis.Error('ErrorStruct: ' + cause.toString(), { cause }); } return new ResultOpaque(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); diff --git a/feature_tests/js/api/ScalarPairWithPadding.mjs b/feature_tests/js/api/ScalarPairWithPadding.mjs index 6d45eb38a..c8c66237d 100644 --- a/feature_tests/js/api/ScalarPairWithPadding.mjs +++ b/feature_tests/js/api/ScalarPairWithPadding.mjs @@ -26,13 +26,13 @@ export class ScalarPairWithPadding { if ("first" in struct_obj) { this.#first = struct_obj.first; } else { - throw new Error("Missing required type first."); + throw new Error("Missing required field first."); } if ("second" in struct_obj) { this.#second = struct_obj.second; } else { - throw new Error("Missing required type second."); + throw new Error("Missing required field second."); } } @@ -67,11 +67,17 @@ export class ScalarPairWithPadding { // and passes it down to individual fields containing the borrow. // This method does not attempt to handle any dependencies between lifetimes, the caller // should handle this when constructing edge arrays. - #fromFFI(ptr) { + static _fromFFI(internalConstructor, ptr) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("ScalarPairWithPadding._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; const firstDeref = (new Uint8Array(wasm.memory.buffer, ptr, 1))[0]; - this.#first = firstDeref; + structObj.first = firstDeref; const secondDeref = (new Uint32Array(wasm.memory.buffer, ptr + 4, 1))[0]; - this.#second = secondDeref; + structObj.second = secondDeref; + + return new ScalarPairWithPadding(structObj); } assertValue() {