Skip to content

Commit d50a8d0

Browse files
authored
Merge pull request #75 from torch2424/type-embed-browser-sdk
Add support for Browser SDK
2 parents d932dec + 3ff3072 commit d50a8d0

File tree

6 files changed

+14013
-28
lines changed

6 files changed

+14013
-28
lines changed

examples/browser-sdk/index.html

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>AssemblyScript SDK Example</title>
6+
</head>
7+
<body>
8+
<script src="/dist/as-bind.iife.js"></script>
9+
<script src="./require.js"></script>
10+
<script>
11+
const SOURCE_CODE = `
12+
export function test(): string {
13+
return "ohai";
14+
}`;
15+
16+
// A symbol allows us to store something as a global without risking
17+
// name clashes or pollution.
18+
const asbindTransform = Symbol();
19+
20+
// Shimming the require() call to *synchronously*
21+
// return our transform module that we expect to be on a global.
22+
// Any calls unrelated to the transform
23+
// are implicitly passed through to RequireJS.
24+
self.require = new Proxy(self.require, {
25+
apply(target, thisArg, [path, ...args]) {
26+
if (path !== "/dist/transform.js") {
27+
return target.call(target, path, ...args);
28+
}
29+
return self[asbindTransform];
30+
},
31+
get(target, propName) {
32+
if (propName !== "resolve") {
33+
return target[propName];
34+
}
35+
return (path, ...args) => {
36+
return path;
37+
};
38+
}
39+
});
40+
41+
require(["/node_modules/assemblyscript/dist/sdk.js"], ({
42+
asc,
43+
assemblyscript
44+
}) => {
45+
// Register the `assemblyscript` property as a module of its own
46+
// as is expected by most transform modules.
47+
define("assemblyscript", assemblyscript);
48+
// Load our ASBind transform...
49+
require(["/dist/transform.amd.js"], transform => {
50+
// ... and put it on a global
51+
self[asbindTransform] = transform;
52+
asc.ready.then(() => {
53+
const stdout = asc.createMemoryStream();
54+
const stderr = asc.createMemoryStream();
55+
asc.main(
56+
// prettier-ignore
57+
[
58+
"module.ts",
59+
"-O3",
60+
"--exportRuntime",
61+
"--runtime", "stub",
62+
"--binaryFile", "module.wasm",
63+
"--transform", "/dist/transform.js",
64+
],
65+
{
66+
stdout,
67+
stderr,
68+
readFile(name, baseDir) {
69+
return name === "module.ts" ? SOURCE_CODE : null;
70+
},
71+
async writeFile(name, data, baseDir) {
72+
if (name.endsWith(".wasm")) {
73+
const instance = await AsBindIIFE.instantiate(data);
74+
console.log("Output:", instance.exports.test());
75+
}
76+
},
77+
listFiles(dirname, baseDir) {
78+
return [];
79+
}
80+
},
81+
err => {
82+
console.log(`>>> STDOUT >>>\n${stdout.toString()}`);
83+
console.log(`>>> STDERR >>>\n${stderr.toString()}`);
84+
if (err) {
85+
console.log(">>> THROWN >>>");
86+
console.log(err);
87+
}
88+
}
89+
);
90+
});
91+
});
92+
});
93+
</script>
94+
<p>See the browser console!</p>
95+
</body>
96+
</html>

0 commit comments

Comments
 (0)