Compare commits

..

29 Commits

Author SHA1 Message Date
0d629a6e82 fix: use correct class instead of proxy 2024-04-03 12:27:15 +03:00
6eea342d04 fix: fuck 2024-04-02 18:24:43 +03:00
ece9cf68dc fix: correctly update proto chain 2024-04-02 18:19:05 +03:00
11ecd8c68f fix: exec debugger close logic on application exit 2024-04-02 18:05:49 +03:00
48bd304c6e fix: environment forks fixes 2024-04-02 18:05:20 +03:00
d8e46c3149 fix: clone environment correctly 2024-03-31 16:11:32 +03:00
5fc5eb08f8 fix: update breakpoints when removing bp 2024-03-30 12:52:44 +02:00
8acbc003c4 fix: properly resolve breakpoints 2024-03-30 12:13:04 +02:00
fda33112a7 fix: load maps when attaching debugger 2024-03-30 11:13:45 +02:00
67b2413d7c bump2 2024-03-30 10:36:55 +02:00
3a05416510 bump 2024-03-30 10:30:26 +02:00
c291328cc3 fix: detach debugger after close 2024-03-30 10:22:12 +02:00
7cb267b0d9 fix: some issues with debugger 2024-03-30 09:55:20 +02:00
4e31766665 fix: add new vscode debugger functions 2024-03-29 21:53:15 +02:00
b5b63c4342 fix: make global cache of native wrappers 2024-03-28 16:08:07 +02:00
18f70a0d58 fix: i hate wrappers 2024-03-28 15:10:21 +02:00
d38b600366 fix: some more wrapper issues 2024-03-28 14:52:49 +02:00
0ac7af2ea3 fix: take into account empty classes 2024-03-28 14:21:23 +02:00
5185c93663 fix: don't include non-exposing wrappers in proto chain
feat: allow adding custom wrappers
2024-03-28 00:57:09 +02:00
510422cab7 feat: implement logic for exposing non-static fields 2024-03-27 23:39:33 +02:00
79e1d1cfaf Merge branch 'master' of https://github.com/TopchetoEU/java-jscript 2024-03-27 23:08:25 +02:00
e0f3274a95 feat: add simple for-of loop (not intended for production usage) 2024-03-27 23:08:21 +02:00
ef5d29105f Update README.md 2024-03-10 02:17:18 +02:00
d8ea6557df fix: buildline expects tag to start with 'v' 2024-03-09 00:45:00 +02:00
5ba858545a fix: defer handles of async functions 2024-03-09 00:28:30 +02:00
446ecd8f2b fix: promise defers callback twice 2024-03-08 17:23:50 +02:00
fbf103439a bump 2024-03-08 16:55:46 +02:00
b30f94de8f refactor: move function pushMsg signatures in EventLoop 2024-03-08 16:53:47 +02:00
47b4dd3c15 refactor: rename code to runtime 2024-03-06 23:23:01 +02:00
85 changed files with 833 additions and 536 deletions

View File

@@ -3,7 +3,7 @@ name: "tagged-release"
on:
push:
tags:
- "v*"
- "*"
jobs:
tagged-release:

View File

@@ -23,7 +23,3 @@ engine.run(true);
// Get our result
System.out.println(awaitable.await());
```
## NOTE:
To setup the typescript bundle in your sources, run `node build.js init-ts`. This will download the latest version of typescript, minify it, and add it to your src folder. If you are going to work with the `node build.js debug|release` command, this is not a necessary step.

View File

@@ -1,4 +1,4 @@
project_group = me.topchetoeu
project_name = jscript
project_version = 0.9.7-beta
project_version = 0.9.27-beta
main_class = me.topchetoeu.jscript.utils.JScriptRepl

View File

@@ -5,7 +5,7 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class Instruction {
public static enum Type {

View File

@@ -1,6 +1,6 @@
package me.topchetoeu.jscript.common.events;
import me.topchetoeu.jscript.core.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
public class Notifier {
private boolean ok = false;

View File

@@ -9,12 +9,12 @@ import me.topchetoeu.jscript.compilation.parsing.Operator;
import me.topchetoeu.jscript.compilation.parsing.ParseRes;
import me.topchetoeu.jscript.compilation.parsing.Parsing;
import me.topchetoeu.jscript.compilation.parsing.Token;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
public class JSON {
public static Object toJs(JSONElement val) {

View File

@@ -104,7 +104,9 @@ public class FunctionMap {
var res = new ArrayList<Location>(candidates.size());
for (var candidate : candidates.entrySet()) {
res.add(candidate.getValue().ceiling(new Location(line, column, candidate.getKey())));
var val = correctBreakpoint(new Location(line, column, candidate.getKey()));
if (val == null) continue;
res.add(val);
}
return res;

View File

@@ -1,7 +1,7 @@
package me.topchetoeu.jscript.compilation;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class ThrowSyntaxStatement extends Statement {
public final String name;

View File

@@ -0,0 +1,73 @@
package me.topchetoeu.jscript.compilation.control;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.Location;
import me.topchetoeu.jscript.common.Instruction.BreakpointType;
import me.topchetoeu.jscript.compilation.CompileResult;
import me.topchetoeu.jscript.compilation.Statement;
public class ForOfStatement extends Statement {
public final String varName;
public final boolean isDeclaration;
public final Statement iterable, body;
public final String label;
public final Location varLocation;
@Override
public void declare(CompileResult target) {
body.declare(target);
if (isDeclaration) target.scope.define(varName);
}
@Override
public void compile(CompileResult target, boolean pollute) {
var key = target.scope.getKey(varName);
if (key instanceof String) target.add(Instruction.makeVar((String)key));
iterable.compile(target, true, BreakpointType.STEP_OVER);
target.add(Instruction.dup());
target.add(Instruction.loadVar("Symbol"));
target.add(Instruction.pushValue("iterator"));
target.add(Instruction.loadMember()).setLocation(iterable.loc());
target.add(Instruction.loadMember()).setLocation(iterable.loc());
target.add(Instruction.call(0)).setLocation(iterable.loc());
int start = target.size();
target.add(Instruction.dup());
target.add(Instruction.dup());
target.add(Instruction.pushValue("next"));
target.add(Instruction.loadMember()).setLocation(iterable.loc());
target.add(Instruction.call(0)).setLocation(iterable.loc());
target.add(Instruction.dup());
target.add(Instruction.pushValue("done"));
target.add(Instruction.loadMember()).setLocation(iterable.loc());
int mid = target.temp();
target.add(Instruction.pushValue("value"));
target.add(Instruction.loadMember()).setLocation(varLocation);
target.add(Instruction.storeVar(key)).setLocationAndDebug(iterable.loc(), BreakpointType.STEP_OVER);
body.compile(target, false, BreakpointType.STEP_OVER);
int end = target.size();
WhileStatement.replaceBreaks(target, label, mid + 1, end, start, end + 1);
target.add(Instruction.jmp(start - end));
target.add(Instruction.discard());
target.add(Instruction.discard());
target.set(mid, Instruction.jmpIf(end - mid + 1));
if (pollute) target.add(Instruction.pushUndefined());
}
public ForOfStatement(Location loc, Location varLocation, String label, boolean isDecl, String varName, Statement object, Statement body) {
super(loc);
this.varLocation = varLocation;
this.label = label;
this.isDeclaration = isDecl;
this.varName = varName;
this.iterable = object;
this.body = body;
}
}

View File

@@ -18,7 +18,7 @@ import me.topchetoeu.jscript.compilation.control.SwitchStatement.SwitchCase;
import me.topchetoeu.jscript.compilation.parsing.ParseRes.State;
import me.topchetoeu.jscript.compilation.scope.LocalScopeRecord;
import me.topchetoeu.jscript.compilation.values.*;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
// TODO: this has to be rewritten
public class Parsing {
@@ -1777,6 +1777,46 @@ public class Parsing {
return ParseRes.res(new ForInStatement(loc, nameLoc, labelRes.result, isDecl, nameRes.result, varVal, objRes.result, bodyRes.result), n);
}
public static ParseRes<ForOfStatement> parseForOf(Filename filename, List<Token> tokens, int i) {
var loc = getLoc(filename, tokens, i);
int n = 0;
var labelRes = parseLabel(tokens, i + n);
var isDecl = false;
n += labelRes.n;
if (!isIdentifier(tokens, i + n++, "for")) return ParseRes.failed();
if (!isOperator(tokens, i + n++, Operator.PAREN_OPEN)) return ParseRes.error(loc, "Expected a open paren after 'for'.");
if (isIdentifier(tokens, i + n, "var")) {
isDecl = true;
n++;
}
var nameRes = parseIdentifier(tokens, i + n);
if (!nameRes.isSuccess()) return ParseRes.error(loc, "Expected a variable name for 'for' loop.");
var nameLoc = getLoc(filename, tokens, i + n);
n += nameRes.n;
if (!isIdentifier(tokens, i + n++, "of")) {
if (nameRes.result.equals("const")) return ParseRes.error(loc, "'const' declarations are not supported.");
else if (nameRes.result.equals("let")) return ParseRes.error(loc, "'let' declarations are not supported.");
else return ParseRes.error(loc, "Expected 'of' keyword after variable declaration.");
}
var objRes = parseValue(filename, tokens, i + n, 0);
if (!objRes.isSuccess()) return ParseRes.error(loc, "Expected a value.", objRes);
n += objRes.n;
if (!isOperator(tokens, i + n++, Operator.PAREN_CLOSE)) return ParseRes.error(loc, "Expected a closing paren after for.");
var bodyRes = parseStatement(filename, tokens, i + n);
if (!bodyRes.isSuccess()) return ParseRes.error(loc, "Expected a for body.", bodyRes);
n += bodyRes.n;
return ParseRes.res(new ForOfStatement(loc, nameLoc, labelRes.result, isDecl, nameRes.result, objRes.result, bodyRes.result), n);
}
public static ParseRes<TryStatement> parseCatch(Filename filename, List<Token> tokens, int i) {
var loc = getLoc(filename, tokens, i);
int n = 0;
@@ -1833,6 +1873,7 @@ public class Parsing {
parseSwitch(filename, tokens, i),
parseFor(filename, tokens, i),
parseForIn(filename, tokens, i),
parseForOf(filename, tokens, i),
parseDoWhile(filename, tokens, i),
parseCatch(filename, tokens, i),
parseCompound(filename, tokens, i),

View File

@@ -7,7 +7,7 @@ import me.topchetoeu.jscript.common.Instruction.Type;
import me.topchetoeu.jscript.compilation.CompileResult;
import me.topchetoeu.jscript.compilation.CompoundStatement;
import me.topchetoeu.jscript.compilation.Statement;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class FunctionStatement extends Statement {
public final CompoundStatement body;

View File

@@ -1,5 +0,0 @@
package me.topchetoeu.jscript.core;
public class Key<T> {
}

View File

@@ -1,32 +0,0 @@
package me.topchetoeu.jscript.core.values;
import me.topchetoeu.jscript.core.Context;
public class NativeWrapper extends ObjectValue {
private static final Object NATIVE_PROTO = new Object();
public final Object wrapped;
@Override
public ObjectValue getPrototype(Context ctx) {
if (ctx.environment != null && prototype == NATIVE_PROTO) return ctx.environment.wrappers.getProto(wrapped.getClass());
else return super.getPrototype(ctx);
}
@Override
public String toString() {
return wrapped.toString();
}
@Override
public boolean equals(Object obj) {
return wrapped.equals(obj);
}
@Override
public int hashCode() {
return wrapped.hashCode();
}
public NativeWrapper(Object wrapped) {
this.wrapped = wrapped;
prototype = NATIVE_PROTO;
}
}

View File

@@ -3,11 +3,11 @@ package me.topchetoeu.jscript.lib;
import java.util.Iterator;
import java.util.Stack;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;

View File

@@ -1,13 +1,13 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Frame;
import me.topchetoeu.jscript.core.values.CodeFunction;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.lib.PromiseLib.Handle;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.CodeFunction;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.WrapperName;
@@ -54,7 +54,7 @@ public class AsyncFunctionLib extends FunctionValue {
public void onReject(EngineException err) {
next(ctx, Values.NO_RETURN, err);
}
});
}.defer(ctx));
}
}

View File

@@ -1,11 +1,11 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Frame;
import me.topchetoeu.jscript.core.values.CodeFunction;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.CodeFunction;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.utils.interop.WrapperName;
@WrapperName("AsyncGeneratorFunction")

View File

@@ -2,12 +2,12 @@ package me.topchetoeu.jscript.lib;
import java.util.Map;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Frame;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.lib.PromiseLib.Handle;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.WrapperName;
@@ -62,7 +62,7 @@ public class AsyncGeneratorLib {
@Override public void onReject(EngineException err) {
next(ctx, Values.NO_RETURN, Values.NO_RETURN, err);
}
});
}.defer(ctx));
}
else if (state == 2) {
var obj = new ObjectValue();

View File

@@ -1,7 +1,7 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;

View File

@@ -2,7 +2,7 @@ package me.topchetoeu.jscript.lib;
import java.io.IOException;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.filesystem.File;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;

View File

@@ -4,9 +4,9 @@ import java.util.ArrayList;
import me.topchetoeu.jscript.common.Buffer;
import me.topchetoeu.jscript.compilation.parsing.Parsing;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeTarget;

View File

@@ -1,10 +1,10 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.values.ObjectValue.PlaceholderProto;
import me.topchetoeu.jscript.core.exceptions.ConvertException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.exceptions.ConvertException;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.runtime.values.ObjectValue.PlaceholderProto;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;

View File

@@ -1,7 +1,7 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.filesystem.File;
import me.topchetoeu.jscript.utils.filesystem.FilesystemException;
import me.topchetoeu.jscript.utils.interop.Arguments;

View File

@@ -4,10 +4,10 @@ import java.io.IOException;
import java.util.Iterator;
import java.util.Stack;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.filesystem.ActionType;
import me.topchetoeu.jscript.utils.filesystem.EntryType;
import me.topchetoeu.jscript.utils.filesystem.ErrorReason;

View File

@@ -1,9 +1,9 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.CodeFunction;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.CodeFunction;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeTarget;

View File

@@ -1,11 +1,11 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Frame;
import me.topchetoeu.jscript.core.values.CodeFunction;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.CodeFunction;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.utils.interop.WrapperName;
@WrapperName("GeneratorFunction")

View File

@@ -1,10 +1,10 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Frame;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.WrapperName;

View File

@@ -2,14 +2,14 @@ package me.topchetoeu.jscript.lib;
import java.util.HashMap;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.EventLoop;
import me.topchetoeu.jscript.core.Key;
import me.topchetoeu.jscript.core.scope.GlobalScope;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.EventLoop;
import me.topchetoeu.jscript.runtime.Key;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.scope.GlobalScope;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.filesystem.Filesystem;
import me.topchetoeu.jscript.utils.filesystem.Mode;
import me.topchetoeu.jscript.utils.interop.Arguments;

View File

@@ -1,8 +1,8 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.common.json.JSON;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeTarget;

View File

@@ -4,10 +4,10 @@ import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;

View File

@@ -1,7 +1,7 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;

View File

@@ -1,11 +1,11 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Symbol;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Symbol;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;

View File

@@ -4,16 +4,16 @@ import java.util.ArrayList;
import java.util.List;
import me.topchetoeu.jscript.common.ResultRunnable;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.EventLoop;
import me.topchetoeu.jscript.core.Extensions;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.EventLoop;
import me.topchetoeu.jscript.runtime.Extensions;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;
@@ -53,26 +53,29 @@ public class PromiseLib {
private Object val;
private void resolveSynchronized(Context ctx, Object val, int newState) {
if (!ctx.hasNotNull(EventLoop.KEY)) throw EngineException.ofError("No event loop");
ctx.get(EventLoop.KEY).pushMsg(() -> {
this.val = val;
this.state = newState;
for (var handle : handles) {
if (newState == STATE_FULFILLED) handle.onFulfil(val);
if (newState == STATE_REJECTED) {
handle.onReject((EngineException)val);
handled = true;
}
this.val = val;
this.state = newState;
for (var handle : handles) {
if (newState == STATE_FULFILLED) handle.onFulfil(val);
if (newState == STATE_REJECTED) {
handle.onReject((EngineException)val);
handled = true;
}
}
if (state == STATE_REJECTED && !handled) {
Values.printError(((EngineException)val).setCtx(ctx), "(in promise)");
}
if (state == STATE_REJECTED && !handled) {
Values.printError(((EngineException)val).setCtx(ctx), "(in promise)");
}
handles = null;
}, true);
handles = null;
// ctx.get(EventLoop.KEY).pushMsg(() -> {
// if (!ctx.hasNotNull(EventLoop.KEY)) throw EngineException.ofError("No event loop");
// handles = null;
// }, true);
}
private synchronized void resolve(Context ctx, Object val, int newState) {

View File

@@ -1,7 +1,7 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.ObjectValue.PlaceholderProto;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue.PlaceholderProto;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;
import me.topchetoeu.jscript.utils.interop.ExposeField;

View File

@@ -4,12 +4,12 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Pattern;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeWrapper;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeWrapper;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;

View File

@@ -4,10 +4,10 @@ import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.stream.Collectors;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;

View File

@@ -2,13 +2,13 @@ package me.topchetoeu.jscript.lib;
import java.util.regex.Pattern;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Symbol;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Symbol;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;

View File

@@ -3,10 +3,10 @@ package me.topchetoeu.jscript.lib;
import java.util.HashMap;
import java.util.Map;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Symbol;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Symbol;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;

View File

@@ -1,7 +1,7 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.ObjectValue.PlaceholderProto;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue.PlaceholderProto;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;
import me.topchetoeu.jscript.utils.interop.ExposeField;

View File

@@ -0,0 +1,18 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
public class ThrowableLib {
@Expose public static String __message(Arguments args) {
if (args.self instanceof Throwable) return ((Throwable)args.self).getMessage();
else return null;
}
@Expose public static String __name(Arguments args) {
return args.self.getClass().getSimpleName();
}
@Expose public static String __toString(Arguments args) {
return __name(args) + ": " + __message(args);
}
}

View File

@@ -1,7 +1,7 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.ObjectValue.PlaceholderProto;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue.PlaceholderProto;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.ExposeConstructor;
import me.topchetoeu.jscript.utils.interop.ExposeField;

View File

@@ -1,10 +1,10 @@
package me.topchetoeu.jscript.core;
package me.topchetoeu.jscript.runtime;
import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.common.FunctionBody;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.scope.ValueVariable;
import me.topchetoeu.jscript.core.values.CodeFunction;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.scope.ValueVariable;
import me.topchetoeu.jscript.runtime.values.CodeFunction;
public interface Compiler {
public Key<Compiler> KEY = new Key<>();

View File

@@ -1,14 +1,14 @@
package me.topchetoeu.jscript.core;
package me.topchetoeu.jscript.runtime;
import java.util.Iterator;
import java.util.List;
import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.core.debug.DebugContext;
import me.topchetoeu.jscript.core.values.CodeFunction;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.scope.ValueVariable;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.scope.ValueVariable;
import me.topchetoeu.jscript.runtime.values.CodeFunction;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
public class Context implements Extensions {
public static final Context NULL = new Context();

View File

@@ -1,12 +1,10 @@
package me.topchetoeu.jscript.core;
package me.topchetoeu.jscript.runtime;
import java.util.concurrent.PriorityBlockingQueue;
import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.common.ResultRunnable;
import me.topchetoeu.jscript.common.events.DataNotifier;
import me.topchetoeu.jscript.core.exceptions.InterruptException;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
public class Engine implements EventLoop {
private static class Task<T> implements Comparable<Task<?>> {
@@ -78,18 +76,6 @@ public class Engine implements EventLoop {
return this.thread != null;
}
public DataNotifier<Object> pushMsg(boolean micro, Environment env, FunctionValue func, Object thisArg, Object ...args) {
return pushMsg(() -> {
return func.call(new Context(env), thisArg, args);
}, micro);
}
public DataNotifier<Object> pushMsg(boolean micro, Environment env, Filename filename, String raw, Object thisArg, Object ...args) {
return pushMsg(() -> {
var ctx = new Context(env);
return ctx.compile(filename, raw).call(new Context(env), thisArg, args);
}, micro);
}
public Engine() {
}
}

View File

@@ -1,12 +1,12 @@
package me.topchetoeu.jscript.core;
package me.topchetoeu.jscript.runtime;
import java.util.HashMap;
import me.topchetoeu.jscript.core.scope.GlobalScope;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.scope.GlobalScope;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.utils.interop.NativeWrapperProvider;
@SuppressWarnings("unchecked")
@@ -61,7 +61,7 @@ public class Environment implements Extensions {
}
public Environment copy() {
var res = new Environment(null, global);
var res = new Environment();
res.wrappers = wrappers.fork(res);
res.global = global;
@@ -80,7 +80,7 @@ public class Environment implements Extensions {
}
public Environment(WrapperProvider nativeConverter, GlobalScope global) {
if (nativeConverter == null) nativeConverter = new NativeWrapperProvider(this);
if (nativeConverter == null) nativeConverter = new NativeWrapperProvider();
if (global == null) global = new GlobalScope();
this.wrappers = nativeConverter;

View File

@@ -1,8 +1,10 @@
package me.topchetoeu.jscript.core;
package me.topchetoeu.jscript.runtime;
import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.common.ResultRunnable;
import me.topchetoeu.jscript.common.events.DataNotifier;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
public interface EventLoop {
public static final Key<EventLoop> KEY = new Key<>();
@@ -20,4 +22,16 @@ public interface EventLoop {
public default DataNotifier<Void> pushMsg(Runnable runnable, boolean micro) {
return pushMsg(() -> { runnable.run(); return null; }, micro);
}
public default DataNotifier<Object> pushMsg(boolean micro, Environment env, FunctionValue func, Object thisArg, Object ...args) {
return pushMsg(() -> {
return func.call(new Context(env), thisArg, args);
}, micro);
}
public default DataNotifier<Object> pushMsg(boolean micro, Environment env, Filename filename, String raw, Object thisArg, Object ...args) {
return pushMsg(() -> {
var ctx = new Context(env);
return ctx.compile(filename, raw).call(new Context(env), thisArg, args);
}, micro);
}
}

View File

@@ -1,6 +1,17 @@
package me.topchetoeu.jscript.core;
package me.topchetoeu.jscript.runtime;
import java.util.List;
public interface Extensions {
public static Extensions EMPTY = new Extensions() {
@Override public <T> void add(Key<T> key, T obj) { }
@Override public boolean remove(Key<?> key) { return false; }
@Override public <T> T get(Key<T> key) { return null; }
@Override public boolean has(Key<?> key) { return false; }
@Override public Iterable<Key<?>> keys() { return List.of(); }
};
<T> T get(Key<T> key);
<T> void add(Key<T> key, T obj);
Iterable<Key<?>> keys();
@@ -35,4 +46,9 @@ public interface Extensions {
add((Key<Object>)key, (Object)source.get(key));
}
}
public static Extensions wrap(Extensions ext) {
if (ext == null) return EMPTY;
else return ext;
}
}

View File

@@ -1,19 +1,19 @@
package me.topchetoeu.jscript.core;
package me.topchetoeu.jscript.runtime;
import java.util.List;
import java.util.Stack;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.core.debug.DebugContext;
import me.topchetoeu.jscript.core.scope.LocalScope;
import me.topchetoeu.jscript.core.scope.ValueVariable;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.CodeFunction;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.ScopeValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.scope.LocalScope;
import me.topchetoeu.jscript.runtime.scope.ValueVariable;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.CodeFunction;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.ScopeValue;
import me.topchetoeu.jscript.runtime.values.Values;
public class Frame {
public static enum TryState {

View File

@@ -1,17 +1,17 @@
package me.topchetoeu.jscript.core;
package me.topchetoeu.jscript.runtime;
import java.util.Collections;
import me.topchetoeu.jscript.core.scope.ValueVariable;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.CodeFunction;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Symbol;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.Operation;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.scope.ValueVariable;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.CodeFunction;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Symbol;
import me.topchetoeu.jscript.runtime.values.Values;
public class InstructionRunner {
private static Object execReturn(Context ctx, Instruction instr, Frame frame) {

View File

@@ -0,0 +1,5 @@
package me.topchetoeu.jscript.runtime;
public class Key<T> {
}

View File

@@ -1,7 +1,7 @@
package me.topchetoeu.jscript.core;
package me.topchetoeu.jscript.runtime;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
public interface WrapperProvider {
public ObjectValue getProto(Class<?> obj);

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.debug;
package me.topchetoeu.jscript.runtime.debug;
import java.util.ArrayList;
import java.util.HashMap;
@@ -10,13 +10,13 @@ import me.topchetoeu.jscript.common.FunctionBody;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.Location;
import me.topchetoeu.jscript.common.mapping.FunctionMap;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Extensions;
import me.topchetoeu.jscript.core.Frame;
import me.topchetoeu.jscript.core.Key;
import me.topchetoeu.jscript.core.values.CodeFunction;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Extensions;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.Key;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.CodeFunction;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
public class DebugContext {
public static final Key<DebugContext> KEY = new Key<>();
@@ -32,10 +32,17 @@ public class DebugContext {
if (sources != null) {
for (var source : sources.entrySet()) debugger.onSourceLoad(source.getKey(), source.getValue());
}
if (maps != null) {
for (var map : maps.entrySet()) debugger.onFunctionLoad(map.getKey(), map.getValue());
}
this.debugger = debugger;
return true;
}
public boolean detachDebugger(DebugHandler debugger) {
if (this.debugger != debugger) return false;
return detachDebugger();
}
public boolean detachDebugger() {
this.debugger = null;
return true;

View File

@@ -1,12 +1,12 @@
package me.topchetoeu.jscript.core.debug;
package me.topchetoeu.jscript.runtime.debug;
import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.common.FunctionBody;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.mapping.FunctionMap;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Frame;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
public interface DebugHandler {
/**

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.exceptions;
package me.topchetoeu.jscript.runtime.exceptions;
public class ConvertException extends RuntimeException {
public final String source, target;

View File

@@ -1,14 +1,14 @@
package me.topchetoeu.jscript.core.exceptions;
package me.topchetoeu.jscript.runtime.exceptions;
import java.util.ArrayList;
import java.util.List;
import me.topchetoeu.jscript.common.Location;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.values.ObjectValue.PlaceholderProto;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.runtime.values.ObjectValue.PlaceholderProto;
public class EngineException extends RuntimeException {
public static class StackElement {

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.exceptions;
package me.topchetoeu.jscript.runtime.exceptions;
public class InterruptException extends RuntimeException {
public InterruptException() { }

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.exceptions;
package me.topchetoeu.jscript.runtime.exceptions;
import me.topchetoeu.jscript.common.Location;

View File

@@ -1,14 +1,14 @@
package me.topchetoeu.jscript.core.scope;
package me.topchetoeu.jscript.runtime.scope;
import java.util.HashSet;
import java.util.Set;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Values;
public class GlobalScope {
public final ObjectValue obj;

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.scope;
package me.topchetoeu.jscript.runtime.scope;
import java.util.ArrayList;

View File

@@ -1,7 +1,7 @@
package me.topchetoeu.jscript.core.scope;
package me.topchetoeu.jscript.runtime.scope;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.values.Values;
public class ValueVariable implements Variable {
public boolean readonly;

View File

@@ -1,6 +1,6 @@
package me.topchetoeu.jscript.core.scope;
package me.topchetoeu.jscript.runtime.scope;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.runtime.Context;
public interface Variable {
Object get(Context ctx);

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.values;
package me.topchetoeu.jscript.runtime.values;
import java.util.Arrays;
import java.util.Collection;
@@ -6,7 +6,7 @@ import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.runtime.Context;
// TODO: Make methods generic
public class ArrayValue extends ObjectValue implements Iterable<Object> {

View File

@@ -1,10 +1,10 @@
package me.topchetoeu.jscript.core.values;
package me.topchetoeu.jscript.runtime.values;
import me.topchetoeu.jscript.common.FunctionBody;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.Frame;
import me.topchetoeu.jscript.core.scope.ValueVariable;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.scope.ValueVariable;
public class CodeFunction extends FunctionValue {
public final FunctionBody body;

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.values;
package me.topchetoeu.jscript.runtime.values;
public enum ConvertHint {
TOSTRING,

View File

@@ -1,8 +1,8 @@
package me.topchetoeu.jscript.core.values;
package me.topchetoeu.jscript.runtime.values;
import java.util.List;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.runtime.Context;
public abstract class FunctionValue extends ObjectValue {
public String name = "";

View File

@@ -1,6 +1,6 @@
package me.topchetoeu.jscript.core.values;
package me.topchetoeu.jscript.runtime.values;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.utils.interop.Arguments;
public class NativeFunction extends FunctionValue {

View File

@@ -0,0 +1,53 @@
package me.topchetoeu.jscript.runtime.values;
import java.util.WeakHashMap;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Extensions;
import me.topchetoeu.jscript.runtime.Key;
public class NativeWrapper extends ObjectValue {
private static final Key<WeakHashMap<Object, NativeWrapper>> WRAPPERS = new Key<>();
private static final Object NATIVE_PROTO = new Object();
public final Object wrapped;
@Override
public ObjectValue getPrototype(Context ctx) {
if (ctx.environment != null && prototype == NATIVE_PROTO) {
var clazz = wrapped.getClass();
var res = ctx.environment.wrappers.getProto(clazz);
if (res != null) return res;
}
return super.getPrototype(ctx);
}
@Override
public String toString() {
return wrapped.toString();
}
@Override
public boolean equals(Object obj) {
return wrapped.equals(obj);
}
@Override
public int hashCode() {
return wrapped.hashCode();
}
private NativeWrapper(Object wrapped) {
this.wrapped = wrapped;
prototype = NATIVE_PROTO;
}
public static NativeWrapper of(Extensions exts, Object wrapped) {
var wrappers = exts == null ? null : exts.get(WRAPPERS);
if (wrappers == null) return new NativeWrapper(wrapped);
if (wrappers.containsKey(wrapped)) return wrappers.get(wrapped);
var res = new NativeWrapper(wrapped);
wrappers.put(wrapped, res);
return res;
}
}

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.values;
package me.topchetoeu.jscript.runtime.values;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -6,8 +6,8 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Environment;
public class ObjectValue {
public static enum PlaceholderProto {
@@ -153,18 +153,18 @@ public class ObjectValue {
}
public ObjectValue getPrototype(Context ctx) {
if (prototype instanceof ObjectValue || prototype == null) return (ObjectValue)prototype;
try {
if (prototype == OBJ_PROTO) return ctx.get(Environment.OBJECT_PROTO);
if (prototype == ARR_PROTO) return ctx.get(Environment.ARRAY_PROTO);
if (prototype == FUNC_PROTO) return ctx.get(Environment.FUNCTION_PROTO);
if (prototype == ERR_PROTO) return ctx.get(Environment.ERROR_PROTO);
if (prototype == RANGE_ERR_PROTO) return ctx.get(Environment.RANGE_ERR_PROTO);
if (prototype == SYNTAX_ERR_PROTO) return ctx.get(Environment.SYNTAX_ERR_PROTO);
if (prototype == TYPE_ERR_PROTO) return ctx.get(Environment.TYPE_ERR_PROTO);
return ctx.get(Environment.OBJECT_PROTO);
}
catch (NullPointerException e) { return null; }
return (ObjectValue)prototype;
}
public final boolean setPrototype(PlaceholderProto val) {
if (!extensible()) return false;

View File

@@ -1,10 +1,10 @@
package me.topchetoeu.jscript.core.values;
package me.topchetoeu.jscript.runtime.values;
import java.util.HashMap;
import java.util.List;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.scope.ValueVariable;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.scope.ValueVariable;
public class ScopeValue extends ObjectValue {
public final ValueVariable[] variables;

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.values;
package me.topchetoeu.jscript.runtime.values;
import java.util.HashMap;

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.values;
package me.topchetoeu.jscript.runtime.values;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
@@ -13,13 +13,13 @@ import java.util.List;
import java.util.Map;
import me.topchetoeu.jscript.common.Operation;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.debug.DebugContext;
import me.topchetoeu.jscript.core.exceptions.ConvertException;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
import me.topchetoeu.jscript.lib.PromiseLib;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.ConvertException;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class Values {
public static enum CompareResult {
@@ -58,9 +58,8 @@ public class Values {
@SuppressWarnings("unchecked")
public static <T> T wrapper(Object val, Class<T> clazz) {
if (!isWrapper(val)) val = new NativeWrapper(val);
var res = (NativeWrapper)val;
if (res != null && clazz.isInstance(res.wrapped)) return (T)res.wrapped;
if (isWrapper(val)) val = ((NativeWrapper)val).wrapped;
if (val != null && clazz.isInstance(val)) return (T)val;
else return null;
}
@@ -471,7 +470,7 @@ public class Values {
else return ctx.environment.wrappers.getConstr((Class<?>)val);
}
return new NativeWrapper(val);
return NativeWrapper.of(ctx, val);
}
@SuppressWarnings("unchecked")
@@ -536,7 +535,7 @@ public class Values {
if (obj == null) return null;
if (clazz.isInstance(obj)) return (T)obj;
if (clazz.isAssignableFrom(NativeWrapper.class)) {
return (T)new NativeWrapper(obj);
return (T)NativeWrapper.of(ctx, obj);
}
throw new ConvertException(type(obj), clazz.getSimpleName());

View File

@@ -4,9 +4,9 @@ import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.common.FunctionBody;
import me.topchetoeu.jscript.compilation.CompileResult;
import me.topchetoeu.jscript.compilation.parsing.Parsing;
import me.topchetoeu.jscript.core.Compiler;
import me.topchetoeu.jscript.core.Extensions;
import me.topchetoeu.jscript.core.debug.DebugContext;
import me.topchetoeu.jscript.runtime.Compiler;
import me.topchetoeu.jscript.runtime.Extensions;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
public class JSCompiler implements Compiler {
public final Extensions ext;

View File

@@ -8,18 +8,18 @@ import java.nio.file.Path;
import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.common.Metadata;
import me.topchetoeu.jscript.common.Reading;
import me.topchetoeu.jscript.core.Compiler;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Engine;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.EventLoop;
import me.topchetoeu.jscript.core.debug.DebugContext;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.exceptions.InterruptException;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
import me.topchetoeu.jscript.lib.Internals;
import me.topchetoeu.jscript.runtime.Compiler;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Engine;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.EventLoop;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.debug.DebugServer;
import me.topchetoeu.jscript.utils.debug.SimpleDebugger;
import me.topchetoeu.jscript.utils.filesystem.Filesystem;

View File

@@ -15,8 +15,8 @@ import me.topchetoeu.jscript.common.events.Notifier;
import me.topchetoeu.jscript.common.json.JSON;
import me.topchetoeu.jscript.common.json.JSONList;
import me.topchetoeu.jscript.common.json.JSONMap;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
import me.topchetoeu.jscript.utils.debug.WebSocketMessage.Type;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
public class DebugServer {
public static String browserDisplayName = Metadata.name() + "/" + Metadata.version();
@@ -137,6 +137,13 @@ public class DebugServer {
}
runAsync(() -> {
var handle = new Thread(() -> {
System.out.println("test");
debugger.close();
});
Runtime.getRuntime().addShutdownHook(handle);
try { handle(ws, debugger); }
catch (RuntimeException | IOException e) {
try {
@@ -145,7 +152,11 @@ public class DebugServer {
}
catch (IOException e2) { /* Shit outta luck */ }
}
finally { ws.close(); debugger.close(); }
finally {
Runtime.getRuntime().removeShutdownHook(handle);
ws.close();
debugger.close();
}
}, "Debug Handler");
}
@@ -164,7 +175,6 @@ public class DebugServer {
var req = HttpRequest.read(socket);
if (req == null) continue;
switch (req.path) {
case "/json/version":
send(req, "{\"Browser\":\"" + browserDisplayName + "\",\"Protocol-Version\":\"1.1\"}");

View File

@@ -1,8 +1,9 @@
package me.topchetoeu.jscript.utils.debug;
import me.topchetoeu.jscript.core.debug.DebugHandler;
import java.io.IOException;
import me.topchetoeu.jscript.runtime.debug.DebugHandler;
public interface Debugger extends DebugHandler {
void close();

View File

@@ -23,25 +23,28 @@ import me.topchetoeu.jscript.common.json.JSONList;
import me.topchetoeu.jscript.common.json.JSONMap;
import me.topchetoeu.jscript.common.mapping.FunctionMap;
import me.topchetoeu.jscript.compilation.parsing.Parsing;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Engine;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.EventLoop;
import me.topchetoeu.jscript.core.Frame;
import me.topchetoeu.jscript.core.debug.DebugContext;
import me.topchetoeu.jscript.core.scope.GlobalScope;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Symbol;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Engine;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.EventLoop;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.scope.GlobalScope;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Symbol;
import me.topchetoeu.jscript.runtime.values.Values;
// very simple indeed
public class SimpleDebugger implements Debugger {
public static final Set<String> VSCODE_EMPTY = Set.of(
"function(...runtimeArgs){\n let t = 1024; let e = null;\n if(e)try{let r=\"<<default preview>>\",i=e.call(this,r);if(i!==r)return String(i)}catch(r){return`<<indescribable>>${JSON.stringify([String(r),\"object\"])}`}if(typeof this==\"object\"&&this){let r;for(let i of[Symbol.for(\"debug.description\"),Symbol.for(\"nodejs.util.inspect.custom\")])try{r=this[i]();break}catch{}if(!r&&!String(this.toString).includes(\"[native code]\")&&(r=String(this)),r&&!r.startsWith(\"[object \"))return r.length>=t?r.slice(0,t)+\"\\u2026\":r}\n ;\n\n}",
"function(...runtimeArgs){\n let r = 1024; let e = null;\n if(e)try{let t=\"<<default preview>>\",n=e.call(this,t);if(n!==t)return String(n)}catch(t){return`<<indescribable>>${JSON.stringify([String(t),\"object\"])}`}if(typeof this==\"object\"&&this){let t;for(let n of[Symbol.for(\"debug.description\"),Symbol.for(\"nodejs.util.inspect.custom\")])if(typeof this[n]==\"function\")try{t=this[n]();break}catch{}if(!t&&!String(this.toString).includes(\"[native code]\")&&(t=String(this)),t&&!t.startsWith(\"[object\"))return t.length>=r?t.slice(0,r)+\"\\u2026\":t};}",
"function(...runtimeArgs){\n let t = 1024; let e = null;\n let r={},i=\"<<default preview>>\";if(typeof this!=\"object\"||!this)return r;for(let[n,s]of Object.entries(this)){if(e)try{let o=e.call(s,i);if(o!==i){r[n]=String(o);continue}}catch(o){r[n]=`<<indescribable>>${JSON.stringify([String(o),n])}`;continue}if(typeof s==\"object\"&&s){let o;for(let a of runtimeArgs[0])try{o=s[a]();break}catch{}!o&&!String(s.toString).includes(\"[native code]\")&&(o=String(s)),o&&!o.startsWith(\"[object \")&&(r[n]=o.length>=t?o.slice(0,t)+\"\\u2026\":o)}}return r\n ;\n\n}",
"function(...runtimeArgs){\n let r = 1024; let e = null;\n let t={},n=\"<<default preview>>\";if(typeof this!=\"object\"||!this)return t;for(let[i,o]of Object.entries(this)){if(e)try{let s=e.call(o,n);if(s!==n){t[i]=String(s);continue}}catch(s){t[i]=`<<indescribable>>${JSON.stringify([String(s),i])}`;continue}if(typeof o==\"object\"&&o){let s;for(let a of runtimeArgs[0])if(typeof o[a]==\"function\")try{s=o[a]();break}catch{}!s&&!String(o.toString).includes(\"[native code]\")&&(s=String(o)),s&&!s.startsWith(\"[object \")&&(t[i]=s.length>=r?s.slice(0,r)+\"\\u2026\":s)}}return t\n ;\n\n}",
"function(){let t={__proto__:this.__proto__\n},e=Object.getOwnPropertyNames(this);for(let r=0;r<e.length;++r){let i=e[r],n=i>>>0;if(String(n>>>0)===i&&n>>>0!==4294967295)continue;let s=Object.getOwnPropertyDescriptor(this,i);s&&Object.defineProperty(t,i,s)}return t}",
"function(){return[Symbol.for(\"debug.description\"),Symbol.for(\"nodejs.util.inspect.custom\")]\n}"
);
@@ -50,6 +53,7 @@ public class SimpleDebugger implements Debugger {
);
public static final String CHROME_GET_PROP_FUNC = "function s(e){let t=this;const n=JSON.parse(e);for(let e=0,i=n.length;e<i;++e)t=t[n[e]];return t}";
public static final String CHROME_GET_PROP_FUNC_2 = "function invokeGetter(getter) { return Reflect.apply(getter, this, []);}";
public static final String VSCODE_CALL = "function(t){return t.call(this)\n}";
public static final String VSCODE_AUTOCOMPLETE = "function(t,e,r){let n=r?\"variable\":\"property\",i=(l,p,f)=>{if(p!==\"function\")return n;if(l===\"constructor\")return\"class\";let m=String(f);return m.startsWith(\"class \")||m.includes(\"[native code]\")&&/^[A-Z]/.test(l)?\"class\":r?\"function\":\"method\"\n},o=l=>{switch(typeof l){case\"number\":case\"boolean\":return`${l}`;case\"object\":return l===null?\"null\":l.constructor.name||\"object\";case\"function\":return`fn(${new Array(l.length).fill(\"?\").join(\", \")})`;default:return typeof l}},s=[],a=new Set,u=\"~\",c=t===void 0?this:t;for(;c!=null;c=c.__proto__){u+=\"~\";let l=Object.getOwnPropertyNames(c).filter(p=>p.startsWith(e)&&!p.match(/^\\d+$/));for(let p of l){if(a.has(p))continue;a.add(p);let f=Object.getOwnPropertyDescriptor(c,p),m=n,h;try{let H=c[p];m=i(p,typeof f?.value,H),h=o(H)}catch{}s.push({label:p,sortText:u+p.replace(/^_+/,H=>\"{\".repeat(H.length)),type:m,detail:h})}r=!1}return{result:s,isArray:this instanceof Array}}";
@@ -83,7 +87,9 @@ public class SimpleDebugger implements Debugger {
public final String condition;
public final Pattern pattern;
public final int line, start;
public final WeakHashMap<FunctionBody, Set<Location>> resolvedLocations = new WeakHashMap<>();
public final long locNum;
public final HashMap<Filename, Location> resolvedLocations = new HashMap<>();
public final HashMap<Filename, Long> resolvedDistances = new HashMap<>();
public Breakpoint(int id, Pattern pattern, int line, int start, String condition) {
this.id = id;
@@ -91,18 +97,28 @@ public class SimpleDebugger implements Debugger {
this.pattern = pattern;
this.line = line;
this.start = start;
this.locNum = start | ((long)line << 32);
if (condition != null && condition.trim().equals("")) condition = null;
}
// TODO: Figure out how to unload a breakpoint
// TODO: Do location resolution with function boundaries
public void addFunc(FunctionBody body, FunctionMap map) {
try {
for (var loc : map.correctBreakpoint(pattern, line, start)) {
if (!resolvedLocations.containsKey(body)) resolvedLocations.put(body, new HashSet<>());
var set = resolvedLocations.get(body);
set.add(loc);
var currNum = loc.start() + ((long)loc.line() << 32);
long currDist = 0;
if (currNum > locNum) currDist = currNum - locNum;
else currDist = locNum - currNum;
if ( currDist > resolvedDistances.getOrDefault(loc.filename(), Long.MAX_VALUE)) continue;
resolvedLocations.put(loc.filename(), loc);
resolvedDistances.put(loc.filename(), currDist);
}
for (var loc : resolvedLocations.values()) {
ws.send(new V8Event("Debugger.breakpointResolved", new JSONMap()
.set("breakpointId", id)
.set("location", serializeLocation(loc))
@@ -146,29 +162,29 @@ public class SimpleDebugger implements Debugger {
.add(new JSONMap()
.set("type", "local")
.set("name", "Local Scope")
.set("object", serializeObj(frame.ctx, local))
.set("object", serializeObj(frame.ctx.environment, local))
)
.add(new JSONMap()
.set("type", "closure")
.set("name", "Closure")
.set("object", serializeObj(frame.ctx, capture))
.set("object", serializeObj(frame.ctx.environment, capture))
)
.add(new JSONMap()
.set("type", "global")
.set("name", "Global Scope")
.set("object", serializeObj(frame.ctx, global))
.set("object", serializeObj(frame.ctx.environment, global))
)
.add(new JSONMap()
.set("type", "other")
.set("name", "Value Stack")
.set("object", serializeObj(frame.ctx, valstack))
.set("object", serializeObj(frame.ctx.environment, valstack))
)
);
}
}
private class ObjRef {
public final ObjectValue obj;
public final Context ctx;
public final Environment env;
public final HashSet<String> heldGroups = new HashSet<>();
public boolean held = true;
@@ -176,19 +192,19 @@ public class SimpleDebugger implements Debugger {
return !held && heldGroups.size() == 0;
}
public ObjRef(Context ctx, ObjectValue obj) {
this.ctx = ctx;
public ObjRef(Environment env, ObjectValue obj) {
this.env = env;
this.obj = obj;
}
}
private static class RunResult {
public final Context ctx;
public final Environment ctx;
public final Object result;
public final EngineException error;
public RunResult(Context ctx, Object result, EngineException error) {
this.ctx = ctx;
public RunResult(Environment env, Object result, EngineException error) {
this.ctx = env;
this.result = result;
this.error = error;
}
@@ -202,8 +218,9 @@ public class SimpleDebugger implements Debugger {
private ObjectValue emptyObject = new ObjectValue();
private WeakHashMap<DebugContext, DebugContext> contexts = new WeakHashMap<>();
private WeakHashMap<FunctionBody, FunctionMap> mappings = new WeakHashMap<>();
private WeakHashMap<FunctionBody, HashMap<Location, Breakpoint>> bpLocs = new WeakHashMap<>();
private HashMap<Location, HashSet<Breakpoint>> bpLocs = new HashMap<>();
private HashMap<Integer, Breakpoint> idToBreakpoint = new HashMap<>();
@@ -226,6 +243,8 @@ public class SimpleDebugger implements Debugger {
private int stepOutPtr = 0;
private boolean compare(String src, String target) {
src = src.replaceAll("\\s", "");
target = target.replaceAll("\\s", "");
if (src.length() != target.length()) return false;
var diff = 0;
var all = 0;
@@ -291,13 +310,11 @@ public class SimpleDebugger implements Debugger {
bpLocs.clear();
for (var bp : idToBreakpoint.values()) {
for (var el : bp.resolvedLocations.entrySet()) {
if (!bpLocs.containsKey(el.getKey())) bpLocs.put(el.getKey(), new HashMap<>());
var map = bpLocs.get(el.getKey());
for (var loc : bp.resolvedLocations.values()) {
bpLocs.putIfAbsent(loc, new HashSet<>());
var set = bpLocs.get(loc);
for (var loc : el.getValue()) {
map.put(loc, bp);
}
set.add(bp);
}
}
}
@@ -321,22 +338,10 @@ public class SimpleDebugger implements Debugger {
.set("columnNumber", loc.start() - 1);
}
private Integer objectId(Context ctx, ObjectValue obj) {
if (objectToId.containsKey(obj)) return objectToId.get(obj);
else {
int id = nextId();
var ref = new ObjRef(ctx, obj);
objectToId.put(obj, id);
idToObject.put(id, ref);
return id;
}
}
private JSONMap serializeObj(Context ctx, Object val, boolean byValue) {
private JSONMap serializeObj(Environment env, Object val, boolean byValue) {
val = Values.normalize(null, val);
var newCtx = new Context();
newCtx.addAll(ctx);
newCtx.add(DebugContext.IGNORE);
ctx = newCtx;
env = sanitizeEnvironment(env);
var ctx = env.context();
if (val == Values.NULL) {
return new JSONMap()
@@ -348,7 +353,16 @@ public class SimpleDebugger implements Debugger {
if (val instanceof ObjectValue) {
var obj = (ObjectValue)val;
var id = objectId(ctx, obj);
int id;
if (objectToId.containsKey(obj)) id = objectToId.get(obj);
else {
id = nextId();
var ref = new ObjRef(env, obj);
objectToId.put(obj, id);
idToObject.put(id, ref);
}
var type = "object";
String subtype = null;
String className = null;
@@ -366,6 +380,7 @@ public class SimpleDebugger implements Debugger {
if (subtype != null) res.set("subtype", subtype);
if (className != null) {
res.set("className", className);
res.set("description", className);
}
if (obj instanceof ArrayValue) res.set("description", "Array(" + ((ArrayValue)obj).size() + ")");
@@ -376,16 +391,16 @@ public class SimpleDebugger implements Debugger {
try {
defaultToString =
Values.getMember(ctx, obj, "toString") ==
Values.getMember(ctx, ctx.get(Environment.OBJECT_PROTO), "toString");
Values.getMember(ctx, env.get(Environment.OBJECT_PROTO), "toString");
}
catch (Exception e) { }
try { res.set("description", className + (defaultToString ? "" : " { " + Values.toString(ctx, obj) + " }")); }
catch (EngineException e) { res.set("description", className); }
catch (EngineException e) { }
}
if (byValue) try { res.put("value", JSON.fromJs(ctx, obj)); }
if (byValue) try { res.put("value", JSON.fromJs(env.context(), obj)); }
catch (Exception e) { }
return res;
@@ -411,8 +426,8 @@ public class SimpleDebugger implements Debugger {
throw new IllegalArgumentException("Unexpected JS object.");
}
private JSONMap serializeObj(Context ctx, Object val) {
return serializeObj(ctx, val, false);
private JSONMap serializeObj(Environment env, Object val) {
return serializeObj(env, val, false);
}
private void addObjectGroup(String name, Object val) {
if (val instanceof ObjectValue) {
@@ -451,11 +466,11 @@ public class SimpleDebugger implements Debugger {
else return JSON.toJs(res);
}
private JSONMap serializeException(Context ctx, EngineException err) {
private JSONMap serializeException(Environment env, EngineException err) {
String text = null;
try {
text = Values.toString(ctx, err.value);
text = Values.toString(env.context(), err.value);
}
catch (EngineException e) {
text = "[error while stringifying]";
@@ -463,7 +478,7 @@ public class SimpleDebugger implements Debugger {
var res = new JSONMap()
.set("exceptionId", nextId())
.set("exception", serializeObj(ctx, err.value))
.set("exception", serializeObj(env, err.value))
.set("text", text);
return res;
@@ -524,30 +539,42 @@ public class SimpleDebugger implements Debugger {
}
}
private Environment sanitizeEnvironment(Environment env) {
var res = env.child();
res.remove(EventLoop.KEY);
res.remove(DebugContext.KEY);
res.add(DebugContext.IGNORE);
return res;
}
private RunResult run(DebugFrame codeFrame, String code) {
if (codeFrame == null) return new RunResult(null, code, new EngineException("Invalid code frame!"));
var engine = new Engine();
var env = codeFrame.frame.function.environment.copy();
var env = codeFrame.frame.ctx.environment.copy();
env.global = new GlobalScope(codeFrame.local);
env.remove(EventLoop.KEY);
env.remove(DebugContext.KEY);
env.add(EventLoop.KEY, engine);
var ctx = new Context(env);
var awaiter = engine.pushMsg(false, ctx.environment, new Filename("jscript", "eval"), code, codeFrame.frame.thisArg, codeFrame.frame.args);
var awaiter = engine.pushMsg(false, env, new Filename("jscript", "eval"), code, codeFrame.frame.thisArg, codeFrame.frame.args);
engine.run(true);
try { return new RunResult(ctx, awaiter.await(), null); }
catch (EngineException e) { return new RunResult(ctx, null, e); }
try {
engine.run(true);
return new RunResult(env, awaiter.await(), null);
}
catch (EngineException e) { return new RunResult(env, null, e); }
catch (SyntaxException e) { return new RunResult(env, null, EngineException.ofSyntax(e.toString())); }
}
private ObjectValue vscodeAutoSuggest(Context ctx, Object target, String query, boolean variable) {
private ObjectValue vscodeAutoSuggest(Environment env, Object target, String query, boolean variable) {
var res = new ArrayValue();
var passed = new HashSet<String>();
var tildas = "~";
if (target == null) target = ctx.environment.global;
var ctx = env.context();
if (target == null) target = env.global;
for (var proto = target; proto != null && proto != Values.NULL; proto = Values.getPrototype(ctx, proto)) {
for (var el : Values.getMembers(ctx, proto, true, true)) {
@@ -602,7 +629,7 @@ public class SimpleDebugger implements Debugger {
desc.defineProperty(ctx, "type", Values.type(val));
break;
}
res.set(ctx, res.size(), desc);
}
@@ -630,15 +657,18 @@ public class SimpleDebugger implements Debugger {
ws.send(msg.respond());
}
@Override public synchronized void close() {
if (state != State.RESUMED) {
resume(State.RESUMED);
}
enabled = false;
execptionType = CatchType.NONE;
state = State.RESUMED;
// idToBptCand.clear();
mappings.clear();
bpLocs.clear();
idToBreakpoint.clear();
// locToBreakpoint.clear();
// tmpBreakpts.clear();
filenameToId.clear();
idToSource.clear();
@@ -656,6 +686,9 @@ public class SimpleDebugger implements Debugger {
stepOutFrame = currFrame = null;
stepOutPtr = 0;
for (var ctx : contexts.keySet()) ctx.detachDebugger(this);
contexts.clear();
updateNotifier.next();
}
@@ -709,16 +742,15 @@ public class SimpleDebugger implements Debugger {
var bpt = new Breakpoint(nextId(), regex, line, col, cond);
idToBreakpoint.put(bpt.id, bpt);
var locs = new JSONList();
for (var el : mappings.entrySet()) {
bpt.addFunc(el.getKey(), el.getValue());
}
for (var el : bpt.resolvedLocations.values()) {
for (var loc : el) {
locs.add(serializeLocation(loc));
}
var locs = new JSONList();
for (var loc : bpt.resolvedLocations.values()) {
locs.add(serializeLocation(loc));
}
ws.send(msg.respond(new JSONMap()
@@ -730,6 +762,7 @@ public class SimpleDebugger implements Debugger {
var id = Integer.parseInt(msg.params.string("breakpointId"));
idToBreakpoint.remove(id);
updateBreakpoints();
ws.send(msg.respond());
}
@Override public synchronized void continueToLocation(V8Message msg) throws IOException {
@@ -818,46 +851,54 @@ public class SimpleDebugger implements Debugger {
@Override public synchronized void getProperties(V8Message msg) throws IOException {
var ref = idToObject.get(Integer.parseInt(msg.params.string("objectId")));
var obj = ref.obj;
var env = ref.env;
var ctx = env.context();
var res = new JSONList();
var ctx = ref.ctx;
var own = true;
if (obj != emptyObject && obj != null) {
for (var key : obj.keys(true)) {
var propDesc = new JSONMap();
while (obj != null) {
for (var key : obj.keys(true)) {
var propDesc = new JSONMap();
if (obj.properties.containsKey(key)) {
var prop = obj.properties.get(key);
if (obj.properties.containsKey(key)) {
var prop = obj.properties.get(key);
propDesc.set("name", Values.toString(ctx, key));
if (prop.getter != null) propDesc.set("get", serializeObj(ctx, prop.getter));
if (prop.setter != null) propDesc.set("set", serializeObj(ctx, prop.setter));
propDesc.set("enumerable", obj.memberEnumerable(key));
propDesc.set("configurable", obj.memberConfigurable(key));
propDesc.set("isOwn", true);
res.add(propDesc);
propDesc.set("name", Values.toString(ctx, key));
if (prop.getter != null) propDesc.set("get", serializeObj(env, prop.getter));
if (prop.setter != null) propDesc.set("set", serializeObj(env, prop.setter));
propDesc.set("enumerable", obj.memberEnumerable(key));
propDesc.set("configurable", obj.memberConfigurable(key));
propDesc.set("isOwn", true);
res.add(propDesc);
}
else {
propDesc.set("name", Values.toString(ctx, key));
propDesc.set("value", serializeObj(env, Values.getMember(ctx, obj, key)));
propDesc.set("writable", obj.memberWritable(key));
propDesc.set("enumerable", obj.memberEnumerable(key));
propDesc.set("configurable", obj.memberConfigurable(key));
propDesc.set("isOwn", own);
res.add(propDesc);
}
}
else {
propDesc.set("name", Values.toString(ctx, key));
propDesc.set("value", serializeObj(ctx, Values.getMember(ctx, obj, key)));
propDesc.set("writable", obj.memberWritable(key));
propDesc.set("enumerable", obj.memberEnumerable(key));
propDesc.set("configurable", obj.memberConfigurable(key));
propDesc.set("isOwn", true);
res.add(propDesc);
var proto = Values.getPrototype(ctx, obj);
if (own) {
var protoDesc = new JSONMap();
protoDesc.set("name", "__proto__");
protoDesc.set("value", serializeObj(env, proto == null ? Values.NULL : proto));
protoDesc.set("writable", true);
protoDesc.set("enumerable", false);
protoDesc.set("configurable", false);
protoDesc.set("isOwn", own);
res.add(protoDesc);
}
obj = proto;
own = false;
}
var proto = Values.getPrototype(ctx, obj);
var protoDesc = new JSONMap();
protoDesc.set("name", "__proto__");
protoDesc.set("value", serializeObj(ctx, proto == null ? Values.NULL : proto));
protoDesc.set("writable", true);
protoDesc.set("enumerable", false);
protoDesc.set("configurable", false);
protoDesc.set("isOwn", true);
res.add(protoDesc);
}
ws.send(msg.respond(new JSONMap().set("result", res)));
@@ -874,7 +915,8 @@ public class SimpleDebugger implements Debugger {
var thisArgRef = idToObject.get(Integer.parseInt(msg.params.string("objectId")));
var thisArg = thisArgRef.obj;
var ctx = thisArgRef.ctx;
var env = thisArgRef.env;
var ctx = env.context();
while (true) {
var start = src.lastIndexOf("//# sourceURL=");
@@ -892,22 +934,25 @@ public class SimpleDebugger implements Debugger {
res = thisArg;
for (var el : JSON.parse(null, (String)args.get(0)).list()) res = Values.getMember(ctx, res, JSON.toJs(el));
}
else if (compare(src, CHROME_GET_PROP_FUNC_2)) {
res = Values.call(ctx, args.get(0), thisArg);
}
else if (compare(src, VSCODE_CALL)) {
var func = (FunctionValue)(args.size() < 1 ? null : args.get(0));
ws.send(msg.respond(new JSONMap().set("result", serializeObj(ctx, func.call(ctx, thisArg)))));
ws.send(msg.respond(new JSONMap().set("result", serializeObj(env, func.call(ctx, thisArg)))));
}
else if (compare(src, VSCODE_AUTOCOMPLETE)) {
var target = args.get(0);
if (target == null) target = thisArg;
res = vscodeAutoSuggest(ctx, target, Values.toString(ctx, args.get(1)), Values.toBoolean(args.get(2)));
res = vscodeAutoSuggest(env, target, Values.toString(ctx, args.get(1)), Values.toBoolean(args.get(2)));
}
else {
ws.send(new V8Error("Please use well-known functions with callFunctionOn"));
return;
}
ws.send(msg.respond(new JSONMap().set("result", serializeObj(ctx, res, byValue))));
ws.send(msg.respond(new JSONMap().set("result", serializeObj(env, res, byValue))));
}
catch (EngineException e) { ws.send(msg.respond(new JSONMap().set("exceptionDetails", serializeException(ctx, e)))); }
catch (EngineException e) { ws.send(msg.respond(new JSONMap().set("exceptionDetails", serializeException(env, e)))); }
}
@Override public synchronized void runtimeEnable(V8Message msg) throws IOException {
@@ -958,10 +1003,11 @@ public class SimpleDebugger implements Debugger {
) {
pauseDebug(ctx, null);
}
else if (isBreakpointable && bpLocs.getOrDefault(cf.function.body, new HashMap<>()).containsKey(loc)) {
var bp = bpLocs.get(cf.function.body).get(loc);
var ok = bp.condition == null ? true : Values.toBoolean(run(currFrame, bp.condition).result);
if (ok) pauseDebug(ctx, bp);
else if (isBreakpointable && bpLocs.containsKey(loc)) {
for (var bp : bpLocs.get(loc)) {
var ok = bp.condition == null ? true : Values.toBoolean(run(currFrame, bp.condition).result);
if (ok) pauseDebug(ctx, bp);
}
}
// else if (isBreakpointable && tmpBreakpts.remove(loc)) pauseDebug(ctx, null);
else if (isBreakpointable && pendingPause) {
@@ -1033,6 +1079,7 @@ public class SimpleDebugger implements Debugger {
public SimpleDebugger attach(DebugContext ctx) {
ctx.attachDebugger(this);
contexts.put(ctx, ctx);
return this;
}

View File

@@ -1,7 +1,7 @@
package me.topchetoeu.jscript.utils.filesystem;
import me.topchetoeu.jscript.core.Extensions;
import me.topchetoeu.jscript.core.Key;
import me.topchetoeu.jscript.runtime.Extensions;
import me.topchetoeu.jscript.runtime.Key;
public interface Filesystem {
public static final Key<Filesystem> KEY = new Key<>();

View File

@@ -2,8 +2,8 @@ package me.topchetoeu.jscript.utils.filesystem;
import java.util.ArrayList;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.Values;
public class FilesystemException extends RuntimeException {
public final ErrorReason reason;

View File

@@ -2,9 +2,9 @@ package me.topchetoeu.jscript.utils.interop;
import java.lang.reflect.Array;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.values.NativeWrapper;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.values.NativeWrapper;
import me.topchetoeu.jscript.runtime.values.Values;
public class Arguments {
public final Object self;

View File

@@ -1,7 +1,6 @@
package me.topchetoeu.jscript.utils.interop;
public enum ExposeType {
INIT,
METHOD,
GETTER,
SETTER,

View File

@@ -10,22 +10,24 @@ import java.util.HashSet;
import java.util.stream.Collectors;
import me.topchetoeu.jscript.common.Location;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.WrapperProvider;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Symbol;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.WrapperProvider;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Symbol;
import me.topchetoeu.jscript.runtime.values.Values;
public class NativeWrapperProvider implements WrapperProvider {
private final HashMap<Class<?>, FunctionValue> constructors = new HashMap<>();
private final HashMap<Class<?>, ObjectValue> prototypes = new HashMap<>();
private final HashMap<Class<?>, ObjectValue> namespaces = new HashMap<>();
private final Environment env;
private final HashMap<Class<?>, Class<?>> classToProxy = new HashMap<>();
private final HashMap<Class<?>, Class<?>> proxyToClass = new HashMap<>();
private final HashSet<Class<?>> ignore = new HashSet<>();
private static Object call(Context ctx, String name, Method method, Object thisArg, Object... args) {
try {
@@ -106,11 +108,12 @@ public class NativeWrapperProvider implements WrapperProvider {
else return name;
}
private static void apply(ObjectValue obj, Environment env, ExposeTarget target, Class<?> clazz) {
private static boolean apply(ObjectValue obj, ExposeTarget target, Class<?> clazz) {
var getters = new HashMap<Object, FunctionValue>();
var setters = new HashMap<Object, FunctionValue>();
var props = new HashSet<Object>();
var nonProps = new HashSet<Object>();
var any = false;
for (var method : clazz.getDeclaredMethods()) {
for (var annotation : method.getAnnotationsByType(Expose.class)) {
@@ -120,15 +123,9 @@ public class NativeWrapperProvider implements WrapperProvider {
var name = getName(method, annotation.value());
var key = getKey(name);
var repeat = false;
any = true;
switch (annotation.type()) {
case INIT:
checkSignature(method, true,
target == ExposeTarget.CONSTRUCTOR ? FunctionValue.class : ObjectValue.class,
Environment.class
);
call(null, null, method, obj, null, env);
break;
case METHOD:
if (props.contains(key) || nonProps.contains(key)) repeat = true;
else {
@@ -168,11 +165,17 @@ public class NativeWrapperProvider implements WrapperProvider {
var name = getName(method, annotation.value());
var key = getKey(name);
var repeat = false;
any = true;
if (props.contains(key) || nonProps.contains(key)) repeat = true;
else {
checkSignature(method, true, Environment.class);
obj.defineProperty(null, key, call(new Context(env), name, method, null, env), true, true, false);
checkSignature(method, true);
try {
obj.defineProperty(null, key, method.invoke(null), true, true, false);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
nonProps.add(key);
}
@@ -191,11 +194,29 @@ public class NativeWrapperProvider implements WrapperProvider {
var name = getName(field, annotation.value());
var key = getKey(name);
var repeat = false;
any = true;
if (props.contains(key) || nonProps.contains(key)) repeat = true;
else {
try {
obj.defineProperty(null, key, Values.normalize(new Context(env), field.get(null)), true, true, false);
if (Modifier.isStatic(field.getModifiers())) {
obj.defineProperty(null, key, Values.normalize(null, field.get(null)), true, true, false);
}
else {
obj.defineProperty(
null, key,
new NativeFunction("get " + key, args -> {
try { return field.get(args.self(clazz)); }
catch (IllegalAccessException e) { e.printStackTrace(); return null; }
}),
Modifier.isFinal(field.getModifiers()) ? null : new NativeFunction("get " + key, args -> {
try { field.set(args.self(clazz), args.convert(0, field.getType())); }
catch (IllegalAccessException e) { e.printStackTrace(); }
return null;
}),
true, false
);
}
nonProps.add(key);
}
catch (IllegalArgumentException | IllegalAccessException e) { }
@@ -210,9 +231,11 @@ public class NativeWrapperProvider implements WrapperProvider {
}
for (var key : props) obj.defineProperty(null, key, getters.get(key), setters.get(key), true, false);
return any;
}
private static Method getConstructor(Environment env, Class<?> clazz) {
private static Method getConstructor(Class<?> clazz) {
for (var method : clazz.getDeclaredMethods()) {
if (!method.isAnnotationPresent(ExposeConstructor.class)) continue;
checkSignature(method, true, Arguments.class);
@@ -228,10 +251,10 @@ public class NativeWrapperProvider implements WrapperProvider {
* All accessors and methods will expect the this argument to be a native wrapper of the given class type.
* @param clazz The class for which a prototype should be generated
*/
public static ObjectValue makeProto(Environment env, Class<?> clazz) {
public static ObjectValue makeProto(Class<?> clazz) {
var res = new ObjectValue();
res.defineProperty(null, Symbol.get("Symbol.typeName"), getName(clazz));
apply(res, env, ExposeTarget.PROTOTYPE, clazz);
if (!apply(res, ExposeTarget.PROTOTYPE, clazz)) return null;
return res;
}
/**
@@ -240,14 +263,14 @@ public class NativeWrapperProvider implements WrapperProvider {
* When the function gets called, the underlying constructor will get called, unless the constructor is inaccessible.
* @param clazz The class for which a constructor should be generated
*/
public static FunctionValue makeConstructor(Environment ctx, Class<?> clazz) {
var constr = getConstructor(ctx, clazz);
public static FunctionValue makeConstructor(Class<?> clazz) {
var constr = getConstructor(clazz);
FunctionValue res = constr == null ?
new NativeFunction(getName(clazz), args -> { throw EngineException.ofError("This constructor is not invokable."); }) :
create(getName(clazz), constr);
apply(res, ctx, ExposeTarget.CONSTRUCTOR, clazz);
apply(res, ExposeTarget.CONSTRUCTOR, clazz);
return res;
}
@@ -257,15 +280,34 @@ public class NativeWrapperProvider implements WrapperProvider {
* This method behaves almost like {@link NativeWrapperProvider#makeConstructor}, but will return an object instead.
* @param clazz The class for which a constructor should be generated
*/
public static ObjectValue makeNamespace(Environment ctx, Class<?> clazz) {
public static ObjectValue makeNamespace(Class<?> clazz) {
var res = new ObjectValue();
res.defineProperty(null, Symbol.get("Symbol.typeName"), getName(clazz));
apply(res, ctx, ExposeTarget.NAMESPACE, clazz);
if (!apply(res, ExposeTarget.NAMESPACE, clazz)) return null;
return res;
}
private void updateProtoChain(Class<?> clazz, ObjectValue proto, FunctionValue constr) {
var parent = clazz;
while (true) {
parent = parent.getSuperclass();
if (parent == null) break;
var parentProto = getProto(parent);
var parentConstr = getConstr(parent);
if (parentProto != null && parentConstr != null) {
Values.setPrototype(Context.NULL, proto, parentProto);
Values.setPrototype(Context.NULL, constr, parentConstr);
return;
}
}
}
private void initType(Class<?> clazz, FunctionValue constr, ObjectValue proto) {
if (constr != null && proto != null) return;
if (constr != null && proto != null || ignore.contains(clazz)) return;
// i vomit
if (
clazz == Object.class ||
@@ -281,8 +323,10 @@ public class NativeWrapperProvider implements WrapperProvider {
clazz.isSynthetic()
) return;
if (constr == null) constr = makeConstructor(env, clazz);
if (proto == null) proto = makeProto(env, clazz);
if (constr == null) constr = makeConstructor(clazz);
if (proto == null) proto = makeProto(clazz);
if (constr == null || proto == null) return;
proto.defineProperty(null, "constructor", constr, true, false, false);
constr.defineProperty(null, "prototype", proto, true, false, false);
@@ -290,59 +334,79 @@ public class NativeWrapperProvider implements WrapperProvider {
prototypes.put(clazz, proto);
constructors.put(clazz, constr);
var parent = clazz.getSuperclass();
if (parent == null) return;
var parentProto = getProto(parent);
var parentConstr = getConstr(parent);
if (parentProto != null) Values.setPrototype(Context.NULL, proto, parentProto);
if (parentConstr != null) Values.setPrototype(Context.NULL, constr, parentConstr);
updateProtoChain(clazz, proto, constr);
}
public ObjectValue getProto(Class<?> clazz) {
if (proxyToClass.containsKey(clazz)) return getProto(proxyToClass.get(clazz));
initType(clazz, constructors.get(clazz), prototypes.get(clazz));
return prototypes.get(clazz);
while (clazz != null) {
var res = prototypes.get(clazz);
if (res != null) return res;
clazz = clazz.getSuperclass();
}
return null;
}
public ObjectValue getNamespace(Class<?> clazz) {
if (!namespaces.containsKey(clazz)) namespaces.put(clazz, makeNamespace(env, clazz));
return namespaces.get(clazz);
if (proxyToClass.containsKey(clazz)) return getNamespace(proxyToClass.get(clazz));
if (!namespaces.containsKey(clazz)) namespaces.put(clazz, makeNamespace(clazz));
while (clazz != null) {
var res = namespaces.get(clazz);
if (res != null) return res;
clazz = clazz.getSuperclass();
}
return null;
}
public FunctionValue getConstr(Class<?> clazz) {
if (proxyToClass.containsKey(clazz)) return getConstr(proxyToClass.get(clazz));
initType(clazz, constructors.get(clazz), prototypes.get(clazz));
return constructors.get(clazz);
while (clazz != null) {
var res = constructors.get(clazz);
if (res != null) return res;
clazz = clazz.getSuperclass();
}
return null;
}
@Override public WrapperProvider fork(Environment env) {
return new NativeWrapperProvider(env);
var res = new NativeWrapperProvider();
for (var pair : classToProxy.entrySet()) {
res.set(pair.getKey(), pair.getValue());
}
return this;
}
public void setProto(Class<?> clazz, ObjectValue value) {
prototypes.put(clazz, value);
}
public void setConstr(Class<?> clazz, FunctionValue value) {
constructors.put(clazz, value);
}
public void set(Class<?> clazz, Class<?> wrapper) {
if (clazz == null) return;
if (wrapper == null) wrapper = clazz;
if (classToProxy.get(clazz) == wrapper) return;
private void initError() {
var proto = new ObjectValue();
proto.defineProperty(null, "message", new NativeFunction("message", args -> {
if (args.self instanceof Throwable) return ((Throwable)args.self).getMessage();
else return null;
}));
proto.defineProperty(null, "name", new NativeFunction("name", args -> getName(args.self.getClass())));
proto.defineProperty(null, "toString", new NativeFunction("toString", args -> args.self.toString()));
classToProxy.remove(wrapper);
proxyToClass.remove(clazz);
classToProxy.put(clazz, wrapper);
proxyToClass.put(wrapper, clazz);
ignore.remove(clazz);
var proto = makeProto(wrapper);
var constr = makeConstructor(wrapper);
prototypes.put(clazz, proto);
constructors.put(clazz, constr);
var constr = makeConstructor(null, Throwable.class);
proto.defineProperty(null, "constructor", constr, true, false, false);
constr.defineProperty(null, "prototype", proto, true, false, false);
setProto(Throwable.class, proto);
setConstr(Throwable.class, constr);
for (var el : prototypes.keySet()) {
if (clazz.isAssignableFrom(el)) {
updateProtoChain(el, getProto(el), getConstr(el));
}
}
}
public NativeWrapperProvider(Environment env) {
this.env = env;
initError();
}
public NativeWrapperProvider() { }
}

View File

@@ -1,6 +1,6 @@
package me.topchetoeu.jscript.utils.modules;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.runtime.Context;
public abstract class Module {
private Object value;

View File

@@ -3,9 +3,9 @@ package me.topchetoeu.jscript.utils.modules;
import java.util.HashMap;
import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Extensions;
import me.topchetoeu.jscript.core.Key;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Extensions;
import me.topchetoeu.jscript.runtime.Key;
import me.topchetoeu.jscript.utils.filesystem.Filesystem;
import me.topchetoeu.jscript.utils.filesystem.Mode;

View File

@@ -2,8 +2,8 @@ package me.topchetoeu.jscript.utils.modules;
import java.util.HashMap;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
public class RootModuleRepo implements ModuleRepo {
public final HashMap<String, ModuleRepo> repos = new HashMap<>();

View File

@@ -1,8 +1,8 @@
package me.topchetoeu.jscript.utils.modules;
import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Environment;
public class SourceModule extends Module {
public final Filename filename;

View File

@@ -1,7 +1,7 @@
package me.topchetoeu.jscript.utils.permissions;
import me.topchetoeu.jscript.core.Extensions;
import me.topchetoeu.jscript.core.Key;
import me.topchetoeu.jscript.runtime.Extensions;
import me.topchetoeu.jscript.runtime.Key;
public interface PermissionsProvider {
public static final Key<PermissionsProvider> KEY = new Key<>();