Skip to content

Commit a603c64

Browse files
committed
feat: compileAsync and executeAsync
1 parent c3ffd75 commit a603c64

File tree

4 files changed

+47
-23
lines changed

4 files changed

+47
-23
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"prettier": "^1.19.1",
6868
"raw-loader": "^4.0.0",
6969
"rimraf": "^3.0.0",
70+
"synchronized-promise": "^0.2.0",
7071
"version-bump-prompt": "^5.0.6",
7172
"webpack": "^4.41.4",
7273
"webpack-cli": "^3.3.10",

src/parser.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
try {
2-
var {
3-
hanzi2num,
4-
hanzi2numstr,
5-
num2hanzi,
6-
bool2hanzi
7-
} = require("./hanzi2num");
8-
var hanzi2pinyin = require("./hanzi2pinyin");
9-
var STDLIB = require("./stdlib");
10-
var { NUMBER_KEYWORDS, KEYWORDS } = require("./keywords");
11-
var version = require("./version");
12-
var compilers = require("./compiler/compilers");
13-
var { typecheck, printSignature } = require("./typecheck");
14-
var { expandMacros, extractMacros } = require("./macro.js");
15-
var { defaultImportReader } = require("./reader");
16-
} catch (e) {}
1+
var { hanzi2num, hanzi2numstr, num2hanzi, bool2hanzi } = require("./hanzi2num");
2+
var hanzi2pinyin = require("./hanzi2pinyin");
3+
var STDLIB = require("./stdlib");
4+
var { NUMBER_KEYWORDS, KEYWORDS } = require("./keywords");
5+
var version = require("./version");
6+
var compilers = require("./compiler/compilers");
7+
var { typecheck, printSignature } = require("./typecheck");
8+
var { expandMacros, extractMacros } = require("./macro.js");
9+
var { defaultImportReader } = require("./reader");
10+
const makeSynchronize = require("synchronized-promise");
1711

1812
const defaultTrustedHosts = [
1913
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master"
@@ -642,7 +636,7 @@ function pyWrapModule(name, src) {
642636
return `#/*___wenyan_module_${name}_start___*/\n${src}\n#/*___wenyan_module_${name}_end___*/\n`;
643637
}
644638

645-
async function compile(arg1, arg2, arg3) {
639+
async function compileAsync(arg1, arg2, arg3) {
646640
let options = {};
647641
let txt = "";
648642

@@ -777,6 +771,8 @@ async function compile(arg1, arg2, arg3) {
777771
return targ;
778772
}
779773

774+
const compile = makeSynchronize(compileAsync, { tick: 10 });
775+
780776
function isLangSupportedForEval(lang) {
781777
if (lang !== "js")
782778
throw new Error(
@@ -832,17 +828,21 @@ function evalCompiled(compiledCode, options = {}) {
832828
})();
833829
}
834830

835-
function execute(source, options = {}) {
831+
async function executeAsync(source, options = {}) {
836832
const { lang = "js" } = options;
837833
isLangSupportedForEval(lang);
838-
const compiled = compile(source, options);
834+
const compiled = compileAsync(source, options);
839835
evalCompiled(compiled, options);
840836
}
841837

838+
const execute = makeSynchronize(executeAsync, { tick: 10 });
839+
842840
var parser = {
843841
compile,
842+
compileAsync,
844843
evalCompiled,
845844
execute,
845+
executeAsync,
846846
version,
847847
wy2tokens,
848848
tokens2asc,

test/examples.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async function runExample(lang, name, options = {}) {
3434
.readFileSync(path.join(exampleDir, name + ".wy"), "utf-8")
3535
.toString();
3636

37-
var compiled = await compile(lang, code, {
37+
var compiled = compile(lang, code, {
3838
logCallback: () => {},
3939
importPaths: [
4040
path.resolve(__dirname, "../lib/"),

0 commit comments

Comments
 (0)