Support undefined in isNil (#9960)
This commit is contained in:
parent
062d7e3a39
commit
aadbdd6b06
3 changed files with 26 additions and 3 deletions
|
|
@ -1884,12 +1884,13 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
|
||||||
of mLtStr:
|
of mLtStr:
|
||||||
binaryExpr(p, n, r, "cmpStrings", "(cmpStrings($1, $2) < 0)")
|
binaryExpr(p, n, r, "cmpStrings", "(cmpStrings($1, $2) < 0)")
|
||||||
of mIsNil:
|
of mIsNil:
|
||||||
|
# we want to accept undefined, so we ==
|
||||||
if mapType(n[1].typ) != etyBaseIndex:
|
if mapType(n[1].typ) != etyBaseIndex:
|
||||||
unaryExpr(p, n, r, "", "($1 === null)")
|
unaryExpr(p, n, r, "", "($1 == null)")
|
||||||
else:
|
else:
|
||||||
var x: TCompRes
|
var x: TCompRes
|
||||||
gen(p, n[1], x)
|
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 mEnumToStr: genRepr(p, n, r)
|
||||||
of mNew, mNewFinalize: genNew(p, n)
|
of mNew, mNewFinalize: genNew(p, n)
|
||||||
of mChr: gen(p, n.sons[1], r)
|
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")
|
if optOverflowCheck notin p.options: binaryExpr(p, n, r, "", "$1 -= $2")
|
||||||
else: binaryExpr(p, n, r, "subInt", "$1 = subInt($3, $2)")
|
else: binaryExpr(p, n, r, "subInt", "$1 = subInt($3, $2)")
|
||||||
of mSetLengthStr:
|
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:
|
of mSetLengthSeq:
|
||||||
var x, y: TCompRes
|
var x, y: TCompRes
|
||||||
gen(p, n.sons[1], x)
|
gen(p, n.sons[1], x)
|
||||||
|
|
|
||||||
|
|
@ -104,6 +104,12 @@ var
|
||||||
jsFilename* {.importc: "__filename", nodecl.}: cstring
|
jsFilename* {.importc: "__filename", nodecl.}: cstring
|
||||||
## JavaScript's __filename pseudo-variable
|
## 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
|
# Exceptions
|
||||||
type
|
type
|
||||||
JsError* {.importc: "Error".} = object of JsRoot
|
JsError* {.importc: "Error".} = object of JsRoot
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,13 @@ true
|
||||||
Event { name: 'click: test' }
|
Event { name: 'click: test' }
|
||||||
Event { name: 'reloaded: test' }
|
Event { name: 'reloaded: test' }
|
||||||
Event { name: 'updates: test' }
|
Event { name: 'updates: test' }
|
||||||
|
true
|
||||||
|
true
|
||||||
|
true
|
||||||
|
true
|
||||||
|
true
|
||||||
|
true
|
||||||
|
true
|
||||||
'''
|
'''
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
@ -317,3 +324,12 @@ block:
|
||||||
jslib.subscribe("updates"):
|
jslib.subscribe("updates"):
|
||||||
console.log jsarguments[0]
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue