Skip to content

Commit

Permalink
Updating generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ambiguousname committed Nov 6, 2024
1 parent fddcc03 commit d579a4b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 4 additions & 4 deletions tool/src/js/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}"),
}
Expand Down
12 changes: 9 additions & 3 deletions tool/templates/js/struct.js.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
}
Expand Down Expand Up @@ -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() %}
Expand Down

0 comments on commit d579a4b

Please sign in to comment.