refactor: make filenames more consistent

This commit is contained in:
TopchetoEU 2023-11-04 11:36:36 +02:00
parent b675411925
commit 69f93b4f87
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
5 changed files with 8 additions and 6 deletions

View File

@ -46,6 +46,8 @@ public class Filename {
public Filename(String protocol, String path) { public Filename(String protocol, String path) {
path = path.trim();
protocol = protocol.trim();
this.protocol = protocol; this.protocol = protocol;
this.path = path; this.path = path;
} }

View File

@ -1,7 +1,7 @@
package me.topchetoeu.jscript; package me.topchetoeu.jscript;
public class Location implements Comparable<Location> { public class Location implements Comparable<Location> {
public static final Location INTERNAL = new Location(0, 0, new Filename("jscript", "internal")); public static final Location INTERNAL = new Location(0, 0, new Filename("jscript", "native"));
private int line; private int line;
private int start; private int start;
private Filename filename; private Filename filename;

View File

@ -2,7 +2,6 @@ package me.topchetoeu.jscript.engine.debug;
import java.util.Map; import java.util.Map;
import me.topchetoeu.jscript.Filename;
import me.topchetoeu.jscript.json.JSON; import me.topchetoeu.jscript.json.JSON;
import me.topchetoeu.jscript.json.JSONElement; import me.topchetoeu.jscript.json.JSONElement;
import me.topchetoeu.jscript.json.JSONMap; import me.topchetoeu.jscript.json.JSONMap;
@ -33,7 +32,7 @@ public class V8Message {
this.params = raw.contains("params") ? raw.map("params") : new JSONMap(); this.params = raw.contains("params") ? raw.map("params") : new JSONMap();
} }
public V8Message(String raw) { public V8Message(String raw) {
this(JSON.parse(new Filename("jscript", "json-msg"), raw).map()); this(JSON.parse(null, raw).map());
} }
public JSONMap toMap() { public JSONMap toMap() {

View File

@ -177,6 +177,7 @@ public class JSON {
return ParseRes.res(values, n); return ParseRes.res(values, n);
} }
public static JSONElement parse(Filename filename, String raw) { public static JSONElement parse(Filename filename, String raw) {
if (filename == null) filename = new Filename("jscript", "json");
var res = parseValue(filename, Parsing.tokenize(filename, raw), 0); var res = parseValue(filename, Parsing.tokenize(filename, raw), 0);
if (res.isFailed()) throw new SyntaxException(null, "Invalid JSON given."); if (res.isFailed()) throw new SyntaxException(null, "Invalid JSON given.");
else if (res.isError()) throw new SyntaxException(null, res.error); else if (res.isError()) throw new SyntaxException(null, res.error);
@ -191,6 +192,7 @@ public class JSON {
.replace("\\", "\\\\") .replace("\\", "\\\\")
.replace("\n", "\\n") .replace("\n", "\\n")
.replace("\r", "\\r") .replace("\r", "\\r")
.replace("\t", "\\t")
.replace("\"", "\\\"") .replace("\"", "\\\"")
+ "\""; + "\"";
if (el.isList()) { if (el.isList()) {

View File

@ -1,17 +1,16 @@
package me.topchetoeu.jscript.lib; package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.Filename;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.exceptions.EngineException; import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.exceptions.SyntaxException; import me.topchetoeu.jscript.exceptions.SyntaxException;
import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.json.JSON; import me.topchetoeu.jscript.json.JSON;
public class JSONLib { @Native("JSON") public class JSONLib {
@Native @Native
public static Object parse(Context ctx, String val) { public static Object parse(Context ctx, String val) {
try { try {
return JSON.toJs(JSON.parse(new Filename("jscript", "json"), val)); return JSON.toJs(JSON.parse(null, val));
} }
catch (SyntaxException e) { throw EngineException.ofSyntax(e.msg); } catch (SyntaxException e) { throw EngineException.ofSyntax(e.msg); }
} }