From 171b03c3861e1bcd722303a42b7ad11b14635215 Mon Sep 17 00:00:00 2001 From: flywind Date: Fri, 5 Mar 2021 21:39:46 +0800 Subject: [PATCH] fix #17264 [backport:1.4] (#17266) * fix #17264 * fix vm * fix js and add tests --- compiler/ccgexprs.nim | 3 ++- compiler/jsgen.nim | 4 ++-- compiler/vmgen.nim | 4 +++- lib/std/isolation.nim | 3 ++- tests/stdlib/isolation.nim | 16 ++++++++++++++++ 5 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 tests/stdlib/isolation.nim diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 86e0ed8c01ab..9babb4c76b26 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -2296,7 +2296,8 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = of mCharToStr: genDollar(p, e, d, "#nimCharToStr($1)") of mFloatToStr: genDollar(p, e, d, "#nimFloatToStr($1)") of mCStrToStr: genDollar(p, e, d, "#cstrToNimstr($1)") - of mStrToStr, mUnown, mIsolate: expr(p, e[1], d) + of mStrToStr, mUnown: expr(p, e[1], d) + of mIsolate: genCall(p, e, d) of mEnumToStr: if optTinyRtti in p.config.globalOptions: genEnumToStr(p, e, d) diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index aabb59eb5f6b..0e71162d7734 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -1435,7 +1435,7 @@ proc genSym(p: PProc, n: PNode, r: var TCompRes) = s.name.s) discard mangleName(p.module, s) r.res = s.loc.r - if lfNoDecl in s.loc.flags or s.magic != mNone or + if lfNoDecl in s.loc.flags or s.magic notin {mNone, mIsolate} or {sfImportc, sfInfixCall} * s.flags != {}: discard elif s.kind == skMethod and getBody(p.module.graph, s).kind == nkEmpty: @@ -2589,7 +2589,7 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) = let s = n[namePos].sym discard mangleName(p.module, s) r.res = s.loc.r - if lfNoDecl in s.loc.flags or s.magic != mNone: discard + if lfNoDecl in s.loc.flags or s.magic notin {mNone, mIsolate}: discard elif not p.g.generatedSyms.containsOrIncl(s.id): p.locals.add(genProc(p, s)) of nkType: r.res = genTypeInfo(p, n.typ) diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index d74bd44d8ae6..a1fcf5a8ad6a 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -1013,7 +1013,9 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) = c.genNarrow(n[1], d) c.genAsgnPatch(n[1], d) c.freeTemp(d) - of mOrd, mChr, mArrToSeq, mUnown, mIsolate: c.gen(n[1], dest) + of mOrd, mChr, mArrToSeq, mUnown: c.gen(n[1], dest) + of mIsolate: + genCall(c, n, dest) of mNew, mNewFinalize: unused(c, n, dest) c.genNew(n) diff --git a/lib/std/isolation.nim b/lib/std/isolation.nim index 7ca5f008008b..7c44f47fb6e3 100644 --- a/lib/std/isolation.nim +++ b/lib/std/isolation.nim @@ -25,7 +25,8 @@ proc `=destroy`*[T](dest: var Isolated[T]) {.inline.} = # delegate to value's destroy operation `=destroy`(dest.value) -func isolate*[T](value: sink T): Isolated[T] {.magic: "Isolate".} +func isolate*[T](value: sink T): Isolated[T] {.magic: "Isolate".} = ## Create an isolated subgraph from the expression `value`. ## Please read https://github.com/nim-lang/RFCs/issues/244 ## for more details. + Isolated[T](value: value) diff --git a/tests/stdlib/isolation.nim b/tests/stdlib/isolation.nim new file mode 100644 index 000000000000..6fbadda75c54 --- /dev/null +++ b/tests/stdlib/isolation.nim @@ -0,0 +1,16 @@ +discard """ + targets: "c cpp js" +""" + +import std/[json, isolation] + + +proc main() = + var x: seq[Isolated[JsonNode]] + x.add isolate(newJString("1234")) + + doAssert $x == """@[(value: "1234")]""" + + +static: main() +main()