effekt icon indicating copy to clipboard operation
effekt copied to clipboard

Special characters in file names break `js` backend

Open marzipankaiser opened this issue 2 years ago • 15 comments

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.

marzipankaiser avatar Sep 23 '22 13:09 marzipankaiser

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

b-studios avatar Sep 23 '22 13:09 b-studios

Adding escaping here would work:

https://github.com/effekt-lang/effekt/blob/5fa89a2cfd03f4a9f899a8f242df33ad51e4e6b5/effekt/shared/src/main/scala/effekt/generator/js/JavaScript.scala#L74

marzipankaiser avatar Sep 23 '22 14:09 marzipankaiser

But we would probably want to do this in a more principled way than replacing only some characters?

marzipankaiser avatar Sep 23 '22 14:09 marzipankaiser

Alternatively, we could just whitelist allowed characters for file and module names.

marzipankaiser avatar Sep 23 '22 14:09 marzipankaiser

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 :)

b-studios avatar Sep 23 '22 14:09 b-studios

Alternatively, we could just whitelist allowed characters for file and module names.

Are you asking to change allowed names in source / Effekt?

b-studios avatar Sep 23 '22 14:09 b-studios

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.

marzipankaiser avatar Sep 23 '22 14:09 marzipankaiser

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. )

marzipankaiser avatar Sep 23 '22 14:09 marzipankaiser

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.

b-studios avatar Sep 23 '22 14:09 b-studios

So we could still also allow - and sanitize it in JS while forbidding emojiis.

b-studios avatar Sep 23 '22 14:09 b-studios

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.)

marzipankaiser avatar Sep 23 '22 14:09 marzipankaiser

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

jiribenes avatar Sep 23 '22 14:09 jiribenes

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.

b-studios avatar Sep 23 '22 14:09 b-studios

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.)

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.

b-studios avatar Sep 23 '22 14:09 b-studios

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.

b-studios avatar Sep 23 '22 14:09 b-studios