Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
timotheecour opened this issue Nov 13, 2020 · 1 comment
Open

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

timotheecour opened this issue Nov 13, 2020 · 1 comment

Comments

@timotheecour
Copy link
Member

timotheecour commented Nov 13, 2020

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

@timotheecour timotheecour added the VM see also `const` label label Nov 13, 2020
@timotheecour timotheecour changed the title VM: Error: cannot generate code for: mSlice with toOpenArray toOpenArray doesn't work in VM; toOpenArray with var openArray doesn't work in nim js Feb 5, 2021
Araq pushed a commit that referenced this issue Feb 10, 2021
* ref 15952 toOpenArray works in JS

* fix
narimiran pushed a commit that referenced this issue Feb 18, 2021
* ref 15952 toOpenArray works in JS

* fix

(cherry picked from commit 9bd4f50)
@timotheecour
Copy link
Member Author

timotheecour commented Feb 25, 2021

IMO the correct way to fix this is to represent openArray's in backends other than C as:

  • origin: a reference to the whole original memory segment (the "backing store") for the underlying array/seq/UncheckedArray we're slicing, more or less an UncheckedArray
  • start: an index into origin
  • length: the length of the openArray

in js

origin is represented as a js array

in vm

origin is represented as a PNode

toOpenArray(x, a1, a2) computation

this can then be computed by propagating x, and computing start, length from a1, a2, x

note

this will fix a large number of issues related to vm and js backends, in particular wrt addr, unsafeAddr, mutation of openArray elements etc

ardek66 pushed a commit to ardek66/Nim that referenced this issue Mar 26, 2021
timotheecour added a commit to timotheecour/Nim that referenced this issue Jun 23, 2021
timotheecour added a commit to timotheecour/Nim that referenced this issue Jun 23, 2021
timotheecour added a commit to timotheecour/Nim that referenced this issue Jun 25, 2021
@ringabout ringabout removed the VM see also `const` label label Oct 21, 2022
ringabout added a commit that referenced this issue Oct 21, 2022
narimiran pushed a commit that referenced this issue Oct 24, 2022
closes #19969; add testcase

(cherry picked from commit 1db25ff)
@ringabout ringabout changed the title toOpenArray doesn't work in VM; toOpenArray with var openArray doesn't work in nim js toOpenArray with var openArray doesn't work in nim js Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants