From 32538d8e7ff4d70761436e931b5e7cc7ee3af4cf Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 12:47:08 -0800 Subject: [PATCH 01/19] Starting on new template for object constructor --- tool/templates/js/struct.js.jinja | 43 ++++++++++++++++++------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/tool/templates/js/struct.js.jinja b/tool/templates/js/struct.js.jinja index cd162d5fb..ce7183f67 100644 --- a/tool/templates/js/struct.js.jinja +++ b/tool/templates/js/struct.js.jinja @@ -2,6 +2,15 @@ /** {{docs}} */ {% endif -%} + +{% if typescript %} +type {{type_name}}_Obj = { + {% for field in fields %} + {field.field_name}: {{field.js_type_name}}; + {% endfor %} +}; +{% endif %} + export class {{type_name}} { {%- for field in fields %} @@ -21,29 +30,27 @@ export class {{type_name}} { {%- if !(typescript && is_out) %} constructor( - {%- if typescript -%} - {%- for field in fields -%} - {{field.field_name}}: {{field.js_type_name}}{% if !loop.last %}, {% endif %} - {%- endfor -%} + {%- if !is_out -%} struct_obj {%- if typescript %} : {{type_name}}_Obj {%- endif -%}{%- endif -%} + {%- if !typescript %}, internalConstructor{% endif %}) {%- if typescript %};{% else %} { + + {%- if is_out %} + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("{{type_name}} is an out struct and can only be created internally."); + } {%- endif -%} - ) {%- if typescript %};{% else %} { + + {%- for field in fields %} + if ("{{field.field_name}}" in struct_obj) { + this.#{{ field.field_name }} = struct_obj.{{field.field_name}}; + } else { + throw new Error("Missing required type {{field.field_name}}."); + } + {%- endfor -%} + {% if !fields.is_empty() -%} if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); } - {%- if !is_out %} else { - {% for field in fields %} - this.#{{ field.field_name }} = arguments[{{loop.index0}}]; - {%- endfor %} - } {%- else %} else { - console.error("{{type_name}} is an out struct and can only be created internally."); - } - {%- endif -%} - {%- else if is_out -%} - if (!(arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor)) { - console.error("{{type_name}} is an out struct and can only be created internally."); - } - {%- endif %} } {%- endif -%} {%- endif %} From f1314a5eb6c57c2d9e02d6abfad66bbf72be9655 Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 12:57:35 -0800 Subject: [PATCH 02/19] Updating template --- tool/templates/js/struct.js.jinja | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tool/templates/js/struct.js.jinja b/tool/templates/js/struct.js.jinja index ce7183f67..cb24ee75a 100644 --- a/tool/templates/js/struct.js.jinja +++ b/tool/templates/js/struct.js.jinja @@ -3,13 +3,14 @@ */ {% endif -%} -{% if typescript %} +{%- if typescript -%} type {{type_name}}_Obj = { - {% for field in fields %} - {field.field_name}: {{field.js_type_name}}; - {% endfor %} + {%- for field in fields %} + {{field.field_name}}: {{field.js_type_name}}; + {%- endfor %} }; -{% endif %} + +{% endif -%} export class {{type_name}} { {%- for field in fields %} @@ -31,7 +32,8 @@ export class {{type_name}} { {%- if !(typescript && is_out) %} constructor( {%- if !is_out -%} struct_obj {%- if typescript %} : {{type_name}}_Obj {%- endif -%}{%- endif -%} - {%- if !typescript %}, internalConstructor{% endif %}) {%- if typescript %};{% else %} { + {%- if is_out && !typescript %}, internalConstructor{% endif -%} + ) {%- if typescript %};{% else %} { {%- if is_out %} if (internalConstructor !== diplomatRuntime.internalConstructor) { @@ -45,12 +47,7 @@ export class {{type_name}} { } else { throw new Error("Missing required type {{field.field_name}}."); } - {%- endfor -%} - - {% if !fields.is_empty() -%} - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); - } + {%~ endfor %} } {%- endif -%} {%- endif %} From fddcc036af8bc05663694882f1e9dbbeba00f751 Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 12:57:52 -0800 Subject: [PATCH 03/19] Generation --- .../lib/api/FixedDecimalFormatterOptions.d.ts | 7 ++- .../lib/api/FixedDecimalFormatterOptions.mjs | 17 +++--- feature_tests/js/api/BigStructWithStuff.d.ts | 10 +++- feature_tests/js/api/BigStructWithStuff.mjs | 38 ++++++++++---- feature_tests/js/api/BorrowedFields.d.ts | 8 ++- feature_tests/js/api/BorrowedFields.mjs | 24 ++++++--- .../js/api/BorrowedFieldsReturning.d.ts | 6 ++- .../js/api/BorrowedFieldsReturning.mjs | 10 ++-- .../js/api/BorrowedFieldsWithBounds.d.ts | 8 ++- .../js/api/BorrowedFieldsWithBounds.mjs | 24 ++++++--- feature_tests/js/api/CyclicStructA.d.ts | 6 ++- feature_tests/js/api/CyclicStructA.mjs | 10 ++-- feature_tests/js/api/CyclicStructB.d.ts | 6 ++- feature_tests/js/api/CyclicStructB.mjs | 10 ++-- feature_tests/js/api/ErrorStruct.d.ts | 7 ++- feature_tests/js/api/ErrorStruct.mjs | 17 +++--- feature_tests/js/api/ImportedStruct.d.ts | 7 ++- feature_tests/js/api/ImportedStruct.mjs | 17 +++--- feature_tests/js/api/MyStruct.d.ts | 12 ++++- feature_tests/js/api/MyStruct.mjs | 52 +++++++++++++++---- feature_tests/js/api/MyZst.d.ts | 5 +- feature_tests/js/api/MyZst.mjs | 3 +- .../js/api/NestedBorrowedFields.d.ts | 8 ++- feature_tests/js/api/NestedBorrowedFields.mjs | 24 ++++++--- feature_tests/js/api/OptionInputStruct.d.ts | 8 ++- feature_tests/js/api/OptionInputStruct.mjs | 24 ++++++--- feature_tests/js/api/OptionStruct.d.ts | 7 +++ feature_tests/js/api/OptionStruct.mjs | 30 +++++++++-- .../js/api/ScalarPairWithPadding.d.ts | 7 ++- .../js/api/ScalarPairWithPadding.mjs | 17 +++--- 30 files changed, 322 insertions(+), 107 deletions(-) diff --git a/example/js/lib/api/FixedDecimalFormatterOptions.d.ts b/example/js/lib/api/FixedDecimalFormatterOptions.d.ts index 2bec1cb16..f5fbdf2a3 100644 --- a/example/js/lib/api/FixedDecimalFormatterOptions.d.ts +++ b/example/js/lib/api/FixedDecimalFormatterOptions.d.ts @@ -2,6 +2,11 @@ import type { FixedDecimalGroupingStrategy } from "./FixedDecimalGroupingStrategy" import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type FixedDecimalFormatterOptions_Obj = { + groupingStrategy: FixedDecimalGroupingStrategy; + someOtherConfig: boolean; +}; + export class FixedDecimalFormatterOptions { get groupingStrategy() : FixedDecimalGroupingStrategy; @@ -9,7 +14,7 @@ export class FixedDecimalFormatterOptions { get someOtherConfig() : boolean; set someOtherConfig(value: boolean); - constructor(groupingStrategy: FixedDecimalGroupingStrategy, someOtherConfig: boolean); + constructor(struct_obj : FixedDecimalFormatterOptions_Obj); static default_(): FixedDecimalFormatterOptions; } \ No newline at end of file diff --git a/example/js/lib/api/FixedDecimalFormatterOptions.mjs b/example/js/lib/api/FixedDecimalFormatterOptions.mjs index 995f67527..4ed7c60d4 100644 --- a/example/js/lib/api/FixedDecimalFormatterOptions.mjs +++ b/example/js/lib/api/FixedDecimalFormatterOptions.mjs @@ -20,14 +20,19 @@ export class FixedDecimalFormatterOptions { set someOtherConfig(value) { this.#someOtherConfig = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("groupingStrategy" in struct_obj) { + this.#groupingStrategy = struct_obj.groupingStrategy; } else { - - this.#groupingStrategy = arguments[0]; - this.#someOtherConfig = arguments[1]; + throw new Error("Missing required type groupingStrategy."); } + + if ("someOtherConfig" in struct_obj) { + this.#someOtherConfig = struct_obj.someOtherConfig; + } else { + throw new Error("Missing required type someOtherConfig."); + } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/BigStructWithStuff.d.ts b/feature_tests/js/api/BigStructWithStuff.d.ts index e66cc2012..08d373a63 100644 --- a/feature_tests/js/api/BigStructWithStuff.d.ts +++ b/feature_tests/js/api/BigStructWithStuff.d.ts @@ -5,6 +5,14 @@ import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; /** Testing JS-specific layout/padding behavior */ +type BigStructWithStuff_Obj = { + first: number; + second: number; + third: number; + fourth: ScalarPairWithPadding; + fifth: number; +}; + export class BigStructWithStuff { get first() : number; @@ -21,7 +29,7 @@ export class BigStructWithStuff { get fifth() : number; set fifth(value: number); - constructor(first: number, second: number, third: number, fourth: ScalarPairWithPadding, fifth: number); + constructor(struct_obj : BigStructWithStuff_Obj); assertValue(extraVal: number): void; } \ No newline at end of file diff --git a/feature_tests/js/api/BigStructWithStuff.mjs b/feature_tests/js/api/BigStructWithStuff.mjs index d912e3b7c..7a474961a 100644 --- a/feature_tests/js/api/BigStructWithStuff.mjs +++ b/feature_tests/js/api/BigStructWithStuff.mjs @@ -47,17 +47,37 @@ export class BigStructWithStuff { set fifth(value) { this.#fifth = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("first" in struct_obj) { + this.#first = struct_obj.first; } else { - - this.#first = arguments[0]; - this.#second = arguments[1]; - this.#third = arguments[2]; - this.#fourth = arguments[3]; - this.#fifth = arguments[4]; + throw new Error("Missing required type first."); } + + if ("second" in struct_obj) { + this.#second = struct_obj.second; + } else { + throw new Error("Missing required type second."); + } + + if ("third" in struct_obj) { + this.#third = struct_obj.third; + } else { + throw new Error("Missing required type third."); + } + + if ("fourth" in struct_obj) { + this.#fourth = struct_obj.fourth; + } else { + throw new Error("Missing required type fourth."); + } + + if ("fifth" in struct_obj) { + this.#fifth = struct_obj.fifth; + } else { + throw new Error("Missing required type fifth."); + } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/BorrowedFields.d.ts b/feature_tests/js/api/BorrowedFields.d.ts index f14cf67f8..67c6a89f0 100644 --- a/feature_tests/js/api/BorrowedFields.d.ts +++ b/feature_tests/js/api/BorrowedFields.d.ts @@ -2,6 +2,12 @@ import type { Bar } from "./Bar" import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type BorrowedFields_Obj = { + a: string; + b: string; + c: string; +}; + export class BorrowedFields { get a() : string; @@ -12,7 +18,7 @@ export class BorrowedFields { get c() : string; set c(value: string); - constructor(a: string, b: string, c: string); + constructor(struct_obj : BorrowedFields_Obj); static fromBarAndStrings(bar: Bar, dstr16: string, utf8Str: string): BorrowedFields; } \ No newline at end of file diff --git a/feature_tests/js/api/BorrowedFields.mjs b/feature_tests/js/api/BorrowedFields.mjs index 653776892..4a7d14d61 100644 --- a/feature_tests/js/api/BorrowedFields.mjs +++ b/feature_tests/js/api/BorrowedFields.mjs @@ -28,15 +28,25 @@ export class BorrowedFields { set c(value) { this.#c = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("a" in struct_obj) { + this.#a = struct_obj.a; } else { - - this.#a = arguments[0]; - this.#b = arguments[1]; - this.#c = arguments[2]; + throw new Error("Missing required type a."); } + + if ("b" in struct_obj) { + this.#b = struct_obj.b; + } else { + throw new Error("Missing required type b."); + } + + if ("c" in struct_obj) { + this.#c = struct_obj.c; + } else { + throw new Error("Missing required type c."); + } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/BorrowedFieldsReturning.d.ts b/feature_tests/js/api/BorrowedFieldsReturning.d.ts index e10182bf9..33a27c0b5 100644 --- a/feature_tests/js/api/BorrowedFieldsReturning.d.ts +++ b/feature_tests/js/api/BorrowedFieldsReturning.d.ts @@ -1,9 +1,13 @@ // generated by diplomat-tool import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type BorrowedFieldsReturning_Obj = { + bytes: string; +}; + export class BorrowedFieldsReturning { get bytes() : string; set bytes(value: string); - constructor(bytes: string); + constructor(struct_obj : BorrowedFieldsReturning_Obj); } \ No newline at end of file diff --git a/feature_tests/js/api/BorrowedFieldsReturning.mjs b/feature_tests/js/api/BorrowedFieldsReturning.mjs index 01ccb9316..d66addffa 100644 --- a/feature_tests/js/api/BorrowedFieldsReturning.mjs +++ b/feature_tests/js/api/BorrowedFieldsReturning.mjs @@ -11,13 +11,13 @@ export class BorrowedFieldsReturning { set bytes(value) { this.#bytes = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("bytes" in struct_obj) { + this.#bytes = struct_obj.bytes; } else { - - this.#bytes = arguments[0]; + throw new Error("Missing required type bytes."); } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/BorrowedFieldsWithBounds.d.ts b/feature_tests/js/api/BorrowedFieldsWithBounds.d.ts index dcc423bbe..6f88ee6fe 100644 --- a/feature_tests/js/api/BorrowedFieldsWithBounds.d.ts +++ b/feature_tests/js/api/BorrowedFieldsWithBounds.d.ts @@ -2,6 +2,12 @@ import type { Foo } from "./Foo" import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type BorrowedFieldsWithBounds_Obj = { + fieldA: string; + fieldB: string; + fieldC: string; +}; + export class BorrowedFieldsWithBounds { get fieldA() : string; @@ -12,7 +18,7 @@ export class BorrowedFieldsWithBounds { get fieldC() : string; set fieldC(value: string); - constructor(fieldA: string, fieldB: string, fieldC: string); + constructor(struct_obj : BorrowedFieldsWithBounds_Obj); static fromFooAndStrings(foo: Foo, dstr16X: string, utf8StrZ: string): BorrowedFieldsWithBounds; } \ No newline at end of file diff --git a/feature_tests/js/api/BorrowedFieldsWithBounds.mjs b/feature_tests/js/api/BorrowedFieldsWithBounds.mjs index 6687b5b28..a2f956875 100644 --- a/feature_tests/js/api/BorrowedFieldsWithBounds.mjs +++ b/feature_tests/js/api/BorrowedFieldsWithBounds.mjs @@ -28,15 +28,25 @@ export class BorrowedFieldsWithBounds { set fieldC(value) { this.#fieldC = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("fieldA" in struct_obj) { + this.#fieldA = struct_obj.fieldA; } else { - - this.#fieldA = arguments[0]; - this.#fieldB = arguments[1]; - this.#fieldC = arguments[2]; + throw new Error("Missing required type fieldA."); } + + if ("fieldB" in struct_obj) { + this.#fieldB = struct_obj.fieldB; + } else { + throw new Error("Missing required type fieldB."); + } + + if ("fieldC" in struct_obj) { + this.#fieldC = struct_obj.fieldC; + } else { + throw new Error("Missing required type fieldC."); + } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/CyclicStructA.d.ts b/feature_tests/js/api/CyclicStructA.d.ts index a0da4df1e..7e9070818 100644 --- a/feature_tests/js/api/CyclicStructA.d.ts +++ b/feature_tests/js/api/CyclicStructA.d.ts @@ -2,11 +2,15 @@ import type { CyclicStructB } from "./CyclicStructB" import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type CyclicStructA_Obj = { + a: CyclicStructB; +}; + export class CyclicStructA { get a() : CyclicStructB; set a(value: CyclicStructB); - constructor(a: CyclicStructB); + constructor(struct_obj : CyclicStructA_Obj); static getB(): CyclicStructB; } \ No newline at end of file diff --git a/feature_tests/js/api/CyclicStructA.mjs b/feature_tests/js/api/CyclicStructA.mjs index d77ecbdb9..161b88a43 100644 --- a/feature_tests/js/api/CyclicStructA.mjs +++ b/feature_tests/js/api/CyclicStructA.mjs @@ -12,13 +12,13 @@ export class CyclicStructA { set a(value) { this.#a = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("a" in struct_obj) { + this.#a = struct_obj.a; } else { - - this.#a = arguments[0]; + throw new Error("Missing required type a."); } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/CyclicStructB.d.ts b/feature_tests/js/api/CyclicStructB.d.ts index 30c2103a2..78a119544 100644 --- a/feature_tests/js/api/CyclicStructB.d.ts +++ b/feature_tests/js/api/CyclicStructB.d.ts @@ -2,11 +2,15 @@ import type { CyclicStructA } from "./CyclicStructA" import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type CyclicStructB_Obj = { + field: number; +}; + export class CyclicStructB { get field() : number; set field(value: number); - constructor(field: number); + constructor(struct_obj : CyclicStructB_Obj); static getA(): CyclicStructA; } \ No newline at end of file diff --git a/feature_tests/js/api/CyclicStructB.mjs b/feature_tests/js/api/CyclicStructB.mjs index 8eae9de95..c3e425741 100644 --- a/feature_tests/js/api/CyclicStructB.mjs +++ b/feature_tests/js/api/CyclicStructB.mjs @@ -12,13 +12,13 @@ export class CyclicStructB { set field(value) { this.#field = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("field" in struct_obj) { + this.#field = struct_obj.field; } else { - - this.#field = arguments[0]; + throw new Error("Missing required type field."); } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/ErrorStruct.d.ts b/feature_tests/js/api/ErrorStruct.d.ts index ba0c281f2..c60ec12f6 100644 --- a/feature_tests/js/api/ErrorStruct.d.ts +++ b/feature_tests/js/api/ErrorStruct.d.ts @@ -1,6 +1,11 @@ // generated by diplomat-tool import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type ErrorStruct_Obj = { + i: number; + j: number; +}; + export class ErrorStruct { get i() : number; @@ -8,5 +13,5 @@ export class ErrorStruct { get j() : number; set j(value: number); - constructor(i: number, j: number); + constructor(struct_obj : ErrorStruct_Obj); } \ No newline at end of file diff --git a/feature_tests/js/api/ErrorStruct.mjs b/feature_tests/js/api/ErrorStruct.mjs index a4280d7f0..10196f475 100644 --- a/feature_tests/js/api/ErrorStruct.mjs +++ b/feature_tests/js/api/ErrorStruct.mjs @@ -19,14 +19,19 @@ export class ErrorStruct { set j(value) { this.#j = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("i" in struct_obj) { + this.#i = struct_obj.i; } else { - - this.#i = arguments[0]; - this.#j = arguments[1]; + throw new Error("Missing required type i."); } + + if ("j" in struct_obj) { + this.#j = struct_obj.j; + } else { + throw new Error("Missing required type j."); + } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/ImportedStruct.d.ts b/feature_tests/js/api/ImportedStruct.d.ts index b01b78222..4166d6e6e 100644 --- a/feature_tests/js/api/ImportedStruct.d.ts +++ b/feature_tests/js/api/ImportedStruct.d.ts @@ -2,6 +2,11 @@ import type { UnimportedEnum } from "./UnimportedEnum" import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type ImportedStruct_Obj = { + foo: UnimportedEnum; + count: number; +}; + export class ImportedStruct { get foo() : UnimportedEnum; @@ -9,5 +14,5 @@ export class ImportedStruct { get count() : number; set count(value: number); - constructor(foo: UnimportedEnum, count: number); + constructor(struct_obj : ImportedStruct_Obj); } \ No newline at end of file diff --git a/feature_tests/js/api/ImportedStruct.mjs b/feature_tests/js/api/ImportedStruct.mjs index 6e3df0040..a9b6cfa99 100644 --- a/feature_tests/js/api/ImportedStruct.mjs +++ b/feature_tests/js/api/ImportedStruct.mjs @@ -20,14 +20,19 @@ export class ImportedStruct { set count(value) { this.#count = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("foo" in struct_obj) { + this.#foo = struct_obj.foo; } else { - - this.#foo = arguments[0]; - this.#count = arguments[1]; + throw new Error("Missing required type foo."); } + + if ("count" in struct_obj) { + this.#count = struct_obj.count; + } else { + throw new Error("Missing required type count."); + } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/MyStruct.d.ts b/feature_tests/js/api/MyStruct.d.ts index 4b9a018f5..1e09a4182 100644 --- a/feature_tests/js/api/MyStruct.d.ts +++ b/feature_tests/js/api/MyStruct.d.ts @@ -3,6 +3,16 @@ import type { MyEnum } from "./MyEnum" import type { MyZst } from "./MyZst" import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type MyStruct_Obj = { + a: number; + b: boolean; + c: number; + d: bigint; + e: number; + f: codepoint; + g: MyEnum; +}; + export class MyStruct { get a() : number; @@ -25,7 +35,7 @@ export class MyStruct { get g() : MyEnum; set g(value: MyEnum); - constructor(a: number, b: boolean, c: number, d: bigint, e: number, f: codepoint, g: MyEnum); + constructor(struct_obj : MyStruct_Obj); static new_(): MyStruct; diff --git a/feature_tests/js/api/MyStruct.mjs b/feature_tests/js/api/MyStruct.mjs index aebb049b4..892c70e09 100644 --- a/feature_tests/js/api/MyStruct.mjs +++ b/feature_tests/js/api/MyStruct.mjs @@ -61,19 +61,49 @@ export class MyStruct { set g(value) { this.#g = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("a" in struct_obj) { + this.#a = struct_obj.a; } else { - - this.#a = arguments[0]; - this.#b = arguments[1]; - this.#c = arguments[2]; - this.#d = arguments[3]; - this.#e = arguments[4]; - this.#f = arguments[5]; - this.#g = arguments[6]; + throw new Error("Missing required type a."); } + + if ("b" in struct_obj) { + this.#b = struct_obj.b; + } else { + throw new Error("Missing required type b."); + } + + if ("c" in struct_obj) { + this.#c = struct_obj.c; + } else { + throw new Error("Missing required type c."); + } + + if ("d" in struct_obj) { + this.#d = struct_obj.d; + } else { + throw new Error("Missing required type d."); + } + + if ("e" in struct_obj) { + this.#e = struct_obj.e; + } else { + throw new Error("Missing required type e."); + } + + if ("f" in struct_obj) { + this.#f = struct_obj.f; + } else { + throw new Error("Missing required type f."); + } + + if ("g" in struct_obj) { + this.#g = struct_obj.g; + } else { + throw new Error("Missing required type g."); + } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/MyZst.d.ts b/feature_tests/js/api/MyZst.d.ts index 322ca63ea..b687465bc 100644 --- a/feature_tests/js/api/MyZst.d.ts +++ b/feature_tests/js/api/MyZst.d.ts @@ -1,6 +1,9 @@ // generated by diplomat-tool import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type MyZst_Obj = { +}; + export class MyZst { - constructor(); + constructor(struct_obj : MyZst_Obj); } \ No newline at end of file diff --git a/feature_tests/js/api/MyZst.mjs b/feature_tests/js/api/MyZst.mjs index 0819803c4..3ff4d0118 100644 --- a/feature_tests/js/api/MyZst.mjs +++ b/feature_tests/js/api/MyZst.mjs @@ -3,8 +3,7 @@ import wasm from "./diplomat-wasm.mjs"; import * as diplomatRuntime from "./diplomat-runtime.mjs"; export class MyZst { - constructor() { - + constructor(struct_obj) { } } \ No newline at end of file diff --git a/feature_tests/js/api/NestedBorrowedFields.d.ts b/feature_tests/js/api/NestedBorrowedFields.d.ts index e62721f0d..cd8882014 100644 --- a/feature_tests/js/api/NestedBorrowedFields.d.ts +++ b/feature_tests/js/api/NestedBorrowedFields.d.ts @@ -5,6 +5,12 @@ import type { BorrowedFieldsWithBounds } from "./BorrowedFieldsWithBounds" import type { Foo } from "./Foo" import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type NestedBorrowedFields_Obj = { + fields: BorrowedFields; + bounds: BorrowedFieldsWithBounds; + bounds2: BorrowedFieldsWithBounds; +}; + export class NestedBorrowedFields { get fields() : BorrowedFields; @@ -15,7 +21,7 @@ export class NestedBorrowedFields { get bounds2() : BorrowedFieldsWithBounds; set bounds2(value: BorrowedFieldsWithBounds); - constructor(fields: BorrowedFields, bounds: BorrowedFieldsWithBounds, bounds2: BorrowedFieldsWithBounds); + constructor(struct_obj : NestedBorrowedFields_Obj); static fromBarAndFooAndStrings(bar: Bar, foo: Foo, dstr16X: string, dstr16Z: string, utf8StrY: string, utf8StrZ: string): NestedBorrowedFields; } \ No newline at end of file diff --git a/feature_tests/js/api/NestedBorrowedFields.mjs b/feature_tests/js/api/NestedBorrowedFields.mjs index 6e3904f24..82694ad5d 100644 --- a/feature_tests/js/api/NestedBorrowedFields.mjs +++ b/feature_tests/js/api/NestedBorrowedFields.mjs @@ -31,15 +31,25 @@ export class NestedBorrowedFields { set bounds2(value) { this.#bounds2 = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("fields" in struct_obj) { + this.#fields = struct_obj.fields; } else { - - this.#fields = arguments[0]; - this.#bounds = arguments[1]; - this.#bounds2 = arguments[2]; + throw new Error("Missing required type fields."); } + + if ("bounds" in struct_obj) { + this.#bounds = struct_obj.bounds; + } else { + throw new Error("Missing required type bounds."); + } + + if ("bounds2" in struct_obj) { + this.#bounds2 = struct_obj.bounds2; + } else { + throw new Error("Missing required type bounds2."); + } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/OptionInputStruct.d.ts b/feature_tests/js/api/OptionInputStruct.d.ts index 9c14c45dd..0518d3e3d 100644 --- a/feature_tests/js/api/OptionInputStruct.d.ts +++ b/feature_tests/js/api/OptionInputStruct.d.ts @@ -2,6 +2,12 @@ import type { OptionEnum } from "./OptionEnum" import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type OptionInputStruct_Obj = { + a: number | null; + b: codepoint | null; + c: OptionEnum | null; +}; + export class OptionInputStruct { get a() : number | null; @@ -12,5 +18,5 @@ export class OptionInputStruct { get c() : OptionEnum | null; set c(value: OptionEnum | null); - constructor(a: number | null, b: codepoint | null, c: OptionEnum | null); + constructor(struct_obj : OptionInputStruct_Obj); } \ No newline at end of file diff --git a/feature_tests/js/api/OptionInputStruct.mjs b/feature_tests/js/api/OptionInputStruct.mjs index ba72bccf4..9bab8234a 100644 --- a/feature_tests/js/api/OptionInputStruct.mjs +++ b/feature_tests/js/api/OptionInputStruct.mjs @@ -28,15 +28,25 @@ export class OptionInputStruct { set c(value) { this.#c = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("a" in struct_obj) { + this.#a = struct_obj.a; } else { - - this.#a = arguments[0]; - this.#b = arguments[1]; - this.#c = arguments[2]; + throw new Error("Missing required type a."); } + + if ("b" in struct_obj) { + this.#b = struct_obj.b; + } else { + throw new Error("Missing required type b."); + } + + if ("c" in struct_obj) { + this.#c = struct_obj.c; + } else { + throw new Error("Missing required type c."); + } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/OptionStruct.d.ts b/feature_tests/js/api/OptionStruct.d.ts index 8d65486cd..ec0ad781c 100644 --- a/feature_tests/js/api/OptionStruct.d.ts +++ b/feature_tests/js/api/OptionStruct.d.ts @@ -3,6 +3,13 @@ import type { OptionOpaque } from "./OptionOpaque" import type { OptionOpaqueChar } from "./OptionOpaqueChar" import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; +type OptionStruct_Obj = { + a: OptionOpaque | null; + b: OptionOpaqueChar | null; + c: number; + d: OptionOpaque | null; +}; + export class OptionStruct { get a() : OptionOpaque | null; diff --git a/feature_tests/js/api/OptionStruct.mjs b/feature_tests/js/api/OptionStruct.mjs index d89c89f81..d04b6cae1 100644 --- a/feature_tests/js/api/OptionStruct.mjs +++ b/feature_tests/js/api/OptionStruct.mjs @@ -29,12 +29,34 @@ export class OptionStruct { return this.#d; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(, internalConstructor) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("OptionStruct is an out struct and can only be created internally."); + } + if ("a" in struct_obj) { + this.#a = struct_obj.a; + } else { + throw new Error("Missing required type a."); + } + + if ("b" in struct_obj) { + this.#b = struct_obj.b; } else { - console.error("OptionStruct is an out struct and can only be created internally."); + throw new Error("Missing required type b."); } + + if ("c" in struct_obj) { + this.#c = struct_obj.c; + } else { + throw new Error("Missing required type c."); + } + + if ("d" in struct_obj) { + this.#d = struct_obj.d; + } else { + throw new Error("Missing required type d."); + } + } // Return this struct in FFI function friendly format. diff --git a/feature_tests/js/api/ScalarPairWithPadding.d.ts b/feature_tests/js/api/ScalarPairWithPadding.d.ts index 414c5f1ab..1262f2cfb 100644 --- a/feature_tests/js/api/ScalarPairWithPadding.d.ts +++ b/feature_tests/js/api/ScalarPairWithPadding.d.ts @@ -4,6 +4,11 @@ import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; /** Testing JS-specific layout/padding behavior */ +type ScalarPairWithPadding_Obj = { + first: number; + second: number; +}; + export class ScalarPairWithPadding { get first() : number; @@ -11,7 +16,7 @@ export class ScalarPairWithPadding { get second() : number; set second(value: number); - constructor(first: number, second: number); + constructor(struct_obj : ScalarPairWithPadding_Obj); assertValue(): void; } \ No newline at end of file diff --git a/feature_tests/js/api/ScalarPairWithPadding.mjs b/feature_tests/js/api/ScalarPairWithPadding.mjs index 617327f56..6d45eb38a 100644 --- a/feature_tests/js/api/ScalarPairWithPadding.mjs +++ b/feature_tests/js/api/ScalarPairWithPadding.mjs @@ -22,14 +22,19 @@ export class ScalarPairWithPadding { set second(value) { this.#second = value; } - constructor() { - if (arguments.length > 0 && arguments[0] === diplomatRuntime.internalConstructor) { - this.#fromFFI(...Array.prototype.slice.call(arguments, 1)); + constructor(struct_obj) { + if ("first" in struct_obj) { + this.#first = struct_obj.first; } else { - - this.#first = arguments[0]; - this.#second = arguments[1]; + throw new Error("Missing required type first."); } + + if ("second" in struct_obj) { + this.#second = struct_obj.second; + } else { + throw new Error("Missing required type second."); + } + } // Return this struct in FFI function friendly format. From d579a4bfe6df8eccadc1c4f4778b5ce6d730d83d Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 13:02:59 -0800 Subject: [PATCH 04/19] Updating generation --- tool/src/js/converter.rs | 8 ++++---- tool/templates/js/struct.js.jinja | 12 +++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/tool/src/js/converter.rs b/tool/src/js/converter.rs index 43151a799..3851177c9 100644 --- a/tool/src/js/converter.rs +++ b/tool/src/js/converter.rs @@ -205,16 +205,16 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> { let type_def = self.tcx.resolve_type(id); match type_def { hir::TypeDef::Struct(st) if st.fields.is_empty() => { - format!("new {type_name}(diplomatRuntime.internalConstructor)").into() + format!("{type_name}._fromFFI(diplomatRuntime.internalConstructor)").into() } hir::TypeDef::Struct(..) => { - format!("new {type_name}(diplomatRuntime.internalConstructor, {variable_name}{edges})").into() + format!("{type_name}._fromFFI(diplomatRuntime.internalConstructor, {variable_name}{edges})").into() } hir::TypeDef::OutStruct(st) if st.fields.is_empty() => { - format!("new {type_name}(diplomatRuntime.internalConstructor)").into() + format!("{type_name}._fromFFI(diplomatRuntime.internalConstructor)").into() } hir::TypeDef::OutStruct(..) => { - format!("new {type_name}(diplomatRuntime.internalConstructor, {variable_name}{edges})").into() + format!("{type_name}._fromFFI(diplomatRuntime.internalConstructor, {variable_name}{edges})").into() } _ => unreachable!("Expected struct type def, found {type_def:?}"), } diff --git a/tool/templates/js/struct.js.jinja b/tool/templates/js/struct.js.jinja index cb24ee75a..2315cbcb8 100644 --- a/tool/templates/js/struct.js.jinja +++ b/tool/templates/js/struct.js.jinja @@ -45,7 +45,7 @@ export class {{type_name}} { if ("{{field.field_name}}" in struct_obj) { this.#{{ field.field_name }} = struct_obj.{{field.field_name}}; } else { - throw new Error("Missing required type {{field.field_name}}."); + throw new Error("Missing required field {{field.field_name}}."); } {%~ endfor %} } @@ -105,11 +105,17 @@ export class {{type_name}} { // This method does not attempt to handle any dependencies between lifetimes, the caller // should handle this when constructing edge arrays. {% endif -%} - #fromFFI(ptr{%- for lifetime in lifetimes.all_lifetimes() -%}, {{lifetimes.fmt_lifetime(lifetime)}}Edges{%- endfor -%}) { + static _fromFFI(internalConstructor, ptr{%- for lifetime in lifetimes.all_lifetimes() -%}, {{lifetimes.fmt_lifetime(lifetime)}}Edges{%- endfor -%}) { + if (internalConstructor !== diplomatRuntime.internalConstructor) { + throw new Error("{{type_name}}._fromFFI is not meant to be called externally. Please use the default constructor."); + } + var structObj = {}; {%- for field in fields %} const {{field.field_name}}Deref = {{field.c_to_js_deref}}; - this.#{{field.field_name}} = {{field.c_to_js}}; + structObj.{{field.field_name}} = {{field.c_to_js}}; {%- endfor %} + + return new {{type_name}}(structObj); } {%- for l in lifetimes.all_lifetimes() %} From b02a362ca10bddf235d8d928339f27e7336b6271 Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 13:04:01 -0800 Subject: [PATCH 05/19] Generation --- .../lib/api/FixedDecimalFormatterOptions.mjs | 18 +++++--- feature_tests/js/api/BigStructWithStuff.mjs | 28 ++++++++----- feature_tests/js/api/BorrowedFields.mjs | 22 ++++++---- .../js/api/BorrowedFieldsReturning.mjs | 12 ++++-- .../js/api/BorrowedFieldsWithBounds.mjs | 22 ++++++---- feature_tests/js/api/CyclicStructA.mjs | 14 +++++-- feature_tests/js/api/CyclicStructB.mjs | 14 +++++-- feature_tests/js/api/ErrorStruct.mjs | 16 ++++--- feature_tests/js/api/Foo.mjs | 2 +- feature_tests/js/api/ImportedStruct.mjs | 16 ++++--- feature_tests/js/api/MyStruct.mjs | 42 +++++++++++-------- feature_tests/js/api/NestedBorrowedFields.mjs | 22 ++++++---- feature_tests/js/api/Opaque.mjs | 2 +- feature_tests/js/api/OptionInputStruct.mjs | 20 +++++---- feature_tests/js/api/OptionOpaque.mjs | 10 ++--- feature_tests/js/api/OptionStruct.mjs | 24 +++++++---- feature_tests/js/api/ResultOpaque.mjs | 2 +- .../js/api/ScalarPairWithPadding.mjs | 16 ++++--- 18 files changed, 193 insertions(+), 109 deletions(-) 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() { From 4be6041f499a10184801ee5a597caa8accf8ff08 Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 15:41:59 -0800 Subject: [PATCH 06/19] Fix generation bug for out structs --- feature_tests/js/api/OptionStruct.d.ts | 7 ------- feature_tests/js/api/OptionStruct.mjs | 2 +- tool/templates/js/struct.js.jinja | 6 +++--- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/feature_tests/js/api/OptionStruct.d.ts b/feature_tests/js/api/OptionStruct.d.ts index ec0ad781c..8d65486cd 100644 --- a/feature_tests/js/api/OptionStruct.d.ts +++ b/feature_tests/js/api/OptionStruct.d.ts @@ -3,13 +3,6 @@ import type { OptionOpaque } from "./OptionOpaque" import type { OptionOpaqueChar } from "./OptionOpaqueChar" import type { pointer, codepoint } from "./diplomat-runtime.d.ts"; -type OptionStruct_Obj = { - a: OptionOpaque | null; - b: OptionOpaqueChar | null; - c: number; - d: OptionOpaque | null; -}; - export class OptionStruct { get a() : OptionOpaque | null; diff --git a/feature_tests/js/api/OptionStruct.mjs b/feature_tests/js/api/OptionStruct.mjs index 5ffbca10e..8569105a3 100644 --- a/feature_tests/js/api/OptionStruct.mjs +++ b/feature_tests/js/api/OptionStruct.mjs @@ -29,7 +29,7 @@ export class OptionStruct { return this.#d; } - constructor(, internalConstructor) { + constructor(struct_obj, internalConstructor) { if (internalConstructor !== diplomatRuntime.internalConstructor) { throw new Error("OptionStruct is an out struct and can only be created internally."); } diff --git a/tool/templates/js/struct.js.jinja b/tool/templates/js/struct.js.jinja index 2315cbcb8..019d6af55 100644 --- a/tool/templates/js/struct.js.jinja +++ b/tool/templates/js/struct.js.jinja @@ -3,7 +3,7 @@ */ {% endif -%} -{%- if typescript -%} +{%- if typescript && !is_out -%} type {{type_name}}_Obj = { {%- for field in fields %} {{field.field_name}}: {{field.js_type_name}}; @@ -30,8 +30,8 @@ export class {{type_name}} { {%- endfor %} {%- if !(typescript && is_out) %} - constructor( - {%- if !is_out -%} struct_obj {%- if typescript %} : {{type_name}}_Obj {%- endif -%}{%- endif -%} + constructor(struct_obj + {%- if typescript %} : {{type_name}}_Obj {%- endif -%} {%- if is_out && !typescript %}, internalConstructor{% endif -%} ) {%- if typescript %};{% else %} { From d65b8f212feba30a26f08d063226c44cda99652e Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 15:42:46 -0800 Subject: [PATCH 07/19] Making tests run --- feature_tests/js/test/struct-ts.mjs | 10 +++++++++- feature_tests/js/test/struct-ts.mts | 10 +++++++++- feature_tests/js/test/struct.mjs | 23 ++++++++++++++++++++--- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/feature_tests/js/test/struct-ts.mjs b/feature_tests/js/test/struct-ts.mjs index ced1b899b..5679066c1 100644 --- a/feature_tests/js/test/struct-ts.mjs +++ b/feature_tests/js/test/struct-ts.mjs @@ -12,6 +12,14 @@ test("Verify invariants of struct", t => { t.is(s.intoA(), 17); }); test("Test struct creation", t => { - const s = new MyStruct(17, true, 209, 1234n, 5991, '餐'.codePointAt(0), MyEnum.B); + const s = new MyStruct({ + a: 17, + b: true, + c: 209, + d: 1234n, + e: 5991, + f: '餐'.codePointAt(0), + g: MyEnum.B + }); t.is(s.intoA(), 17); }); diff --git a/feature_tests/js/test/struct-ts.mts b/feature_tests/js/test/struct-ts.mts index 64fb8c623..e6e45c93b 100644 --- a/feature_tests/js/test/struct-ts.mts +++ b/feature_tests/js/test/struct-ts.mts @@ -14,6 +14,14 @@ test("Verify invariants of struct", t => { }); test("Test struct creation", t => { - const s = new MyStruct(17, true, 209, 1234n, 5991, '餐'.codePointAt(0), MyEnum.B); + const s = new MyStruct({ + a: 17, + b: true, + c: 209, + d: 1234n, + e: 5991, + f: '餐'.codePointAt(0), + g: MyEnum.B + }); t.is(s.intoA(), 17); }); \ No newline at end of file diff --git a/feature_tests/js/test/struct.mjs b/feature_tests/js/test/struct.mjs index 86fbcd30e..2134e207b 100644 --- a/feature_tests/js/test/struct.mjs +++ b/feature_tests/js/test/struct.mjs @@ -14,18 +14,35 @@ test("Verify invariants of struct", t => { }); test("Test struct creation", t => { - const s = new MyStruct(17, true, 209, 1234n, 5991, '餐'.codePointAt(0), MyEnum.B); + const s = new MyStruct({ + a: 17, + b: true, + c: 209, + d: 1234n, + e: 5991, + f: '餐'.codePointAt(0), + g: MyEnum.B + }); t.is(s.intoA(), 17); }); test("Test struct layout: scalar pair layout", t => { - const s = new ScalarPairWithPadding(122, 414); + const s = new ScalarPairWithPadding({ + first: 122, + second: 414 + }); s.assertValue(); t.is(true, true); // Ava doesn't like tests without assertions }); test("Test struct layout: complex struct with multiple padding types and contained scalar pair", t => { - const s = new BigStructWithStuff(101, 505, 9345, new ScalarPairWithPadding(122, 414), 99); + const s = new BigStructWithStuff({ + first: 101, + second: 505, + third: 9345, + fourth: new ScalarPairWithPadding(122, 414), + fifth: 99 + }); s.assertValue(853); t.is(true, true); // Ava doesn't like tests without assertions }); From 4d0c41cfff9d0b9df19805b5352790ec1bc5a8dc Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 20:09:57 -0800 Subject: [PATCH 08/19] Add support for options --- tool/src/js/gen.rs | 11 ++++++++++- tool/templates/js/struct.js.jinja | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tool/src/js/gen.rs b/tool/src/js/gen.rs index 8170f4628..520bca715 100644 --- a/tool/src/js/gen.rs +++ b/tool/src/js/gen.rs @@ -181,6 +181,11 @@ impl<'ctx, 'tcx> TyGenContext<'ctx, 'tcx> { let js_type_name = self.gen_js_type_str(&field.ty); + let is_option = match &field.ty { + hir::Type::DiplomatOption(..) => true, + _ => false + }; + let c_to_js_deref = self.gen_c_to_js_deref_for_type(&field.ty, "ptr".into(), struct_field_info.fields[i].offset); let c_to_js = self.gen_c_to_js_for_type( @@ -283,7 +288,8 @@ impl<'ctx, 'tcx> TyGenContext<'ctx, 'tcx> { c_to_js, js_to_c, js_to_c_write, - maybe_struct_borrow_info: maybe_struct_borrow_info.map(|i| i.param_info) + maybe_struct_borrow_info: maybe_struct_borrow_info.map(|i| i.param_info), + is_optional: is_option } }).collect::>(); @@ -597,6 +603,9 @@ pub(super) struct FieldInfo<'info, P: hir::TyPosition> { js_to_c_write: String, /// Used in `get _fieldsForLifetime...` fields, which themselves are used in [`display_lifetime_edge`]. maybe_struct_borrow_info: Option>, + + /// Used in the constructor() function to determine whether or not this field is required for construction. + is_optional : bool, } // Helpers used in templates (Askama has restrictions on Rust syntax) diff --git a/tool/templates/js/struct.js.jinja b/tool/templates/js/struct.js.jinja index 019d6af55..7d7de380b 100644 --- a/tool/templates/js/struct.js.jinja +++ b/tool/templates/js/struct.js.jinja @@ -45,7 +45,11 @@ export class {{type_name}} { if ("{{field.field_name}}" in struct_obj) { this.#{{ field.field_name }} = struct_obj.{{field.field_name}}; } else { + {%- if field.is_optional %} + this.#{{ field.field_name }} = null; + {%- else %} throw new Error("Missing required field {{field.field_name}}."); + {%- endif %} } {%~ endfor %} } From cdad86baab7f3c9fad49e5311a49ed38ccbc05bb Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 20:11:30 -0800 Subject: [PATCH 09/19] Generation --- feature_tests/js/api/OptionInputStruct.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/feature_tests/js/api/OptionInputStruct.mjs b/feature_tests/js/api/OptionInputStruct.mjs index 94ad38110..f0f51ddc8 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 field a."); + this.#a = null; } if ("b" in struct_obj) { this.#b = struct_obj.b; } else { - throw new Error("Missing required field b."); + this.#b = null; } if ("c" in struct_obj) { this.#c = struct_obj.c; } else { - throw new Error("Missing required field c."); + this.#c = null; } } From db2bb2e955e6d2d863c9e019a80ecdfff78fd553 Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 20:47:09 -0800 Subject: [PATCH 10/19] Fix test calls, throw error if object not passed --- feature_tests/js/test/option.mjs | 2 +- feature_tests/js/test/struct.mjs | 2 +- tool/templates/js/struct.js.jinja | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/feature_tests/js/test/option.mjs b/feature_tests/js/test/option.mjs index d1889fb91..829536add 100644 --- a/feature_tests/js/test/option.mjs +++ b/feature_tests/js/test/option.mjs @@ -31,7 +31,7 @@ test("DiplomatOption tests", t => { let maybeStruct = OptionOpaque.acceptsOptionInputStruct(null); t.assert(maybeStruct === null); - maybeStruct = OptionOpaque.acceptsOptionInputStruct(new OptionInputStruct(7, null, OptionEnum.Bar)); + maybeStruct = OptionOpaque.acceptsOptionInputStruct(new OptionInputStruct({a: 7, c: OptionEnum.Bar})); t.is(maybeStruct.a, 7); t.assert(maybeStruct.b === null); t.is(maybeStruct.c.value, OptionEnum.Bar.value); diff --git a/feature_tests/js/test/struct.mjs b/feature_tests/js/test/struct.mjs index 2134e207b..c8601bc47 100644 --- a/feature_tests/js/test/struct.mjs +++ b/feature_tests/js/test/struct.mjs @@ -40,7 +40,7 @@ test("Test struct layout: complex struct with multiple padding types and contain first: 101, second: 505, third: 9345, - fourth: new ScalarPairWithPadding(122, 414), + fourth: new ScalarPairWithPadding({first: 122, second: 414}), fifth: 99 }); s.assertValue(853); diff --git a/tool/templates/js/struct.js.jinja b/tool/templates/js/struct.js.jinja index 7d7de380b..7d187baf4 100644 --- a/tool/templates/js/struct.js.jinja +++ b/tool/templates/js/struct.js.jinja @@ -34,6 +34,9 @@ export class {{type_name}} { {%- if typescript %} : {{type_name}}_Obj {%- endif -%} {%- if is_out && !typescript %}, internalConstructor{% endif -%} ) {%- if typescript %};{% else %} { + if (typeof struct_obj !== "object") { + throw new Error("{{type_name}}'s constructor takes an object of {{type_name}}'s fields."); + } {%- if is_out %} if (internalConstructor !== diplomatRuntime.internalConstructor) { From 5b1e03332e6a203c99a1f64fd7cc47604bbbf884 Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 20:49:49 -0800 Subject: [PATCH 11/19] Generation, generation fixes --- example/js/lib/api/FixedDecimalFormatterOptions.mjs | 4 ++++ feature_tests/js/api/BigStructWithStuff.mjs | 4 ++++ feature_tests/js/api/BorrowedFields.mjs | 4 ++++ feature_tests/js/api/BorrowedFieldsReturning.mjs | 4 ++++ feature_tests/js/api/BorrowedFieldsWithBounds.mjs | 4 ++++ feature_tests/js/api/CyclicStructA.mjs | 4 ++++ feature_tests/js/api/CyclicStructB.mjs | 4 ++++ feature_tests/js/api/ErrorStruct.mjs | 4 ++++ feature_tests/js/api/ImportedStruct.mjs | 4 ++++ feature_tests/js/api/MyStruct.mjs | 4 ++++ feature_tests/js/api/MyZst.mjs | 4 ++++ feature_tests/js/api/NestedBorrowedFields.mjs | 4 ++++ feature_tests/js/api/OptionInputStruct.mjs | 4 ++++ feature_tests/js/api/OptionStruct.mjs | 4 ++++ feature_tests/js/api/ScalarPairWithPadding.mjs | 4 ++++ tool/templates/js/struct.js.jinja | 2 +- 16 files changed, 61 insertions(+), 1 deletion(-) diff --git a/example/js/lib/api/FixedDecimalFormatterOptions.mjs b/example/js/lib/api/FixedDecimalFormatterOptions.mjs index 16252cf16..941100f6e 100644 --- a/example/js/lib/api/FixedDecimalFormatterOptions.mjs +++ b/example/js/lib/api/FixedDecimalFormatterOptions.mjs @@ -21,6 +21,10 @@ export class FixedDecimalFormatterOptions { this.#someOtherConfig = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("FixedDecimalFormatterOptions's constructor takes an object of FixedDecimalFormatterOptions's fields."); + } + if ("groupingStrategy" in struct_obj) { this.#groupingStrategy = struct_obj.groupingStrategy; } else { diff --git a/feature_tests/js/api/BigStructWithStuff.mjs b/feature_tests/js/api/BigStructWithStuff.mjs index a8bfe6fdd..640381e16 100644 --- a/feature_tests/js/api/BigStructWithStuff.mjs +++ b/feature_tests/js/api/BigStructWithStuff.mjs @@ -48,6 +48,10 @@ export class BigStructWithStuff { this.#fifth = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("BigStructWithStuff's constructor takes an object of BigStructWithStuff's fields."); + } + if ("first" in struct_obj) { this.#first = struct_obj.first; } else { diff --git a/feature_tests/js/api/BorrowedFields.mjs b/feature_tests/js/api/BorrowedFields.mjs index 24546aeab..f6f0ea22d 100644 --- a/feature_tests/js/api/BorrowedFields.mjs +++ b/feature_tests/js/api/BorrowedFields.mjs @@ -29,6 +29,10 @@ export class BorrowedFields { this.#c = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("BorrowedFields's constructor takes an object of BorrowedFields's fields."); + } + if ("a" in struct_obj) { this.#a = struct_obj.a; } else { diff --git a/feature_tests/js/api/BorrowedFieldsReturning.mjs b/feature_tests/js/api/BorrowedFieldsReturning.mjs index 1bd1f27fc..b32575596 100644 --- a/feature_tests/js/api/BorrowedFieldsReturning.mjs +++ b/feature_tests/js/api/BorrowedFieldsReturning.mjs @@ -12,6 +12,10 @@ export class BorrowedFieldsReturning { this.#bytes = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("BorrowedFieldsReturning's constructor takes an object of BorrowedFieldsReturning's fields."); + } + if ("bytes" in struct_obj) { this.#bytes = struct_obj.bytes; } else { diff --git a/feature_tests/js/api/BorrowedFieldsWithBounds.mjs b/feature_tests/js/api/BorrowedFieldsWithBounds.mjs index 44a4fa4cd..bc4fa6a35 100644 --- a/feature_tests/js/api/BorrowedFieldsWithBounds.mjs +++ b/feature_tests/js/api/BorrowedFieldsWithBounds.mjs @@ -29,6 +29,10 @@ export class BorrowedFieldsWithBounds { this.#fieldC = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("BorrowedFieldsWithBounds's constructor takes an object of BorrowedFieldsWithBounds's fields."); + } + if ("fieldA" in struct_obj) { this.#fieldA = struct_obj.fieldA; } else { diff --git a/feature_tests/js/api/CyclicStructA.mjs b/feature_tests/js/api/CyclicStructA.mjs index 0543e1dba..ce4a0ae3e 100644 --- a/feature_tests/js/api/CyclicStructA.mjs +++ b/feature_tests/js/api/CyclicStructA.mjs @@ -13,6 +13,10 @@ export class CyclicStructA { this.#a = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("CyclicStructA's constructor takes an object of CyclicStructA's fields."); + } + if ("a" in struct_obj) { this.#a = struct_obj.a; } else { diff --git a/feature_tests/js/api/CyclicStructB.mjs b/feature_tests/js/api/CyclicStructB.mjs index feb577e58..7f4c0c3fe 100644 --- a/feature_tests/js/api/CyclicStructB.mjs +++ b/feature_tests/js/api/CyclicStructB.mjs @@ -13,6 +13,10 @@ export class CyclicStructB { this.#field = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("CyclicStructB's constructor takes an object of CyclicStructB's fields."); + } + if ("field" in struct_obj) { this.#field = struct_obj.field; } else { diff --git a/feature_tests/js/api/ErrorStruct.mjs b/feature_tests/js/api/ErrorStruct.mjs index 49de5f33b..207b7fd77 100644 --- a/feature_tests/js/api/ErrorStruct.mjs +++ b/feature_tests/js/api/ErrorStruct.mjs @@ -20,6 +20,10 @@ export class ErrorStruct { this.#j = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("ErrorStruct's constructor takes an object of ErrorStruct's fields."); + } + if ("i" in struct_obj) { this.#i = struct_obj.i; } else { diff --git a/feature_tests/js/api/ImportedStruct.mjs b/feature_tests/js/api/ImportedStruct.mjs index da38048cc..db12057b2 100644 --- a/feature_tests/js/api/ImportedStruct.mjs +++ b/feature_tests/js/api/ImportedStruct.mjs @@ -21,6 +21,10 @@ export class ImportedStruct { this.#count = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("ImportedStruct's constructor takes an object of ImportedStruct's fields."); + } + if ("foo" in struct_obj) { this.#foo = struct_obj.foo; } else { diff --git a/feature_tests/js/api/MyStruct.mjs b/feature_tests/js/api/MyStruct.mjs index 07edfd3e7..3938d18fc 100644 --- a/feature_tests/js/api/MyStruct.mjs +++ b/feature_tests/js/api/MyStruct.mjs @@ -62,6 +62,10 @@ export class MyStruct { this.#g = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("MyStruct's constructor takes an object of MyStruct's fields."); + } + if ("a" in struct_obj) { this.#a = struct_obj.a; } else { diff --git a/feature_tests/js/api/MyZst.mjs b/feature_tests/js/api/MyZst.mjs index 3ff4d0118..2f09ca954 100644 --- a/feature_tests/js/api/MyZst.mjs +++ b/feature_tests/js/api/MyZst.mjs @@ -4,6 +4,10 @@ import * as diplomatRuntime from "./diplomat-runtime.mjs"; export class MyZst { constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("MyZst's constructor takes an object of MyZst's fields."); + } + } } \ No newline at end of file diff --git a/feature_tests/js/api/NestedBorrowedFields.mjs b/feature_tests/js/api/NestedBorrowedFields.mjs index a00df4f4e..037b67da6 100644 --- a/feature_tests/js/api/NestedBorrowedFields.mjs +++ b/feature_tests/js/api/NestedBorrowedFields.mjs @@ -32,6 +32,10 @@ export class NestedBorrowedFields { this.#bounds2 = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("NestedBorrowedFields's constructor takes an object of NestedBorrowedFields's fields."); + } + if ("fields" in struct_obj) { this.#fields = struct_obj.fields; } else { diff --git a/feature_tests/js/api/OptionInputStruct.mjs b/feature_tests/js/api/OptionInputStruct.mjs index f0f51ddc8..946326858 100644 --- a/feature_tests/js/api/OptionInputStruct.mjs +++ b/feature_tests/js/api/OptionInputStruct.mjs @@ -29,6 +29,10 @@ export class OptionInputStruct { this.#c = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("OptionInputStruct's constructor takes an object of OptionInputStruct's fields."); + } + if ("a" in struct_obj) { this.#a = struct_obj.a; } else { diff --git a/feature_tests/js/api/OptionStruct.mjs b/feature_tests/js/api/OptionStruct.mjs index 8569105a3..ee688412c 100644 --- a/feature_tests/js/api/OptionStruct.mjs +++ b/feature_tests/js/api/OptionStruct.mjs @@ -30,6 +30,10 @@ export class OptionStruct { } constructor(struct_obj, internalConstructor) { + if (typeof struct_obj !== "object") { + throw new Error("OptionStruct's constructor takes an object of OptionStruct's fields."); + } + if (internalConstructor !== diplomatRuntime.internalConstructor) { throw new Error("OptionStruct is an out struct and can only be created internally."); } diff --git a/feature_tests/js/api/ScalarPairWithPadding.mjs b/feature_tests/js/api/ScalarPairWithPadding.mjs index c8c66237d..9f01612d7 100644 --- a/feature_tests/js/api/ScalarPairWithPadding.mjs +++ b/feature_tests/js/api/ScalarPairWithPadding.mjs @@ -23,6 +23,10 @@ export class ScalarPairWithPadding { this.#second = value; } constructor(struct_obj) { + if (typeof struct_obj !== "object") { + throw new Error("ScalarPairWithPadding's constructor takes an object of ScalarPairWithPadding's fields."); + } + if ("first" in struct_obj) { this.#first = struct_obj.first; } else { diff --git a/tool/templates/js/struct.js.jinja b/tool/templates/js/struct.js.jinja index 7d187baf4..73e4ced73 100644 --- a/tool/templates/js/struct.js.jinja +++ b/tool/templates/js/struct.js.jinja @@ -38,7 +38,7 @@ export class {{type_name}} { throw new Error("{{type_name}}'s constructor takes an object of {{type_name}}'s fields."); } - {%- if is_out %} + {%~ if is_out %} if (internalConstructor !== diplomatRuntime.internalConstructor) { throw new Error("{{type_name}} is an out struct and can only be created internally."); } From 9ff913bb04c78353d898a7da5bd8bf59dbf5a4dd Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 20:57:45 -0800 Subject: [PATCH 12/19] Call correct constructor if struct is empty --- feature_tests/js/api/MyStruct.mjs | 4 ++-- tool/src/js/converter.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/feature_tests/js/api/MyStruct.mjs b/feature_tests/js/api/MyStruct.mjs index 3938d18fc..6fa8c4970 100644 --- a/feature_tests/js/api/MyStruct.mjs +++ b/feature_tests/js/api/MyStruct.mjs @@ -196,7 +196,7 @@ export class MyStruct { try { if (result !== 1) { - const cause = MyZst._fromFFI(diplomatRuntime.internalConstructor); + const cause = new MyZst({}, diplomatRuntime.internalConstructor); throw new globalThis.Error('MyZst', { cause }); } @@ -210,7 +210,7 @@ export class MyStruct { try { if (result !== 1) { - const cause = MyZst._fromFFI(diplomatRuntime.internalConstructor); + const cause = new MyZst({}, diplomatRuntime.internalConstructor); throw new globalThis.Error('MyZst', { cause }); } diff --git a/tool/src/js/converter.rs b/tool/src/js/converter.rs index 3851177c9..90884f9de 100644 --- a/tool/src/js/converter.rs +++ b/tool/src/js/converter.rs @@ -205,13 +205,13 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> { let type_def = self.tcx.resolve_type(id); match type_def { hir::TypeDef::Struct(st) if st.fields.is_empty() => { - format!("{type_name}._fromFFI(diplomatRuntime.internalConstructor)").into() + format!("new {type_name}({{}}, diplomatRuntime.internalConstructor)").into() } hir::TypeDef::Struct(..) => { format!("{type_name}._fromFFI(diplomatRuntime.internalConstructor, {variable_name}{edges})").into() } hir::TypeDef::OutStruct(st) if st.fields.is_empty() => { - format!("{type_name}._fromFFI(diplomatRuntime.internalConstructor)").into() + format!("new {type_name}({{}}, diplomatRuntime.internalConstructor)").into() } hir::TypeDef::OutStruct(..) => { format!("{type_name}._fromFFI(diplomatRuntime.internalConstructor, {variable_name}{edges})").into() From 50f152ea424d74112bfbd2a73f2366f06ea93b6f Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 21:00:34 -0800 Subject: [PATCH 13/19] Add internalConstructor to _intoFFI --- example/js/lib/api/FixedDecimalFormatterOptions.mjs | 2 +- feature_tests/js/api/BigStructWithStuff.mjs | 2 +- feature_tests/js/api/BorrowedFields.mjs | 2 +- feature_tests/js/api/BorrowedFieldsReturning.mjs | 2 +- feature_tests/js/api/BorrowedFieldsWithBounds.mjs | 2 +- feature_tests/js/api/CyclicStructA.mjs | 2 +- feature_tests/js/api/CyclicStructB.mjs | 2 +- feature_tests/js/api/ErrorStruct.mjs | 2 +- feature_tests/js/api/ImportedStruct.mjs | 2 +- feature_tests/js/api/MyStruct.mjs | 2 +- feature_tests/js/api/NestedBorrowedFields.mjs | 2 +- feature_tests/js/api/OptionInputStruct.mjs | 2 +- feature_tests/js/api/OptionStruct.mjs | 2 +- feature_tests/js/api/ScalarPairWithPadding.mjs | 2 +- tool/templates/js/struct.js.jinja | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/example/js/lib/api/FixedDecimalFormatterOptions.mjs b/example/js/lib/api/FixedDecimalFormatterOptions.mjs index 941100f6e..e5399adf3 100644 --- a/example/js/lib/api/FixedDecimalFormatterOptions.mjs +++ b/example/js/lib/api/FixedDecimalFormatterOptions.mjs @@ -79,7 +79,7 @@ export class FixedDecimalFormatterOptions { const someOtherConfigDeref = (new Uint8Array(wasm.memory.buffer, ptr + 4, 1))[0] === 1; structObj.someOtherConfig = someOtherConfigDeref; - return new FixedDecimalFormatterOptions(structObj); + return new FixedDecimalFormatterOptions(structObj, internalConstructor); } static default_() { diff --git a/feature_tests/js/api/BigStructWithStuff.mjs b/feature_tests/js/api/BigStructWithStuff.mjs index 640381e16..1f5347cd9 100644 --- a/feature_tests/js/api/BigStructWithStuff.mjs +++ b/feature_tests/js/api/BigStructWithStuff.mjs @@ -128,7 +128,7 @@ export class BigStructWithStuff { const fifthDeref = (new Uint8Array(wasm.memory.buffer, ptr + 16, 1))[0]; structObj.fifth = fifthDeref; - return new BigStructWithStuff(structObj); + return new BigStructWithStuff(structObj, internalConstructor); } assertValue(extraVal) { diff --git a/feature_tests/js/api/BorrowedFields.mjs b/feature_tests/js/api/BorrowedFields.mjs index f6f0ea22d..c827cbfef 100644 --- a/feature_tests/js/api/BorrowedFields.mjs +++ b/feature_tests/js/api/BorrowedFields.mjs @@ -89,7 +89,7 @@ export class BorrowedFields { const cDeref = ptr + 16; structObj.c = new diplomatRuntime.DiplomatSliceStr(wasm, cDeref, "string8", aEdges); - return new BorrowedFields(structObj); + return new BorrowedFields(structObj, internalConstructor); } // Return all fields corresponding to lifetime `'a` diff --git a/feature_tests/js/api/BorrowedFieldsReturning.mjs b/feature_tests/js/api/BorrowedFieldsReturning.mjs index b32575596..48d50f8d1 100644 --- a/feature_tests/js/api/BorrowedFieldsReturning.mjs +++ b/feature_tests/js/api/BorrowedFieldsReturning.mjs @@ -54,7 +54,7 @@ export class BorrowedFieldsReturning { const bytesDeref = ptr; structObj.bytes = new diplomatRuntime.DiplomatSliceStr(wasm, bytesDeref, "string8", aEdges); - return new BorrowedFieldsReturning(structObj); + return new BorrowedFieldsReturning(structObj, internalConstructor); } // Return all fields corresponding to lifetime `'a` diff --git a/feature_tests/js/api/BorrowedFieldsWithBounds.mjs b/feature_tests/js/api/BorrowedFieldsWithBounds.mjs index bc4fa6a35..c03a7f150 100644 --- a/feature_tests/js/api/BorrowedFieldsWithBounds.mjs +++ b/feature_tests/js/api/BorrowedFieldsWithBounds.mjs @@ -91,7 +91,7 @@ export class BorrowedFieldsWithBounds { const fieldCDeref = ptr + 16; structObj.fieldC = new diplomatRuntime.DiplomatSliceStr(wasm, fieldCDeref, "string8", cEdges); - return new BorrowedFieldsWithBounds(structObj); + return new BorrowedFieldsWithBounds(structObj, internalConstructor); } // Return all fields corresponding to lifetime `'a` diff --git a/feature_tests/js/api/CyclicStructA.mjs b/feature_tests/js/api/CyclicStructA.mjs index ce4a0ae3e..7304303d2 100644 --- a/feature_tests/js/api/CyclicStructA.mjs +++ b/feature_tests/js/api/CyclicStructA.mjs @@ -57,7 +57,7 @@ export class CyclicStructA { const aDeref = ptr; structObj.a = CyclicStructB._fromFFI(diplomatRuntime.internalConstructor, aDeref); - return new CyclicStructA(structObj); + return new CyclicStructA(structObj, internalConstructor); } static getB() { diff --git a/feature_tests/js/api/CyclicStructB.mjs b/feature_tests/js/api/CyclicStructB.mjs index 7f4c0c3fe..715b7666a 100644 --- a/feature_tests/js/api/CyclicStructB.mjs +++ b/feature_tests/js/api/CyclicStructB.mjs @@ -57,7 +57,7 @@ export class CyclicStructB { const fieldDeref = (new Uint8Array(wasm.memory.buffer, ptr, 1))[0]; structObj.field = fieldDeref; - return new CyclicStructB(structObj); + return new CyclicStructB(structObj, internalConstructor); } static getA() { diff --git a/feature_tests/js/api/ErrorStruct.mjs b/feature_tests/js/api/ErrorStruct.mjs index 207b7fd77..fba33803f 100644 --- a/feature_tests/js/api/ErrorStruct.mjs +++ b/feature_tests/js/api/ErrorStruct.mjs @@ -73,6 +73,6 @@ export class ErrorStruct { const jDeref = (new Int32Array(wasm.memory.buffer, ptr + 4, 1))[0]; structObj.j = jDeref; - return new ErrorStruct(structObj); + return new ErrorStruct(structObj, internalConstructor); } } \ No newline at end of file diff --git a/feature_tests/js/api/ImportedStruct.mjs b/feature_tests/js/api/ImportedStruct.mjs index db12057b2..8ca699f95 100644 --- a/feature_tests/js/api/ImportedStruct.mjs +++ b/feature_tests/js/api/ImportedStruct.mjs @@ -79,6 +79,6 @@ export class ImportedStruct { const countDeref = (new Uint8Array(wasm.memory.buffer, ptr + 4, 1))[0]; structObj.count = countDeref; - return new ImportedStruct(structObj); + return new ImportedStruct(structObj, internalConstructor); } } \ No newline at end of file diff --git a/feature_tests/js/api/MyStruct.mjs b/feature_tests/js/api/MyStruct.mjs index 6fa8c4970..f5406025f 100644 --- a/feature_tests/js/api/MyStruct.mjs +++ b/feature_tests/js/api/MyStruct.mjs @@ -160,7 +160,7 @@ export class MyStruct { const gDeref = diplomatRuntime.enumDiscriminant(wasm, ptr + 24); structObj.g = new MyEnum(diplomatRuntime.internalConstructor, gDeref); - return new MyStruct(structObj); + return new MyStruct(structObj, internalConstructor); } static new_() { diff --git a/feature_tests/js/api/NestedBorrowedFields.mjs b/feature_tests/js/api/NestedBorrowedFields.mjs index 037b67da6..766517509 100644 --- a/feature_tests/js/api/NestedBorrowedFields.mjs +++ b/feature_tests/js/api/NestedBorrowedFields.mjs @@ -94,7 +94,7 @@ export class NestedBorrowedFields { const bounds2Deref = ptr + 48; structObj.bounds2 = BorrowedFieldsWithBounds._fromFFI(diplomatRuntime.internalConstructor, bounds2Deref, zEdges, zEdges, zEdges); - return new NestedBorrowedFields(structObj); + return new NestedBorrowedFields(structObj, internalConstructor); } // Return all fields corresponding to lifetime `'x` diff --git a/feature_tests/js/api/OptionInputStruct.mjs b/feature_tests/js/api/OptionInputStruct.mjs index 946326858..fed12da15 100644 --- a/feature_tests/js/api/OptionInputStruct.mjs +++ b/feature_tests/js/api/OptionInputStruct.mjs @@ -91,6 +91,6 @@ export class OptionInputStruct { const cDeref = ptr + 12; 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); + return new OptionInputStruct(structObj, internalConstructor); } } \ No newline at end of file diff --git a/feature_tests/js/api/OptionStruct.mjs b/feature_tests/js/api/OptionStruct.mjs index ee688412c..17bc28fa6 100644 --- a/feature_tests/js/api/OptionStruct.mjs +++ b/feature_tests/js/api/OptionStruct.mjs @@ -104,6 +104,6 @@ export class OptionStruct { const dDeref = diplomatRuntime.ptrRead(wasm, ptr + 12); structObj.d = dDeref === 0 ? null : new OptionOpaque(diplomatRuntime.internalConstructor, dDeref, []); - return new OptionStruct(structObj); + return new OptionStruct(structObj, internalConstructor); } } \ No newline at end of file diff --git a/feature_tests/js/api/ScalarPairWithPadding.mjs b/feature_tests/js/api/ScalarPairWithPadding.mjs index 9f01612d7..8ca5ac9d2 100644 --- a/feature_tests/js/api/ScalarPairWithPadding.mjs +++ b/feature_tests/js/api/ScalarPairWithPadding.mjs @@ -81,7 +81,7 @@ export class ScalarPairWithPadding { const secondDeref = (new Uint32Array(wasm.memory.buffer, ptr + 4, 1))[0]; structObj.second = secondDeref; - return new ScalarPairWithPadding(structObj); + return new ScalarPairWithPadding(structObj, internalConstructor); } assertValue() { diff --git a/tool/templates/js/struct.js.jinja b/tool/templates/js/struct.js.jinja index 73e4ced73..54e5fc39f 100644 --- a/tool/templates/js/struct.js.jinja +++ b/tool/templates/js/struct.js.jinja @@ -122,7 +122,7 @@ export class {{type_name}} { structObj.{{field.field_name}} = {{field.c_to_js}}; {%- endfor %} - return new {{type_name}}(structObj); + return new {{type_name}}(structObj, internalConstructor); } {%- for l in lifetimes.all_lifetimes() %} From c956cd9891b4f0b222df17493d11e23f2238ff64 Mon Sep 17 00:00:00 2001 From: Tyler K Date: Thu, 7 Nov 2024 12:04:22 -0800 Subject: [PATCH 14/19] Demo_gen fixes for structs --- tool/src/demo_gen/terminus.rs | 5 +++++ tool/templates/demo_gen/struct.js.jinja | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tool/src/demo_gen/terminus.rs b/tool/src/demo_gen/terminus.rs index c2cf9ed2e..0b1e464d3 100644 --- a/tool/src/demo_gen/terminus.rs +++ b/tool/src/demo_gen/terminus.rs @@ -446,8 +446,11 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> { #[template(path = "demo_gen/struct.js.jinja", escape = "none")] struct StructInfo { type_name: String, + fields: Vec, } + let mut fields = Vec::new(); + for field in st.fields.iter() { self.evaluate_param( &field.ty, @@ -455,10 +458,12 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> { &mut child, field.attrs.demo_attrs.clone(), ); + fields.push(field.name.to_string()); } child.method_js = StructInfo { type_name: type_name.clone(), + fields } .render() .unwrap(); diff --git a/tool/templates/demo_gen/struct.js.jinja b/tool/templates/demo_gen/struct.js.jinja index 8b7bef1b9..5e247156a 100644 --- a/tool/templates/demo_gen/struct.js.jinja +++ b/tool/templates/demo_gen/struct.js.jinja @@ -1,3 +1,7 @@ (function (...args) { - return new {{type_name}}(...args); + return new {{type_name}}({ + {% for field in fields %} + {{field}}: args[{{loop.index0}}] + {% endfor %} + }); }) \ No newline at end of file From f6da356ccdfe5bedcabd951d255552845e2814a6 Mon Sep 17 00:00:00 2001 From: Tyler K Date: Thu, 7 Nov 2024 12:09:57 -0800 Subject: [PATCH 15/19] Generation bug fix --- example/demo_gen/demo/FixedDecimalFormatter.mjs | 4 +++- tool/src/demo_gen/terminus.rs | 2 +- tool/templates/demo_gen/struct.js.jinja | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/example/demo_gen/demo/FixedDecimalFormatter.mjs b/example/demo_gen/demo/FixedDecimalFormatter.mjs index 312cca8fd..6411db57d 100644 --- a/example/demo_gen/demo/FixedDecimalFormatter.mjs +++ b/example/demo_gen/demo/FixedDecimalFormatter.mjs @@ -22,7 +22,9 @@ export function formatWrite(name, grouping_strategy, some_other_config, v) { ] ), (function (...args) { - return new FixedDecimalFormatterOptions(...args); + return new FixedDecimalFormatterOptions({ + groupingStrategy: args[0], + someOtherConfig: args[1]}); }).apply( null, [ diff --git a/tool/src/demo_gen/terminus.rs b/tool/src/demo_gen/terminus.rs index 0b1e464d3..d5ee7e8b2 100644 --- a/tool/src/demo_gen/terminus.rs +++ b/tool/src/demo_gen/terminus.rs @@ -458,7 +458,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> { &mut child, field.attrs.demo_attrs.clone(), ); - fields.push(field.name.to_string()); + fields.push(self.formatter.fmt_param_name(&field.name.to_string()).into()); } child.method_js = StructInfo { diff --git a/tool/templates/demo_gen/struct.js.jinja b/tool/templates/demo_gen/struct.js.jinja index 5e247156a..f1b9e00a0 100644 --- a/tool/templates/demo_gen/struct.js.jinja +++ b/tool/templates/demo_gen/struct.js.jinja @@ -1,7 +1,7 @@ (function (...args) { return new {{type_name}}({ - {% for field in fields %} - {{field}}: args[{{loop.index0}}] - {% endfor %} + {%- for field in fields %} + {{field}}: args[{{loop.index0}}]{% if !loop.last %},{% endif %} + {%- endfor -%} }); }) \ No newline at end of file From ac136fb2be73026b3a649001df4a6a986307450e Mon Sep 17 00:00:00 2001 From: Tyler K Date: Thu, 7 Nov 2024 12:10:44 -0800 Subject: [PATCH 16/19] JS camel case --- .../lib/api/FixedDecimalFormatterOptions.d.ts | 2 +- .../lib/api/FixedDecimalFormatterOptions.mjs | 12 +++---- feature_tests/js/api/BigStructWithStuff.d.ts | 2 +- feature_tests/js/api/BigStructWithStuff.mjs | 24 +++++++------- feature_tests/js/api/BorrowedFields.d.ts | 2 +- feature_tests/js/api/BorrowedFields.mjs | 16 +++++----- .../js/api/BorrowedFieldsReturning.d.ts | 2 +- .../js/api/BorrowedFieldsReturning.mjs | 8 ++--- .../js/api/BorrowedFieldsWithBounds.d.ts | 2 +- .../js/api/BorrowedFieldsWithBounds.mjs | 16 +++++----- feature_tests/js/api/CyclicStructA.d.ts | 2 +- feature_tests/js/api/CyclicStructA.mjs | 8 ++--- feature_tests/js/api/CyclicStructB.d.ts | 2 +- feature_tests/js/api/CyclicStructB.mjs | 8 ++--- feature_tests/js/api/ErrorStruct.d.ts | 2 +- feature_tests/js/api/ErrorStruct.mjs | 12 +++---- feature_tests/js/api/ImportedStruct.d.ts | 2 +- feature_tests/js/api/ImportedStruct.mjs | 12 +++---- feature_tests/js/api/MyStruct.d.ts | 2 +- feature_tests/js/api/MyStruct.mjs | 32 +++++++++---------- feature_tests/js/api/MyZst.d.ts | 2 +- feature_tests/js/api/MyZst.mjs | 4 +-- .../js/api/NestedBorrowedFields.d.ts | 2 +- feature_tests/js/api/NestedBorrowedFields.mjs | 16 +++++----- feature_tests/js/api/OptionInputStruct.d.ts | 2 +- feature_tests/js/api/OptionInputStruct.mjs | 16 +++++----- feature_tests/js/api/OptionStruct.mjs | 20 ++++++------ .../js/api/ScalarPairWithPadding.d.ts | 2 +- .../js/api/ScalarPairWithPadding.mjs | 12 +++---- tool/templates/js/struct.js.jinja | 8 ++--- 30 files changed, 126 insertions(+), 126 deletions(-) diff --git a/example/js/lib/api/FixedDecimalFormatterOptions.d.ts b/example/js/lib/api/FixedDecimalFormatterOptions.d.ts index f5fbdf2a3..0055dac38 100644 --- a/example/js/lib/api/FixedDecimalFormatterOptions.d.ts +++ b/example/js/lib/api/FixedDecimalFormatterOptions.d.ts @@ -14,7 +14,7 @@ export class FixedDecimalFormatterOptions { get someOtherConfig() : boolean; set someOtherConfig(value: boolean); - constructor(struct_obj : FixedDecimalFormatterOptions_Obj); + constructor(structObj : FixedDecimalFormatterOptions_Obj); static default_(): FixedDecimalFormatterOptions; } \ No newline at end of file diff --git a/example/js/lib/api/FixedDecimalFormatterOptions.mjs b/example/js/lib/api/FixedDecimalFormatterOptions.mjs index e5399adf3..623784e9e 100644 --- a/example/js/lib/api/FixedDecimalFormatterOptions.mjs +++ b/example/js/lib/api/FixedDecimalFormatterOptions.mjs @@ -20,19 +20,19 @@ export class FixedDecimalFormatterOptions { set someOtherConfig(value) { this.#someOtherConfig = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("FixedDecimalFormatterOptions's constructor takes an object of FixedDecimalFormatterOptions's fields."); } - if ("groupingStrategy" in struct_obj) { - this.#groupingStrategy = struct_obj.groupingStrategy; + if ("groupingStrategy" in structObj) { + this.#groupingStrategy = structObj.groupingStrategy; } else { throw new Error("Missing required field groupingStrategy."); } - if ("someOtherConfig" in struct_obj) { - this.#someOtherConfig = struct_obj.someOtherConfig; + if ("someOtherConfig" in structObj) { + this.#someOtherConfig = structObj.someOtherConfig; } else { throw new Error("Missing required field someOtherConfig."); } diff --git a/feature_tests/js/api/BigStructWithStuff.d.ts b/feature_tests/js/api/BigStructWithStuff.d.ts index 08d373a63..7f8b66ffd 100644 --- a/feature_tests/js/api/BigStructWithStuff.d.ts +++ b/feature_tests/js/api/BigStructWithStuff.d.ts @@ -29,7 +29,7 @@ export class BigStructWithStuff { get fifth() : number; set fifth(value: number); - constructor(struct_obj : BigStructWithStuff_Obj); + constructor(structObj : BigStructWithStuff_Obj); assertValue(extraVal: number): void; } \ No newline at end of file diff --git a/feature_tests/js/api/BigStructWithStuff.mjs b/feature_tests/js/api/BigStructWithStuff.mjs index 1f5347cd9..fc8b080dc 100644 --- a/feature_tests/js/api/BigStructWithStuff.mjs +++ b/feature_tests/js/api/BigStructWithStuff.mjs @@ -47,37 +47,37 @@ export class BigStructWithStuff { set fifth(value) { this.#fifth = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("BigStructWithStuff's constructor takes an object of BigStructWithStuff's fields."); } - if ("first" in struct_obj) { - this.#first = struct_obj.first; + if ("first" in structObj) { + this.#first = structObj.first; } else { throw new Error("Missing required field first."); } - if ("second" in struct_obj) { - this.#second = struct_obj.second; + if ("second" in structObj) { + this.#second = structObj.second; } else { throw new Error("Missing required field second."); } - if ("third" in struct_obj) { - this.#third = struct_obj.third; + if ("third" in structObj) { + this.#third = structObj.third; } else { throw new Error("Missing required field third."); } - if ("fourth" in struct_obj) { - this.#fourth = struct_obj.fourth; + if ("fourth" in structObj) { + this.#fourth = structObj.fourth; } else { throw new Error("Missing required field fourth."); } - if ("fifth" in struct_obj) { - this.#fifth = struct_obj.fifth; + if ("fifth" in structObj) { + this.#fifth = structObj.fifth; } else { throw new Error("Missing required field fifth."); } diff --git a/feature_tests/js/api/BorrowedFields.d.ts b/feature_tests/js/api/BorrowedFields.d.ts index 67c6a89f0..5856c2ea6 100644 --- a/feature_tests/js/api/BorrowedFields.d.ts +++ b/feature_tests/js/api/BorrowedFields.d.ts @@ -18,7 +18,7 @@ export class BorrowedFields { get c() : string; set c(value: string); - constructor(struct_obj : BorrowedFields_Obj); + constructor(structObj : BorrowedFields_Obj); static fromBarAndStrings(bar: Bar, dstr16: string, utf8Str: string): BorrowedFields; } \ No newline at end of file diff --git a/feature_tests/js/api/BorrowedFields.mjs b/feature_tests/js/api/BorrowedFields.mjs index c827cbfef..99dfcfffc 100644 --- a/feature_tests/js/api/BorrowedFields.mjs +++ b/feature_tests/js/api/BorrowedFields.mjs @@ -28,25 +28,25 @@ export class BorrowedFields { set c(value) { this.#c = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("BorrowedFields's constructor takes an object of BorrowedFields's fields."); } - if ("a" in struct_obj) { - this.#a = struct_obj.a; + if ("a" in structObj) { + this.#a = structObj.a; } else { throw new Error("Missing required field a."); } - if ("b" in struct_obj) { - this.#b = struct_obj.b; + if ("b" in structObj) { + this.#b = structObj.b; } else { throw new Error("Missing required field b."); } - if ("c" in struct_obj) { - this.#c = struct_obj.c; + if ("c" in structObj) { + this.#c = structObj.c; } else { throw new Error("Missing required field c."); } diff --git a/feature_tests/js/api/BorrowedFieldsReturning.d.ts b/feature_tests/js/api/BorrowedFieldsReturning.d.ts index 33a27c0b5..40f8aa9cf 100644 --- a/feature_tests/js/api/BorrowedFieldsReturning.d.ts +++ b/feature_tests/js/api/BorrowedFieldsReturning.d.ts @@ -9,5 +9,5 @@ export class BorrowedFieldsReturning { get bytes() : string; set bytes(value: string); - constructor(struct_obj : BorrowedFieldsReturning_Obj); + constructor(structObj : BorrowedFieldsReturning_Obj); } \ No newline at end of file diff --git a/feature_tests/js/api/BorrowedFieldsReturning.mjs b/feature_tests/js/api/BorrowedFieldsReturning.mjs index 48d50f8d1..76683025f 100644 --- a/feature_tests/js/api/BorrowedFieldsReturning.mjs +++ b/feature_tests/js/api/BorrowedFieldsReturning.mjs @@ -11,13 +11,13 @@ export class BorrowedFieldsReturning { set bytes(value) { this.#bytes = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("BorrowedFieldsReturning's constructor takes an object of BorrowedFieldsReturning's fields."); } - if ("bytes" in struct_obj) { - this.#bytes = struct_obj.bytes; + if ("bytes" in structObj) { + this.#bytes = structObj.bytes; } else { throw new Error("Missing required field bytes."); } diff --git a/feature_tests/js/api/BorrowedFieldsWithBounds.d.ts b/feature_tests/js/api/BorrowedFieldsWithBounds.d.ts index 6f88ee6fe..a448fd141 100644 --- a/feature_tests/js/api/BorrowedFieldsWithBounds.d.ts +++ b/feature_tests/js/api/BorrowedFieldsWithBounds.d.ts @@ -18,7 +18,7 @@ export class BorrowedFieldsWithBounds { get fieldC() : string; set fieldC(value: string); - constructor(struct_obj : BorrowedFieldsWithBounds_Obj); + constructor(structObj : BorrowedFieldsWithBounds_Obj); static fromFooAndStrings(foo: Foo, dstr16X: string, utf8StrZ: string): BorrowedFieldsWithBounds; } \ No newline at end of file diff --git a/feature_tests/js/api/BorrowedFieldsWithBounds.mjs b/feature_tests/js/api/BorrowedFieldsWithBounds.mjs index c03a7f150..851f20c7e 100644 --- a/feature_tests/js/api/BorrowedFieldsWithBounds.mjs +++ b/feature_tests/js/api/BorrowedFieldsWithBounds.mjs @@ -28,25 +28,25 @@ export class BorrowedFieldsWithBounds { set fieldC(value) { this.#fieldC = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("BorrowedFieldsWithBounds's constructor takes an object of BorrowedFieldsWithBounds's fields."); } - if ("fieldA" in struct_obj) { - this.#fieldA = struct_obj.fieldA; + if ("fieldA" in structObj) { + this.#fieldA = structObj.fieldA; } else { throw new Error("Missing required field fieldA."); } - if ("fieldB" in struct_obj) { - this.#fieldB = struct_obj.fieldB; + if ("fieldB" in structObj) { + this.#fieldB = structObj.fieldB; } else { throw new Error("Missing required field fieldB."); } - if ("fieldC" in struct_obj) { - this.#fieldC = struct_obj.fieldC; + if ("fieldC" in structObj) { + this.#fieldC = structObj.fieldC; } else { throw new Error("Missing required field fieldC."); } diff --git a/feature_tests/js/api/CyclicStructA.d.ts b/feature_tests/js/api/CyclicStructA.d.ts index 7e9070818..bb32e72cb 100644 --- a/feature_tests/js/api/CyclicStructA.d.ts +++ b/feature_tests/js/api/CyclicStructA.d.ts @@ -10,7 +10,7 @@ export class CyclicStructA { get a() : CyclicStructB; set a(value: CyclicStructB); - constructor(struct_obj : CyclicStructA_Obj); + constructor(structObj : CyclicStructA_Obj); static getB(): CyclicStructB; } \ No newline at end of file diff --git a/feature_tests/js/api/CyclicStructA.mjs b/feature_tests/js/api/CyclicStructA.mjs index 7304303d2..93fd1c41b 100644 --- a/feature_tests/js/api/CyclicStructA.mjs +++ b/feature_tests/js/api/CyclicStructA.mjs @@ -12,13 +12,13 @@ export class CyclicStructA { set a(value) { this.#a = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("CyclicStructA's constructor takes an object of CyclicStructA's fields."); } - if ("a" in struct_obj) { - this.#a = struct_obj.a; + if ("a" in structObj) { + this.#a = structObj.a; } else { throw new Error("Missing required field a."); } diff --git a/feature_tests/js/api/CyclicStructB.d.ts b/feature_tests/js/api/CyclicStructB.d.ts index 78a119544..658eb7636 100644 --- a/feature_tests/js/api/CyclicStructB.d.ts +++ b/feature_tests/js/api/CyclicStructB.d.ts @@ -10,7 +10,7 @@ export class CyclicStructB { get field() : number; set field(value: number); - constructor(struct_obj : CyclicStructB_Obj); + constructor(structObj : CyclicStructB_Obj); static getA(): CyclicStructA; } \ No newline at end of file diff --git a/feature_tests/js/api/CyclicStructB.mjs b/feature_tests/js/api/CyclicStructB.mjs index 715b7666a..53f4f3a50 100644 --- a/feature_tests/js/api/CyclicStructB.mjs +++ b/feature_tests/js/api/CyclicStructB.mjs @@ -12,13 +12,13 @@ export class CyclicStructB { set field(value) { this.#field = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("CyclicStructB's constructor takes an object of CyclicStructB's fields."); } - if ("field" in struct_obj) { - this.#field = struct_obj.field; + if ("field" in structObj) { + this.#field = structObj.field; } else { throw new Error("Missing required field field."); } diff --git a/feature_tests/js/api/ErrorStruct.d.ts b/feature_tests/js/api/ErrorStruct.d.ts index c60ec12f6..16b02b617 100644 --- a/feature_tests/js/api/ErrorStruct.d.ts +++ b/feature_tests/js/api/ErrorStruct.d.ts @@ -13,5 +13,5 @@ export class ErrorStruct { get j() : number; set j(value: number); - constructor(struct_obj : ErrorStruct_Obj); + constructor(structObj : ErrorStruct_Obj); } \ No newline at end of file diff --git a/feature_tests/js/api/ErrorStruct.mjs b/feature_tests/js/api/ErrorStruct.mjs index fba33803f..004ba1bbf 100644 --- a/feature_tests/js/api/ErrorStruct.mjs +++ b/feature_tests/js/api/ErrorStruct.mjs @@ -19,19 +19,19 @@ export class ErrorStruct { set j(value) { this.#j = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("ErrorStruct's constructor takes an object of ErrorStruct's fields."); } - if ("i" in struct_obj) { - this.#i = struct_obj.i; + if ("i" in structObj) { + this.#i = structObj.i; } else { throw new Error("Missing required field i."); } - if ("j" in struct_obj) { - this.#j = struct_obj.j; + if ("j" in structObj) { + this.#j = structObj.j; } else { throw new Error("Missing required field j."); } diff --git a/feature_tests/js/api/ImportedStruct.d.ts b/feature_tests/js/api/ImportedStruct.d.ts index 4166d6e6e..73b223e28 100644 --- a/feature_tests/js/api/ImportedStruct.d.ts +++ b/feature_tests/js/api/ImportedStruct.d.ts @@ -14,5 +14,5 @@ export class ImportedStruct { get count() : number; set count(value: number); - constructor(struct_obj : ImportedStruct_Obj); + constructor(structObj : ImportedStruct_Obj); } \ No newline at end of file diff --git a/feature_tests/js/api/ImportedStruct.mjs b/feature_tests/js/api/ImportedStruct.mjs index 8ca699f95..185847a85 100644 --- a/feature_tests/js/api/ImportedStruct.mjs +++ b/feature_tests/js/api/ImportedStruct.mjs @@ -20,19 +20,19 @@ export class ImportedStruct { set count(value) { this.#count = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("ImportedStruct's constructor takes an object of ImportedStruct's fields."); } - if ("foo" in struct_obj) { - this.#foo = struct_obj.foo; + if ("foo" in structObj) { + this.#foo = structObj.foo; } else { throw new Error("Missing required field foo."); } - if ("count" in struct_obj) { - this.#count = struct_obj.count; + if ("count" in structObj) { + this.#count = structObj.count; } else { throw new Error("Missing required field count."); } diff --git a/feature_tests/js/api/MyStruct.d.ts b/feature_tests/js/api/MyStruct.d.ts index 1e09a4182..2440c8923 100644 --- a/feature_tests/js/api/MyStruct.d.ts +++ b/feature_tests/js/api/MyStruct.d.ts @@ -35,7 +35,7 @@ export class MyStruct { get g() : MyEnum; set g(value: MyEnum); - constructor(struct_obj : MyStruct_Obj); + constructor(structObj : MyStruct_Obj); static new_(): MyStruct; diff --git a/feature_tests/js/api/MyStruct.mjs b/feature_tests/js/api/MyStruct.mjs index f5406025f..da25317e7 100644 --- a/feature_tests/js/api/MyStruct.mjs +++ b/feature_tests/js/api/MyStruct.mjs @@ -61,49 +61,49 @@ export class MyStruct { set g(value) { this.#g = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("MyStruct's constructor takes an object of MyStruct's fields."); } - if ("a" in struct_obj) { - this.#a = struct_obj.a; + if ("a" in structObj) { + this.#a = structObj.a; } else { throw new Error("Missing required field a."); } - if ("b" in struct_obj) { - this.#b = struct_obj.b; + if ("b" in structObj) { + this.#b = structObj.b; } else { throw new Error("Missing required field b."); } - if ("c" in struct_obj) { - this.#c = struct_obj.c; + if ("c" in structObj) { + this.#c = structObj.c; } else { throw new Error("Missing required field c."); } - if ("d" in struct_obj) { - this.#d = struct_obj.d; + if ("d" in structObj) { + this.#d = structObj.d; } else { throw new Error("Missing required field d."); } - if ("e" in struct_obj) { - this.#e = struct_obj.e; + if ("e" in structObj) { + this.#e = structObj.e; } else { throw new Error("Missing required field e."); } - if ("f" in struct_obj) { - this.#f = struct_obj.f; + if ("f" in structObj) { + this.#f = structObj.f; } else { throw new Error("Missing required field f."); } - if ("g" in struct_obj) { - this.#g = struct_obj.g; + if ("g" in structObj) { + this.#g = structObj.g; } else { throw new Error("Missing required field g."); } diff --git a/feature_tests/js/api/MyZst.d.ts b/feature_tests/js/api/MyZst.d.ts index b687465bc..3e98c26f9 100644 --- a/feature_tests/js/api/MyZst.d.ts +++ b/feature_tests/js/api/MyZst.d.ts @@ -5,5 +5,5 @@ type MyZst_Obj = { }; export class MyZst { - constructor(struct_obj : MyZst_Obj); + constructor(structObj : MyZst_Obj); } \ No newline at end of file diff --git a/feature_tests/js/api/MyZst.mjs b/feature_tests/js/api/MyZst.mjs index 2f09ca954..6caca21a0 100644 --- a/feature_tests/js/api/MyZst.mjs +++ b/feature_tests/js/api/MyZst.mjs @@ -3,8 +3,8 @@ import wasm from "./diplomat-wasm.mjs"; import * as diplomatRuntime from "./diplomat-runtime.mjs"; export class MyZst { - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("MyZst's constructor takes an object of MyZst's fields."); } diff --git a/feature_tests/js/api/NestedBorrowedFields.d.ts b/feature_tests/js/api/NestedBorrowedFields.d.ts index cd8882014..0a2746ad1 100644 --- a/feature_tests/js/api/NestedBorrowedFields.d.ts +++ b/feature_tests/js/api/NestedBorrowedFields.d.ts @@ -21,7 +21,7 @@ export class NestedBorrowedFields { get bounds2() : BorrowedFieldsWithBounds; set bounds2(value: BorrowedFieldsWithBounds); - constructor(struct_obj : NestedBorrowedFields_Obj); + constructor(structObj : NestedBorrowedFields_Obj); static fromBarAndFooAndStrings(bar: Bar, foo: Foo, dstr16X: string, dstr16Z: string, utf8StrY: string, utf8StrZ: string): NestedBorrowedFields; } \ No newline at end of file diff --git a/feature_tests/js/api/NestedBorrowedFields.mjs b/feature_tests/js/api/NestedBorrowedFields.mjs index 766517509..ceb6d3a80 100644 --- a/feature_tests/js/api/NestedBorrowedFields.mjs +++ b/feature_tests/js/api/NestedBorrowedFields.mjs @@ -31,25 +31,25 @@ export class NestedBorrowedFields { set bounds2(value) { this.#bounds2 = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("NestedBorrowedFields's constructor takes an object of NestedBorrowedFields's fields."); } - if ("fields" in struct_obj) { - this.#fields = struct_obj.fields; + if ("fields" in structObj) { + this.#fields = structObj.fields; } else { throw new Error("Missing required field fields."); } - if ("bounds" in struct_obj) { - this.#bounds = struct_obj.bounds; + if ("bounds" in structObj) { + this.#bounds = structObj.bounds; } else { throw new Error("Missing required field bounds."); } - if ("bounds2" in struct_obj) { - this.#bounds2 = struct_obj.bounds2; + if ("bounds2" in structObj) { + this.#bounds2 = structObj.bounds2; } else { throw new Error("Missing required field bounds2."); } diff --git a/feature_tests/js/api/OptionInputStruct.d.ts b/feature_tests/js/api/OptionInputStruct.d.ts index 0518d3e3d..04c9d834d 100644 --- a/feature_tests/js/api/OptionInputStruct.d.ts +++ b/feature_tests/js/api/OptionInputStruct.d.ts @@ -18,5 +18,5 @@ export class OptionInputStruct { get c() : OptionEnum | null; set c(value: OptionEnum | null); - constructor(struct_obj : OptionInputStruct_Obj); + constructor(structObj : OptionInputStruct_Obj); } \ No newline at end of file diff --git a/feature_tests/js/api/OptionInputStruct.mjs b/feature_tests/js/api/OptionInputStruct.mjs index fed12da15..14da5eb00 100644 --- a/feature_tests/js/api/OptionInputStruct.mjs +++ b/feature_tests/js/api/OptionInputStruct.mjs @@ -28,25 +28,25 @@ export class OptionInputStruct { set c(value) { this.#c = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("OptionInputStruct's constructor takes an object of OptionInputStruct's fields."); } - if ("a" in struct_obj) { - this.#a = struct_obj.a; + if ("a" in structObj) { + this.#a = structObj.a; } else { this.#a = null; } - if ("b" in struct_obj) { - this.#b = struct_obj.b; + if ("b" in structObj) { + this.#b = structObj.b; } else { this.#b = null; } - if ("c" in struct_obj) { - this.#c = struct_obj.c; + if ("c" in structObj) { + this.#c = structObj.c; } else { this.#c = null; } diff --git a/feature_tests/js/api/OptionStruct.mjs b/feature_tests/js/api/OptionStruct.mjs index 17bc28fa6..3df863c6a 100644 --- a/feature_tests/js/api/OptionStruct.mjs +++ b/feature_tests/js/api/OptionStruct.mjs @@ -29,34 +29,34 @@ export class OptionStruct { return this.#d; } - constructor(struct_obj, internalConstructor) { - if (typeof struct_obj !== "object") { + constructor(structObj, internalConstructor) { + if (typeof structObj !== "object") { throw new Error("OptionStruct's constructor takes an object of OptionStruct's fields."); } if (internalConstructor !== diplomatRuntime.internalConstructor) { throw new Error("OptionStruct is an out struct and can only be created internally."); } - if ("a" in struct_obj) { - this.#a = struct_obj.a; + if ("a" in structObj) { + this.#a = structObj.a; } else { throw new Error("Missing required field a."); } - if ("b" in struct_obj) { - this.#b = struct_obj.b; + if ("b" in structObj) { + this.#b = structObj.b; } else { throw new Error("Missing required field b."); } - if ("c" in struct_obj) { - this.#c = struct_obj.c; + if ("c" in structObj) { + this.#c = structObj.c; } else { throw new Error("Missing required field c."); } - if ("d" in struct_obj) { - this.#d = struct_obj.d; + if ("d" in structObj) { + this.#d = structObj.d; } else { throw new Error("Missing required field d."); } diff --git a/feature_tests/js/api/ScalarPairWithPadding.d.ts b/feature_tests/js/api/ScalarPairWithPadding.d.ts index 1262f2cfb..d7fa935b8 100644 --- a/feature_tests/js/api/ScalarPairWithPadding.d.ts +++ b/feature_tests/js/api/ScalarPairWithPadding.d.ts @@ -16,7 +16,7 @@ export class ScalarPairWithPadding { get second() : number; set second(value: number); - constructor(struct_obj : ScalarPairWithPadding_Obj); + constructor(structObj : ScalarPairWithPadding_Obj); assertValue(): void; } \ No newline at end of file diff --git a/feature_tests/js/api/ScalarPairWithPadding.mjs b/feature_tests/js/api/ScalarPairWithPadding.mjs index 8ca5ac9d2..a3302e717 100644 --- a/feature_tests/js/api/ScalarPairWithPadding.mjs +++ b/feature_tests/js/api/ScalarPairWithPadding.mjs @@ -22,19 +22,19 @@ export class ScalarPairWithPadding { set second(value) { this.#second = value; } - constructor(struct_obj) { - if (typeof struct_obj !== "object") { + constructor(structObj) { + if (typeof structObj !== "object") { throw new Error("ScalarPairWithPadding's constructor takes an object of ScalarPairWithPadding's fields."); } - if ("first" in struct_obj) { - this.#first = struct_obj.first; + if ("first" in structObj) { + this.#first = structObj.first; } else { throw new Error("Missing required field first."); } - if ("second" in struct_obj) { - this.#second = struct_obj.second; + if ("second" in structObj) { + this.#second = structObj.second; } else { throw new Error("Missing required field second."); } diff --git a/tool/templates/js/struct.js.jinja b/tool/templates/js/struct.js.jinja index 54e5fc39f..636225de5 100644 --- a/tool/templates/js/struct.js.jinja +++ b/tool/templates/js/struct.js.jinja @@ -30,11 +30,11 @@ export class {{type_name}} { {%- endfor %} {%- if !(typescript && is_out) %} - constructor(struct_obj + constructor(structObj {%- if typescript %} : {{type_name}}_Obj {%- endif -%} {%- if is_out && !typescript %}, internalConstructor{% endif -%} ) {%- if typescript %};{% else %} { - if (typeof struct_obj !== "object") { + if (typeof structObj !== "object") { throw new Error("{{type_name}}'s constructor takes an object of {{type_name}}'s fields."); } @@ -45,8 +45,8 @@ export class {{type_name}} { {%- endif -%} {%- for field in fields %} - if ("{{field.field_name}}" in struct_obj) { - this.#{{ field.field_name }} = struct_obj.{{field.field_name}}; + if ("{{field.field_name}}" in structObj) { + this.#{{ field.field_name }} = structObj.{{field.field_name}}; } else { {%- if field.is_optional %} this.#{{ field.field_name }} = null; From 4d814df100bf6088eae99ffa2c66b88abac0ebbe Mon Sep 17 00:00:00 2001 From: Tyler K Date: Thu, 7 Nov 2024 12:29:26 -0800 Subject: [PATCH 17/19] Formatting --- tool/src/demo_gen/terminus.rs | 8 ++++++-- tool/src/js/gen.rs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tool/src/demo_gen/terminus.rs b/tool/src/demo_gen/terminus.rs index d5ee7e8b2..738db7331 100644 --- a/tool/src/demo_gen/terminus.rs +++ b/tool/src/demo_gen/terminus.rs @@ -458,12 +458,16 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> { &mut child, field.attrs.demo_attrs.clone(), ); - fields.push(self.formatter.fmt_param_name(&field.name.to_string()).into()); + fields.push( + self.formatter + .fmt_param_name(&field.name.to_string()) + .into(), + ); } child.method_js = StructInfo { type_name: type_name.clone(), - fields + fields, } .render() .unwrap(); diff --git a/tool/src/js/gen.rs b/tool/src/js/gen.rs index 520bca715..9bf96a5a5 100644 --- a/tool/src/js/gen.rs +++ b/tool/src/js/gen.rs @@ -605,7 +605,7 @@ pub(super) struct FieldInfo<'info, P: hir::TyPosition> { maybe_struct_borrow_info: Option>, /// Used in the constructor() function to determine whether or not this field is required for construction. - is_optional : bool, + is_optional: bool, } // Helpers used in templates (Askama has restrictions on Rust syntax) From eb6bc9d4af4dc4820034c21df5de7aa890f5eab6 Mon Sep 17 00:00:00 2001 From: Tyler K Date: Thu, 7 Nov 2024 12:55:57 -0800 Subject: [PATCH 18/19] Clippy --- tool/src/demo_gen/terminus.rs | 2 +- tool/src/js/gen.rs | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/tool/src/demo_gen/terminus.rs b/tool/src/demo_gen/terminus.rs index 738db7331..d74cc2b75 100644 --- a/tool/src/demo_gen/terminus.rs +++ b/tool/src/demo_gen/terminus.rs @@ -460,7 +460,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> { ); fields.push( self.formatter - .fmt_param_name(&field.name.to_string()) + .fmt_param_name(field.name.as_ref()) .into(), ); } diff --git a/tool/src/js/gen.rs b/tool/src/js/gen.rs index 9bf96a5a5..ae3bb1001 100644 --- a/tool/src/js/gen.rs +++ b/tool/src/js/gen.rs @@ -181,10 +181,7 @@ impl<'ctx, 'tcx> TyGenContext<'ctx, 'tcx> { let js_type_name = self.gen_js_type_str(&field.ty); - let is_option = match &field.ty { - hir::Type::DiplomatOption(..) => true, - _ => false - }; + let is_option = matches!(&field.ty, hir::Type::DiplomatOption(..)); let c_to_js_deref = self.gen_c_to_js_deref_for_type(&field.ty, "ptr".into(), struct_field_info.fields[i].offset); From 9edb56145c809627d8b87fa346507e14c6c0cfc3 Mon Sep 17 00:00:00 2001 From: Tyler K Date: Thu, 7 Nov 2024 12:56:36 -0800 Subject: [PATCH 19/19] Formatting --- tool/src/demo_gen/terminus.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tool/src/demo_gen/terminus.rs b/tool/src/demo_gen/terminus.rs index d74cc2b75..68310c978 100644 --- a/tool/src/demo_gen/terminus.rs +++ b/tool/src/demo_gen/terminus.rs @@ -458,11 +458,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> { &mut child, field.attrs.demo_attrs.clone(), ); - fields.push( - self.formatter - .fmt_param_name(field.name.as_ref()) - .into(), - ); + fields.push(self.formatter.fmt_param_name(field.name.as_ref()).into()); } child.method_js = StructInfo {