Skip to content

Commit 61ffd24

Browse files
committed
fix: use constant constructors instead of parsing
1 parent a88dc4d commit 61ffd24

File tree

6 files changed

+105
-110
lines changed

6 files changed

+105
-110
lines changed

transform/lib/index.js

Lines changed: 24 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/lib/range.js

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/lib/range.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/src/index.ts

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { ClassDeclaration, FieldDeclaration, IdentifierExpression, Parser, Source, NodeKind, CommonFlags, ImportStatement, Node, Tokenizer, SourceKind, NamedTypeNode, Range, FEATURE_SIMD, FunctionExpression, MethodDeclaration } from "assemblyscript/dist/assemblyscript.js";
1+
import { ClassDeclaration, FieldDeclaration, IdentifierExpression, Parser, Source, NodeKind, CommonFlags, ImportStatement, Node, Tokenizer, SourceKind, NamedTypeNode, Range, FEATURE_SIMD, FunctionExpression, MethodDeclaration, Statement } from "assemblyscript/dist/assemblyscript.js";
22
import { Transform } from "assemblyscript/dist/transform.js";
33
import { Visitor } from "./visitor.js";
44
import { SimpleParser, toString } from "./util.js";
55
import * as path from "path";
66
import { fileURLToPath } from "url";
77
import { Property, PropertyFlags, Schema } from "./types.js";
88
import { getClasses, getImportedClass } from "./linker.js";
9-
import { realpathSync } from "fs";
10-
9+
import { RangeTransform } from "./range.js";
1110
let indent = " ";
1211

1312
class JSONTransform extends Visitor {
@@ -17,11 +16,7 @@ class JSONTransform extends Visitor {
1716
public sources = new Set<Source>();
1817
public imports: ImportStatement[] = [];
1918

20-
public jsonImport: string | null = null;
21-
public bsImport: string | null = null;
22-
public newStmts: {
23-
simd: string[];
24-
} = { simd: [] };
19+
public topStatements: Statement[] = [];
2520

2621
visitClassDeclaration(node: ClassDeclaration): void {
2722
if (!node.decorators?.length) return;
@@ -471,33 +466,56 @@ class JSONTransform extends Visitor {
471466
if (!bsRel.startsWith(".") && !bsRel.startsWith("/")) bsRel = "./" + bsRel;
472467

473468
if (bsImport) {
474-
const txt = `import { bs } from "${bsRel}";`;
475-
if (!this.bsImport && path.dirname(path.resolve(bsRel)) != path.dirname(path.resolve(bsImport.path.value))) {
476-
this.bsImport = txt;
477-
node.statements.splice(node.statements.indexOf(bsImport), 1);
478-
if (process.env["JSON_DEBUG"]) console.log("Modified as-bs import: " + txt + "\n");
479-
}
469+
// const txt = `import { bs } from "${bsRel}";`;
470+
// if (!this.bsImport && path.dirname(path.resolve(bsRel)) != path.dirname(path.resolve(bsImport.path.value))) {
471+
// this.bsImport = txt;
472+
// node.statements.splice(node.statements.indexOf(bsImport), 1);
473+
// if (process.env["JSON_DEBUG"]) console.log("Modified as-bs import: " + txt + "\n");
474+
// }
480475
} else {
481476
const txt = `import { bs } from "${bsRel}";`;
482-
if (!this.bsImport) {
483-
this.bsImport = txt;
484-
if (process.env["JSON_DEBUG"]) console.log("Added as-bs import: " + txt + "\n");
485-
}
477+
const replacer = new RangeTransform(node);
478+
const replaceNode = Node.createImportStatement(
479+
[
480+
Node.createImportDeclaration(
481+
Node.createIdentifierExpression("bs", node.range, false),
482+
Node.createIdentifierExpression("bs", node.range, false),
483+
node.range
484+
)
485+
],
486+
Node.createStringLiteralExpression(bsRel, node.range),
487+
node.range
488+
);
489+
490+
this.topStatements.push(replaceNode);
491+
if (process.env["JSON_DEBUG"]) console.log("Added as-bs import: " + txt + "\n");
486492
}
487493

488494
if (!this.imports.find((i) => i.declarations?.find((d) => d.foreignName.text == "JSON"))) {
489-
let jsonPath = path.relative(path.dirname(node.range.source.normalizedPath), path.resolve(fileDir, "../../assembly/index.ts")).replace(".ts", "");
495+
let jsonRel = path.relative(path.dirname(node.range.source.normalizedPath), path.resolve(fileDir, "../../assembly/index.ts")).replace(".ts", "");
490496

491-
if (!jsonPath.startsWith(".") && !jsonPath.startsWith("/")) jsonPath = "./" + jsonPath;
497+
if (!jsonRel.startsWith(".") && !jsonRel.startsWith("/")) jsonRel = "./" + jsonRel;
492498
// if (!existsSync(relativePath)) {
493499
// throw new Error("Could not find a valid json-as library to import from! Please add import { JSON } from \"path-to-json-as\"; in " + node.range.source.normalizedPath + "!");
494500
// }
495501

496-
const txt = `import { JSON } from "${jsonPath}";`;
497-
if (!this.jsonImport) {
498-
this.jsonImport = txt;
499-
if (process.env["JSON_DEBUG"]) console.log("Added json-as import: " + txt + "\n");
500-
}
502+
const txt = `import { JSON } from "${jsonRel}";`;
503+
const replacer = new RangeTransform(node);
504+
const replaceNode = Node.createImportStatement(
505+
[
506+
Node.createImportDeclaration(
507+
Node.createIdentifierExpression("JSON", node.range, false),
508+
Node.createIdentifierExpression("JSON", node.range, false),
509+
node.range
510+
)
511+
],
512+
Node.createStringLiteralExpression(jsonRel, node.range),
513+
node.range
514+
);
515+
// replacer.visit(replaceNode);
516+
517+
this.topStatements.push(replaceNode);
518+
if (process.env["JSON_DEBUG"]) console.log("Added json-as import: " + txt + "\n");
501519
}
502520
}
503521

@@ -506,14 +524,14 @@ class JSONTransform extends Visitor {
506524
const sizes = strToNum(data, simd);
507525
let offset = 0;
508526
for (const [size, num] of sizes) {
509-
if (size == "v128") {
510-
// This could be put in its own file
511-
let index = this.newStmts.simd.findIndex((v) => v.includes(num));
512-
let name = "SIMD_" + (index == -1 ? this.newStmts.simd.length : index);
513-
if (index && !this.newStmts.simd.includes(`const ${name} = ${num};`)) this.newStmts.simd.push(`const ${name} = ${num};`);
514-
out.push("store<v128>(bs.offset, " + name + ", " + offset + "); // " + data.slice(offset >> 1, (offset >> 1) + 8));
515-
offset += 16;
516-
}
527+
// if (size == "v128") {
528+
// // This could be put in its own file
529+
// let index = this.newStmts.simd.findIndex((v) => v.includes(num));
530+
// let name = "SIMD_" + (index == -1 ? this.newStmts.simd.length : index);
531+
// if (index && !this.newStmts.simd.includes(`const ${name} = ${num};`)) this.newStmts.simd.push(`const ${name} = ${num};`);
532+
// out.push("store<v128>(bs.offset, " + name + ", " + offset + "); // " + data.slice(offset >> 1, (offset >> 1) + 8));
533+
// offset += 16;
534+
// }
517535
if (size == "u64") {
518536
out.push("store<u64>(bs.offset, " + num + ", " + offset + "); // " + data.slice(offset >> 1, (offset >> 1) + 4));
519537
offset += 8;
@@ -568,32 +586,13 @@ export default class Transformer extends Transform {
568586
for (const source of sources) {
569587
// console.log("Source: " + source.normalizedPath);
570588
transformer.imports = [];
589+
transformer.topStatements = [];
571590
transformer.currentSource = source;
572591
// Ignore all lib and std. Visit everything else.
573592
transformer.visit(source);
574593

575-
if (transformer.newStmts.simd) {
576-
const tokenizer = new Tokenizer(new Source(SourceKind.User, source.normalizedPath, transformer.newStmts.simd.join("\n")));
577-
parser.currentSource = tokenizer.source;
578-
for (let i = 0; i < transformer.newStmts.simd.length; i++) source.statements.unshift(parser.parseTopLevelStatement(tokenizer)!);
579-
parser.currentSource = source;
580-
transformer.newStmts.simd = [];
581-
}
582-
583-
if (transformer.jsonImport) {
584-
const tokenizer = new Tokenizer(new Source(SourceKind.User, source.normalizedPath, transformer.jsonImport));
585-
parser.currentSource = tokenizer.source;
586-
source.statements.unshift(parser.parseTopLevelStatement(tokenizer)!);
587-
parser.currentSource = source;
588-
transformer.jsonImport = null;
589-
}
590-
591-
if (transformer.bsImport) {
592-
const tokenizer = new Tokenizer(new Source(SourceKind.User, source.normalizedPath, transformer.bsImport));
593-
parser.currentSource = tokenizer.source;
594-
source.statements.unshift(parser.parseTopLevelStatement(tokenizer)!);
595-
parser.currentSource = source;
596-
transformer.bsImport = null;
594+
if (transformer.topStatements.length) {
595+
source.statements.unshift(...transformer.topStatements);
597596
}
598597
}
599598
// Check that every parent and child class is hooked up correctly

transform/src/range.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Node } from "assemblyscript/dist/assemblyscript.js";
2+
import { Visitor } from "./visitor.js";
3+
4+
export class RangeTransform extends Visitor {
5+
constructor(public baseNode: Node) {
6+
super();
7+
}
8+
_visit(node: Node, ref: Node | null): void {
9+
node.range = this.baseNode.range;
10+
super._visit(node, ref);
11+
}
12+
}

0 commit comments

Comments
 (0)