Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Browser SDK #75

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions examples/browser-sdk/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>AssemblyScript SDK Example</title>
</head>
<body>
<script src="/dist/as-bind.iife.js"></script>
<script src="./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);
// Load our ASBind transform...
require(["/dist/transform.amd.js"], transform => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woooaaahhh that's interesting how you load a transform in the browser SDK 😯

// ... and put it on a global
self[asbindTransform] = transform;
asc.ready.then(() => {
const stdout = asc.createMemoryStream();
const stderr = asc.createMemoryStream();
asc.main(
// prettier-ignore
[
"module.ts",
"-O3",
"--exportRuntime",
"--runtime", "stub",
"--binaryFile", "module.wasm",
"--transform", "/dist/transform.js",
],
{
stdout,
stderr,
readFile(name, baseDir) {
return name === "module.ts" ? SOURCE_CODE : null;
},
async writeFile(name, data, baseDir) {
if (name.endsWith(".wasm")) {
const instance = await AsBindIIFE.instantiate(data);
console.log("Output:", instance.exports.test());
}
},
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>
Loading