From 0fcd838fd938a26ff9d032de71d5c78a1028e955 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 15 May 2024 00:07:47 +0800 Subject: [PATCH] fixes openarray views default values in JS (#23607) --- compiler/jsgen.nim | 4 ++-- tests/views/tviews2.nim | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/views/tviews2.nim diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index f7e4e9e721..0cc052b381 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -1241,7 +1241,7 @@ proc needsNoCopy(p: PProc; y: PNode): bool = return y.kind in nodeKindsNeedNoCopy or ((mapType(y.typ) != etyBaseIndex) and (skipTypes(y.typ, abstractInst).kind in - {tyRef, tyPtr, tyLent, tyVar, tyCstring, tyProc, tyOwned} + IntegralTypes)) + {tyRef, tyPtr, tyLent, tyVar, tyCstring, tyProc, tyOwned, tyOpenArray} + IntegralTypes)) proc genAsgnAux(p: PProc, x, y: PNode, noCopyNeeded: bool) = var a, b: TCompRes = default(TCompRes) @@ -1966,7 +1966,7 @@ proc createVar(p: PProc, typ: PType, indirect: bool): Rope = result = putToSeq("null", indirect) of tySequence, tyString: result = putToSeq("[]", indirect) - of tyCstring, tyProc: + of tyCstring, tyProc, tyOpenArray: result = putToSeq("null", indirect) of tyStatic: if t.n != nil: diff --git a/tests/views/tviews2.nim b/tests/views/tviews2.nim new file mode 100644 index 0000000000..36ba589934 --- /dev/null +++ b/tests/views/tviews2.nim @@ -0,0 +1,15 @@ +discard """ + targets: "c js" +""" + +{.experimental: "views".} + +type + Foo = object + id: openArray[char] + +proc foo(): Foo = + var source = "1245" + result = Foo(id: source.toOpenArray(0, 1)) + +doAssert foo().id == @['1', '2']