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