Skip to content

Commit

Permalink
Update WebSDK demo to use new option
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Apr 24, 2021
1 parent e3b5158 commit ac3d6c4
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 1,174 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ All primitive types, ie. Numbers (`u8`, `f32`, ...) , Strings, Typed Arrays (`Ui

Custom classes are currently not support, but planned.

## Browser SDK

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).

## Reference API

### AsBind
Expand Down
44 changes: 12 additions & 32 deletions examples/browser-sdk/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,30 @@
<title>AssemblyScript SDK Example</title>
</head>
<body>
<!-- Load as-bind runtime -->
<script src="/dist/as-bind.iife.js"></script>
<script src="./require.js"></script>
<!-- Load requirejs to handle the AMD version of the sdk -->
<script src="/node_modules/requirejs/require.js"></script>
<script>
const SOURCE_CODE = `
export function test(): string {
return "ohai";
}`;

// A symbol allows us to store something as a global without risking
// name clashes or pollution.
const asbindTransform = Symbol();

// Shimming the require() call to *synchronously*
// return our transform module that we expect to be on a global.
// Any calls unrelated to the transform
// are implicitly passed through to RequireJS.
self.require = new Proxy(self.require, {
apply(target, thisArg, [path, ...args]) {
if (path !== "/dist/transform.js") {
return target.call(target, path, ...args);
}
return self[asbindTransform];
},
get(target, propName) {
if (propName !== "resolve") {
return target[propName];
}
return (path, ...args) => {
return path;
};
}
});

require(["/node_modules/assemblyscript/dist/sdk.js"], ({
asc,
assemblyscript
}) => {
// Register the `assemblyscript` property as a module of its own
// as is expected by most transform modules.
define("assemblyscript", assemblyscript);
define("assemblyscript", [], assemblyscript);
// `visitor-as/as` is usually a facade for `assemblyscript` to avoid
// multiple instances of `assemblyscript` being loaded.
// So in this case we can take matters into our own hands and just replace
// it with out `assemblyscript` instance.
define("visitor-as/as", [], assemblyscript);
// Load our ASBind transform...
require(["/dist/transform.amd.js"], transform => {
// ... and put it on a global
self[asbindTransform] = transform;
require(["/dist/transform.amd.js"], asbind => {
asc.ready.then(() => {
const stdout = asc.createMemoryStream();
const stderr = asc.createMemoryStream();
Expand All @@ -59,12 +39,12 @@
"-O3",
"--exportRuntime",
"--runtime", "stub",
"--binaryFile", "module.wasm",
"--transform", "/dist/transform.js",
"--binaryFile", "module.wasm"
],
{
stdout,
stderr,
transforms: [asbind],
readFile(name, baseDir) {
return name === "module.ts" ? SOURCE_CODE : null;
},
Expand Down

0 comments on commit ac3d6c4

Please sign in to comment.