-
-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathindex.ts
More file actions
97 lines (84 loc) · 3.35 KB
/
index.ts
File metadata and controls
97 lines (84 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import * as fs from "fs";
import * as path from "path";
import * as binaryen from "binaryen";
import * as util from "util";
import * as loader from "../../lib/loader";
import * as find from "../../cli/util/find";
import AssemblyScript from "../../out/assemblyscript";
Error.stackTraceLimit = Infinity;
// Load stdlib
const libDir = path.join(__dirname, "..", "..", "std", "assembly");
const libraryFiles = {};
find.files(libDir, /^(?!.*\.d\.ts$).*\.ts$/).forEach((file: string) => {
libraryFiles[file.replace(/\.ts$/, "")] = fs.readFileSync(path.join(libDir, file), "utf8" );
});
async function test(build: string): Promise<void> {
await binaryen.ready;
const { exports: asc } = await loader.instantiate<typeof AssemblyScript>(
fs.promises.readFile(`${ __dirname }/../../out/assemblyscript.${ build }.wasm`),
{ binaryen }
);
console.log(util.inspect(asc, true));
const cachedStrings = new Map<string,number>();
function cachedString(text: string): number {
if (cachedStrings.has(text)) return cachedStrings.get(text);
var ptr = asc.__retain(asc.__allocString(text));
cachedStrings.set(text, ptr);
return ptr;
}
const programPtr = ((): number => {
const optionsPtr = asc.newOptions();
const ptr = asc.newProgram(optionsPtr);
asc.__release(optionsPtr);
return ptr;
})();
console.log("\nParsing standard library ...");
Object.keys(libraryFiles).forEach((libPath: string) => {
if (libPath.indexOf("/") >= 0) return;
const textPtr = cachedString(libraryFiles[libPath]);
const pathPtr = cachedString("~lib/" + libPath + ".ts");
console.log(" " + asc.__getString(pathPtr));
asc.parse(programPtr, textPtr, pathPtr, false);
});
console.log("\nParsing runtime ...");
{
const textPtr = cachedString(libraryFiles["rt/index-stub"]);
const pathPtr = cachedString("~lib/rt/index-stub.ts");
console.log(" " + asc.__getString(pathPtr));
asc.parse(programPtr, textPtr, pathPtr, true);
}
console.log("\nParsing backlog ...");
var nextFilePtr = asc.nextFile(programPtr);
while (nextFilePtr) {
const nextFile = asc.__getString(nextFilePtr);
if (!nextFile.startsWith("~lib/")) throw Error("unexpected file: " + nextFile);
const text = libraryFiles[nextFile.substring(5)];
if (text == null) throw Error("missing file: " + nextFile);
const textPtr = cachedString(libraryFiles[nextFile.substring(5)]);
const pathPtr = cachedString(nextFile + ".ts");
console.log(" " + asc.__getString(pathPtr));
asc.parse(programPtr, textPtr, pathPtr, false);
asc.__release(nextFilePtr);
nextFilePtr = asc.nextFile(programPtr);
}
console.log("\nParsing entry file ...");
{
const textPtr = cachedString("export function add(a: i32, b: i32): i32 { return a + b; }\n");
const pathPtr = cachedString("index.ts");
console.log(" " + asc.__getString(pathPtr));
asc.parse(programPtr, textPtr, pathPtr, true);
}
console.log("\nInitializing program ...");
{
asc.initializeProgram(programPtr);
console.log("\nCompiling program ...");
const modulePtr = asc.compile(programPtr);
const moduleRef = new Uint32Array(asc.memory.buffer, modulePtr)[0];
console.log(binaryen.wrapModule(moduleRef).emitText());
asc.__release(modulePtr);
}
asc.__release(programPtr);
cachedStrings.forEach(asc.__release);
console.log("\nSo far, so good.");
}
test("untouched");