Fixed seq.insert and seq.delete for js.
This commit is contained in:
parent
217a30a2e0
commit
b3cecddd64
2 changed files with 34 additions and 13 deletions
|
|
@ -91,3 +91,4 @@ proc initDefines*() =
|
||||||
defineSymbol("nimnomagic64")
|
defineSymbol("nimnomagic64")
|
||||||
defineSymbol("nimvarargstyped")
|
defineSymbol("nimvarargstyped")
|
||||||
defineSymbol("nimtypedescfixed")
|
defineSymbol("nimtypedescfixed")
|
||||||
|
defineSymbol("nimKnowsNimvm")
|
||||||
|
|
|
||||||
|
|
@ -1175,9 +1175,12 @@ const
|
||||||
|
|
||||||
seqShallowFlag = low(int)
|
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.
|
## may be used only in "when" expression.
|
||||||
## It is true in Nim VM context and false otherwise
|
## It is true in Nim VM context and false otherwise
|
||||||
|
else:
|
||||||
|
const nimvm*: bool = false
|
||||||
|
|
||||||
proc compileOption*(option: string): bool {.
|
proc compileOption*(option: string): bool {.
|
||||||
magic: "CompileOption", noSideEffect.}
|
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.} =
|
proc del*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
|
||||||
## deletes the item at index `i` by putting ``x[high(x)]`` into position `i`.
|
## deletes the item at index `i` by putting ``x[high(x)]`` into position `i`.
|
||||||
## This is an O(1) operation.
|
## This is an O(1) operation.
|
||||||
let xl = x.len
|
let xl = x.len - 1
|
||||||
shallowCopy(x[i], x[xl-1])
|
shallowCopy(x[i], x[xl])
|
||||||
setLen(x, xl-1)
|
setLen(x, xl)
|
||||||
|
|
||||||
proc delete*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
|
proc delete*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
|
||||||
## deletes the item at index `i` by moving ``x[i+1..]`` by one position.
|
## deletes the item at index `i` by moving ``x[i+1..]`` by one position.
|
||||||
## This is an O(n) operation.
|
## This is an O(n) operation.
|
||||||
let xl = x.len
|
template defaultImpl =
|
||||||
for j in i..xl-2: shallowCopy(x[j], x[j+1])
|
let xl = x.len
|
||||||
setLen(x, xl-1)
|
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.} =
|
proc insert*[T](x: var seq[T], item: T, i = 0.Natural) {.noSideEffect.} =
|
||||||
## inserts `item` into `x` at position `i`.
|
## inserts `item` into `x` at position `i`.
|
||||||
let xl = x.len
|
template defaultImpl =
|
||||||
setLen(x, xl+1)
|
let xl = x.len
|
||||||
var j = xl-1
|
setLen(x, xl+1)
|
||||||
while j >= i:
|
var j = xl-1
|
||||||
shallowCopy(x[j+1], x[j])
|
while j >= i:
|
||||||
dec(j)
|
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
|
x[i] = item
|
||||||
|
|
||||||
proc repr*[T](x: T): string {.magic: "Repr", noSideEffect.}
|
proc repr*[T](x: T): string {.magic: "Repr", noSideEffect.}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue