From e8a7ac8da8a70b64a6a4654416b6ce7ac57c9479 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sun, 26 Nov 2023 16:49:47 +0200 Subject: [PATCH] refactor: reorganize assets --- .../jscript => assets}/js/bootstrap.js | 234 +++++++++--------- .../topchetoeu/jscript => assets}/js/lib.d.ts | 0 .../topchetoeu/jscript => assets}/js/ts.js | 0 src/me/topchetoeu/jscript/Reading.java | 19 +- .../jscript/engine/debug/DebugServer.java | 8 +- 5 files changed, 128 insertions(+), 133 deletions(-) rename src/{me/topchetoeu/jscript => assets}/js/bootstrap.js (94%) rename src/{me/topchetoeu/jscript => assets}/js/lib.d.ts (100%) rename src/{me/topchetoeu/jscript => assets}/js/ts.js (100%) diff --git a/src/me/topchetoeu/jscript/js/bootstrap.js b/src/assets/js/bootstrap.js similarity index 94% rename from src/me/topchetoeu/jscript/js/bootstrap.js rename to src/assets/js/bootstrap.js index fa05c2a..65a8e25 100644 --- a/src/me/topchetoeu/jscript/js/bootstrap.js +++ b/src/assets/js/bootstrap.js @@ -1,115 +1,119 @@ -(function (_arguments) { - var ts = _arguments[0]; - var src = '', version = 0; - var lib = _arguments[2].concat([ - 'declare const exit: never;', - 'declare const go: any;', - 'declare function getTsDeclarations(): string[];' - ]).join(''); - var libSnapshot = ts.ScriptSnapshot.fromString(lib); - var environments = {}; - var declSnapshots = []; - - var settings = { - outDir: "/out", - declarationDir: "/out", - target: ts.ScriptTarget.ES5, - lib: [ ], - module: ts.ModuleKind.None, - declaration: true, - stripInternal: true, - downlevelIteration: true, - forceConsistentCasingInFileNames: true, - experimentalDecorators: true, - strict: true, - sourceMap: true, - }; - - var reg = ts.createDocumentRegistry(); - var service = ts.createLanguageService({ - getCurrentDirectory: function() { return "/"; }, - getDefaultLibFileName: function() { return "/lib.d.ts"; }, - getScriptFileNames: function() { - var res = [ "/src.ts", "/lib.d.ts" ]; - for (var i = 0; i < declSnapshots.length; i++) res.push("/glob." + (i + 1) + ".d.ts"); - return res; - }, - getCompilationSettings: function () { return settings; }, - fileExists: function(filename) { return filename === "/lib.d.ts" || filename === "/src.ts" || filename === "/glob.d.ts"; }, - - getScriptSnapshot: function(filename) { - if (filename === "/lib.d.ts") return libSnapshot; - if (filename === "/src.ts") return ts.ScriptSnapshot.fromString(src); - - var index = /\/glob\.(\d+)\.d\.ts/g.exec(filename); - if (index && index[1] && (index = Number(index[1])) && index > 0 && index <= declSnapshots.length) { - return declSnapshots[index - 1]; - } - - throw new Error("File '" + filename + "' doesn't exist."); - }, - getScriptVersion: function (filename) { - if (filename === "/lib.d.ts" || filename.startsWith("/glob.")) return 0; - else return version; - }, - }, reg); - - service.getEmitOutput("/lib.d.ts"); - log("Loaded libraries!"); - - function compile(code, filename, env) { - src = code; - version++; - - if (!environments[env.id]) environments[env.id] = [] - declSnapshots = environments[env.id]; - var emit = service.getEmitOutput("/src.ts"); - - var diagnostics = [] - .concat(service.getCompilerOptionsDiagnostics()) - .concat(service.getSyntacticDiagnostics("/src.ts")) - .concat(service.getSemanticDiagnostics("/src.ts")) - .map(function (diagnostic) { - var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); - if (diagnostic.file) { - var pos = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); - var file = diagnostic.file.fileName.substring(1); - if (file === "src.ts") file = filename; - return file + ":" + (pos.line + 1) + ":" + (pos.character + 1) + ": " + message; - } - else return message; - }); - - if (diagnostics.length > 0) { - throw new SyntaxError(diagnostics.join("\n")); - } - - var map = emit.outputFiles[0].text; - var result = emit.outputFiles[1].text; - var declaration = emit.outputFiles[2].text; - - log(map); - - return { - source: result, - runner: function(func) { - return function() { - var val = func.apply(this, arguments); - if (declaration !== '') { - declSnapshots.push(ts.ScriptSnapshot.fromString(declaration)); - } - return val; - } - } - }; - } - - function apply(env) { - env.compile = compile; - env.global.getTsDeclarations = function() { - return environments[env.id]; - } - } - - apply(_arguments[1]); -})(arguments); +(function (_arguments) { + var ts = _arguments[0]; + var src = '', version = 0; + var lib = _arguments[2].concat([ + 'declare const exit: never;', + 'declare const go: any;', + 'declare function getTsDeclarations(): string[];' + ]).join(''); + var libSnapshot = ts.ScriptSnapshot.fromString(lib); + var environments = {}; + var declSnapshots = []; + + var settings = { + outDir: "/out", + declarationDir: "/out", + target: ts.ScriptTarget.ES5, + lib: [ ], + module: ts.ModuleKind.None, + declaration: true, + stripInternal: true, + downlevelIteration: true, + forceConsistentCasingInFileNames: true, + experimentalDecorators: true, + strict: true, + sourceMap: true, + }; + + var reg = ts.createDocumentRegistry(); + var service = ts.createLanguageService({ + getCurrentDirectory: function() { return "/"; }, + getDefaultLibFileName: function() { return "/lib.d.ts"; }, + getScriptFileNames: function() { + var res = [ "/src.ts", "/lib.d.ts" ]; + for (var i = 0; i < declSnapshots.length; i++) res.push("/glob." + (i + 1) + ".d.ts"); + return res; + }, + getCompilationSettings: function () { return settings; }, + fileExists: function(filename) { return filename === "/lib.d.ts" || filename === "/src.ts" || filename === "/glob.d.ts"; }, + + getScriptSnapshot: function(filename) { + if (filename === "/lib.d.ts") return libSnapshot; + if (filename === "/src.ts") return ts.ScriptSnapshot.fromString(src); + + var index = /\/glob\.(\d+)\.d\.ts/g.exec(filename); + if (index && index[1] && (index = Number(index[1])) && index > 0 && index <= declSnapshots.length) { + return declSnapshots[index - 1]; + } + + throw new Error("File '" + filename + "' doesn't exist."); + }, + getScriptVersion: function (filename) { + if (filename === "/lib.d.ts" || filename.startsWith("/glob.")) return 0; + else return version; + }, + }, reg); + + service.getEmitOutput("/lib.d.ts"); + log("Loaded libraries!"); + + function compile(code, filename, env) { + src = code; + version++; + + if (!environments[env.id]) environments[env.id] = [] + declSnapshots = environments[env.id]; + var emit = service.getEmitOutput("/src.ts"); + + var diagnostics = [] + .concat(service.getCompilerOptionsDiagnostics()) + .concat(service.getSyntacticDiagnostics("/src.ts")) + .concat(service.getSemanticDiagnostics("/src.ts")) + .map(function (diagnostic) { + var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); + if (diagnostic.file) { + var pos = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); + var file = diagnostic.file.fileName.substring(1); + if (file === "src.ts") file = filename; + return file + ":" + (pos.line + 1) + ":" + (pos.character + 1) + ": " + message; + } + else return message; + }); + + if (diagnostics.length > 0) { + throw new SyntaxError(diagnostics.join("\n")); + } + + var map = JSON.parse(emit.outputFiles[0].text); + var result = emit.outputFiles[1].text; + var declaration = emit.outputFiles[2].text; + + return { + source: result, + map: log(JSON.stringify({ + version: 3, + sources: [ filename ], + file: filename, + mappings: map.mappings + })), + runner: function(func) { + return function() { + var val = func.apply(this, arguments); + if (declaration !== '') { + declSnapshots.push(ts.ScriptSnapshot.fromString(declaration)); + } + return val; + } + } + }; + } + + function apply(env) { + env.compile = compile; + env.global.getTsDeclarations = function() { + return environments[env.id]; + } + } + + apply(_arguments[1]); +})(arguments); diff --git a/src/me/topchetoeu/jscript/js/lib.d.ts b/src/assets/js/lib.d.ts similarity index 100% rename from src/me/topchetoeu/jscript/js/lib.d.ts rename to src/assets/js/lib.d.ts diff --git a/src/me/topchetoeu/jscript/js/ts.js b/src/assets/js/ts.js similarity index 100% rename from src/me/topchetoeu/jscript/js/ts.js rename to src/assets/js/ts.js diff --git a/src/me/topchetoeu/jscript/Reading.java b/src/me/topchetoeu/jscript/Reading.java index 6d797c8..c076f53 100644 --- a/src/me/topchetoeu/jscript/Reading.java +++ b/src/me/topchetoeu/jscript/Reading.java @@ -15,22 +15,13 @@ public class Reading { } public static String streamToString(InputStream in) { - try { - StringBuilder out = new StringBuilder(); - BufferedReader br = new BufferedReader(new InputStreamReader(in)); - - for(var line = br.readLine(); line != null; line = br.readLine()) { - out.append(line).append('\n'); - } - - br.close(); - return out.toString(); - } + try { return new String(in.readAllBytes()); } catch (Throwable e) { throw new UncheckedException(e); } } + public static InputStream resourceToStream(String name) { + return Reading.class.getResourceAsStream("/assets/" + name); + } public static String resourceToString(String name) { - var str = Main.class.getResourceAsStream("/me/topchetoeu/jscript/" + name); - if (str == null) return null; - return streamToString(str); + return streamToString(resourceToStream(name)); } } diff --git a/src/me/topchetoeu/jscript/engine/debug/DebugServer.java b/src/me/topchetoeu/jscript/engine/debug/DebugServer.java index 3154ab6..5ac7c53 100644 --- a/src/me/topchetoeu/jscript/engine/debug/DebugServer.java +++ b/src/me/topchetoeu/jscript/engine/debug/DebugServer.java @@ -9,6 +9,7 @@ import java.util.Base64; import java.util.HashMap; import me.topchetoeu.jscript.Metadata; +import me.topchetoeu.jscript.Reading; import me.topchetoeu.jscript.engine.debug.WebSocketMessage.Type; import me.topchetoeu.jscript.events.Notifier; import me.topchetoeu.jscript.exceptions.SyntaxException; @@ -232,10 +233,9 @@ public class DebugServer { public DebugServer() { try { - this.favicon = getClass().getClassLoader().getResourceAsStream("assets/favicon.png").readAllBytes(); - this.protocol = getClass().getClassLoader().getResourceAsStream("assets/protocol.json").readAllBytes(); - var index = new String(getClass().getClassLoader().getResourceAsStream("assets/index.html").readAllBytes()); - this.index = index + this.favicon = Reading.resourceToStream("debugger/favicon.png").readAllBytes(); + this.protocol = Reading.resourceToStream("debugger/protocol.json").readAllBytes(); + this.index = Reading.resourceToString("debugger/index.html") .replace("${NAME}", Metadata.name()) .replace("${VERSION}", Metadata.version()) .replace("${AUTHOR}", Metadata.author())