Skip to content

toOpenArray with var openArray doesn't work in nim js #15952

Open
@timotheecour

Description

@timotheecour

VM: Error: cannot generate code for: mSlice with toOpenArray
js: Error: internal error: genAddr: nkCall with toOpenArray + var openArray

Example 1

proc main1[T](a: openArray[T]) = discard
proc main2[T](a: var openArray[T]) = discard

proc main =
  var a = [1,2,3,4,5]
  main1(a.toOpenArray(1,3))
  main2(a.toOpenArray(1,3))
# static: main() # bug1: Error: cannot generate code for: mSlice
main() # bug2: in js: Error: internal error: genAddr: nkCall

Current Output

see above

Expected Output

works

Additional Information

devel 1.5.1 1413818

note

fixing this would avoid workarounds like this in #16459:

    proc assign(dest: var openArray[byte], src: Uint8Array, base: int, size: int) =
      getRandomValues(src)
      for j in 0 ..< size:
        dest[base + j] = src[j]

Example 2

as of f3e4c4d (after #17001):

when true:
  proc main2[T](a: var openArray[T]) =
    a[0] = 10
  var a = [1,2,3,4,5]
  main2(a.toOpenArray(1,3))
  doAssert a[1] == 10, $a[1]

nim r -b:js main: assert fails

implementation

for js: maybe using js slice refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

for vm: TBD

links

see also nim-lang/RFCs#381

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions