Skip to content

Commit f80c33c

Browse files
authored
Add a browser SDK example (AssemblyScript#1079)
1 parent ab964b4 commit f80c33c

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
lines changed

examples/sdk/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Browser SDK
2+
===========
3+
4+
An [AssemblyScript](http://assemblyscript.org) example using the [AssemblyScript browser SDK](https://github.com/AssemblyScript/assemblyscript/tree/master/lib/sdk).
5+
6+
Instructions
7+
------------
8+
9+
Open [index.html](./index.html) in a browser and see the console. View the source to learn how it works.

examples/sdk/index.html

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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>

lib/sdk/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Exports
1515
* **binaryen**<br />
1616
The version of binaryen required by the compiler.
1717

18+
* **long**<br />
19+
The version of long.js required by the compiler.
20+
1821
* **assemblyscript**<br />
1922
The AssemblyScript compiler as a library.
2023

@@ -27,12 +30,14 @@ Example usage
2730

2831
```js
2932
require(
30-
["https://cdn.jsdelivr.net/npm/assemblyscript@nightly/dist/sdk"],
33+
["https://cdn.jsdelivr.net/npm/assemblyscript@latest/dist/sdk"],
3134
function(sdk) {
32-
const { binaryen, assemblyscript, asc } = sdk;
35+
const { asc } = sdk;
3336
asc.ready.then(() => {
3437
asc.main(...);
3538
});
3639
}
3740
);
3841
```
42+
43+
There is also the [SDK example](https://github.com/AssemblyScript/assemblyscript/tree/master/examples/sdk) showing how to compile some actual code.

lib/sdk/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
const BINARYEN_VERSION = "nightly";
2+
const LONG_VERSION = "latest";
23
const ASSEMBLYSCRIPT_VERSION = "nightly";
34

45
// AMD/require.js (browser)
56
if (typeof define === "function" && define.amd) {
67
const paths = {
78
"binaryen": "https://cdn.jsdelivr.net/npm/binaryen@" + BINARYEN_VERSION + "/index",
9+
"long": "https://cdn.jsdelivr.net/npm/long@" + LONG_VERSION + "/dist/long",
810
"assemblyscript": "https://cdn.jsdelivr.net/npm/assemblyscript@" + ASSEMBLYSCRIPT_VERSION + "/dist/assemblyscript",
911
"assemblyscript/cli/asc": "https://cdn.jsdelivr.net/npm/assemblyscript@" + ASSEMBLYSCRIPT_VERSION + "/dist/asc",
1012
};
1113
require.config({ paths });
12-
define(Object.keys(paths), (binaryen, assemblyscript, asc) => ({
14+
define(Object.keys(paths), (binaryen, long, assemblyscript, asc) => ({
1315
BINARYEN_VERSION,
16+
LONG_VERSION,
1417
ASSEMBLYSCRIPT_VERSION,
1518
binaryen,
19+
long,
1620
assemblyscript,
1721
asc
1822
}));
@@ -21,8 +25,10 @@ if (typeof define === "function" && define.amd) {
2125
} else if (typeof module === "object" && module.exports) {
2226
module.exports = {
2327
BINARYEN_VERSION,
28+
LONG_VERSION,
2429
ASSEMBLYSCRIPT_VERSION,
2530
binaryen: require("binaryen"),
31+
long: require("long"),
2632
assemblyscript: require("assemblyscript"),
2733
asc: require("assemblyscript/cli/asc")
2834
};

lib/sdk/tests/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
if (!x) throw Error("assertion failed");
55
}
66
require(["../index"], function(sdk) {
7-
const { BINARYEN_VERSION, ASSEMBLYSCRIPT_VERSION, binaryen, assemblyscript, asc } = sdk;
7+
const { BINARYEN_VERSION, LONG_VERSION, ASSEMBLYSCRIPT_VERSION, binaryen, long, assemblyscript, asc } = sdk;
88
assert(typeof BINARYEN_VERSION === "string");
9+
assert(typeof LONG_VERSION === "string");
910
assert(typeof ASSEMBLYSCRIPT_VERSION === "string");
1011
console.log("Binaryen@" + BINARYEN_VERSION);
12+
console.log("Long@" + LONG_VERSION);
1113
console.log("AssemblyScript@" + ASSEMBLYSCRIPT_VERSION);
1214
assert(typeof binaryen === "object" && binaryen && typeof binaryen._BinaryenTypeNone === "function");
15+
assert(typeof long === "function" && long && typeof long.fromInt === "function");
1316
assert(typeof assemblyscript === "object" && assemblyscript && typeof assemblyscript.parse === "function");
1417
assert(typeof asc === "object" && asc && typeof asc.main === "function");
1518
asc.ready.then(() => console.log("ready", sdk));

scripts/build-sdk.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const path = require("path");
22
const fs = require("fs");
3-
const pkg = require("../package.json");
3+
const pkg = require("../package-lock.json");
44

55
fs.readFile(path.join(__dirname, "..", "lib", "sdk", "index.js"), "utf8", function(err, data) {
66
if (err) throw err;
77
data = data
8-
.replace(/BINARYEN_VERSION = "nightly"/, "BINARYEN_VERSION = " + JSON.stringify(pkg.dependencies.binaryen))
8+
.replace(/BINARYEN_VERSION = "nightly"/, "BINARYEN_VERSION = " + JSON.stringify(pkg.dependencies.binaryen.version))
9+
.replace(/LONG_VERSION = "latest"/, "LONG_VERSION = " + JSON.stringify(pkg.dependencies.long.version))
910
.replace(/ASSEMBLYSCRIPT_VERSION = "nightly"/, "ASSEMBLYSCRIPT_VERSION = " + JSON.stringify(pkg.version));
1011
fs.writeFile(path.join(__dirname, "..", "dist", "sdk.js"), data, function(err) {
1112
if (err) throw err;

0 commit comments

Comments
 (0)