* ref 15952 toOpenArray works in JS * fix
This commit is contained in:
parent
216be4060a
commit
9bd4f503f4
2 changed files with 32 additions and 10 deletions
|
|
@ -14,27 +14,28 @@ The JS code generator contains only 2 tricks:
|
||||||
|
|
||||||
Trick 1
|
Trick 1
|
||||||
-------
|
-------
|
||||||
Some locations (for example 'var int') require "fat pointers" (``etyBaseIndex``)
|
Some locations (for example 'var int') require "fat pointers" (`etyBaseIndex`)
|
||||||
which are pairs (array, index). The derefence operation is then 'array[index]'.
|
which are pairs (array, index). The derefence operation is then 'array[index]'.
|
||||||
Check ``mapType`` for the details.
|
Check `mapType` for the details.
|
||||||
|
|
||||||
Trick 2
|
Trick 2
|
||||||
-------
|
-------
|
||||||
It is preferable to generate '||' and '&&' if possible since that is more
|
It is preferable to generate '||' and '&&' if possible since that is more
|
||||||
idiomatic and hence should be friendlier for the JS JIT implementation. However
|
idiomatic and hence should be friendlier for the JS JIT implementation. However
|
||||||
code like ``foo and (let bar = baz())`` cannot be translated this way. Instead
|
code like `foo and (let bar = baz())` cannot be translated this way. Instead
|
||||||
the expressions need to be transformed into statements. ``isSimpleExpr``
|
the expressions need to be transformed into statements. `isSimpleExpr`
|
||||||
implements the required case distinction.
|
implements the required case distinction.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
import
|
import
|
||||||
ast, strutils, trees, magicsys, options,
|
ast, trees, magicsys, options,
|
||||||
nversion, msgs, idents, types, tables,
|
nversion, msgs, idents, types,
|
||||||
ropes, math, passes, ccgutils, wordrecg, renderer,
|
ropes, passes, ccgutils, wordrecg, renderer,
|
||||||
intsets, cgmeth, lowerings, sighashes, modulegraphs, lineinfos, rodutils,
|
cgmeth, lowerings, sighashes, modulegraphs, lineinfos, rodutils,
|
||||||
transf, injectdestructors, sourcemap, json, sets
|
transf, injectdestructors, sourcemap
|
||||||
|
|
||||||
|
import std/[json, sets, math, tables, intsets, strutils]
|
||||||
|
|
||||||
from modulegraphs import ModuleGraph, PPassContext
|
from modulegraphs import ModuleGraph, PPassContext
|
||||||
|
|
||||||
|
|
@ -1350,7 +1351,15 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
|
||||||
of nkStmtListExpr:
|
of nkStmtListExpr:
|
||||||
if n.len == 1: gen(p, n[0], r)
|
if n.len == 1: gen(p, n[0], r)
|
||||||
else: internalError(p.config, n[0].info, "genAddr for complex nkStmtListExpr")
|
else: internalError(p.config, n[0].info, "genAddr for complex nkStmtListExpr")
|
||||||
else: internalError(p.config, n[0].info, "genAddr: " & $n[0].kind)
|
of nkCallKinds:
|
||||||
|
if n[0].typ.kind == tyOpenArray:
|
||||||
|
# 'var openArray' for instance produces an 'addr' but this is harmless:
|
||||||
|
# namely toOpenArray(a, 1, 3)
|
||||||
|
gen(p, n[0], r)
|
||||||
|
else:
|
||||||
|
internalError(p.config, n[0].info, "genAddr: " & $n[0].kind)
|
||||||
|
else:
|
||||||
|
internalError(p.config, n[0].info, "genAddr: " & $n[0].kind)
|
||||||
|
|
||||||
proc attachProc(p: PProc; content: Rope; s: PSym) =
|
proc attachProc(p: PProc; content: Rope; s: PSym) =
|
||||||
p.g.code.add(content)
|
p.g.code.add(content)
|
||||||
|
|
|
||||||
13
tests/openarray/topenarray.nim
Normal file
13
tests/openarray/topenarray.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
discard """
|
||||||
|
targets: "c cpp js"
|
||||||
|
"""
|
||||||
|
|
||||||
|
proc pro[T](a: var openArray[T]) = discard
|
||||||
|
|
||||||
|
proc main =
|
||||||
|
var a = [1,2,3,4,5]
|
||||||
|
|
||||||
|
pro(toOpenArray(a, 1, 3))
|
||||||
|
pro(a.toOpenArray(1,3))
|
||||||
|
|
||||||
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue