-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
14,013 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => { | ||
// ... 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> |
Oops, something went wrong.