Skip to content

Commit dd2f050

Browse files
committed
Add transform example
1 parent e89f6b2 commit dd2f050

File tree

8 files changed

+123
-12
lines changed

8 files changed

+123
-12
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@ General examples showing how to utilize specific AssemblyScript features.
5252
* [Browser SDK](./sdk)<br />
5353
Shows how to use the browser SDK to run the AssemblyScript compiler in the browser.
5454

55-
<img src="./sdk/preview.jpg" />
55+
<img src="./sdk/preview.jpg" />
56+
57+
* [Compiler transforms](./transform)<br />
58+
An example of using compiler transforms to hook into the compilation process.

loader/assembly/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export function getMyArray(size: i32): Int32Array {
4646

4747
// Example 5: Passing an array from JavaScript to WebAssembly.
4848

49-
// Likewise, we can also allocate an array on the JavaScript side and pass it
50-
// its pointer to WebAssembly, then doing something with it.
49+
// Likewise, we can also allocate an array on the JavaScript side and pass its
50+
// pointer to WebAssembly, then doing something with it.
5151

5252
// see: tests/index.js "Test for Example 5"
5353

loader/tests/index.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,48 @@ const {
121121
player = new myModule.Game.Player(namePtr);
122122
__release(namePtr);
123123
}
124-
console.log(" Player (new): " + __getString(player.toString()));
124+
125+
// Let's see how our player looks now by calling toString
126+
{
127+
const strPtr = player.toString();
128+
console.log(" Player (new): " + __getString(strPtr));
129+
__release(strPtr);
130+
}
125131

126132
// Move them and log again
127-
player.move(10, 20);
128-
console.log(" Player (moved): " + __getString(player.toString()));
133+
{
134+
player.move(10, 20);
135+
const strPtr = player.toString();
136+
console.log(" Player (moved): " + __getString(strPtr));
137+
__release(strPtr);
138+
}
129139

130140
// Obtaining just the position. Note that we can `wrap` any pointer with
131-
// the matching class within the object structure made by the loader.
141+
// the matching class within the object structure made by the loader, and
142+
// that a position's x and y properties are just basic values, not objects,
143+
// so tracking references does not apply to them.
132144
{
133-
const positionPtr = player.position; // calls a getter (implicit `return`)
145+
const positionPtr = player.position; // implicit getter, retained for us
134146
const position = myModule.Game.Position.wrap(positionPtr);
135147
console.log(" Position (wrapped): " + position.x + "/" + position.y);
136148

137149
position.x -= 100;
138150
position.y += 200;
139-
console.log(" Position (moved): " + __getString(position.toString()));
140151

141-
__release(positionPtr); // we are done with the returned object
152+
const strPtr = position.toString();
153+
console.log(" Position (moved): " + __getString(strPtr));
154+
__release(strPtr);
155+
156+
__release(positionPtr);
142157
}
143158

144159
// Finish 'em
145-
player.kill();
146-
console.log(" Player (finished): " + __getString(player.toString()));
160+
{
161+
player.kill();
162+
const strPtr = player.toString();
163+
console.log(" Player (finished): " + __getString(strPtr));
164+
__release(strPtr); // we are done with the returned object
165+
}
147166

148167
__release(player); // a tidy house, a tidy mind.
149168
}

transform/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Compiler transforms
2+
===================
3+
4+
An [AssemblyScript](http://assemblyscript.org) example of using compiler transforms to hook into the compilation process.
5+
6+
Instructions
7+
------------
8+
9+
* [JavaScript transform](./mytransform.js)<br />
10+
An ES6 JavaScript transform. Recommended.
11+
12+
* [TypeScript transform](./mytransform.ts)<br />
13+
A transform written in TypeScript. This works currently because the compiler
14+
utilizes ts-node anyway, so can as well transpile the transform to ES6.
15+
16+
See [package.json](./package.json) on how to utilize the transforms with `asc`.
17+
You may also run
18+
19+
```
20+
$> npm install
21+
$> npm test
22+
```
23+
24+
to verify that it works.

transform/assembly/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

transform/mytransform.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { Transform } = require("assemblyscript/cli/transform");
2+
const { SourceKind, ElementKind } = require("assemblyscript");
3+
const binaryen = require("binaryen");
4+
5+
class MyTransform extends Transform {
6+
afterParse(parser) {
7+
this.log("[mytransform.js] afterParse called, baseDir = " + this.baseDir);
8+
var sources = parser.program.sources;
9+
sources.forEach(source => this.log(" " + source.internalPath + " [" + SourceKind[source.sourceKind] + "]"));
10+
}
11+
afterInitialize(program) {
12+
this.log("[mytransform.js] afterInitialize called");
13+
var elements = program.elementsByName;
14+
elements.forEach(element => this.log(" " + element.internalName + " [" + ElementKind[element.kind] + "]"));
15+
}
16+
afterCompile(asModule) {
17+
this.log("[mytransform.js] afterCompile called");
18+
var module = binaryen.wrapModule(asModule.ref);
19+
this.log(module.emitBinary());
20+
}
21+
}
22+
23+
module.exports = MyTransform;

transform/mytransform.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Transform } from "assemblyscript/cli/transform";
2+
import { Parser, Module, SourceKind, ElementKind, Program } from "assemblyscript";
3+
import * as binaryen from "binaryen";
4+
5+
class MyTransform extends Transform {
6+
afterParse(parser: Parser): void {
7+
this.log("[mytransform.ts] afterParse called, baseDir = " + this.baseDir);
8+
var sources = parser.program.sources;
9+
sources.forEach(source => this.log(" " + source.internalPath + " [" + SourceKind[source.sourceKind] + "]"));
10+
}
11+
afterInitialize(program: Program) {
12+
this.log("[mytransform.ts] afterInitialize called");
13+
var elements = program.elementsByName;
14+
elements.forEach(element => this.log(" " + element.internalName + " [" + ElementKind[element.kind] + "]"));
15+
}
16+
afterCompile(asModule: Module): void {
17+
this.log("[mytransform.ts] afterCompile called");
18+
var module = binaryen.wrapModule(asModule.ref);
19+
this.log(module.emitBinary());
20+
}
21+
}
22+
23+
console.log()
24+
25+
export = MyTransform;

transform/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@assemblyscript/transform-example",
3+
"version": "1.0.0",
4+
"private": true,
5+
"scripts": {
6+
"test:js": "asc assembly/index.ts --runtime none --transform ./mytransform.js",
7+
"test:ts": "asc assembly/index.ts --runtime none --transform ./mytransform.ts",
8+
"test:multi": "asc assembly/index.ts --runtime none --transform ./mytransform.js --transform ./mytransform.ts",
9+
"test": "npm run test:js && npm run test:ts && npm run test:multi"
10+
},
11+
"devDependencies": {
12+
"assemblyscript": "latest",
13+
"ts-node": "^8.10.1",
14+
"typescript": "^3.8.3"
15+
}
16+
}

0 commit comments

Comments
 (0)