refactor: reorganize assets

This commit is contained in:
TopchetoEU 2023-11-26 16:49:47 +02:00
parent 6b1cb852c2
commit e8a7ac8da8
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
5 changed files with 128 additions and 133 deletions

View File

@ -1,115 +1,119 @@
(function (_arguments) { (function (_arguments) {
var ts = _arguments[0]; var ts = _arguments[0];
var src = '', version = 0; var src = '', version = 0;
var lib = _arguments[2].concat([ var lib = _arguments[2].concat([
'declare const exit: never;', 'declare const exit: never;',
'declare const go: any;', 'declare const go: any;',
'declare function getTsDeclarations(): string[];' 'declare function getTsDeclarations(): string[];'
]).join(''); ]).join('');
var libSnapshot = ts.ScriptSnapshot.fromString(lib); var libSnapshot = ts.ScriptSnapshot.fromString(lib);
var environments = {}; var environments = {};
var declSnapshots = []; var declSnapshots = [];
var settings = { var settings = {
outDir: "/out", outDir: "/out",
declarationDir: "/out", declarationDir: "/out",
target: ts.ScriptTarget.ES5, target: ts.ScriptTarget.ES5,
lib: [ ], lib: [ ],
module: ts.ModuleKind.None, module: ts.ModuleKind.None,
declaration: true, declaration: true,
stripInternal: true, stripInternal: true,
downlevelIteration: true, downlevelIteration: true,
forceConsistentCasingInFileNames: true, forceConsistentCasingInFileNames: true,
experimentalDecorators: true, experimentalDecorators: true,
strict: true, strict: true,
sourceMap: true, sourceMap: true,
}; };
var reg = ts.createDocumentRegistry(); var reg = ts.createDocumentRegistry();
var service = ts.createLanguageService({ var service = ts.createLanguageService({
getCurrentDirectory: function() { return "/"; }, getCurrentDirectory: function() { return "/"; },
getDefaultLibFileName: function() { return "/lib.d.ts"; }, getDefaultLibFileName: function() { return "/lib.d.ts"; },
getScriptFileNames: function() { getScriptFileNames: function() {
var res = [ "/src.ts", "/lib.d.ts" ]; var res = [ "/src.ts", "/lib.d.ts" ];
for (var i = 0; i < declSnapshots.length; i++) res.push("/glob." + (i + 1) + ".d.ts"); for (var i = 0; i < declSnapshots.length; i++) res.push("/glob." + (i + 1) + ".d.ts");
return res; return res;
}, },
getCompilationSettings: function () { return settings; }, getCompilationSettings: function () { return settings; },
fileExists: function(filename) { return filename === "/lib.d.ts" || filename === "/src.ts" || filename === "/glob.d.ts"; }, fileExists: function(filename) { return filename === "/lib.d.ts" || filename === "/src.ts" || filename === "/glob.d.ts"; },
getScriptSnapshot: function(filename) { getScriptSnapshot: function(filename) {
if (filename === "/lib.d.ts") return libSnapshot; if (filename === "/lib.d.ts") return libSnapshot;
if (filename === "/src.ts") return ts.ScriptSnapshot.fromString(src); if (filename === "/src.ts") return ts.ScriptSnapshot.fromString(src);
var index = /\/glob\.(\d+)\.d\.ts/g.exec(filename); var index = /\/glob\.(\d+)\.d\.ts/g.exec(filename);
if (index && index[1] && (index = Number(index[1])) && index > 0 && index <= declSnapshots.length) { if (index && index[1] && (index = Number(index[1])) && index > 0 && index <= declSnapshots.length) {
return declSnapshots[index - 1]; return declSnapshots[index - 1];
} }
throw new Error("File '" + filename + "' doesn't exist."); throw new Error("File '" + filename + "' doesn't exist.");
}, },
getScriptVersion: function (filename) { getScriptVersion: function (filename) {
if (filename === "/lib.d.ts" || filename.startsWith("/glob.")) return 0; if (filename === "/lib.d.ts" || filename.startsWith("/glob.")) return 0;
else return version; else return version;
}, },
}, reg); }, reg);
service.getEmitOutput("/lib.d.ts"); service.getEmitOutput("/lib.d.ts");
log("Loaded libraries!"); log("Loaded libraries!");
function compile(code, filename, env) { function compile(code, filename, env) {
src = code; src = code;
version++; version++;
if (!environments[env.id]) environments[env.id] = [] if (!environments[env.id]) environments[env.id] = []
declSnapshots = environments[env.id]; declSnapshots = environments[env.id];
var emit = service.getEmitOutput("/src.ts"); var emit = service.getEmitOutput("/src.ts");
var diagnostics = [] var diagnostics = []
.concat(service.getCompilerOptionsDiagnostics()) .concat(service.getCompilerOptionsDiagnostics())
.concat(service.getSyntacticDiagnostics("/src.ts")) .concat(service.getSyntacticDiagnostics("/src.ts"))
.concat(service.getSemanticDiagnostics("/src.ts")) .concat(service.getSemanticDiagnostics("/src.ts"))
.map(function (diagnostic) { .map(function (diagnostic) {
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
if (diagnostic.file) { if (diagnostic.file) {
var pos = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); var pos = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
var file = diagnostic.file.fileName.substring(1); var file = diagnostic.file.fileName.substring(1);
if (file === "src.ts") file = filename; if (file === "src.ts") file = filename;
return file + ":" + (pos.line + 1) + ":" + (pos.character + 1) + ": " + message; return file + ":" + (pos.line + 1) + ":" + (pos.character + 1) + ": " + message;
} }
else return message; else return message;
}); });
if (diagnostics.length > 0) { if (diagnostics.length > 0) {
throw new SyntaxError(diagnostics.join("\n")); throw new SyntaxError(diagnostics.join("\n"));
} }
var map = emit.outputFiles[0].text; var map = JSON.parse(emit.outputFiles[0].text);
var result = emit.outputFiles[1].text; var result = emit.outputFiles[1].text;
var declaration = emit.outputFiles[2].text; var declaration = emit.outputFiles[2].text;
log(map); return {
source: result,
return { map: log(JSON.stringify({
source: result, version: 3,
runner: function(func) { sources: [ filename ],
return function() { file: filename,
var val = func.apply(this, arguments); mappings: map.mappings
if (declaration !== '') { })),
declSnapshots.push(ts.ScriptSnapshot.fromString(declaration)); runner: function(func) {
} return function() {
return val; 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];
} function apply(env) {
} env.compile = compile;
env.global.getTsDeclarations = function() {
apply(_arguments[1]); return environments[env.id];
})(arguments); }
}
apply(_arguments[1]);
})(arguments);

View File

@ -15,22 +15,13 @@ public class Reading {
} }
public static String streamToString(InputStream in) { public static String streamToString(InputStream in) {
try { try { return new String(in.readAllBytes()); }
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();
}
catch (Throwable e) { throw new UncheckedException(e); } 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) { public static String resourceToString(String name) {
var str = Main.class.getResourceAsStream("/me/topchetoeu/jscript/" + name); return streamToString(resourceToStream(name));
if (str == null) return null;
return streamToString(str);
} }
} }

View File

@ -9,6 +9,7 @@ import java.util.Base64;
import java.util.HashMap; import java.util.HashMap;
import me.topchetoeu.jscript.Metadata; import me.topchetoeu.jscript.Metadata;
import me.topchetoeu.jscript.Reading;
import me.topchetoeu.jscript.engine.debug.WebSocketMessage.Type; import me.topchetoeu.jscript.engine.debug.WebSocketMessage.Type;
import me.topchetoeu.jscript.events.Notifier; import me.topchetoeu.jscript.events.Notifier;
import me.topchetoeu.jscript.exceptions.SyntaxException; import me.topchetoeu.jscript.exceptions.SyntaxException;
@ -232,10 +233,9 @@ public class DebugServer {
public DebugServer() { public DebugServer() {
try { try {
this.favicon = getClass().getClassLoader().getResourceAsStream("assets/favicon.png").readAllBytes(); this.favicon = Reading.resourceToStream("debugger/favicon.png").readAllBytes();
this.protocol = getClass().getClassLoader().getResourceAsStream("assets/protocol.json").readAllBytes(); this.protocol = Reading.resourceToStream("debugger/protocol.json").readAllBytes();
var index = new String(getClass().getClassLoader().getResourceAsStream("assets/index.html").readAllBytes()); this.index = Reading.resourceToString("debugger/index.html")
this.index = index
.replace("${NAME}", Metadata.name()) .replace("${NAME}", Metadata.name())
.replace("${VERSION}", Metadata.version()) .replace("${VERSION}", Metadata.version())
.replace("${AUTHOR}", Metadata.author()) .replace("${AUTHOR}", Metadata.author())