Skip to content

Commit

Permalink
Updated Dependencies / Updated AssemblyScript Examples to latest AS a…
Browse files Browse the repository at this point in the history
…nd as-bind versions (#129)

* updated wasm-by-example deps, and fixed as-bind examples

* Updated the AS Wasi Example
  • Loading branch information
torch2424 authored Jul 28, 2021
1 parent cb799d8 commit 589cd51
Show file tree
Hide file tree
Showing 18 changed files with 9,552 additions and 3,962 deletions.
12 changes: 8 additions & 4 deletions build-system/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const fs = require("fs");
const mkdirp = require("mkdirp");
const cpy = require("cpy");
const recursive = require("recursive-readdir");
const recursiveCopy = require("recursive-copy");
const highlightJs = require("highlight.js");
Expand Down Expand Up @@ -39,7 +38,9 @@ const capitalizeWord = word => {
// https://stackoverflow.com/questions/48843806/how-to-use-npm-marked-with-highlightjs
marked.setOptions({
highlight: (code, lang) => {
return highlightJs.highlight(lang, code).value;
return highlightJs.highlight(code, {
language: lang
}).value;
}
});

Expand Down Expand Up @@ -356,14 +357,17 @@ const buildTask = async () => {

// Copy over any extra directories
mkdirp.sync("./dist/demo-util");
await cpy(["demo-util/"], "dist/demo-util");
await recursiveCopy("./demo-util", "./dist/demo-util", {
overwrite: true,
dot: true
});
await recursiveCopy("./assets", "./dist", {
overwrite: true,
dot: true
});

// Copy over our manifest.json (PWA Support)
await cpy(["shell/manifest.json"], "dist/");
fs.copyFileSync("shell/manifest.json", "./dist/manifest.json");

// Generate our Service Worker
// TODO: Once bug is fixed, set cache strategy on precache
Expand Down
37 changes: 19 additions & 18 deletions examples/classes/classes.assemblyscript.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,44 +82,45 @@ Classes in AssemblyScript can not yet implement an interface. AssemblyScript doe
enforce access level modifiers such as `public`, `private`, and `protected`. The `private`
keyword does prevent a public class from exporting the attribute or method to JavaScript,
but that is all. Now that we have our AssemblyScript written, we need to compile it into a
WASM module. To use `as-bind`, we have to include the `as-bind` library that we installed
WASM module. To use `as-bind`, we have to include the `as-bind` compiler transform that we installed
using `npm`. Use this `asc` command to compile the WASM module:

```bash
asc ./node_modules/as-bind/lib/assembly/as-bind.ts ASBindTest.ts -o ASBindTest.wasm
asc ASBindTest.ts -o ASBindTest.wasm --exportRuntime --transform as-bind
```

Compiling our code with the `as-bind.ts` file simplifies what we must do from our JavaScript.
Compiling our code with the `as-bind` transform simplifies what we must do from our JavaScript.
I will be using Node.js to run the JavaScript that calls into our WebAssembly module.
Create a file called `ASBindTest.js` and add the following code:

```typescript
```javascript
// I'm using node for this example
const { AsBind } = require("as-bind");
const AsBind = require("as-bind/dist/as-bind.cjs.js");
const fs = require("fs");
const wasm = fs.readFileSync("./ASBindTest.wasm");

// asynchronous IIFE for async/await
(async () => {
// use as-bind to instantiate WebAssembly
const asBindInstance = await AsBind.instantiate(wasm);
// destructure the classes created in ASBindTest.ts
({ Vector2D, Vector3D } = asBindInstance.unboundExports);
({ Vector2D, Vector3D } = asBindInstance.exports);
let vec2 = new Vector2D(3, 4); // create new Vector2D object
let vec3 = new Vector3D(3, 4, 5); // create new Vector3D objecst

console.log(`
----- 2D VECTOR -----
x: ${vec2.x}
y: ${vec2.y}
Magnitude: ${vec2.Magnitude()}
Magnitude Squared: ${vec2.MagSQ()}
----- 3D VECTOR -----
x: ${vec3.x}
y: ${vec3.y}
z: ${vec3.z}
w: ${vec3.w}
Magnitude: ${vec3.Magnitude()}
Magnitude Squared: ${vec3.MagSQ()}
----- 2D VECTOR -----
x: ${vec2.x}
y: ${vec2.y}
Magnitude: ${vec2.Magnitude()}
Magnitude Squared: ${vec2.MagSQ()}
----- 3D VECTOR -----
x: ${vec3.x}
y: ${vec3.y}
z: ${vec3.z}
w: ${vec3.w}
Magnitude: ${vec3.Magnitude()}
Magnitude Squared: ${vec3.MagSQ()}
`);
})();
```
Expand Down
Binary file modified examples/classes/demo/assemblyscript/ASBindTest.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions examples/classes/demo/assemblyscript/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// I'm using node for this example
const { AsBind } = require("as-bind");
const AsBind = require("as-bind/dist/as-bind.cjs.js");
const fs = require("fs");
const wasm = fs.readFileSync("./ASBindTest.wasm");
// asynchronous IIFE for async/await
(async () => {
// use as-bind to instantiate WebAssembly
const asBindInstance = await AsBind.instantiate(wasm);
// destructure the classes created in ASBindTest.ts
({ Vector2D, Vector3D } = asBindInstance.unboundExports);
({ Vector2D, Vector3D } = asBindInstance.exports);
let vec2 = new Vector2D(3, 4); // create new Vector2D object
let vec3 = new Vector3D(3, 4, 5); // create new Vector3D objecst

Expand Down
133 changes: 129 additions & 4 deletions examples/classes/demo/assemblyscript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions examples/classes/demo/assemblyscript/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"scripts": {
"build": "asc ./node_modules/as-bind/lib/assembly/as-bind.ts ASBindTest.ts -o ASBindTest.wasm"
},
"dependencies": {
"as-bind": "^0.3.1"
"start": "npm run build && node index.js",
"build": "asc ASBindTest.ts -o ASBindTest.wasm --exportRuntime --transform as-bind"
},
"devDependencies": {
"assemblyscript": "^0.9.4"
"assemblyscript": "^0.19.9"
},
"dependencies": {
"as-bind": "^0.8.0"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// We are including as-bind from the npm CDN unpkg. If you use a JavaScript bundler, you could use "as-bind".
import { AsBind } from "https://unpkg.com/as-bind@0.3.1/dist/as-bind.esm.js";
import * as AsBind from "https://unpkg.com/as-bind@0.8.0/dist/as-bind.esm.js";
import { domConsoleLog } from "/demo-util/domConsole.js";

const wasm = fetch("./addWasmByExample.wasm");
Expand Down
Loading

0 comments on commit 589cd51

Please sign in to comment.