Skip to content

Commit d5c6818

Browse files
committed
feat: async import reader
1 parent 55d2885 commit d5c6818

File tree

4 files changed

+50
-43
lines changed

4 files changed

+50
-43
lines changed

src/macro.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function extractMacros(txt, { lib, reader, lang }) {
1+
async function extractMacros(txt, { lib, reader, lang, importPaths }) {
22
function getImports() {
33
var imps = [];
44
for (var i = 0; i < txt.length; i++) {
@@ -106,9 +106,9 @@ function extractMacros(txt, { lib, reader, lang }) {
106106
} else if (imports[i] in lib) {
107107
isrc = lib[imports[i]];
108108
} else {
109-
isrc = reader(imports[i]);
109+
isrc = await reader(imports[i], importPaths);
110110
}
111-
macros = macros.concat(extractMacros(isrc, { lib, reader, lang }));
111+
macros = macros.concat(await extractMacros(isrc, { lib, reader, lang }));
112112
}
113113
return macros;
114114
}

src/parser.js

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ try {
1212
var compilers = require("./compiler/compilers");
1313
var { typecheck, printSignature } = require("./typecheck");
1414
var { expandMacros, extractMacros } = require("./macro.js");
15+
var { defaultImportReader } = require("./reader");
1516
} catch (e) {}
1617

1718
function wy2tokens(
@@ -631,30 +632,7 @@ function pyWrapModule(name, src) {
631632
return `#/*___wenyan_module_${name}_start___*/\n${src}\n#/*___wenyan_module_${name}_end___*/\n`;
632633
}
633634

634-
function defaultReader(x) {
635-
try {
636-
const fs = eval("require")("fs");
637-
try {
638-
return fs.readFileSync(x + ".wy").toString();
639-
} catch (e) {
640-
var files = fs.readdirSync("./");
641-
for (var i = 0; i < files.length; i++) {
642-
if (fs.lstatSync(files[i]).isDirectory()) {
643-
try {
644-
return fs.readFileSync(files[i] + "/" + x + ".wy").toString();
645-
} catch (e) {}
646-
}
647-
}
648-
}
649-
console.log("Cannot import ", x);
650-
} catch (e) {
651-
console.error(
652-
`Cannot import ${x}, please specify the "reader" option in compile.`
653-
);
654-
}
655-
}
656-
657-
function compile(arg1, arg2, arg3) {
635+
async function compile(arg1, arg2, arg3) {
658636
let options = {};
659637
let txt = "";
660638

@@ -677,7 +655,8 @@ function compile(arg1, arg2, arg3) {
677655
: console.dir(x, { depth: null, maxArrayLength: null }),
678656
errorCallback = process.exit,
679657
lib = typeof STDLIB == "undefined" ? {} : STDLIB,
680-
reader = defaultReader,
658+
reader = defaultImportReader,
659+
importPaths = [],
681660
strict = false
682661
} = options;
683662

@@ -710,7 +689,7 @@ function compile(arg1, arg2, arg3) {
710689
return 0;
711690
}
712691

713-
var macros = extractMacros(txt, { lib, reader, lang });
692+
var macros = await extractMacros(txt, { lib, reader, lang, importPaths });
714693
txt = expandMacros(txt, macros);
715694

716695
logCallback("\n\n=== [PASS 0] EXPAND-MACROS ===");
@@ -745,7 +724,7 @@ function compile(arg1, arg2, arg3) {
745724
}
746725
var klass = compilers[lang];
747726
var compiler = new klass(asc);
748-
var result = compiler.compile({ imports });
727+
var result = await compiler.compile({ imports });
749728
var { imports, result } = result;
750729
var targ = result;
751730
logCallback(targ);
@@ -758,19 +737,21 @@ function compile(arg1, arg2, arg3) {
758737
} else if (imports[i] in lib) {
759738
isrc = lib[imports[i]];
760739
} else {
761-
isrc = reader(imports[i]);
740+
isrc = await reader(imports[i], importPaths);
762741
}
763742
targ =
764743
mwrapper(
765744
imports[i],
766-
compile(isrc, {
745+
await compile(isrc, {
767746
lang,
768747
romanizeIdentifiers,
769748
resetVarCnt: false,
770749
strict: false,
771750
logCallback,
772751
errorCallback,
773-
lib
752+
lib,
753+
reader,
754+
importPaths
774755
})
775756
) + targ;
776757
}

src/reader.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
async function defaultImportReader(moduleName, importPaths = []) {
2+
let fs, path;
3+
try {
4+
fs = eval("require")("fs");
5+
path = eval("require")("path");
6+
} catch (e) {
7+
throw new Error(
8+
`Cannot import ${moduleName}, please specify the "reader" option in compile. ${e}`
9+
);
10+
}
11+
12+
if (typeof importPaths === "string") importPaths = [importPaths];
13+
14+
for (dir of importPaths) {
15+
try {
16+
return fs.readFileSync(path.join(dir, moduleName + ".wy"), "utf-8");
17+
} catch (e) {}
18+
}
19+
20+
try {
21+
return fs.readFileSync(moduleName + ".wy", "utf-8");
22+
} catch (e) {
23+
throw new Error(
24+
`Module "${moduleName}" is not found. Searched in ${importPaths}`
25+
);
26+
}
27+
}
28+
29+
try {
30+
module.exports = {
31+
defaultImportReader
32+
};
33+
} catch (e) {}

test/examples.test.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,14 @@ function getPythonExecutable() {
2929
return undefined;
3030
}
3131

32-
function readOtherExample(x) {
33-
console.log(x);
34-
return fs
35-
.readFileSync(path.resolve(__dirname, "../examples/" + x + ".wy"), "utf-8")
36-
.toString();
37-
}
38-
39-
function runExample(lang, name, options = {}) {
32+
async function runExample(lang, name, options = {}) {
4033
var code = fs
4134
.readFileSync(path.join(exampleDir, name + ".wy"), "utf-8")
4235
.toString();
4336

44-
var compiled = compile(lang, code, {
37+
var compiled = await compile(lang, code, {
4538
logCallback: () => {},
46-
reader: readOtherExample,
39+
importPaths: [path.resolve(__dirname, "../examples/")],
4740
lib: lib,
4841
...options
4942
});

0 commit comments

Comments
 (0)