[closes #11625 and closes #2488]add global and threadvar(with --threads:off mode ) pragmas supports for JS backend (#15772)
* add global pragma support for js backend * globalThis * add support for threadvar * more tests * Update compiler/jsgen.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
parent
6fe2e8977d
commit
2cfe5e0745
2 changed files with 36 additions and 1 deletions
|
|
@ -1736,6 +1736,7 @@ proc genVarInit(p: PProc, v: PSym, n: PNode) =
|
||||||
varCode: string
|
varCode: string
|
||||||
varName = mangleName(p.module, v)
|
varName = mangleName(p.module, v)
|
||||||
useReloadingGuard = sfGlobal in v.flags and p.config.hcrOn
|
useReloadingGuard = sfGlobal in v.flags and p.config.hcrOn
|
||||||
|
useGlobalPragmas = sfGlobal in v.flags and ({sfPure, sfThread} * v.flags != {})
|
||||||
|
|
||||||
if v.constraint.isNil:
|
if v.constraint.isNil:
|
||||||
if useReloadingGuard:
|
if useReloadingGuard:
|
||||||
|
|
@ -1743,6 +1744,10 @@ proc genVarInit(p: PProc, v: PSym, n: PNode) =
|
||||||
lineF(p, "if ($1 === undefined) {$n", varName)
|
lineF(p, "if ($1 === undefined) {$n", varName)
|
||||||
varCode = $varName
|
varCode = $varName
|
||||||
inc p.extraIndent
|
inc p.extraIndent
|
||||||
|
elif useGlobalPragmas:
|
||||||
|
lineF(p, "if (globalThis.$1 === undefined) {$n", varName)
|
||||||
|
varCode = $varName
|
||||||
|
inc p.extraIndent
|
||||||
else:
|
else:
|
||||||
varCode = "var $2"
|
varCode = "var $2"
|
||||||
else:
|
else:
|
||||||
|
|
@ -1795,7 +1800,7 @@ proc genVarInit(p: PProc, v: PSym, n: PNode) =
|
||||||
else:
|
else:
|
||||||
line(p, runtimeFormat(varCode & " = $3;$n", [returnType, v.loc.r, s]))
|
line(p, runtimeFormat(varCode & " = $3;$n", [returnType, v.loc.r, s]))
|
||||||
|
|
||||||
if useReloadingGuard:
|
if useReloadingGuard or useGlobalPragmas:
|
||||||
dec p.extraIndent
|
dec p.extraIndent
|
||||||
lineF(p, "}$n")
|
lineF(p, "}$n")
|
||||||
|
|
||||||
|
|
|
||||||
30
tests/js/tglobal.nim
Normal file
30
tests/js/tglobal.nim
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
block global:
|
||||||
|
proc getState(): int =
|
||||||
|
var state0 {.global.}: int
|
||||||
|
inc state0
|
||||||
|
result = state0
|
||||||
|
|
||||||
|
for i in 0 ..< 3:
|
||||||
|
doAssert getState() == i + 1
|
||||||
|
|
||||||
|
for i in 0 ..< 3:
|
||||||
|
once:
|
||||||
|
doAssert i == 0
|
||||||
|
|
||||||
|
|
||||||
|
block threadvar:
|
||||||
|
proc getThreadState0(): int =
|
||||||
|
var state0 {.threadvar.}: int
|
||||||
|
inc state0
|
||||||
|
result = state0
|
||||||
|
|
||||||
|
for i in 0 ..< 3:
|
||||||
|
doAssert getThreadState0() == i + 1
|
||||||
|
|
||||||
|
proc getThreadState1(): int =
|
||||||
|
var state1 {.threadvar.}: int
|
||||||
|
inc state1
|
||||||
|
result = state1
|
||||||
|
|
||||||
|
for i in 0 ..< 3:
|
||||||
|
doAssert getThreadState1() == i + 1
|
||||||
Loading…
Add table
Add a link
Reference in a new issue