fix: some annoying bugs, as well as splice

This commit is contained in:
TopchetoEU 2023-11-06 00:55:30 +02:00
parent 15f4278cb1
commit f5d1287948
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
8 changed files with 170122 additions and 23 deletions

View File

@ -15,7 +15,7 @@ jobs:
uses: actions/setup-java@v3
with:
distribution: 'adopt'
java-version: '17'
java-version: '11'
- name: Clone repository
uses: GuillaumeFalourd/clone-github-repo-action@main
with:

View File

@ -58,7 +58,7 @@ public class Engine implements DebugController {
private LinkedBlockingDeque<Task> microTasks = new LinkedBlockingDeque<>();
public final int id = ++nextId;
public final Data data = new Data().set(StackData.MAX_FRAMES, 200);
public final Data data = new Data().set(StackData.MAX_FRAMES, 10000);
public final boolean debugging;
private final HashMap<Filename, String> sources = new HashMap<>();
private final HashMap<Filename, TreeSet<Location>> bpts = new HashMap<>();

View File

@ -17,7 +17,8 @@ public class StackData {
public static void pushFrame(Context ctx, CodeFrame frame) {
var frames = ctx.data.get(FRAMES, new ArrayList<>());
frames.add(frame);
if (frames.size() > ctx.data.get(MAX_FRAMES, 10000)) throw EngineException.ofRange("Stack overflow!");
if (frames.size() > ctx.data.get(MAX_FRAMES, 10000))
throw EngineException.ofRange("Stack overflow!");
ctx.pushEnv(frame.function.environment);
}
public static boolean popFrame(Context ctx, CodeFrame frame) {

View File

@ -596,6 +596,16 @@ public class Values {
return fromJavaIterator(ctx, it.iterator());
}
private static boolean isEmptyFunc(ObjectValue val) {
if (!(val instanceof FunctionValue)) return false;
if (!val.values.containsKey("prototype") || val.values.size() + val.properties.size() > 1) return false;
var proto = val.values.get("prototype");
if (!(proto instanceof ObjectValue)) return false;
var protoObj = (ObjectValue)proto;
if (protoObj.values.get("constructor") != val) return false;
if (protoObj.values.size() + protoObj.properties.size() != 1) return false;
return true;
}
private static void printValue(Context ctx, Object val, HashSet<Object> passed, int tab) {
if (tab == 0 && val instanceof String) {
System.out.print(val);
@ -643,7 +653,7 @@ public class Values {
passed.add(val);
var obj = (ObjectValue)val;
if (obj.values.size() + obj.properties.size() == 0) {
if (obj.values.size() + obj.properties.size() == 0 || isEmptyFunc(obj)) {
if (!printed) System.out.println("{}");
}
else {
@ -665,8 +675,9 @@ public class Values {
for (int i = 0; i < tab; i++) System.out.print(" ");
System.out.print("}");
passed.remove(val);
}
passed.remove(val);
}
else if (val == null) System.out.print("undefined");
else if (val == Values.NULL) System.out.print("null");

View File

@ -1,6 +1,6 @@
(function (_arguments) {
var ts = _arguments[0];
var src = '', lib = _arguments[2].concat([ 'declare const exit: never;' ]).join(''), decls = '', version = 0;
var src = '', lib = _arguments[2].concat([ 'declare const exit: never; declare const go: any;' ]).join(''), decls = '', version = 0;
var libSnapshot = ts.ScriptSnapshot.fromString(lib);
var settings = {
@ -20,7 +20,7 @@
var reg = ts.createDocumentRegistry();
var service = ts.createLanguageService({
getCurrentDirectory: function() { return "/"; },
getDefaultLibFileName: function() { return "/lib_.d.ts"; },
getDefaultLibFileName: function() { return "/lib.d.ts"; },
getScriptFileNames: function() { return [ "/src.ts", "/lib.d.ts", "/glob.d.ts" ]; },
getCompilationSettings: function () { return settings; },
fileExists: function(filename) { return filename === "/lib.d.ts" || filename === "/src.ts" || filename === "/glob.d.ts"; },
@ -37,11 +37,12 @@
},
}, reg);
service.getEmitOutput('/lib.d.ts');
log('Loaded libraries!');
service.getEmitOutput("/lib.d.ts");
log("Loaded libraries!");
function compile(code, filename) {
src = code, version++;
src = code;
version++;
var emit = service.getEmitOutput("/src.ts");
@ -61,7 +62,7 @@
});
if (diagnostics.length > 0) {
throw new SyntaxError(diagnostics.join('\n'));
throw new SyntaxError(diagnostics.join("\n"));
}
return {

File diff suppressed because one or more lines are too long

View File

@ -309,8 +309,9 @@ import me.topchetoeu.jscript.interop.NativeSetter;
return res;
}
@Native(thisArg = true) public static ArrayValue splice(Context ctx, ArrayValue arr, int start, int deleteCount, Object ...items) {
@Native(thisArg = true) public static ArrayValue splice(Context ctx, ArrayValue arr, int start, Object _deleteCount, Object ...items) {
start = normalizeI(arr.size(), start, true);
int deleteCount = _deleteCount == null ? arr.size() - 1 : (int)Values.toNumber(ctx, _deleteCount);
deleteCount = normalizeI(arr.size(), deleteCount, true);
if (start + deleteCount >= arr.size()) deleteCount = arr.size() - start;
@ -323,9 +324,6 @@ import me.topchetoeu.jscript.interop.NativeSetter;
return res;
}
@Native(thisArg = true) public static ArrayValue splice(Context ctx, ArrayValue arr, int start) {
return splice(ctx, arr, start, arr.size() - start);
}
@Native(thisArg = true) public static String toString(Context ctx, ArrayValue arr) {
return join(ctx, arr, ",");
}

View File

@ -18,12 +18,15 @@ public class Internals {
private static final DataKey<Integer> I = new DataKey<>();
@Native public static void log(Context ctx, Object ...args) {
@Native public static Object log(Context ctx, Object ...args) {
for (var arg : args) {
Values.printValue(ctx, arg);
System.out.print(" ");
}
System.out.println();
if (args.length == 0) return null;
else return args[0];
}
@Native public static String readline(Context ctx) {
try {