Minor launcher changes to run in the browser.
This commit is contained in:
committed by
alexander-gorshenev
parent
d488e35219
commit
7aa6977ea6
@@ -0,0 +1,14 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<script src="/dist/konan/nativelib/launcher.js"> </script>
|
||||||
|
<script>
|
||||||
|
loadAndInvoke("/program.wasm");
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
@@ -18,13 +18,43 @@ var module;
|
|||||||
var instance;
|
var instance;
|
||||||
var heap;
|
var heap;
|
||||||
var memory;
|
var memory;
|
||||||
var global_arguments = arguments;
|
var global_arguments;
|
||||||
var exit_status = 0;
|
var globalBase = 0; // TODO: Is there any way to obtain global_base from JavaScript?
|
||||||
var globalBase = 0; // Is there any way to obtain global_base from JavaScript?
|
|
||||||
|
function isBrowser() {
|
||||||
|
if (typeof window === 'undefined') {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isBrowser()) {
|
||||||
|
runtime = {
|
||||||
|
print: console.log,
|
||||||
|
stdout: '',
|
||||||
|
write: function (message) {
|
||||||
|
this.stdout += message;
|
||||||
|
var lastNewlineIndex = this.stdout.lastIndexOf('\n');
|
||||||
|
if (lastNewlineIndex == -1) return;
|
||||||
|
this.print(this.stdout.substring(0, lastNewlineIndex));
|
||||||
|
this.stdout = this.stdout.substring(lastNewlineIndex + 1)
|
||||||
|
},
|
||||||
|
flush: function () {
|
||||||
|
this.print(this.stdout);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
runtime = {
|
||||||
|
write: write,
|
||||||
|
print: print,
|
||||||
|
flush: function() {},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function print_usage() {
|
function print_usage() {
|
||||||
// TODO: any reliable way to obtain the current script name?
|
// TODO: any reliable way to obtain the current script name?
|
||||||
print('Usage: d8 --expose-wasm launcher.js -- <program.wasm> <program arg1> <program arg2> ...')
|
runtime.print('Usage: d8 --expose-wasm launcher.js -- <program.wasm> <program arg1> <program arg2> ...')
|
||||||
quit(1); // TODO: this is d8 specific
|
quit(1); // TODO: this is d8 specific
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,32 +153,54 @@ var konan_dependencies = {
|
|||||||
if (fd != 1 && fd != 2) throw ("write(" + fd + ", ...)");
|
if (fd != 1 && fd != 2) throw ("write(" + fd + ", ...)");
|
||||||
// TODO: There is no writeErr() in d8.
|
// TODO: There is no writeErr() in d8.
|
||||||
// Approximate it with write() to stdout for now.
|
// Approximate it with write() to stdout for now.
|
||||||
write(utf8decode(toString(str))); // TODO: write() d8 specific.
|
runtime.write(utf8decode(toString(str)));
|
||||||
},
|
},
|
||||||
memory: new WebAssembly.Memory({ initial: 256, maximum: 16384 })
|
memory: new WebAssembly.Memory({ initial: 256, maximum: 16384 })
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (arguments.length < 1) print_usage();
|
function invokeModule(buffer, args) {
|
||||||
|
if (args.length < 1) print_usage();
|
||||||
|
global_arguments = args;
|
||||||
|
|
||||||
module = new WebAssembly.Module(new Uint8Array(readbuffer(arguments[0])));
|
module = new WebAssembly.Module(new Uint8Array(buffer));
|
||||||
module.env = {};
|
module.env = {};
|
||||||
module.env.memoryBase = 0;
|
module.env.memoryBase = 0;
|
||||||
module.env.tablebase = 0;
|
module.env.tablebase = 0;
|
||||||
|
|
||||||
instance = new WebAssembly.Instance(module, konan_dependencies);
|
instance = new WebAssembly.Instance(module, konan_dependencies);
|
||||||
memory = konan_dependencies.env.memory
|
memory = konan_dependencies.env.memory
|
||||||
heap = new Uint8Array(konan_dependencies.env.memory.buffer);
|
heap = new Uint8Array(konan_dependencies.env.memory.buffer);
|
||||||
konanStackTop = stackTop();
|
konanStackTop = stackTop();
|
||||||
|
|
||||||
try {
|
var exit_status = 0;
|
||||||
runGlobalInitializers(instance.exports);
|
try {
|
||||||
exit_status = instance.exports.Konan_js_main(arguments.length);
|
runGlobalInitializers(instance.exports);
|
||||||
} catch (e) {
|
exit_status = instance.exports.Konan_js_main(args.length);
|
||||||
print("Exception executing Konan_js_main: " + e);
|
} catch (e) {
|
||||||
print(e.stack);
|
runtime.print("Exception executing Konan_js_main: " + e);
|
||||||
exit_status = 1;
|
runtime.print(e.stack);
|
||||||
|
exit_status = 1;
|
||||||
|
}
|
||||||
|
runtime.flush();
|
||||||
|
return exit_status;
|
||||||
}
|
}
|
||||||
|
|
||||||
quit(exit_status); // TODO: d8 specific.
|
// Invoke from the browser.
|
||||||
|
function loadAndInvoke(filename) {
|
||||||
|
function listener () {
|
||||||
|
invokeModule(this.response, [filename]);
|
||||||
|
}
|
||||||
|
|
||||||
|
var request = new XMLHttpRequest();
|
||||||
|
request.addEventListener("load", listener);
|
||||||
|
request.open("GET", filename);
|
||||||
|
request.responseType = "arraybuffer";
|
||||||
|
request.send();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invoke from d8.
|
||||||
|
if (!isBrowser()) {
|
||||||
|
quit(invokeModule(readbuffer(arguments[0]), arguments));
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user