Loader: support .wasm imports (#3328)

* loader: support .wasm imports

* http_server: true

* Support named exports

* Clippy
This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2019-11-14 05:31:39 -08:00 committed by Ry Dahl
parent fdf0ede2ac
commit 4189cc1ab5
20 changed files with 388 additions and 9 deletions

View file

@ -28,7 +28,8 @@ enum MediaType {
TypeScript = 2,
TSX = 3,
Json = 4,
Unknown = 5
Wasm = 5,
Unknown = 6
}
// Warning! The values in this enum are duplicated in cli/msg.rs
@ -44,8 +45,8 @@ enum CompilerRequestType {
const console = new Console(core.print);
window.console = console;
window.workerMain = workerMain;
function denoMain(): void {
os.start(true, "TS");
function denoMain(compilerType?: string): void {
os.start(true, compilerType || "TS");
}
window["denoMain"] = denoMain;
@ -371,6 +372,9 @@ function getExtension(fileName: string, mediaType: MediaType): ts.Extension {
return ts.Extension.Tsx;
case MediaType.Json:
return ts.Extension.Json;
case MediaType.Wasm:
// Custom marker for Wasm type.
return ts.Extension.Js;
case MediaType.Unknown:
default:
throw TypeError("Cannot resolve extension.");
@ -724,3 +728,47 @@ window.compilerMain = function compilerMain(): void {
workerClose();
};
};
function base64ToUint8Array(data: string): Uint8Array {
const binString = window.atob(data);
const size = binString.length;
const bytes = new Uint8Array(size);
for (let i = 0; i < size; i++) {
bytes[i] = binString.charCodeAt(i);
}
return bytes;
}
window.wasmCompilerMain = function wasmCompilerMain(): void {
// workerMain should have already been called since a compiler is a worker.
window.onmessage = async ({
data: binary
}: {
data: string;
}): Promise<void> => {
const buffer = base64ToUint8Array(binary);
// @ts-ignore
const compiled = await WebAssembly.compile(buffer);
util.log(">>> WASM compile start");
const importList = Array.from(
// @ts-ignore
new Set(WebAssembly.Module.imports(compiled).map(({ module }) => module))
);
const exportList = Array.from(
// @ts-ignore
new Set(WebAssembly.Module.exports(compiled).map(({ name }) => name))
);
postMessage({
importList,
exportList
});
util.log("<<< WASM compile end");
// The compiler isolate exits after a single message.
workerClose();
};
};