Comments (6)
Firstly note you can just write compileCore
to cache fetch (which you probably should).
I'd be open to this, but as a variation of the --instantiation
flag to indicate it's a third virtualization level sitting in between full virtualization and host-level resolution.
The current instantiation in this general form doesn't assume the location of Wasm modules on the filesystem or top-level await. In theory by not relying on Promise.all we also allow bindings work to continue even when some modules are still being compiled.
Perhaps --instantiation=full
is used to distinguish the current form, or something similar? Would you be interested in working on a PR?
from jco.
Firstly note you can just write
compileCore
to cache fetch (which you probably should).
Good idea. This would load and compile the cores "lazily" on the first instantiate, which has it's advantages, but might not always be desired.
I am still "stuck" in the "Component::new"/Module::new and "component.instantiate"/Module.instantiate being 2 separate things, but I guess I could do something like this:
export function preLoadCores(compileCore) {
return Promise.all([
compileCore('component.core.wasm'),
compileCore('component.core2.wasm'),
compileCore('component.core3.wasm'),
]);
}
export async function instantiate(compileCore, imports, instantiateCore = WebAssembly.instantiate) {
const module0 = compileCore('component.core.wasm');
const module1 = compileCore('component.core2.wasm');
const module2 = compileCore('component.core3.wasm');
// ...
}
The current instantiation in this general form doesn't assume the location of Wasm modules on the filesystem
That's true, the module bytes could be in-memory, which they are in my use-case
... or top-level await
I am not familiar enough with how JS modules are loaded to understand the pros and cons of top-level awaits.
One thing I will not though is that the exports without instantiation mode are kind of messed up.
You get an $init
object that you are supposed to await
, and the exported functions, which you should call after awaiting the init object, but the exported function are not the result of awaiting the init object, you just get and can call them "wrongly".
I understand that this is workaround for situation where the top-level await will not be awaited, but it's still kind of messed up.
... to indicate it's a third virtualization level sitting in between full virtualization and host-level resolution.
I will admit I have no idea what that means.
In theory by not relying on Promise.all we also allow bindings work to continue even when some modules are still being compiled.
That's a good point. I added the // TODO: should be Promise.all
mostly because in the proof of concept code the cores were being loaded sequentially instead of simultaneously, which Promise.all
would solve.
Perhaps --instantiation=full is used to distinguish the current form, or something similar? Would you be interested in working on a PR?
Maybe, what I would be interested in is exporting two separate functions:
compile
that compiles the cores and returns the instantiate function. This function can takecompileCore
as an argument, but it would be even better if it was an optional argument and the default would be to take the cores from the same directory as the .js script is it, the same way it is done without the instantiation flag.instantiate
function that works with the already-compiled cores. You cannot callinstantiate
without calling callingcompile
first, because you get theinstantiate
function as a result of thecompile
function.
If this workflow is something you would be willing to merge (enabled with the --instantiation=full
flag, let's say), then I can work on it.
from jco.
Your suggestions sounds fairly sensible to me. I would still prefer that compile
be optional though and that just-in-time compilation can still be used by default as it is currently structured. So that instead of this being an explicit compile
"step", it is an optional precompile
step.
Something like:
let compileCache = {};
const defaultCompileCore = name => compileCache[name] || (compileCache[name] = fetchCompile(name));
async function fetchCompile (name) {
// ... standard implementation ...
}
export const precompile = (compileCore = defaultCompileCore) => Promise.all([
compileCore('name1.core.wasm'),
// ...
]);
export async function instantiate(imports, compileCore = defaultCompileCore, instantiateCore = WebAssembly.instantiate, compileCore = ) {
// .. compileCore interleaving remains ..
const modulen = await compileCore('componentn.core.wasm');
}
Does the above work for you? If so, in this framing we don't even need another mode for it and could make it the default, possibly with an optional --no-precompile
flag or something to disable it?
from jco.
The argument order in instantiate
changed, but that can be fixed with something like
export async function instantiate(compileCore, imports, instantiateCore = WebAssembly.instantiate) {
compileCore = compileCore || defaultCompileCore,
// .. compileCore interleaving remains ..
const modulen = await compileCore('componentn.core.wasm');
}
Now it's slightly confusing that the first argument is optional, but changing the argument order is a breaking change.
I could start working on a PR, but currently I have other things I want to finish first.
from jco.
@kajacx JCO is currently under active development so it's fine to make breaking changes.
from jco.
We almost got to a concrete proposal here, which I'd be very happy to land in this project, but short of that work actually happening - since this is in theory purely ergonomic and neither a feature nor a bug, closing for now.
from jco.
Related Issues (20)
- How do I use `js-component-bindgen`? HOT 1
- Incorrect codegen for `wasi:http` resource `RequestOptions` HOT 2
- JCO serve error on composed component HOT 1
- Are TypeScript definitions generated by transpile supposed to include comments from the WIT file? HOT 2
- Validity of file descriptors in the context of the `SyncKit` worker HOT 3
- Unsupported URL wasi when running in Node.js HOT 2
- Transpile bindings for `u32` map to `number` in *.d.ts HOT 3
- Non-conformant exceptions should unwrap as traps HOT 2
- Jco transpile: implementation of wasi-nn for Web HOT 2
- Source phase imports output
- Performance
- JS/WIT Reference
- Lifting `list<borrow<res>>` during transpilation emits invalid code HOT 3
- Resource transferring seems not working with wasi-virt HOT 1
- Crash on startup with an out-of-memory error when environment variables are large HOT 3
- Provide wasm-tools APIs in the browser HOT 2
- `definedResourceTables` codegen generates invalid JS with `--instantiation async` HOT 1
- plain enums are generating complex typescript HOT 8
- Question: `jco types` generating namespaces instead of modules? HOT 1
- Better instantiation workflows
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jco.