summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2015-08-21 21:25:41 +0200
committerAndreas Rumpf <rumpf_a@web.de>2015-08-21 21:25:41 +0200
commit6733af414d5b3221ef27462c79f3741d528dcd09 (patch)
tree8894b62c821b3f287e3a1e525fb829c1888bda7a
parente4c164144da79979d3df47caba3009d660541a33 (diff)
parenta5be556a4cd58371f21cab6bffc8b333a9aa5c51 (diff)
downloadNim-6733af414d5b3221ef27462c79f3741d528dcd09.tar.gz
Merge pull request #3232 from yglukhov/js-pointer-fix
JS: Untyped pointers codegen changed. addr expression fixed.
-rw-r--r--compiler/jsgen.nim2
-rw-r--r--compiler/semmagic.nim2
-rw-r--r--lib/system/jssys.nim19
-rw-r--r--tests/js/taddr.nim8
4 files changed, 21 insertions, 10 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index 89a1b84a2..774f6501a 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -136,7 +136,7 @@ proc mapType(typ: PType): TJSTypeKind =
       result = etyBaseIndex
   of tyPointer:
     # treat a tyPointer like a typed pointer to an array of bytes
-    result = etyInt
+    result = etyBaseIndex
   of tyRange, tyDistinct, tyOrdinal, tyConst, tyMutable, tyIter, tyProxy:
     result = mapType(t.sons[0])
   of tyInt..tyInt64, tyUInt..tyUInt64, tyEnum, tyChar: result = etyInt
diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim
index 0afbf1f07..5d16470b0 100644
--- a/compiler/semmagic.nim
+++ b/compiler/semmagic.nim
@@ -13,6 +13,8 @@
 proc semAddr(c: PContext; n: PNode; isUnsafeAddr=false): PNode =
   result = newNodeI(nkAddr, n.info)
   let x = semExprWithType(c, n)
+  if x.kind == nkSym:
+    x.sym.flags.incl(sfAddrTaken)
   if isAssignable(c, x, isUnsafeAddr) notin {arLValue, arLocalLValue}:
     localError(n.info, errExprHasNoAddress)
   result.add x
diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim
index 242f42c16..a6839c48e 100644
--- a/lib/system/jssys.nim
+++ b/lib/system/jssys.nim
@@ -532,15 +532,17 @@ proc nimMax(a, b: int): int {.compilerproc.} = return if a >= b: a else: b
 type NimString = string # hack for hti.nim
 include "system/hti"
 
+type JSRef = int # Fake type.
+
 proc isFatPointer(ti: PNimType): bool =
   # This has to be consistent with the code generator!
   return ti.base.kind notin {tyObject,
     tyArray, tyArrayConstr, tyTuple,
     tyOpenArray, tySet, tyVar, tyRef, tyPtr}
 
-proc nimCopy(dest, src: pointer, ti: PNimType): pointer {.compilerproc.}
+proc nimCopy(dest, src: JSRef, ti: PNimType): JSRef {.compilerproc.}
 
-proc nimCopyAux(dest, src: pointer, n: ptr TNimNode) {.compilerproc.} =
+proc nimCopyAux(dest, src: JSRef, n: ptr TNimNode) {.compilerproc.} =
   case n.kind
   of nkNone: sysAssert(false, "nimCopyAux")
   of nkSlot:
@@ -562,7 +564,7 @@ proc nimCopyAux(dest, src: pointer, n: ptr TNimNode) {.compilerproc.} =
       }
     """
 
-proc nimCopy(dest, src: pointer, ti: PNimType): pointer =
+proc nimCopy(dest, src: JSRef, ti: PNimType): JSRef =
   case ti.kind
   of tyPtr, tyRef, tyVar, tyNil:
     if not isFatPointer(ti):
@@ -603,12 +605,11 @@ proc nimCopy(dest, src: pointer, ti: PNimType): pointer =
   else:
     result = src
 
-proc genericReset(x: pointer, ti: PNimType): pointer {.compilerproc.} =
+proc genericReset(x: JSRef, ti: PNimType): JSRef {.compilerproc.} =
+  asm "`result` = null;"
   case ti.kind
   of tyPtr, tyRef, tyVar, tyNil:
-    if not isFatPointer(ti):
-      result = nil
-    else:
+    if isFatPointer(ti):
       asm """
         `result` = [null, 0];
       """
@@ -633,9 +634,9 @@ proc genericReset(x: pointer, ti: PNimType): pointer {.compilerproc.} =
       }
     """
   else:
-    result = nil
+    discard
 
-proc arrayConstr(len: int, value: pointer, typ: PNimType): pointer {.
+proc arrayConstr(len: int, value: JSRef, typ: PNimType): JSRef {.
                  asmNoStackFrame, compilerproc.} =
   # types are fake
   asm """
diff --git a/tests/js/taddr.nim b/tests/js/taddr.nim
index e5c8d0881..1fba30d55 100644
--- a/tests/js/taddr.nim
+++ b/tests/js/taddr.nim
@@ -62,3 +62,11 @@ var t : tuple[a, b: int]
 var pt = addr t[1]
 pt[] = 123
 doAssert(t.b == 123)
+
+#block: # Test "untyped" pointer.
+proc testPtr(p: pointer, a: int) =
+  doAssert(a == 5)
+  (cast[ptr int](p))[] = 124
+var i = 123
+testPtr(addr i, 5)
+doAssert(i == 124)