From d579a4bfe6df8eccadc1c4f4778b5ce6d730d83d Mon Sep 17 00:00:00 2001 From: Tyler K Date: Wed, 6 Nov 2024 13:02:59 -0800 Subject: [PATCH] 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() %}