effekt
effekt copied to clipboard
Special characters in file names break `js` backend
The file name just gets inserted into js as a identifier names, so ./examples/llvm/hello-world.effekt
generates the following js code:
const $effekt = require("./effekt.js");
const $hello-world = { };
function main() {
const tmp344 = $effekt.println("hello world");
return $effekt.pure(tmp344);
}
module.exports = Object.assign($hello-world, { main: main });
Which is not correct js, as -
can't be part of an identifier.
Can this be escaped as part of the following?
https://github.com/effekt-lang/effekt/blob/5fa89a2cfd03f4a9f899a8f242df33ad51e4e6b5/effekt/shared/src/main/scala/effekt/generator/js/JavaScript.scala#L72
Adding escaping here would work:
https://github.com/effekt-lang/effekt/blob/5fa89a2cfd03f4a9f899a8f242df33ad51e4e6b5/effekt/shared/src/main/scala/effekt/generator/js/JavaScript.scala#L74
But we would probably want to do this in a more principled way than replacing only some characters?
Alternatively, we could just whitelist allowed characters for file and module names.
But we would probably want to do this in a more principled way than replacing only some characters?
What do you have in mind?
Also, moduleName
and moduleFile
should also probably be in sync with each other :)
Alternatively, we could just whitelist allowed characters for file and module names.
Are you asking to change allowed names in source / Effekt?
If we want arbitrary file names to work, also with weird combinations, we would need to encode the characters used somehow (eg _??
for characer \x??
).
Replacing everything "special" with _
would solve most common cases, as long as this does not produce conflicting names.
Alternatively, we could just whitelist allowed characters for file and module names.
Are you asking to change allowed names in source / Effekt?
Module and file names, yes. That would also probably be an easy solution ( @jiribenes just proposed that as one way to go about it. )
Ok, so then maybe forbidding and erroring out early might be a good idea. I think a combination would be good. That is, the set of accepted white-listed characters should be as large as possible, while staying manageable.
So we could still also allow -
and sanitize it in JS while forbidding emojiis.
Ok, should hello-world
, hello_world
and hello/world
be different?
Then we can't just replace all of them with _
. (Although this admittedly is a corner case.)
The parser for module names only considers a small subset of possible file names -- all of which should be easily usable as JavaScript identifiers :) We might want to enforce this in file names as well. https://github.com/effekt-lang/effekt/blob/5fa89a2cfd03f4a9f899a8f242df33ad51e4e6b5/effekt/shared/src/main/scala/effekt/Parser.scala#L55-L58
We might want to enforce this in file names as well.
As long our "module system" work like it does, we also might want to check that file names and (manually specified) module names coincide.
Ok, should
hello-world
,hello_world
andhello/world
be different? Then we can't just replace all of them with_
. (Although this admittedly is a corner case.)
You mean, because then we will have clashes?
TBH I think it is fine for now. But can we maybe have a meeting next week where we try to discuss once and for all how the namespacing, files, and modules should be like? That would be a BIG step forward to having more and bigger case studies.
This is related to #30, where @phischu collected some requirements on a "module system".
It is also related to how the FFI works and how we determine what the "entrypoint" aka main
is. If we use something like ScalaJSs @export("main")
and switch to whole-program, then we can simply mingle all names and stop caring about them.