diff --git a/example/demo_gen/demo/FixedDecimal.mjs b/example/demo_gen/demo/FixedDecimal.mjs index 49faa7f24..428f1d642 100644 --- a/example/demo_gen/demo/FixedDecimal.mjs +++ b/example/demo_gen/demo/FixedDecimal.mjs @@ -1,13 +1,12 @@ import { FixedDecimal } from "../../js/lib/api/index.mjs" -export function toString() { - var terminusArgs = arguments; +export function toString(v) { return (function (...args) { return args[0].toString(...args.slice(1)) }).apply( null, [ FixedDecimal.new_.apply( null, [ - terminusArgs[0] + v ] ) ] diff --git a/example/demo_gen/demo/FixedDecimalFormatter.mjs b/example/demo_gen/demo/FixedDecimalFormatter.mjs index 7e53e38f3..312cca8fd 100644 --- a/example/demo_gen/demo/FixedDecimalFormatter.mjs +++ b/example/demo_gen/demo/FixedDecimalFormatter.mjs @@ -3,8 +3,7 @@ import { FixedDecimal } from "../../js/lib/api/index.mjs" import { FixedDecimalFormatter } from "../../js/lib/api/index.mjs" import { FixedDecimalFormatterOptions } from "../../js/lib/api/index.mjs" import { Locale } from "../../js/lib/api/index.mjs" -export function formatWrite() { - var terminusArgs = arguments; +export function formatWrite(name, grouping_strategy, some_other_config, v) { return (function (...args) { return args[0].formatWrite(...args.slice(1)) }).apply( null, [ @@ -14,7 +13,7 @@ export function formatWrite() { Locale.new_.apply( null, [ - terminusArgs[0] + name ] ), DataProvider.newStatic.apply( @@ -27,8 +26,8 @@ export function formatWrite() { }).apply( null, [ - terminusArgs[1], - terminusArgs[2] + grouping_strategy, + some_other_config ] ) ] @@ -36,7 +35,7 @@ export function formatWrite() { FixedDecimal.new_.apply( null, [ - terminusArgs[3] + v ] ) ] diff --git a/feature_tests/demo_gen/demo/Float64Vec.mjs b/feature_tests/demo_gen/demo/Float64Vec.mjs index 7d78f0ff3..f321848ce 100644 --- a/feature_tests/demo_gen/demo/Float64Vec.mjs +++ b/feature_tests/demo_gen/demo/Float64Vec.mjs @@ -1,13 +1,12 @@ import { Float64Vec } from "../../js/api/index.mjs" -export function toString() { - var terminusArgs = arguments; +export function toString(v) { return (function (...args) { return args[0].toString(...args.slice(1)) }).apply( null, [ Float64Vec.newFromOwned.apply( null, [ - terminusArgs[0] + v ] ) ] diff --git a/feature_tests/demo_gen/demo/MyString.mjs b/feature_tests/demo_gen/demo/MyString.mjs index 6e1934cf1..efba98356 100644 --- a/feature_tests/demo_gen/demo/MyString.mjs +++ b/feature_tests/demo_gen/demo/MyString.mjs @@ -1,24 +1,22 @@ import { MyString } from "../../js/api/index.mjs" -export function getStr() { - var terminusArgs = arguments; +export function getStr(v) { return (function (...args) { return args[0].getStr }).apply( null, [ MyString.new_.apply( null, [ - terminusArgs[0] + v ] ) ] ); } -export function stringTransform() { - var terminusArgs = arguments; +export function stringTransform(foo) { return MyString.stringTransform.apply( null, [ - terminusArgs[0] + foo ] ); } diff --git a/feature_tests/demo_gen/demo/Opaque.mjs b/feature_tests/demo_gen/demo/Opaque.mjs index 885269b7b..626b27275 100644 --- a/feature_tests/demo_gen/demo/Opaque.mjs +++ b/feature_tests/demo_gen/demo/Opaque.mjs @@ -1,6 +1,5 @@ import { Opaque } from "../../js/api/index.mjs" export function getDebugStr() { - var terminusArgs = arguments; return (function (...args) { return args[0].getDebugStr(...args.slice(1)) }).apply( null, [ diff --git a/feature_tests/demo_gen/demo/OptionString.mjs b/feature_tests/demo_gen/demo/OptionString.mjs index 56828791e..d4401ca8b 100644 --- a/feature_tests/demo_gen/demo/OptionString.mjs +++ b/feature_tests/demo_gen/demo/OptionString.mjs @@ -1,13 +1,12 @@ import { OptionString } from "../../js/api/index.mjs" -export function write() { - var terminusArgs = arguments; +export function write(diplomatStr) { return (function (...args) { return args[0].write(...args.slice(1)) }).apply( null, [ OptionString.new_.apply( null, [ - terminusArgs[0] + diplomatStr ] ) ] diff --git a/feature_tests/demo_gen/demo/Utf16Wrap.mjs b/feature_tests/demo_gen/demo/Utf16Wrap.mjs index 5c58b51ca..0ba39fba7 100644 --- a/feature_tests/demo_gen/demo/Utf16Wrap.mjs +++ b/feature_tests/demo_gen/demo/Utf16Wrap.mjs @@ -1,13 +1,12 @@ import { Utf16Wrap } from "../../js/api/index.mjs" -export function getDebugStr() { - var terminusArgs = arguments; +export function getDebugStr(input) { return (function (...args) { return args[0].getDebugStr(...args.slice(1)) }).apply( null, [ Utf16Wrap.fromUtf16.apply( null, [ - terminusArgs[0] + input ] ) ] diff --git a/tool/src/demo_gen/mod.rs b/tool/src/demo_gen/mod.rs index 9bb72e263..2f21dc40e 100644 --- a/tool/src/demo_gen/mod.rs +++ b/tool/src/demo_gen/mod.rs @@ -3,7 +3,10 @@ //! Designed to work in conjunction with the JS backend. //! //! See docs/demo_gen.md for more. -use std::{collections::BTreeSet, fmt::Write}; +use std::{ + collections::{BTreeSet, HashMap}, + fmt::Write, +}; use askama::{self, Template}; use diplomat_core::hir::{BackendAttrSupport, TypeContext}; @@ -201,6 +204,8 @@ pub(crate) fn run<'tcx>( imports: BTreeSet::new(), }, + out_param_collision: HashMap::new(), + relative_import_path: import_path.clone(), module_name: module_name.clone(), }; diff --git a/tool/src/demo_gen/terminus.rs b/tool/src/demo_gen/terminus.rs index 5227f3eb5..c2cf9ed2e 100644 --- a/tool/src/demo_gen/terminus.rs +++ b/tool/src/demo_gen/terminus.rs @@ -1,4 +1,4 @@ -use std::collections::BTreeSet; +use std::collections::{BTreeSet, HashMap}; use diplomat_core::hir::{ self, DemoInfo, Method, OpaqueDef, StructDef, StructPath, TyPosition, Type, TypeContext, @@ -54,6 +54,9 @@ pub(super) struct RenderTerminusContext<'ctx, 'tcx> { pub errors: &'ctx ErrorStore<'tcx, String>, pub terminus_info: TerminusInfo, + /// To avoid similar parameter names while we're collecting [`OutParam`]s. + pub out_param_collision: HashMap, + pub relative_import_path: String, pub module_name: String, } @@ -236,8 +239,18 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> { } }; + let (p, n) = if self.out_param_collision.contains_key(¶m_name) { + let n = self.out_param_collision.get(¶m_name).unwrap(); + + (format!("{param_name}_{n}"), n + 1) + } else { + (param_name.clone(), 1) + }; + + self.out_param_collision.insert(param_name, n); + let out_param = OutParam { - param_name, + param_name: p.clone(), label, type_name: type_name.clone(), type_use, @@ -246,10 +259,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> { self.terminus_info.out_params.push(out_param); - let param_info = ParamInfo { - // Grab arguments without having to name them - js: format!("terminusArgs[{}]", self.terminus_info.out_params.len() - 1), - }; + let param_info = ParamInfo { js: p }; node.params.push(param_info); } diff --git a/tool/templates/demo_gen/terminus.js.jinja b/tool/templates/demo_gen/terminus.js.jinja index 174a1b5e5..1a8f638b3 100644 --- a/tool/templates/demo_gen/terminus.js.jinja +++ b/tool/templates/demo_gen/terminus.js.jinja @@ -1,9 +1,8 @@ export function {{function_name}}( - {%- if typescript %}{%- for param in out_params -%} + {%- for param in out_params -%} {{param.param_name}}{% if typescript %}: {{param.type_name}}{% endif %}{% if !loop.last %}, {% endif %} - {%- endfor -%}{% endif -%} + {%- endfor -%} ){% if typescript %};{% else %} { - var terminusArgs = arguments; return {{node_call_stack|indent(4)}}; } {%- endif %} \ No newline at end of file