summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSven Keller <svkeller85@gmail.com>2021-12-16 08:58:32 +0100
committerGitHub <noreply@github.com>2021-12-16 08:58:32 +0100
commit5d2bab7558b54813b212a26fbcdb51556ff37b05 (patch)
tree2b09ab657993ae04f761f8f03053c0cbdb9bf4a5
parentc17baaefbcff5c207a4e95242fa0790e64ca6c8c (diff)
downloadNim-5d2bab7558b54813b212a26fbcdb51556ff37b05.tar.gz
suggestion to respect typedarray type (#19257)
* suggestion to respect typedarray

* Update jssys.nim

Co-authored-by: Sven Keller <s.keller@cortona.de>
-rw-r--r--lib/system/jssys.nim28
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim
index 250bd069d..6608a2927 100644
--- a/lib/system/jssys.nim
+++ b/lib/system/jssys.nim
@@ -587,7 +587,33 @@ proc nimCopy(dest, src: JSRef, ti: PNimType): JSRef =
     else:
       asm "`result` = (`dest` === null || `dest` === undefined) ? {} : `dest`;"
     nimCopyAux(result, src, ti.node)
-  of tySequence, tyArrayConstr, tyOpenArray, tyArray:
+  of tyArrayConstr, tyArray:
+    # In order to prevent a type change (TypedArray -> Array) and to have better copying performance,
+    # arrays constructors are considered separately
+    asm """
+      if(ArrayBuffer.isView(`src`)) { 
+        if(`dest` === null || `dest` === undefined || `dest`.length != `src`.length) {
+          `dest` = new `src`.constructor(`src`);
+        } else {
+          `dest`.set(`src`, 0);
+        }
+        `result` = `dest`;
+      } else {
+        if (`src` === null) {
+          `result` = null;
+        }
+        else {
+          if (`dest` === null || `dest` === undefined || `dest`.length != `src`.length) {
+            `dest` = new Array(`src`.length);
+          }
+          `result` = `dest`;
+          for (var i = 0; i < `src`.length; ++i) {
+            `result`[i] = nimCopy(`result`[i], `src`[i], `ti`.base);
+          }
+        }
+      }
+    """
+  of tySequence, tyOpenArray:
     asm """
       if (`src` === null) {
         `result` = null;