(code)
| 635 | } |
| 636 | |
| 637 | function execute(code) { |
| 638 | let res = null; |
| 639 | try { |
| 640 | const parsed = parse(code, acornOptions).body; |
| 641 | res = execParsedCode(parsed); |
| 642 | } catch (e) { |
| 643 | res = execParsedCode([]); |
| 644 | } |
| 645 | |
| 646 | return res; |
| 647 | |
| 648 | function execParsedCode(parsed) { |
| 649 | let extra = ""; |
| 650 | parsed.map((st) => { |
| 651 | if (st.type === "VariableDeclaration") { |
| 652 | if (["const", "let"].indexOf(st.kind) < 0) return; |
| 653 | |
| 654 | const exCode = code.substring(st.start, st.end) + ";"; |
| 655 | extra += exCode; |
| 656 | } |
| 657 | }); |
| 658 | |
| 659 | if (extra) { |
| 660 | const script = tag("script"); |
| 661 | script.textContent = extra; |
| 662 | document.body.appendChild(script); |
| 663 | document.body.removeChild(script); |
| 664 | return exec(code); |
| 665 | } else { |
| 666 | return exec(code); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | function exec(code) { |
| 671 | let res = null; |
| 672 | try { |
| 673 | res = { type: "result", value: window.eval(code) }; |
| 674 | } catch (error) { |
| 675 | res = { type: "error", value: error }; |
| 676 | } |
| 677 | |
| 678 | return res; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | function onError(err) { |
| 683 | const error = err.error; |
no test coverage detected