Created
September 24, 2022 13:32
-
-
Save sigmaSd/92436e85eccd8adfd451c748e6ffc181 to your computer and use it in GitHub Desktop.
console.log(js({a:4, b: "hello"}))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std/genasts | |
import std/jsconsole | |
import std/strutils | |
import std/macros | |
import std/tables | |
import std/strformat | |
import std/sequtils | |
from std/jsffi import JsObject | |
macro js*(args: untyped): untyped = | |
args.expectKind nnkTableConstr | |
var argsWithType = "" | |
var argsWithOutType = "" | |
var jscode = "" | |
for arg in args: | |
let ntype = case arg[1].kind | |
of nnkIntLit: | |
"int" | |
of nnkStrLit: | |
"string" | |
else: | |
"create a pr!!!" | |
let ele = arg.repr.split(": ") | |
let id = ele[0] | |
let value = ele[1] | |
argsWithType.add(&"{id}: {ntype}, ") | |
argsWithOutType.add(&"{value}, ") | |
jscode.add(&"{id}: {value}, ") | |
jscode = jscode.replace('"','\'') | |
jscode = &"{{ {jscode} }} /*@*/" | |
let procJs= parseStmt(&"proc toJs({argsWithType}): JsObject {{.importjs: \"{jscode}\".}}") | |
let procInvoke = parseStmt(&"toJs({argsWithOutType})") | |
quote do: | |
`procJs` | |
`procInvoke` | |
console.log(js({a:4, b: "hello"})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment