summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Sotoudeh <matthewsot@outlook.com>2017-07-26 15:06:35 -0700
committerMatthew Sotoudeh <matthewsot@outlook.com>2017-07-26 15:06:35 -0700
commit8cea21ce551baf8fe75fb796a555b45b61e30bf7 (patch)
tree3da8fd2bb98c2e1db5778e79327baad26c3b5fb0
parent68cc445f6c77646a6e423ff6483100e05633c3cf (diff)
the javascript runner works?
-rw-r--r--content/scripts/editor.js36
-rw-r--r--content/scripts/wrap-await.js58
-rw-r--r--editor.html1
-rw-r--r--package.json4
4 files changed, 81 insertions, 18 deletions
diff --git a/content/scripts/editor.js b/content/scripts/editor.js
index 23cab20..a148779 100644
--- a/content/scripts/editor.js
+++ b/content/scripts/editor.js
@@ -45,31 +45,31 @@ function runYAEPL() {
}
interpretLine(0);
};
+
+const vm = require('vm');
function runJS() {
term.clear();
- var lines = editor.getValue().split('\n');
+ var scriptStr = editor.getValue();
+ scriptStr = wrapAwait(scriptStr);
+ alert(scriptStr);
- var yaepl = new Yaepl({
- outHandle: function (str) { window.term.print(str); },
- promptHandle: function (str, callback) {
- window.term.input(str, function (ret) {
- callback(ret);
+ const script = new vm.Script(scriptStr);
+ var sandbox = {
+ 'alert': window.alert,
+ 'write': function (a) { term.print(a); },
+ 'prompt': async function (a) {
+ var promptPromise = new Promise(function (resolve, reject) {
+ window.term.input(a, function (ret) {
+ resolve(ret);
+ });
});
+ return await promptPromise;
}
- });
+ };
+ const context = new vm.createContext(sandbox);
- var l = 0;
- var maxL = lines.length;
- function interpretLine(l) {
- yaepl.interpretLine(lines[l], function () {
- l++;
- if (l < maxL) {
- interpretLine(l);
- }
- });
- }
- interpretLine(0);
+ script.runInContext(context);
};
function run() {
switch (window.lang) {
diff --git a/content/scripts/wrap-await.js b/content/scripts/wrap-await.js
new file mode 100644
index 0000000..d7be4ac
--- /dev/null
+++ b/content/scripts/wrap-await.js
@@ -0,0 +1,58 @@
+let esprima = require("esprima");
+let escodegen = require("escodegen");
+function wrapSingle(el) {
+ if (el === undefined) return undefined;
+
+ el.argument = wrapSingle(el.argument);
+ el.arguments = wrapList(el.arguments);
+
+ el.left = wrapSingle(el.left);
+ el.right = wrapSingle(el.right);
+
+ if (el.type == "CallExpression") {
+ el = {
+ type: 'AwaitExpression',
+ argument: el
+ };
+ }
+
+ el.expression = wrapSingle(el.expression);
+ el.body = wrapList(el.body);
+ return el;
+}
+function wrapList(list) {
+ if (list === undefined) return undefined;
+
+ if (list.body !== undefined) {
+ list.body = wrapList(list.body);
+ return list;
+ }
+
+ var newList = []
+ for (var i = 0; i < list.length; i++) {
+ list[i] = wrapSingle(list[i]);
+ }
+ return list;
+}
+
+function wrapAwait(script) {
+ var parsed = esprima.parse(script).body;
+
+ var functions = [];
+ var body = [];
+ for (var i = 0; i < parsed.length; i++) {
+ if (parsed[i].type == "FunctionDeclaration") {
+ parsed[i].async = true;
+ parsed[i].body = wrapList(parsed[i].body);
+ functions.push(parsed[i]);
+ } else {
+ body.push(parsed[i]);
+ }
+ }
+ body = wrapList(body);
+
+ var pre = escodegen.generate({ type: "Program", body: functions });
+ var internal = escodegen.generate({ type: "Program", body: body });
+
+ return pre + "\nasync function __main_script__() {\n" + internal + "\n}\n__main_script__();";
+}
diff --git a/editor.html b/editor.html
index f825a6b..c032bfa 100644
--- a/editor.html
+++ b/editor.html
@@ -23,6 +23,7 @@
<script src="content/scripts/yaepl.js"></script>
<script src="content/scripts/js-mode.js"></script>
<script src="content/scripts/terminal.js"></script>
+ <script src="content/scripts/wrap-await.js"></script>
<script src="content/scripts/editor.js"></script>
<script src="content/scripts/open-save.js"></script>
</body>
diff --git a/package.json b/package.json
index 426d3c7..4655afe 100644
--- a/package.json
+++ b/package.json
@@ -11,5 +11,9 @@
"license": "CC0-1.0",
"devDependencies": {
"electron": "~1.6.2"
+ },
+ "dependencies": {
+ "escodegen": "^1.8.1",
+ "esprima": "^4.0.0"
}
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback