Skip to content

Commit 5b51057

Browse files
authored
Refactor resolving paths in asc (AssemblyScript#846)
1 parent fd23df7 commit 5b51057

14 files changed

Lines changed: 162 additions & 243 deletions

File tree

cli/asc.js

Lines changed: 102 additions & 196 deletions
Large diffs are not rendered by default.

src/ast.ts

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -683,19 +683,12 @@ export abstract class Node {
683683
if (path) {
684684
let normalizedPath = normalizePath(path.value);
685685
if (path.value.startsWith(".")) { // relative
686-
stmt.normalizedPath = resolvePath(
687-
normalizedPath,
688-
range.source.normalizedPath
689-
);
686+
normalizedPath = resolvePath(normalizedPath, range.source.internalPath);
690687
} else { // absolute
691-
if (!normalizedPath.startsWith(LIBRARY_PREFIX)) {
692-
normalizedPath = LIBRARY_PREFIX + normalizedPath;
693-
}
694-
stmt.normalizedPath = normalizedPath;
688+
if (!normalizedPath.startsWith(LIBRARY_PREFIX)) normalizedPath = LIBRARY_PREFIX + normalizedPath;
695689
}
696-
stmt.internalPath = mangleInternalPath(stmt.normalizedPath);
690+
stmt.internalPath = mangleInternalPath(normalizedPath);
697691
} else {
698-
stmt.normalizedPath = null;
699692
stmt.internalPath = null;
700693
}
701694
stmt.isDeclare = isDeclare;
@@ -772,17 +765,11 @@ export abstract class Node {
772765
stmt.path = path;
773766
var normalizedPath = normalizePath(path.value);
774767
if (path.value.startsWith(".")) { // relative in project
775-
stmt.normalizedPath = resolvePath(
776-
normalizedPath,
777-
range.source.normalizedPath
778-
);
768+
normalizedPath = resolvePath(normalizedPath, range.source.internalPath);
779769
} else { // absolute in library
780-
if (!normalizedPath.startsWith(LIBRARY_PREFIX)) {
781-
normalizedPath = LIBRARY_PREFIX + normalizedPath;
782-
}
783-
stmt.normalizedPath = normalizedPath;
770+
if (!normalizedPath.startsWith(LIBRARY_PREFIX)) normalizedPath = LIBRARY_PREFIX + normalizedPath;
784771
}
785-
stmt.internalPath = mangleInternalPath(stmt.normalizedPath);
772+
stmt.internalPath = mangleInternalPath(normalizedPath);
786773
return stmt;
787774
}
788775

@@ -798,17 +785,11 @@ export abstract class Node {
798785
stmt.path = path;
799786
var normalizedPath = normalizePath(path.value);
800787
if (path.value.startsWith(".")) {
801-
stmt.normalizedPath = resolvePath(
802-
normalizedPath,
803-
range.source.normalizedPath
804-
);
788+
normalizedPath = resolvePath(normalizedPath, range.source.internalPath);
805789
} else {
806-
if (!normalizedPath.startsWith(LIBRARY_PREFIX)) {
807-
normalizedPath = LIBRARY_PREFIX + normalizedPath;
808-
}
809-
stmt.normalizedPath = normalizedPath;
790+
if (!normalizedPath.startsWith(LIBRARY_PREFIX)) normalizedPath = LIBRARY_PREFIX + normalizedPath;
810791
}
811-
stmt.internalPath = mangleInternalPath(stmt.normalizedPath);
792+
stmt.internalPath = mangleInternalPath(normalizedPath);
812793
return stmt;
813794
}
814795

@@ -1636,7 +1617,7 @@ export class Source extends Node {
16361617

16371618
/** Source kind. */
16381619
sourceKind: SourceKind;
1639-
/** Normalized path. */
1620+
/** Normalized path with file extension. */
16401621
normalizedPath: string;
16411622
/** Path used internally. */
16421623
internalPath: string;
@@ -1812,9 +1793,7 @@ export class ExportStatement extends Statement {
18121793
members: ExportMember[] | null;
18131794
/** Path being exported from, if applicable. */
18141795
path: StringLiteralExpression | null;
1815-
/** Normalized path, if `path` is set. */
1816-
normalizedPath: string | null;
1817-
/** Mangled internal path being referenced, if `path` is set. */
1796+
/** Internal path being referenced, if `path` is set. */
18181797
internalPath: string | null;
18191798
/** Whether this is a declared export. */
18201799
isDeclare: bool;
@@ -1934,9 +1913,7 @@ export class ImportStatement extends Statement {
19341913
namespaceName: IdentifierExpression | null;
19351914
/** Path being imported from. */
19361915
path: StringLiteralExpression;
1937-
/** Normalized path. */
1938-
normalizedPath: string;
1939-
/** Mangled internal path being referenced. */
1916+
/** Internal path being referenced. */
19401917
internalPath: string;
19411918
}
19421919

src/compiler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,17 +1666,17 @@ export class Compiler extends DiagnosticEmitter {
16661666
break;
16671667
}
16681668
case NodeKind.EXPORT: {
1669-
if ((<ExportStatement>statement).normalizedPath != null) {
1669+
if ((<ExportStatement>statement).internalPath != null) {
16701670
this.compileFileByPath(
1671-
<string>(<ExportStatement>statement).normalizedPath,
1671+
<string>(<ExportStatement>statement).internalPath,
16721672
<StringLiteralExpression>(<ExportStatement>statement).path
16731673
);
16741674
}
16751675
break;
16761676
}
16771677
case NodeKind.IMPORT: {
16781678
this.compileFileByPath(
1679-
(<ImportStatement>statement).normalizedPath,
1679+
(<ImportStatement>statement).internalPath,
16801680
(<ImportStatement>statement).path
16811681
);
16821682
break;

src/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ import { Parser } from "./parser";
1313
import { Program } from "./program";
1414

1515
/** Parses a source file. If `parser` has been omitted a new one is created. */
16-
export function parseFile(text: string, path: string, isEntry: bool = false,
16+
export function parseFile(
17+
/** Source text of the file. */
18+
text: string,
19+
/** Normalized path of the file. */
20+
path: string,
21+
/** Whether this is an entry file. */
22+
isEntry: bool = false,
23+
/** Parser reference. */
1724
parser: Parser | null = null
1825
): Parser {
1926
if (!parser) parser = new Parser();
@@ -154,6 +161,11 @@ export function finishParsing(parser: Parser): Program {
154161
return parser.finish();
155162
}
156163

164+
/** Obtains the source of the given file. */
165+
export function getSource(program: Program, internalPath: string): string | null {
166+
return program.getSource(internalPath);
167+
}
168+
157169
/** Compiles the sources computed by the parser to a module. */
158170
export function compileProgram(program: Program, options: Options | null = null): Module {
159171
return new Compiler(program, options).compile();

src/parser.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,14 @@ export class Parser extends DiagnosticEmitter {
115115

116116
/** Parses a file and adds its definitions to the program. */
117117
parseFile(
118+
/** Source text of the file. */
118119
text: string,
120+
/** Normalized path of the file. */
119121
path: string,
122+
/** Whether this is an entry file. */
120123
isEntry: bool
121124
): void {
125+
// the frontend gives us paths with .ts endings
122126
var normalizedPath = normalizePath(path);
123127
var internalPath = mangleInternalPath(normalizedPath);
124128
// check if already processed
@@ -379,12 +383,10 @@ export class Parser extends DiagnosticEmitter {
379383
return backlog.length ? backlog.shift() : null;
380384
}
381385

382-
/** Obtains the dependee for a given import */
386+
/** Obtains the dependee of the given imported file. */
383387
getDependee(dependent: string): string | null {
384388
var source = this.dependees.get(dependent);
385-
if (source) {
386-
return source.internalPath;
387-
}
389+
if (source) return source.internalPath;
388390
return null;
389391
}
390392

src/program.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,16 @@ export class Program extends DiagnosticEmitter {
483483
this.resolver = new Resolver(this);
484484
}
485485

486+
/** Obtains the source matching the specified internal path. */
487+
getSource(internalPath: string): string | null {
488+
var sources = this.sources;
489+
for (let i = 0; i < sources.length; ++i) {
490+
let source = sources[i];
491+
if (source.internalPath == internalPath) return source.text;
492+
}
493+
return null;
494+
}
495+
486496
/** Writes a common runtime header to the specified buffer. */
487497
writeRuntimeHeader(buffer: Uint8Array, offset: i32, classInstance: Class, payloadSize: u32): void {
488498
// BLOCK {

tests/packages/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"scripts": {
3-
"test": "npm run a && npm run b && npm run c && npm run d && npm run e && npm run f && npm run g && npm run as",
3+
"test": "npm run a && npm run b && npm run c && npm run d && npm run e && npm run f && npm run g && npm run as && npm run h",
44
"a": "cd ./packages/a && node ../../../../bin/asc assembly/index.ts --noEmit --runtime stub --validate",
55
"as": "cd ./packages/as && node ../../../../bin/asc as/index.ts --noEmit --runtime stub --validate",
66
"b": "cd ./packages/b && node ../../../../bin/asc assembly/index.ts --noEmit --runtime stub && node ../../../../bin/asc assembly/index.ts --noEmit --runtime stub --listFiles",
77
"c": "cd ./packages/c && node ../../../../bin/asc assembly/index.ts --noEmit --runtime stub --validate",
88
"d": "cd ./packages/d && node ../../../../bin/asc assembly/index.ts --path packages --noEmit --runtime stub --validate --traceResolution",
99
"e": "cd ./packages/d/packages/e && node ../../../../../../bin/asc assembly/index.ts --noEmit --runtime stub --validate",
1010
"f": "cd ./packages/d/packages/e/packages/f && node ../../../../../../../../bin/asc assembly/index.ts --noEmit --runtime stub --validate",
11-
"g": "cd ./packages/g && node test.js"
11+
"g": "cd ./packages/g && node test.js",
12+
"h": "cd ./packages/h && node ../../../../bin/asc assembly/index.ts --noEmit --runtime none --validate --traceResolution"
1213
},
1314
"author": "Willem Wyndham",
1415
"license": "Apache-2.0"

tests/packages/packages/d/node_modules/as/as/as.ts renamed to tests/packages/packages/d/node_modules/as/notassembly/as.ts

File renamed without changes.

tests/packages/packages/d/node_modules/as/as/index.ts renamed to tests/packages/packages/d/node_modules/as/notassembly/index.ts

File renamed without changes.

tests/packages/packages/d/node_modules/as/package.json

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

0 commit comments

Comments
 (0)