-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #552 from antfu/feat/ide-iframe
Use iframe for ide output
- Loading branch information
Showing
7 changed files
with
156 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var { num2hanzi, bool2hanzi } = require("./hanzi2num"); | ||
|
||
function isLangSupportedForEval(lang) { | ||
if (lang !== "js") | ||
throw new Error( | ||
`Executing for target language "${lang}" is not supported in current environment` | ||
); | ||
return true; | ||
} | ||
|
||
function hanzinize(value) { | ||
if (typeof value == "number") { | ||
return num2hanzi(value); | ||
} else if (typeof value == "boolean") { | ||
return bool2hanzi(value); | ||
} else if (Array.isArray(value)) { | ||
return value.map(i => hanzinize(i)).join("。"); | ||
} else { | ||
return value; | ||
} | ||
} | ||
|
||
function outputHanziWrapper(log, outputHanzi) { | ||
return function output(...args) { | ||
log(...args.map(i => (outputHanzi ? hanzinize(i) : i))); | ||
}; | ||
} | ||
|
||
function evalCompiled(compiledCode, options = {}) { | ||
const { | ||
outputHanzi = true, | ||
scoped = false, | ||
lang = "js", | ||
output = console.log | ||
} = options; | ||
|
||
isLangSupportedForEval(lang); | ||
|
||
let code = compiledCode; | ||
|
||
(() => { | ||
const _console_log = console.log; | ||
console.log = outputHanziWrapper(output, outputHanzi); | ||
try { | ||
if (!scoped && "window" in this) { | ||
window.eval(code); | ||
} else { | ||
eval(code); | ||
} | ||
} catch (e) { | ||
throw e; | ||
} finally { | ||
console.log = _console_log; | ||
} | ||
})(); | ||
} | ||
|
||
module.exports = { | ||
isLangSupportedForEval, | ||
evalCompiled | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<head> | ||
<script src="./dist/execute.js"></script> | ||
</head> | ||
<body> | ||
<pre id="out"></pre> | ||
<script> | ||
const outdiv = document.getElementById('out') | ||
|
||
window.addEventListener('message', (e) => { | ||
console.log(e.data) | ||
const { text, code, options } = e.data | ||
|
||
if (text) | ||
outdiv.innerText = text | ||
|
||
if (code){ | ||
outdiv.innerText = "" | ||
try { | ||
Wenyan.evalCompiled(code, { | ||
...options, | ||
output: (...args) => outdiv.innerText += args.join(" ") + "\n" | ||
}); | ||
} catch (e) { | ||
outdiv.innerText = e.toString(); | ||
console.error(e); | ||
} | ||
} | ||
}, false); | ||
</script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters