|
| 1 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script> |
| 2 | +<script> |
| 3 | +require([ "https://cdn.jsdelivr.net/npm/assemblyscript@latest/dist/sdk.js" ], function(sdk) { |
| 4 | + const { asc } = sdk; |
| 5 | + asc.ready.then(() => { |
| 6 | + console.log("Running simple example..."); |
| 7 | + simpleExample(asc); |
| 8 | + console.log("\nRunning extended example..."); |
| 9 | + extendedExample(asc); |
| 10 | + }); |
| 11 | +}); |
| 12 | + |
| 13 | +// This uses `asc.compileString`, a convenience API useful if all one wants to |
| 14 | +// do is to quickly compile a single source string to WebAssembly. |
| 15 | +function simpleExample(asc) { |
| 16 | + const { text, binary } = asc.compileString(`export function test(): void {}`, { |
| 17 | + optimizeLevel: 3, |
| 18 | + runtime: "none" |
| 19 | + }); |
| 20 | + console.log(">>> TEXT >>>\n" + text); |
| 21 | + console.log(">>> BINARY >>>\n" + binary.length + " bytes"); |
| 22 | +} |
| 23 | + |
| 24 | +// The full API works very much like asc on the command line, with additional |
| 25 | +// environment bindings being provided to access the (virtual) file system. |
| 26 | +function extendedExample(asc) { |
| 27 | + const stdout = asc.createMemoryStream(); |
| 28 | + const stderr = asc.createMemoryStream(); |
| 29 | + asc.main([ |
| 30 | + "module.ts", |
| 31 | + "-O3", |
| 32 | + "--runtime", "none", |
| 33 | + "--binaryFile", "module.wasm", |
| 34 | + "--textFile", "module.wat", |
| 35 | + "--sourceMap" |
| 36 | + ], { |
| 37 | + stdout: stdout, |
| 38 | + stderr: stderr, |
| 39 | + readFile: (name, baseDir) => { |
| 40 | + if (name === "module.ts") return `export function test(): void {}`; |
| 41 | + return null; |
| 42 | + }, |
| 43 | + writeFile: (name, data, baseDir) => { |
| 44 | + console.log(">>> WRITE:" + name + " >>>\n" + data.length); |
| 45 | + }, |
| 46 | + listFiles: (dirname, baseDir) => { |
| 47 | + return []; |
| 48 | + } |
| 49 | + }, err => { |
| 50 | + console.log(">>> STDOUT >>>\n" + stdout.toString()); |
| 51 | + console.log(">>> STDERR >>>\n" + stderr.toString()); |
| 52 | + if (err) { |
| 53 | + console.log(">>> THROWN >>>"); |
| 54 | + console.log(err); |
| 55 | + } |
| 56 | + }); |
| 57 | +} |
| 58 | +</script> |
| 59 | +<p>See the browser console!</p> |
0 commit comments