-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add shadow variable assignment similar to proc backend shadow calls #13400
Comments
Alternatively/Additionally, it would be very convenient if there was a way to store the last used identifier and be able to reference it in pragmas that follow, so that the above could be done as: var varName = value
{.emit: "$ID = " & jsVarName .} |
As far as I can see, this could be done pretty easily by storing a the name of the last (mangled) var identifier on proc genVarInit(p: PProc, v: PSym, n: PNode) =
var
a: TCompRes
s: Rope
varCode: string
# The mangled varName which will be output
varName = mangleName(p.module, v)
useReloadingGuard = sfGlobal in v.flags and p.config.hcrOn
# store varName on p so that we can reference it later
p.lastVarName = varName In # replace $ID special ref placeholder with stored lastVarName
proc replaceSpecial(p: PProc, strVal: string): string =
strVal.replace "$ID", p.lastVarName
proc genAsmOrEmitStmt(p: PProc, n: PNode): PProc =
genLineDir(p, n)
p.body.add p.indentLine(nil)
for i in 0..<n.len:
let it = n[i]
case it.kind
of nkStrLit..nkTripleStrLit:
# replace $ID special ref placeholder with stored lastVarName
var strVal = replaceSpecial(p, it.strVal)
p.body.add(strVal) |
It's really unclear to me what you're proposing. Please do not compare the C backend with the JS backend, it's only distracting. These are different things with different purposes. Also don't try to outline how the implementation is gonna look like. Instead focus on the feature description, how it should look like and what problem it solves. |
Ok, I have reworked the feature proposal to make it more clear and not mention C backend |
This PR addresses:
They all have the objective of making JS generation more flexible and powerful in general and able to support ES modules and other more advanced JS features. Still a WIP. Hope to get it done by next weekend. |
Summary
Add ability to shadow variable assignment similar to proc shadowing calls to backend functions
Description
For a proc, we can redirect the function to call an underlying backend function with the same arguments
For
jsgen
it means it is currently impossible to do ES module imports (as far as I can see)Emits
Here the most recent
jsgen
code for proc and var generation respectively:For variable generation, there is no indirection, mapping to backend variables (that I can see)
Would be great if we could do sth like:
Where
$#
is the generated (mangled) var identifier andjsX
is the pointer to a Javascript backend variableThis would emit the correct Javascript:
Instead of
The text was updated successfully, but these errors were encountered: