Created
September 7, 2026 18:34
-
-
Save eugrus/7fa68e3e46039deba334c305d004a56a to your computer and use it in GitHub Desktop.
REPL for WSH JScript
This file contains hidden or 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
| (function () { | |
| var stdin; | |
| var stdout; | |
| var stderr; | |
| var line; | |
| var command; | |
| var buffer; | |
| var inBlock; | |
| var globalEval; | |
| if (WScript.FullName.toLowerCase().indexOf("cscript.exe") === -1) { | |
| WScript.Echo("Run this script with cscript.exe, not wscript.exe."); | |
| WScript.Quit(1); | |
| } | |
| stdin = WScript.StdIn; | |
| stdout = WScript.StdOut; | |
| stderr = WScript.StdErr; | |
| buffer = ""; | |
| inBlock = false; | |
| stdout.WriteLine("WSH JScript REPL"); | |
| stdout.WriteLine("Commands: .help, .exec, .begin, .end, .abort, .exit"); | |
| while (true) { | |
| if (inBlock) { | |
| stdout.Write("... "); | |
| } else { | |
| stdout.Write("js> "); | |
| } | |
| try { | |
| line = stdin.ReadLine(); | |
| } catch (eof) { | |
| break; | |
| } | |
| command = line.replace(/^\s+|\s+$/g, "").toLowerCase(); | |
| if (inBlock) { | |
| if (command === ".end") { | |
| executeCode(buffer, true); | |
| buffer = ""; | |
| inBlock = false; | |
| } else if (command === ".abort") { | |
| buffer = ""; | |
| inBlock = false; | |
| stdout.WriteLine("Block discarded."); | |
| } else { | |
| if (buffer !== "") { | |
| buffer += "\r\n"; | |
| } | |
| buffer += line; | |
| } | |
| continue; | |
| } | |
| if (command === ".exit" || command === ".quit") { | |
| break; | |
| } | |
| if (command === ".help") { | |
| showHelp(); | |
| } else if (command === ".begin") { | |
| buffer = ""; | |
| inBlock = true; | |
| } else if (command.indexOf(".exec") === 0 && (command.length === 5 || /\s/.test(command.charAt(5)))) { | |
| executeCode(line.substring(5).replace(/^\s+/, ""), false); | |
| } else if (command !== "") { | |
| executeCode(line, true); | |
| } | |
| } | |
| function executeCode(source, printResult) { | |
| var result; | |
| if (source.replace(/^\s+|\s+$/g, "") === "") { | |
| return; | |
| } | |
| try { | |
| result = eval(source); | |
| if (printResult && typeof result !== "undefined") { | |
| stdout.WriteLine(formatValue(result)); | |
| } | |
| } catch (error) { | |
| stderr.WriteLine("Error: " + error.name + ": " + error.message); | |
| } | |
| } | |
| function formatValue(value) { | |
| if (value === null) { | |
| return "null"; | |
| } | |
| if (typeof value === "undefined") { | |
| return "undefined"; | |
| } | |
| if (typeof value === "string") { | |
| return value; | |
| } | |
| try { | |
| if (typeof value === "object") { | |
| return JSON.stringify(value); | |
| } | |
| } catch (error) { | |
| return String(value); | |
| } | |
| return String(value); | |
| } | |
| function showHelp() { | |
| stdout.WriteLine("expression Evaluate code and print its resulting value"); | |
| stdout.WriteLine(".exec code Execute code without printing a result"); | |
| stdout.WriteLine(".begin Start a multi-line JavaScript block"); | |
| stdout.WriteLine(".end Evaluate the buffered block"); | |
| stdout.WriteLine(".abort Discard the buffered block"); | |
| stdout.WriteLine(".exit / .quit Leave the REPL"); | |
| } | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment