Skip to content

Instantly share code, notes, and snippets.

@geotre
Created September 24, 2022 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geotre/227f7be1621b254c4266a54d3446b0f4 to your computer and use it in GitHub Desktop.
Save geotre/227f7be1621b254c4266a54d3446b0f4 to your computer and use it in GitHub Desktop.
Quick test wrapping a js function that can have undefined args
when not defined(js):
{.error: "only works with the js backend".}
proc undefined*(T: typedesc): T =
asm "`result` = undefined;"
# defines a javascript function that could exist in a js lib
asm """
function canTakeUndefineds (a, b, c) {
const result = ``received a: ${a}, b: ${b}, c: ${c}``
console.log(result)
return result
}
"""
proc canTakeUndefineds(a: int, b=undefined(cstring), c=undefined(int)): cstring {.importc.}
assert canTakeUndefineds(3) == "received a: 3, b: undefined, c: undefined"
assert canTakeUndefineds(3, "hello world") == "received a: 3, b: hello world, c: undefined"
assert canTakeUndefineds(3, c=45) == "received a: 3, b: undefined, c: 45"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment