From 70a1c4254230eb8f3c4d24ccbc253c0a28065d03 Mon Sep 17 00:00:00 2001 From: flywind <43030857+xflywind@users.noreply.github.com> Date: Fri, 27 Nov 2020 03:30:19 +0800 Subject: [PATCH] add `**` to jsffi (#16141) * fix rope index * add testcase * fix ropes format * add `**` to jsffi * add testcase * changelog Co-authored-by: Andreas Rumpf --- changelog.md | 1 + lib/js/jsffi.nim | 1 + tests/js/tjsffi.nim | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index bfbe59e1a..ba1bc9438 100644 --- a/changelog.md +++ b/changelog.md @@ -47,6 +47,7 @@ - `repr` now doesn't insert trailing newline; previous behavior was very inconsistent, see #16034. Use `-d:nimLegacyReprWithNewline` for previous behavior. +- Added `**` to jsffi. - `writeStackTrace` is available in JS backend now. ## Language changes diff --git a/lib/js/jsffi.nim b/lib/js/jsffi.nim index 0dce6f3b9..eedf45bb0 100644 --- a/lib/js/jsffi.nim +++ b/lib/js/jsffi.nim @@ -180,6 +180,7 @@ proc `>` *(x, y: JsObject): JsObject {.importcpp: "(# > #)".} proc `<` *(x, y: JsObject): JsObject {.importcpp: "(# < #)".} proc `>=` *(x, y: JsObject): JsObject {.importcpp: "(# >= #)".} proc `<=` *(x, y: JsObject): JsObject {.importcpp: "(# <= #)".} +proc `**` *(x, y: JsObject): JsObject {.importcpp: "((#) ** #)".} proc `and`*(x, y: JsObject): JsObject {.importcpp: "(# && #)".} proc `or` *(x, y: JsObject): JsObject {.importcpp: "(# || #)".} proc `not`*(x: JsObject): JsObject {.importcpp: "(!#)".} diff --git a/tests/js/tjsffi.nim b/tests/js/tjsffi.nim index af33cee8a..143ac4a25 100644 --- a/tests/js/tjsffi.nim +++ b/tests/js/tjsffi.nim @@ -323,7 +323,6 @@ block: console.log jsarguments[0] block: - echo jsUndefined == jsNull echo jsUndefined == nil echo jsNull == nil @@ -331,3 +330,36 @@ block: echo jsNull.isNil echo jsNull.isNull echo jsUndefined.isUndefined + +block: # test ** + var a = toJs(0) + var b = toJs(0) + doAssert to(a ** b, int) == 1 + a = toJs(1) + b = toJs(1) + doAssert to(a ** b, int) == 1 + a = toJs(-1) + b = toJs(-1) + doAssert to(a ** b, int) == -1 + a = toJs(6) + b = toJs(6) + doAssert to(a ** b, int) == 46656 + a = toJs(5.5) + b = toJs(3) + doAssert to(a ** b, float) == 166.375 + a = toJs(5) + b = toJs(3.0) + doAssert to(a ** b, float) == 125.0 + a = toJs(7.0) + b = toJS(6.0) + doAssert to(a ** b, float) == 117649.0 + a = toJs(8) + b = toJS(-2) + doAssert to(a ** b, float) == 0.015625 + + a = toJs(1) + b = toJs(1) + doAssert to(`**`(a + a, b), int) == 2 + + doAssert to(`**`(toJs(1) + toJs(1), toJs(2)), int) == 4 +