From bc7644830e0b68363e91769ad95f59ebbd6f1410 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Mon, 20 Apr 2020 22:06:43 -0700 Subject: [PATCH 1/2] `$` works for ref|ptr|pointer for all targets (c,cpp,js,vm) --- compiler/vm.nim | 12 +++++++---- compiler/vmops.nim | 4 ++++ lib/pure/hashes.nim | 13 ++---------- lib/system.nim | 2 +- lib/system/dollars.nim | 30 ++++++++++++++++++++++++++ tests/system/tostring.nim | 44 ++++++++++++++++++++++++++++++++------- 6 files changed, 82 insertions(+), 23 deletions(-) diff --git a/compiler/vm.nim b/compiler/vm.nim index c15f9e2681de7..4ac152117284b 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -602,18 +602,22 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = of opcCastPtrToInt: # RENAME opcCastPtrOrRefToInt decodeBImm(rkInt) + var val = 0 case imm of 1: # PtrLikeKinds case regs[rb].kind of rkNode: - regs[ra].intVal = cast[int](regs[rb].node.intVal) - of rkNodeAddr: - regs[ra].intVal = cast[int](regs[rb].nodeAddr) + let node = regs[rb].node + if nfIsRef in node.flags: val = cast[int](node) + elif node.kind == nkIntLit: val = cast[int](node.intVal) + else: assert false, $node.kind + of rkNodeAddr: val = cast[int](regs[rb].nodeAddr) else: stackTrace(c, tos, pc, "opcCastPtrToInt: got " & $regs[rb].kind) of 2: # tyRef - regs[ra].intVal = cast[int](regs[rb].node) + val = cast[int](regs[rb].node) else: assert false, $imm + regs[ra].intVal = val of opcCastIntToPtr: let rb = instr.regB let typ = regs[ra].node.typ diff --git a/compiler/vmops.nim b/compiler/vmops.nim index 27d1ef479964f..0895fe06cfcaa 100644 --- a/compiler/vmops.nim +++ b/compiler/vmops.nim @@ -19,6 +19,7 @@ from sighashes import symBodyDigest from times import cpuTime from hashes import hash +from system/dollars import toHexImpl from osproc import nil import vmconv @@ -200,6 +201,9 @@ proc registerAdditionalOps*(c: PCtx) = registerCallback c, "stdlib.os.getCurrentCompilerExe", proc (a: VmArgs) {.nimcall.} = setResult(a, getAppFilename()) + registerCallback c, "stdlib.dollars.toHexImpl", proc (a: VmArgs) {.nimcall.} = + setResult(a, a.getInt(0).int.toHexImpl) + registerCallback c, "stdlib.macros.symBodyHash", proc (a: VmArgs) {.nimcall.} = let n = getNode(a, 0) if n.kind != nkSym: diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index 1024ce26f5cf7..8a44573237e26 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -152,21 +152,12 @@ proc hashData*(data: pointer, size: int): Hash = result = !$h when defined(js): - var objectID = 0 + from system/dollars import getNimJsObjectID proc hash*(x: pointer): Hash {.inline.} = ## Efficient hashing of pointers. when defined(js): - asm """ - if (typeof `x` == "object") { - if ("_NimID" in `x`) - `result` = `x`["_NimID"]; - else { - `result` = ++`objectID`; - `x`["_NimID"] = `result`; - } - } - """ + result = getNimJsObjectID(x) else: result = cast[Hash](cast[uint](x) shr 3) # skip the alignment diff --git a/lib/system.nim b/lib/system.nim index 85be43cbac43b..4c0730c4b15cf 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2078,7 +2078,7 @@ const ## Odd for devel, even for releases. import system/dollars -export dollars +export dollars except toHexImpl, getNimJsObjectID const NimVersion*: string = $NimMajor & "." & $NimMinor & "." & $NimPatch diff --git a/lib/system/dollars.nim b/lib/system/dollars.nim index b2f0186b31ddb..4535938fd7be9 100644 --- a/lib/system/dollars.nim +++ b/lib/system/dollars.nim @@ -82,6 +82,36 @@ else: return true return false +proc c_sprintf(buf, frmt: cstring): cint {.importc: "sprintf", header: "", varargs, noSideEffect.} + +proc toHexImpl*(x: int): string {.inline.} = + var buf {.noinit.}: array[int.sizeof*2+3, char] # 3 for 0x+\0 + let num = c_sprintf(buf.addr, "%p", cast[int](x)) + result = newString(num) + for i in 0.. Date: Tue, 21 Apr 2020 02:05:57 -0700 Subject: [PATCH 2/2] fix tests --- tests/concepts/tconcepts_issues.nim | 7 +++++-- tests/destructor/tdestructor3.nim | 6 ++---- tests/generics/tgeneric0.nim | 2 +- tests/js/trefbyvar.nim | 2 +- tests/statictypes/tstackmatrix.nim | 6 +----- tests/types/tissues_types.nim | 4 ++-- 6 files changed, 12 insertions(+), 15 deletions(-) diff --git a/tests/concepts/tconcepts_issues.nim b/tests/concepts/tconcepts_issues.nim index 65f6d46604e6f..b529fcb09ca17 100644 --- a/tests/concepts/tconcepts_issues.nim +++ b/tests/concepts/tconcepts_issues.nim @@ -82,6 +82,9 @@ type v: T converter toObj1[T](t: T): Obj1[T] = return Obj1[T](v: t) + +proc echo2(a: varargs[string, mytostr]) = echo a[0] + block t976: type int1 = distinct int @@ -117,12 +120,12 @@ block t976: PrintAble = concept x $x is string - proc `$`[T](nt: Obj1[T]): string = + proc mytostr[T](nt: Obj1[T]): string = when T is PrintAble: result = "Printable" else: result = "Non Printable" - echo Obj2() + echo2 Obj2() block t1128: diff --git a/tests/destructor/tdestructor3.nim b/tests/destructor/tdestructor3.nim index b68aedce91f33..14feeb7453118 100644 --- a/tests/destructor/tdestructor3.nim +++ b/tests/destructor/tdestructor3.nim @@ -6,11 +6,9 @@ destroy 123 destroy Foo: 123 destroy Foo: 5 -(x1: (val: ...)) destroy --------------- app begin -(val: ...) destroy app end ''' @@ -96,7 +94,7 @@ proc newObj2(x:int, y: float): MyObject2 = proc test = let obj2 = newObj2(1, 1.0) - echo obj2 + doAssert obj2.x1.val != nil test() @@ -119,7 +117,7 @@ proc createTop(): ptr TopObject = proc test2() = let x = createTop() - echo $x.internal + doAssert x.internal.val != nil deleteTop(x) echo "---------------" diff --git a/tests/generics/tgeneric0.nim b/tests/generics/tgeneric0.nim index 44c34917d1ac0..424750e72b7f2 100644 --- a/tests/generics/tgeneric0.nim +++ b/tests/generics/tgeneric0.nim @@ -4,7 +4,7 @@ discard """ 0 float32 float32 -(name: "Resource 1", readers: ..., writers: ...) +(name: "Resource 1", readers: @[], writers: @[]) ''' """ diff --git a/tests/js/trefbyvar.nim b/tests/js/trefbyvar.nim index 5b168044ee44c..848e0bc655c74 100644 --- a/tests/js/trefbyvar.nim +++ b/tests/js/trefbyvar.nim @@ -66,4 +66,4 @@ proc initTypeA1(a: int; b: string; c: pointer = nil): TypeA1 = result.c_impl = c let x = initTypeA1(1, "a") -doAssert($x == "(a_impl: 1, b_impl: \"a\", c_impl: ...)") +doAssert($x == "(a_impl: 1, b_impl: \"a\", c_impl: nil)") diff --git a/tests/statictypes/tstackmatrix.nim b/tests/statictypes/tstackmatrix.nim index 2509d21f8d0cd..38f9710b6abfa 100644 --- a/tests/statictypes/tstackmatrix.nim +++ b/tests/statictypes/tstackmatrix.nim @@ -1,7 +1,3 @@ -discard """ - output: "(M: 3, N: 3, fp: ...)" -""" - # bug #6843 type @@ -26,4 +22,4 @@ var [7'f64, 8, 9] ] m = stackMatrix(data) -echo m \ No newline at end of file +doAssert m.M == 3 and m.N == 3 and m.fp != nil diff --git a/tests/types/tissues_types.nim b/tests/types/tissues_types.nim index 7ed0547bfc7e7..72f80411ba8d1 100644 --- a/tests/types/tissues_types.nim +++ b/tests/types/tissues_types.nim @@ -6,8 +6,8 @@ true ptr Foo (member: "hello world") (member: 123.456) -(member: "hello world", x: ...) -(member: 123.456, x: ...) +(member: "hello world", x: nil) +(member: 123.456, x: nil) 0 false '''