json-as 1.1.8 → 1.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +9 -2
- package/assembly/__benches__/large.bench.ts +214 -104
- package/assembly/__tests__/array.spec.ts +8 -5
- package/assembly/__tests__/lib/index.ts +20 -6
- package/assembly/__tests__/struct.spec.ts +2 -0
- package/assembly/__tests__/test.spec.ts +0 -119
- package/assembly/index.ts +4 -2
- package/assembly/test.ts +141 -251
- package/assembly/types.ts +70 -0
- package/index.ts +1 -1
- package/package.json +6 -6
- package/run-bench.as.sh +1 -1
- package/transform/lib/builder.js +3 -0
- package/transform/lib/builder.js.map +1 -1
- package/transform/lib/index.js +463 -224
- package/transform/lib/index.js.map +1 -1
- package/transform/lib/linker.js +38 -6
- package/transform/lib/linker.js.map +1 -1
- package/transform/lib/util.js +5 -0
- package/transform/lib/util.js.map +1 -1
- package/transform/src/builder.ts +4 -0
- package/transform/src/index.ts +531 -275
- package/transform/src/linker.ts +48 -7
- package/transform/src/types.ts +1 -1
- package/transform/src/util.ts +11 -16
package/transform/src/linker.ts
CHANGED
|
@@ -1,21 +1,62 @@
|
|
|
1
|
-
import { ClassDeclaration, ImportStatement, NodeKind, Parser, Source } from "assemblyscript/dist/assemblyscript.js";
|
|
1
|
+
import { ClassDeclaration, CommonFlags, ImportStatement, NodeKind, Parser, Source } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
+
import { Visitor } from "./visitor.js";
|
|
3
|
+
import { Node } from "types:assemblyscript/src/ast";
|
|
4
|
+
|
|
5
|
+
class ImportGetter extends Visitor {
|
|
6
|
+
static SN: ImportGetter = new ImportGetter();
|
|
7
|
+
|
|
8
|
+
private imports: ImportStatement[] = [];
|
|
9
|
+
|
|
10
|
+
visitImportStatement(node: ImportStatement, ref?: Node | null): void {
|
|
11
|
+
this.imports.push(node);
|
|
12
|
+
}
|
|
13
|
+
static getImports(source: Source): ImportStatement[] {
|
|
14
|
+
ImportGetter.SN.imports = [];
|
|
15
|
+
ImportGetter.SN.visit(source);
|
|
16
|
+
return ImportGetter.SN.imports;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
2
19
|
|
|
3
20
|
export function getImports(source: Source): ImportStatement[] {
|
|
4
|
-
return
|
|
21
|
+
return ImportGetter.getImports(source);
|
|
5
22
|
}
|
|
6
23
|
|
|
7
24
|
export function getImportedClass(name: string, source: Source, parser: Parser): ClassDeclaration | null {
|
|
8
25
|
for (const stmt of getImports(source)) {
|
|
9
|
-
const externalSource = parser.sources.find((src) => src.internalPath
|
|
26
|
+
const externalSource = parser.sources.filter((src) => src.internalPath != source.internalPath).find((src) => src.internalPath == stmt.internalPath);
|
|
10
27
|
if (!externalSource) continue;
|
|
11
28
|
|
|
12
|
-
const classDeclaration =
|
|
13
|
-
|
|
14
|
-
|
|
29
|
+
const classDeclaration = ClassGetter.getClass(name, externalSource);
|
|
30
|
+
if (!(classDeclaration.flags & CommonFlags.Export)) continue;
|
|
31
|
+
return classDeclaration;
|
|
15
32
|
}
|
|
16
33
|
return null;
|
|
17
34
|
}
|
|
18
35
|
|
|
36
|
+
class ClassGetter extends Visitor {
|
|
37
|
+
static SN: ClassGetter = new ClassGetter();
|
|
38
|
+
|
|
39
|
+
private classes: ClassDeclaration[] = [];
|
|
40
|
+
|
|
41
|
+
visitClassDeclaration(node: ClassDeclaration): void {
|
|
42
|
+
this.classes.push(node);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static getClass(name: string, source: Source): ClassDeclaration | null {
|
|
46
|
+
return ClassGetter.getClasses(source).find((c) => c.name.text == name) || null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static getClasses(source: Source): ClassDeclaration[] {
|
|
50
|
+
ClassGetter.SN.classes = [];
|
|
51
|
+
ClassGetter.SN.visit(source);
|
|
52
|
+
return ClassGetter.SN.classes;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
19
56
|
export function getClasses(source: Source): ClassDeclaration[] {
|
|
20
|
-
return
|
|
57
|
+
return ClassGetter.getClasses(source);
|
|
21
58
|
}
|
|
59
|
+
|
|
60
|
+
export function getClass(name: string, source: Source): ClassDeclaration | null {
|
|
61
|
+
return ClassGetter.getClass(name, source);
|
|
62
|
+
}
|
package/transform/src/types.ts
CHANGED
package/transform/src/util.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Taken from https://github.com/as-pect/visitor-as/blob/master/src/simpleParser.ts
|
|
2
2
|
import { Parser, Tokenizer, Source, SourceKind, Expression, Statement, NamespaceDeclaration, ClassDeclaration, DeclarationStatement, Range, Node, NodeKind } from "assemblyscript/dist/assemblyscript.js";
|
|
3
3
|
import { ASTBuilder } from "./builder.js";
|
|
4
|
+
import * as path from "path";
|
|
4
5
|
|
|
5
6
|
export class SimpleParser {
|
|
6
7
|
private static get parser(): Parser {
|
|
@@ -55,11 +56,7 @@ export function toString(node: Node): string {
|
|
|
55
56
|
return ASTBuilder.build(node);
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
export function replaceRef(
|
|
59
|
-
node: Node,
|
|
60
|
-
replacement: Node | Node[],
|
|
61
|
-
ref: Node | Node[] | null,
|
|
62
|
-
): void {
|
|
59
|
+
export function replaceRef(node: Node, replacement: Node | Node[], ref: Node | Node[] | null): void {
|
|
63
60
|
if (!node || !ref) return;
|
|
64
61
|
const nodeExpr = stripExpr(node);
|
|
65
62
|
|
|
@@ -77,8 +74,7 @@ export function replaceRef(
|
|
|
77
74
|
if (Array.isArray(current)) {
|
|
78
75
|
for (let i = 0; i < current.length; i++) {
|
|
79
76
|
if (stripExpr(current[i]) === nodeExpr) {
|
|
80
|
-
if (Array.isArray(replacement))
|
|
81
|
-
current.splice(i, 1, ...replacement);
|
|
77
|
+
if (Array.isArray(replacement)) current.splice(i, 1, ...replacement);
|
|
82
78
|
else current.splice(i, 1, replacement);
|
|
83
79
|
return;
|
|
84
80
|
}
|
|
@@ -91,17 +87,11 @@ export function replaceRef(
|
|
|
91
87
|
}
|
|
92
88
|
}
|
|
93
89
|
|
|
94
|
-
export function cloneNode(
|
|
95
|
-
input: Node | Node[] | null,
|
|
96
|
-
seen = new WeakMap(),
|
|
97
|
-
path = "",
|
|
98
|
-
): Node | Node[] | null {
|
|
90
|
+
export function cloneNode(input: Node | Node[] | null, seen = new WeakMap(), path = ""): Node | Node[] | null {
|
|
99
91
|
if (input === null || typeof input !== "object") return input;
|
|
100
92
|
|
|
101
93
|
if (Array.isArray(input)) {
|
|
102
|
-
return input.map((item, index) =>
|
|
103
|
-
cloneNode(item, seen, `${path}[${index}]`),
|
|
104
|
-
) as Node | Node[] | null;
|
|
94
|
+
return input.map((item, index) => cloneNode(item, seen, `${path}[${index}]`)) as Node | Node[] | null;
|
|
105
95
|
}
|
|
106
96
|
|
|
107
97
|
if (seen.has(input)) return seen.get(input);
|
|
@@ -130,4 +120,9 @@ export function stripExpr(node: Node): Node {
|
|
|
130
120
|
if (!node) return node;
|
|
131
121
|
if (node.kind == NodeKind.Expression) return node["expression"];
|
|
132
122
|
return node;
|
|
133
|
-
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function removeExtension(filePath: string): string {
|
|
126
|
+
const parsed = path.parse(filePath);
|
|
127
|
+
return path.join(parsed.dir, parsed.name);
|
|
128
|
+
}
|