I was trying to generate some of my code into js, where I use varargs in my code. It was fine when I just use nim.
And an example of the code might be:
proc demoVarargs(xs: varargs[string]): string {.exportc.} =
xs.join("&&&")
proc callingDemoVarargs(): string {.exportc.} =
demoVarargs("a", "b")
the code generated will be fine. When demoVarargs is called, an array is passed so it's correct.
function demoVarargs(xs_13885267) { // <---- this one
var result_13885268 = [];
result_13885268 = nimCopy(null, nsuJoinSep(xs_13885267, makeNimstrLit("&&&")), NTI1188013);
return result_13885268;
}
function callingDemoVarargs() {
var result_13885284 = [];
result_13885284 = nimCopy(null, demoVarargs([makeNimstrLit("a"), makeNimstrLit("b")]), NTI1188013);
return result_13885284;
}
The thing is I want to use demoVarargs(a, b, c) directly rather than using an array(too many places...). However I can't tell if any function f if f is using varargs.
I have a solution on js side. Old days when arguments spreading was not ready, we usually access arguments directly in function body to have spreading(or varargs on the context of Nim).
There are quite some varargs in the code I want to compile, so I can't just edit all them manually. Is there any trick I can use to customize code generation?
import strutils
proc demoVarargs(xs: string): string {.exportc, varargs.} =
xs.join("&&&")
proc callingDemoVarargs(): string {.exportc.} =
demoVarargs("a", "b")
function demoVarargs(xs_385875971) {
var result_385875972 = [];
result_385875972 = nimCopy(null, join_385875974(xs_385875971, makeNimstrLit("&&&")), NTI33554442)
return result_385875972;
}
function callingDemoVarargs() {
var result_385876037 = [];
result_385876037 = nimCopy(null, demoVarargs(makeNimstrLit("a"), "b"), NTI33554442);
return result_385876037;
}
it's counterintuitive but... it did passed the compilation.
I still have questions since the compiled js code does not work correctly. this {.varargs.} seems a little different from what I expected for arguments spreading... https://nim-lang.org/docs/manual.html#foreign-function-interface-varargs-pragma .
and taking a deeper look at the generated function, I see nothing related to arguments handling.
function demoVarargs(xs_385875971) {
var result_385875972 = [];
result_385875972 = nimCopy(null, join_385875974(xs_385875971, makeNimstrLit("&&&")), NTI33554442)
return result_385875972;
}
And the function call demoVarargs(makeNimstrLit("a"), "b") also looked erroneous for "b" not turned in to a literal and not accessed by the function above.