summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2015-09-09 17:32:55 +0200
committerAndreas Rumpf <rumpf_a@web.de>2015-09-09 17:32:55 +0200
commit0c7d52e34ccf69a1b1d7ab6fb15e36ca53e9864b (patch)
tree74f9b67da78e6e260ba8d9137d706f6e021c9977
parentca9c1968d8fc26bb1d2e9700fab2e94932bc0696 (diff)
parentb3cecddd64389df03763f7b67f5254a5aab18095 (diff)
downloadNim-0c7d52e34ccf69a1b1d7ab6fb15e36ca53e9864b.tar.gz
Merge pull request #3274 from yglukhov/seq-insert-js-fix
Fixed seq.insert and seq.delete for js.
-rw-r--r--compiler/condsyms.nim1
-rw-r--r--lib/system.nim46
2 files changed, 34 insertions, 13 deletions
diff --git a/compiler/condsyms.nim b/compiler/condsyms.nim
index 297b865b2..31bd85a06 100644
--- a/compiler/condsyms.nim
+++ b/compiler/condsyms.nim
@@ -91,3 +91,4 @@ proc initDefines*() =
   defineSymbol("nimnomagic64")
   defineSymbol("nimvarargstyped")
   defineSymbol("nimtypedescfixed")
+  defineSymbol("nimKnowsNimvm")
diff --git a/lib/system.nim b/lib/system.nim
index 3d7d4bd28..1890ce5be 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1175,9 +1175,12 @@ const
 
   seqShallowFlag = low(int)
 
-let nimvm* {.magic: "Nimvm".}: bool = false
+when defined(nimKnowsNimvm):
+  let nimvm* {.magic: "Nimvm".}: bool = false
     ## may be used only in "when" expression.
     ## It is true in Nim VM context and false otherwise
+else:
+  const nimvm*: bool = false
 
 proc compileOption*(option: string): bool {.
   magic: "CompileOption", noSideEffect.}
@@ -1293,25 +1296,42 @@ proc shallowCopy*[T](x: var T, y: T) {.noSideEffect, magic: "ShallowCopy".}
 proc del*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
   ## deletes the item at index `i` by putting ``x[high(x)]`` into position `i`.
   ## This is an O(1) operation.
-  let xl = x.len
-  shallowCopy(x[i], x[xl-1])
-  setLen(x, xl-1)
+  let xl = x.len - 1
+  shallowCopy(x[i], x[xl])
+  setLen(x, xl)
 
 proc delete*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
   ## deletes the item at index `i` by moving ``x[i+1..]`` by one position.
   ## This is an O(n) operation.
-  let xl = x.len
-  for j in i..xl-2: shallowCopy(x[j], x[j+1])
-  setLen(x, xl-1)
+  template defaultImpl =
+    let xl = x.len
+    for j in i..xl-2: shallowCopy(x[j], x[j+1])
+    setLen(x, xl-1)
+
+  when nimvm:
+    defaultImpl()
+  else:
+    when defined(js):
+      {.emit: "`x`[`x`_Idx].splice(`i`, 1);".}
+    else:
+      defaultImpl()
 
 proc insert*[T](x: var seq[T], item: T, i = 0.Natural) {.noSideEffect.} =
   ## inserts `item` into `x` at position `i`.
-  let xl = x.len
-  setLen(x, xl+1)
-  var j = xl-1
-  while j >= i:
-    shallowCopy(x[j+1], x[j])
-    dec(j)
+  template defaultImpl =
+    let xl = x.len
+    setLen(x, xl+1)
+    var j = xl-1
+    while j >= i:
+      shallowCopy(x[j+1], x[j])
+      dec(j)
+  when nimvm:
+    defaultImpl()
+  else:
+    when defined(js):
+      {.emit: "`x`[`x`_Idx].splice(`i`, 0, null);".}
+    else:
+      defaultImpl()
   x[i] = item
 
 proc repr*[T](x: T): string {.magic: "Repr", noSideEffect.}