From 1902e41f610178a5acdfeb5bbe6c11f88fa518ef Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sun, 26 Nov 2023 11:51:52 +0200 Subject: [PATCH] feat: make typescript output mappings --- src/me/topchetoeu/jscript/js/bootstrap.js | 12 ++-- src/me/topchetoeu/jscript/lib/Internals.java | 57 +++++++++++++++++++ .../topchetoeu/jscript/parsing/Parsing.java | 3 +- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/me/topchetoeu/jscript/js/bootstrap.js b/src/me/topchetoeu/jscript/js/bootstrap.js index 6736a28..fa05c2a 100644 --- a/src/me/topchetoeu/jscript/js/bootstrap.js +++ b/src/me/topchetoeu/jscript/js/bootstrap.js @@ -2,7 +2,8 @@ var ts = _arguments[0]; var src = '', version = 0; var lib = _arguments[2].concat([ - 'declare const exit: never; declare const go: any;', + 'declare const exit: never;', + 'declare const go: any;', 'declare function getTsDeclarations(): string[];' ]).join(''); var libSnapshot = ts.ScriptSnapshot.fromString(lib); @@ -21,6 +22,7 @@ forceConsistentCasingInFileNames: true, experimentalDecorators: true, strict: true, + sourceMap: true, }; var reg = ts.createDocumentRegistry(); @@ -82,9 +84,11 @@ throw new SyntaxError(diagnostics.join("\n")); } - var result = emit.outputFiles[0].text; - var declaration = emit.outputFiles[1].text; - + var map = emit.outputFiles[0].text; + var result = emit.outputFiles[1].text; + var declaration = emit.outputFiles[2].text; + + log(map); return { source: result, diff --git a/src/me/topchetoeu/jscript/lib/Internals.java b/src/me/topchetoeu/jscript/lib/Internals.java index d9473a5..44b4434 100644 --- a/src/me/topchetoeu/jscript/lib/Internals.java +++ b/src/me/topchetoeu/jscript/lib/Internals.java @@ -10,8 +10,11 @@ import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.scope.GlobalScope; import me.topchetoeu.jscript.engine.values.FunctionValue; import me.topchetoeu.jscript.engine.values.Values; +import me.topchetoeu.jscript.exceptions.EngineException; +import me.topchetoeu.jscript.filesystem.Buffer; import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.NativeGetter; +import me.topchetoeu.jscript.parsing.Parsing; public class Internals { private static final DataKey> THREADS = new DataKey<>(); @@ -112,6 +115,60 @@ public class Internals { @NativeGetter public static double Infinity(Context ctx) { return Double.POSITIVE_INFINITY; } + private static final String HEX = "0123456789ABCDEF"; + + private static String encodeUriAny(String str, String keepAlphabet) { + if (str == null) str = "undefined"; + + var bytes = str.getBytes(); + var sb = new StringBuilder(bytes.length); + + for (byte c : bytes) { + if (Parsing.isAlphanumeric((char)c) || Parsing.isAny((char)c, keepAlphabet)) sb.append((char)c); + else { + sb.append('%'); + sb.append(HEX.charAt(c / 16)); + sb.append(HEX.charAt(c % 16)); + } + } + + return sb.toString(); + } + private static String decodeUriAny(String str, String keepAlphabet) { + if (str == null) str = "undefined"; + + var res = new Buffer(); + var bytes = str.getBytes(); + + for (var i = 0; i < bytes.length; i++) { + var c = bytes[i]; + if (c == '%') { + if (i >= bytes.length - 2) throw EngineException.ofError("URIError", "URI malformed."); + var b = Parsing.fromHex((char)bytes[i + 1]) * 16 | Parsing.fromHex((char)bytes[i + 2]); + if (!Parsing.isAny((char)b, keepAlphabet)) { + i += 2; + res.append((byte)b); + continue; + } + } + res.append(c); + } + + return new String(res.data()); + } + + @Native public static String encodeURIComponent(String str) { + return encodeUriAny(str, ".-_!~*'()"); + } + @Native public static String decodeURIComponent(String str) { + return decodeUriAny(str, ""); + } + @Native public static String encodeURI(String str) { + return encodeUriAny(str, ";,/?:@&=+$#.-_!~*'()"); + } + @Native public static String decodeURI(String str) { + return decodeUriAny(str, ",/?:@&=+$#."); + } public static Environment apply(Environment env) { var wp = env.wrappers; diff --git a/src/me/topchetoeu/jscript/parsing/Parsing.java b/src/me/topchetoeu/jscript/parsing/Parsing.java index f57ab7e..95e030f 100644 --- a/src/me/topchetoeu/jscript/parsing/Parsing.java +++ b/src/me/topchetoeu/jscript/parsing/Parsing.java @@ -90,7 +90,6 @@ public class Parsing { // We allow yield and await, because they're part of the custom async and generator functions } - public static boolean isDigit(char c) { return c >= '0' && c <= '9'; } @@ -396,7 +395,7 @@ public class Parsing { return tokens; } - private static int fromHex(char c) { + public static int fromHex(char c) { if (c >= 'A' && c <= 'F') return c - 'A' + 10; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= '0' && c <= '9') return c - '0';