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