summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlexander Ivanov <alehander42@gmail.com>2018-12-30 03:48:37 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-12-30 02:48:37 +0100
commitaadbdd6b066bc7251c6d24bb5ef5ce3f940825ec (patch)
treeb61370e62eb33269bfbcf046fd3e9de84e08df41
parent062d7e3a398f7afb039ce568cceb32e908e3340e (diff)
downloadNim-aadbdd6b066bc7251c6d24bb5ef5ce3f940825ec.tar.gz
Support undefined in isNil (#9960)
-rw-r--r--compiler/jsgen.nim7
-rw-r--r--lib/js/jsffi.nim6
-rw-r--r--tests/js/tjsffi.nim16
3 files changed, 26 insertions, 3 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index daaa145e1..83d205bc2 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -1884,12 +1884,13 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
   of mLtStr:
     binaryExpr(p, n, r, "cmpStrings", "(cmpStrings($1, $2) < 0)")
   of mIsNil:
+    # we want to accept undefined, so we ==
     if mapType(n[1].typ) != etyBaseIndex:
-      unaryExpr(p, n, r, "", "($1 === null)")
+      unaryExpr(p, n, r, "", "($1 == null)")
     else:
       var x: TCompRes
       gen(p, n[1], x)
-      r.res = "($# === null && $# === 0)" % [x.address, x.res]
+      r.res = "($# == null && $# === 0)" % [x.address, x.res]
   of mEnumToStr: genRepr(p, n, r)
   of mNew, mNewFinalize: genNew(p, n)
   of mChr: gen(p, n.sons[1], r)
@@ -1922,7 +1923,7 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
       if optOverflowCheck notin p.options: binaryExpr(p, n, r, "", "$1 -= $2")
       else: binaryExpr(p, n, r, "subInt", "$1 = subInt($3, $2)")
   of mSetLengthStr:
-    binaryExpr(p, n, r, "mnewString", "($1 === null ? $3 = mnewString($2) : $3.length = $2)")
+    binaryExpr(p, n, r, "mnewString", "($1 == null ? $3 = mnewString($2) : $3.length = $2)")
   of mSetLengthSeq:
     var x, y: TCompRes
     gen(p, n.sons[1], x)
diff --git a/lib/js/jsffi.nim b/lib/js/jsffi.nim
index e1c59803d..7f0939b08 100644
--- a/lib/js/jsffi.nim
+++ b/lib/js/jsffi.nim
@@ -104,6 +104,12 @@ var
   jsFilename* {.importc: "__filename", nodecl.}: cstring
     ## JavaScript's __filename pseudo-variable
 
+proc isNull*[T](x: T): bool {.noSideEffect, importcpp: "(# === null)".}
+  ## check if a value is exactly null
+
+proc isUndefined*[T](x: T): bool {.noSideEffect, importcpp: "(# === undefined)".}
+  ## check if a value is exactly undefined
+
 # Exceptions
 type
   JsError* {.importc: "Error".} = object of JsRoot
diff --git a/tests/js/tjsffi.nim b/tests/js/tjsffi.nim
index 2420c60f6..8bd40a3c4 100644
--- a/tests/js/tjsffi.nim
+++ b/tests/js/tjsffi.nim
@@ -22,6 +22,13 @@ true
 Event { name: 'click: test' }
 Event { name: 'reloaded: test' }
 Event { name: 'updates: test' }
+true
+true
+true
+true
+true
+true
+true
 '''
 """
 
@@ -317,3 +324,12 @@ block:
   jslib.subscribe("updates"):
     console.log jsarguments[0]
 
+block:
+
+  echo jsUndefined == jsNull
+  echo jsUndefined == nil
+  echo jsNull == nil
+  echo jsUndefined.isNil
+  echo jsNull.isNil
+  echo jsNull.isNull
+  echo jsUndefined.isUndefined