summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-02-06 15:28:46 +0100
committerAndreas Rumpf <rumpf_a@web.de>2016-02-06 15:28:46 +0100
commit3941b8968cd44e4c629e29493fc4c8ffda5005f8 (patch)
treed14fdfd641ddf414885195e8e4abf5485d52df46 /compiler
parent73291aa1bc16f9f0d207a494847a0ca2c59021e1 (diff)
parentd148fdab5c21aaab6843aafc0f9b613d4eea78e4 (diff)
downloadNim-3941b8968cd44e4c629e29493fc4c8ffda5005f8.tar.gz
Merge pull request #3817 from yglukhov/js-typed-arrays
Prefer JS typed arrays for arrays.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/jsgen.nim31
1 files changed, 27 insertions, 4 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index 9424a9fc5..7ff822dd1 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -1138,6 +1138,18 @@ proc createObjInitList(p: PProc, typ: PType, excludedFieldIDs: IntSet, output: v
     createRecordVarAux(p, t.n, excludedFieldIDs, output)
     t = t.sons[0]
 
+proc arrayTypeForElemType(typ: PType): string =
+  case typ.kind
+  of tyInt, tyInt32: "Int32Array"
+  of tyInt16: "Int16Array"
+  of tyInt8: "Int8Array"
+  of tyUint, tyUint32: "Uint32Array"
+  of tyUint16: "Uint16Array"
+  of tyUint8: "Uint8Array"
+  of tyFloat32: "Float32Array"
+  of tyFloat64, tyFloat: "Float64Array"
+  else: nil
+
 proc createVar(p: PProc, typ: PType, indirect: bool): Rope =
   var t = skipTypes(typ, abstractInst)
   case t.kind
@@ -1152,9 +1164,12 @@ proc createVar(p: PProc, typ: PType, indirect: bool): Rope =
   of tyBool:
     result = putToSeq("false", indirect)
   of tyArray, tyArrayConstr:
-    var length = int(lengthOrd(t))
-    var e = elemType(t)
-    if length > 32:
+    let length = int(lengthOrd(t))
+    let e = elemType(t)
+    let jsTyp = arrayTypeForElemType(e)
+    if not jsTyp.isNil:
+      result = "new $1($2)" % [rope(jsTyp), rope(length)]
+    elif length > 32:
       useMagic(p, "arrayConstr")
       # XXX: arrayConstr depends on nimCopy. This line shouldn't be necessary.
       useMagic(p, "nimCopy")
@@ -1766,7 +1781,15 @@ proc genHeader(): Rope =
             "/*   (c) 2015 Andreas Rumpf */$n$n" &
             "var framePtr = null;$n" &
             "var excHandler = 0;$n" &
-            "var lastJSError = null;$n") %
+            "var lastJSError = null;$n" &
+            "if (typeof Int8Array === 'undefined') Int8Array = Array;$n" &
+            "if (typeof Int16Array === 'undefined') Int16Array = Array;$n" &
+            "if (typeof Int32Array === 'undefined') Int32Array = Array;$n" &
+            "if (typeof Uint8Array === 'undefined') Uint8Array = Array;$n" &
+            "if (typeof Uint16Array === 'undefined') Uint16Array = Array;$n" &
+            "if (typeof Uint32Array === 'undefined') Uint32Array = Array;$n" &
+            "if (typeof Float32Array === 'undefined') Float32Array = Array;$n" &
+            "if (typeof Float64Array === 'undefined') Float64Array = Array;$n") %
            [rope(VersionAsString)]
 
 proc genModule(p: PProc, n: PNode) =