diff options
author | Andrea Ferretti <ferrettiandrea@gmail.com> | 2016-10-06 11:35:29 +0200 |
---|---|---|
committer | Andrea Ferretti <ferrettiandrea@gmail.com> | 2016-10-06 11:35:29 +0200 |
commit | f0308e9bcf79990be85edc16592d0bfbdfc338bd (patch) | |
tree | 55f1d4cae7847e5a1151704481f0caf7497ccec0 /lib | |
parent | 4c52239394897637ca41441497472d3f3f771f9e (diff) | |
download | Nim-f0308e9bcf79990be85edc16592d0bfbdfc338bd.tar.gz |
New version of js console using macros - avoid the use of .apply method
Diffstat (limited to 'lib')
-rw-r--r-- | lib/js/jsconsole.nim | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/lib/js/jsconsole.nim b/lib/js/jsconsole.nim index cd1bd8d2a..d9ced95f0 100644 --- a/lib/js/jsconsole.nim +++ b/lib/js/jsconsole.nim @@ -13,14 +13,32 @@ when not defined(js) and not defined(Nimdoc): {.error: "This module only works on the JavaScript platform".} +import macros + type Console* {.importc.} = ref object of RootObj proc convertToConsoleLoggable*[T](v: T): RootRef {.importcpp: "#".} template convertToConsoleLoggable*(v: string): RootRef = cast[RootRef](cstring(v)) -proc log*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]) {.importcpp: "#.log.apply(null, #)".} -proc debug*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]) {.importcpp: "#.debug.apply(null, #)".} -proc info*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]) {.importcpp: "#.info.apply(null, #)".} -proc error*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]) {.importcpp: "#.error.apply(null, #)".} +proc logImpl(console: Console) {.importcpp: "log", varargs.} +proc debugImpl(console: Console) {.importcpp: "debug", varargs.} +proc infoImpl(console: Console) {.importcpp: "info", varargs.} +proc errorImpl(console: Console) {.importcpp: "error", varargs.} + +proc makeConsoleCall(console: NimNode, procName: NimNode, args: NimNode): NimNode = + result = newCall(procName, console) + for c in args: result.add(c) + +macro log*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped = + makeConsoleCall(console, bindSym "logImpl", args) + +macro debug*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped = + makeConsoleCall(console, bindSym "debugImpl", args) + +macro info*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped = + makeConsoleCall(console, bindSym "infoImpl", args) + +macro error*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped = + makeConsoleCall(console, bindSym "errorImpl", args) var console* {.importc, nodecl.}: Console \ No newline at end of file |