Skip to content

Commit ac3d6c4

Browse files
committed
Update WebSDK demo to use new option
1 parent e3b5158 commit ac3d6c4

File tree

7 files changed

+69
-1174
lines changed

7 files changed

+69
-1174
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ All primitive types, ie. Numbers (`u8`, `f32`, ...) , Strings, Typed Arrays (`Ui
169169

170170
Custom classes are currently not support, but planned.
171171

172+
## Browser SDK
173+
174+
as-bind works with the Browser SDK. For a fully working example, see the [`browser-sdk` example](/torch2424/as-bind/tree/master/examples/browser-sdk).
175+
172176
## Reference API
173177

174178
### AsBind

examples/browser-sdk/index.html

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,30 @@
55
<title>AssemblyScript SDK Example</title>
66
</head>
77
<body>
8+
<!-- Load as-bind runtime -->
89
<script src="/dist/as-bind.iife.js"></script>
9-
<script src="./require.js"></script>
10+
<!-- Load requirejs to handle the AMD version of the sdk -->
11+
<script src="/node_modules/requirejs/require.js"></script>
1012
<script>
1113
const SOURCE_CODE = `
1214
export function test(): string {
1315
return "ohai";
1416
}`;
1517

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-
4118
require(["/node_modules/assemblyscript/dist/sdk.js"], ({
4219
asc,
4320
assemblyscript
4421
}) => {
4522
// Register the `assemblyscript` property as a module of its own
4623
// as is expected by most transform modules.
47-
define("assemblyscript", assemblyscript);
24+
define("assemblyscript", [], assemblyscript);
25+
// `visitor-as/as` is usually a facade for `assemblyscript` to avoid
26+
// multiple instances of `assemblyscript` being loaded.
27+
// So in this case we can take matters into our own hands and just replace
28+
// it with out `assemblyscript` instance.
29+
define("visitor-as/as", [], assemblyscript);
4830
// Load our ASBind transform...
49-
require(["/dist/transform.amd.js"], transform => {
50-
// ... and put it on a global
51-
self[asbindTransform] = transform;
31+
require(["/dist/transform.amd.js"], asbind => {
5232
asc.ready.then(() => {
5333
const stdout = asc.createMemoryStream();
5434
const stderr = asc.createMemoryStream();
@@ -59,12 +39,12 @@
5939
"-O3",
6040
"--exportRuntime",
6141
"--runtime", "stub",
62-
"--binaryFile", "module.wasm",
63-
"--transform", "/dist/transform.js",
42+
"--binaryFile", "module.wasm"
6443
],
6544
{
6645
stdout,
6746
stderr,
47+
transforms: [asbind],
6848
readFile(name, baseDir) {
6949
return name === "module.ts" ? SOURCE_CODE : null;
7050
},

0 commit comments

Comments
 (0)