goja icon indicating copy to clipboard operation
goja copied to clipboard

Support import/export aka modules

Open mstoykov opened this issue 3 years ago • 3 comments

I am adding this issue mostly to say that I and @oleiade will be working on it this week and hopefully will have at least a WIP/PoC PR by the end of it, in case somebody else decides to work on it in the meantime.

After we have made some progress and have nailed down some things I will update this issue with more info

mstoykov avatar Dec 06 '21 13:12 mstoykov

What does the code look like for selecting the actual file/module?

Something like this in my head if my question isn't clear.

vm.SetModuleLoader(func(module string) (string, error) {
    // do something to load module code
    moduleCode := getModuleCodeById(module)
    return moduleCode, nil
})

faisalraja avatar Dec 16 '21 23:12 faisalraja

Sorry for the slow reply @faisalraja , there has been some holidays ;).

The API is far from complete (everything really is) - but no. In order for the possibility of having not JS modules, but for example having some made by the host environment (the program inside which the JS is running), the specification specifies (:wink:) HostResolveImportedModule which returns an instance of a Module(Record).

So that means that you will need, if the Module that is being imported is a text module(js code basically), to parse it and then return it as shown in the tc39_test.go changes we have done: https://github.com/MStoykov/goja/blob/96d718b7c25a57a2c028dd87484f244c46324e21/tc39_test.go#L686-L716 Instead of just returning the code. I am not dismissing the idea of having a helper for this, but there are more pressing matters ;)

I would like to also note that all of this code is a PoC/WIP, and it still doesn't work, which hopefully will take me a day or two to fix. And after it actually works ~it's likely~ all of the API will be redone to be again, close to the specification, but not as verbatim as currently.

mstoykov avatar Jan 05 '22 16:01 mstoykov

A draft PR has been opened :tada:

mstoykov avatar Sep 08 '22 07:09 mstoykov