diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-04-10 18:23:07 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-04-10 18:23:07 -0400 |
| commit | a014f3dc02c041b5c5a7c418f2cbc748b0a6bcc2 (patch) | |
| tree | 6b58d08d83632c723b094919ca10941d4b87852b | |
| parent | 7988a14d4af00f50ea99660131c9b07661082f8c (diff) | |
•JS for Javascript evaluation
| -rwxr-xr-x | docs/bqn.js | 16 | ||||
| -rw-r--r-- | docs/repl.js | 4 |
2 files changed, 18 insertions, 2 deletions
diff --git a/docs/bqn.js b/docs/bqn.js index e104f4f8..d7e117ea 100755 --- a/docs/bqn.js +++ b/docs/bqn.js @@ -311,8 +311,22 @@ let fmtErr = (s,e) => { } let unixtime = (x,w) => Date.now()/1000; +let dojs = (x,w) => { + if (!(x.sh && x.sh.length==1 && x.every(c=>typeof c==="string"))) + throw Error("•JS 𝕩: 𝕩 must be a string"); + if (has(w)) throw Error("•JS: 𝕨 not allowed"); + let r = Function("'use strict'; return ("+x.join("")+")")(); + let toBQN = x => { + if (isnum(x)) return x; + if (typeof x==='string') { if (Array.from(x).length!==1) throw Error("•JS: JS strings are one character; use Array.from for BQN strings"); return x; } + if (x instanceof Array) return arr(x.map(toBQN),x.sh||[x.length],toBQN(x.fill)); + if (isfunc(x)) { let f=(a,b)=>toBQN(x(a,b)); f.m=x.m; return f; } + throw Error("•JS: Unrecognized JS result"); + } + return toBQN(r); +} let sysvals = { - bqn, type, glyph, decompose, fmt:fmt1, listsys:0, + bqn, js:dojs, type, glyph, decompose, fmt:fmt1, listsys:0, unixtime }; diff --git a/docs/repl.js b/docs/repl.js index 4bdb653f..ec79324f 100644 --- a/docs/repl.js +++ b/docs/repl.js @@ -33,6 +33,7 @@ let repl = ()=>{ doc.rslt.classList.add('err'); doc.rslt.textContent=fmtErr(src,e); } + sysvals.js=dojs; // In case it was disabled by fragment loading }, 0); } if (doc.run) doc.run.onclick = repl; @@ -158,7 +159,8 @@ if (location.hash) { b=new Uint8Array([...b].map(c=>c.charCodeAt(0))); setcount(doc.code.value = (new TextDecoder()).decode(b)); if (ee && doc.doexplain) doc.doexplain.onclick(); - if (run) repl(); + nojs = () => { throw Error("Possible script injection; press Run to confirm"); } + if (run) { sysvals.js=nojs; repl(); } doc.code.focus(); } } |
