Skip to content

Commit

Permalink
Rename functions if constructor is overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
ambiguousname committed Dec 27, 2024
1 parent 96acd0a commit fbb2f70
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
7 changes: 3 additions & 4 deletions tool/templates/js/enum.js.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
static getAllEntries() {
return {{type_name}}.#values.entries();
}

{# TODO: I think it's possible to allow for constructors,
but we just need to check if a constructor will be generated. -#}
constructor(value) {

{# TODO: Add "fromValue" function. #}
{% if overrides_constructor -%} #internalConstructor {%- else -%} constructor {%- endif -%}(value) {
if (arguments.length > 1 && arguments[0] === diplomatRuntime.internalConstructor) {
// We pass in two internalConstructor arguments to create *new*
// instances of this type, otherwise the enums are treated as singletons.
Expand Down
1 change: 1 addition & 0 deletions tool/templates/js/js_class.js.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/** {{doc_str}}
*/
{% endif -%}
{%- let overrides_constructor = methods.special_methods.constructor.is_some() -%}

{% block header_info %}{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion tool/templates/js/opaque.js.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const {{type_name}}_box_destroy_registry = new FinalizationRegistry((ptr) => {
#{{lifetimes.fmt_lifetime(lifetime)}}Edge = [];
{%- endfor %}

constructor(symbol, ptr, selfEdge {%- for lifetime in lifetimes.all_lifetimes() %}, {{lifetimes.fmt_lifetime(lifetime)}}Edge{% endfor %}) {
{% if overrides_constructor -%} #internalConstructor {%- else -%} constructor {%- endif -%}(symbol, ptr, selfEdge {%- for lifetime in lifetimes.all_lifetimes() %}, {{lifetimes.fmt_lifetime(lifetime)}}Edge{% endfor %}) {
if (symbol !== diplomatRuntime.internalConstructor) {
console.error("{{type_name}} is an Opaque type. You cannot call its constructor.");
return;
Expand Down
4 changes: 3 additions & 1 deletion tool/templates/js/struct.js.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ type {{type_name}}_obj = {
{%- endif -%}
{%- endfor %}

{# TODO: Add "fromFields" method #}

{%- if !(typescript && is_out) %}
constructor(structObj
{% if overrides_constructor -%} #internalConstructor {%- else -%} constructor {%- endif -%} (structObj
{%- if typescript %} : {{type_name}}_obj {%- endif -%}
{%- if is_out && !typescript %}, internalConstructor{% endif -%}
) {%- if typescript %};{% else %} {
Expand Down

0 comments on commit fbb2f70

Please sign in to comment.