Compare commits
38 Commits
0.9.11-bet
...
0.9.43-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
8a8de518a6
|
|||
|
099201e4ad
|
|||
|
f8553b79f9
|
|||
|
ba6462458c
|
|||
|
e33cdbb172
|
|||
|
fc6ddf7d3c
|
|||
|
7f275095a2
|
|||
|
90d019f92a
|
|||
|
6fb31be12c
|
|||
|
d6ede0b404
|
|||
|
71b40240c0
|
|||
|
a8775d212f
|
|||
|
71872a8d64
|
|||
|
c707f880f7
|
|||
|
0d629a6e82
|
|||
|
6eea342d04
|
|||
|
ece9cf68dc
|
|||
|
11ecd8c68f
|
|||
|
48bd304c6e
|
|||
|
d8e46c3149
|
|||
|
5fc5eb08f8
|
|||
|
8acbc003c4
|
|||
|
fda33112a7
|
|||
|
67b2413d7c
|
|||
|
3a05416510
|
|||
|
c291328cc3
|
|||
|
7cb267b0d9
|
|||
|
4e31766665
|
|||
|
b5b63c4342
|
|||
|
18f70a0d58
|
|||
|
d38b600366
|
|||
|
0ac7af2ea3
|
|||
|
5185c93663
|
|||
|
510422cab7
|
|||
|
79e1d1cfaf
|
|||
|
e0f3274a95
|
|||
| ef5d29105f | |||
|
d8ea6557df
|
2
.github/workflows/tagged-release.yml
vendored
2
.github/workflows/tagged-release.yml
vendored
@@ -3,7 +3,7 @@ name: "tagged-release"
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
- "*"
|
||||
|
||||
jobs:
|
||||
tagged-release:
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
project_group = me.topchetoeu
|
||||
project_name = jscript
|
||||
project_version = 0.9.11-beta
|
||||
project_version = 0.9.41-beta
|
||||
main_class = me.topchetoeu.jscript.utils.JScriptRepl
|
||||
|
||||
@@ -9,7 +9,7 @@ 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.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
|
||||
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
|
||||
import me.topchetoeu.jscript.runtime.values.ArrayValue;
|
||||
@@ -32,7 +32,7 @@ public class JSON {
|
||||
if (val.isNull()) return Values.NULL;
|
||||
return null;
|
||||
}
|
||||
private static JSONElement fromJs(Context ctx, Object val, HashSet<Object> prev) {
|
||||
private static JSONElement fromJs(Extensions ext, Object val, HashSet<Object> prev) {
|
||||
if (val instanceof Boolean) return JSONElement.bool((boolean)val);
|
||||
if (val instanceof Number) return JSONElement.number(((Number)val).doubleValue());
|
||||
if (val instanceof String) return JSONElement.string((String)val);
|
||||
@@ -44,7 +44,7 @@ public class JSON {
|
||||
var res = new JSONList();
|
||||
|
||||
for (var el : ((ArrayValue)val).toArray()) {
|
||||
var jsonEl = fromJs(ctx, el, prev);
|
||||
var jsonEl = fromJs(ext, el, prev);
|
||||
if (jsonEl == null) jsonEl = JSONElement.NULL;
|
||||
res.add(jsonEl);
|
||||
}
|
||||
@@ -58,8 +58,8 @@ public class JSON {
|
||||
|
||||
var res = new JSONMap();
|
||||
|
||||
for (var el : Values.getMembers(ctx, val, false, false)) {
|
||||
var jsonEl = fromJs(ctx, Values.getMember(ctx, val, el), prev);
|
||||
for (var el : Values.getMembers(ext, val, false, false)) {
|
||||
var jsonEl = fromJs(ext, Values.getMember(ext, val, el), prev);
|
||||
if (jsonEl == null) continue;
|
||||
if (el instanceof String || el instanceof Number) res.put(el.toString(), jsonEl);
|
||||
}
|
||||
@@ -70,8 +70,8 @@ public class JSON {
|
||||
if (val == null) return null;
|
||||
return null;
|
||||
}
|
||||
public static JSONElement fromJs(Context ctx, Object val) {
|
||||
return fromJs(ctx, val, new HashSet<>());
|
||||
public static JSONElement fromJs(Extensions ext, Object val) {
|
||||
return fromJs(ext, val, new HashSet<>());
|
||||
}
|
||||
|
||||
public static ParseRes<String> parseIdentifier(List<Token> tokens, int i) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -10,7 +10,7 @@ public class ContinueStatement extends Statement {
|
||||
|
||||
@Override
|
||||
public void compile(CompileResult target, boolean pollute) {
|
||||
target.add(Instruction.nop(loc(), "cont", label));
|
||||
target.add(Instruction.nop("cont", label));
|
||||
if (pollute) target.add(Instruction.pushUndefined());
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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),
|
||||
|
||||
@@ -2,6 +2,7 @@ package me.topchetoeu.jscript.lib;
|
||||
|
||||
import me.topchetoeu.jscript.lib.PromiseLib.Handle;
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.Frame;
|
||||
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
|
||||
import me.topchetoeu.jscript.runtime.values.CodeFunction;
|
||||
@@ -65,8 +66,9 @@ public class AsyncFunctionLib extends FunctionValue {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object call(Context ctx, Object thisArg, Object ...args) {
|
||||
public Object call(Extensions ext, Object thisArg, Object ...args) {
|
||||
var handler = new AsyncHelper();
|
||||
var ctx = Context.of(ext);
|
||||
|
||||
var newArgs = new Object[args.length + 1];
|
||||
newArgs[0] = new NativeFunction("await", handler::await);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.topchetoeu.jscript.lib;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.Frame;
|
||||
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
|
||||
import me.topchetoeu.jscript.runtime.values.CodeFunction;
|
||||
@@ -13,7 +14,7 @@ public class AsyncGeneratorFunctionLib extends FunctionValue {
|
||||
public final CodeFunction func;
|
||||
|
||||
@Override
|
||||
public Object call(Context ctx, Object thisArg, Object ...args) {
|
||||
public Object call(Extensions ext, Object thisArg, Object ...args) {
|
||||
var handler = new AsyncGeneratorLib();
|
||||
|
||||
var newArgs = new Object[args.length + 2];
|
||||
@@ -21,7 +22,7 @@ public class AsyncGeneratorFunctionLib extends FunctionValue {
|
||||
newArgs[1] = new NativeFunction("yield", handler::yield);
|
||||
System.arraycopy(args, 0, newArgs, 2, args.length);
|
||||
|
||||
handler.frame = new Frame(ctx, thisArg, newArgs, func);
|
||||
handler.frame = new Frame(Context.of(ext), thisArg, newArgs, func);
|
||||
return handler;
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ public class AsyncGeneratorLib {
|
||||
}
|
||||
@Expose public PromiseLib __throw(Arguments args) {
|
||||
this.currPromise = new PromiseLib();
|
||||
next(args.ctx, Values.NO_RETURN, Values.NO_RETURN, new EngineException(args.get(0)).setCtx(args.ctx));
|
||||
next(args.ctx, Values.NO_RETURN, Values.NO_RETURN, new EngineException(args.get(0)).setExtensions(args.ctx));
|
||||
return this.currPromise;
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ public class FilesystemLib {
|
||||
|
||||
try {
|
||||
if (fs.stat(path).type != EntryType.FILE) {
|
||||
throw new FilesystemException(ErrorReason.DOESNT_EXIST, "Not a file").setAction(ActionType.OPEN);
|
||||
throw new FilesystemException(ErrorReason.DOESNT_EXIST, "Not a file").setAction(ActionType.OPEN).setPath(path);
|
||||
}
|
||||
|
||||
return new FileLib(fs.open(path, _mode));
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
package me.topchetoeu.jscript.lib;
|
||||
|
||||
import me.topchetoeu.jscript.common.Filename;
|
||||
import me.topchetoeu.jscript.runtime.Compiler;
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
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.NativeFunction;
|
||||
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;
|
||||
import me.topchetoeu.jscript.utils.interop.ExposeTarget;
|
||||
import me.topchetoeu.jscript.utils.interop.WrapperName;
|
||||
|
||||
@WrapperName("Function")
|
||||
public class FunctionLib {
|
||||
private static int i;
|
||||
|
||||
@Expose public static Object __apply(Arguments args) {
|
||||
return args.self(FunctionValue.class).call(args.ctx, args.get(0), args.convert(1, ArrayValue.class).toArray());
|
||||
}
|
||||
@@ -51,4 +59,25 @@ public class FunctionLib {
|
||||
public static FunctionValue __generator(Arguments args) {
|
||||
return new GeneratorFunctionLib(args.convert(0, CodeFunction.class));
|
||||
}
|
||||
|
||||
@ExposeConstructor
|
||||
public static Object __constructor(Arguments args) {
|
||||
var compiler = Compiler.get(args);
|
||||
|
||||
var parts = args.convert(String.class);
|
||||
if (parts.length == 0) parts = new String[] { "" };
|
||||
|
||||
var src = "return function(";
|
||||
|
||||
for (var i = 0; i < parts.length - 1; i++) {
|
||||
if (i != 0) src += ",";
|
||||
src += parts[i];
|
||||
}
|
||||
|
||||
src += "){" + parts[parts.length - 1] + "}";
|
||||
|
||||
var body = compiler.compile(new Filename("jscript", "func/" + i++), src);
|
||||
var func = new CodeFunction(Context.clean(args.ctx), "", body, new ValueVariable[0]);
|
||||
return Values.call(args, func, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.topchetoeu.jscript.lib;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.Frame;
|
||||
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
|
||||
import me.topchetoeu.jscript.runtime.values.CodeFunction;
|
||||
@@ -12,14 +13,14 @@ import me.topchetoeu.jscript.utils.interop.WrapperName;
|
||||
public class GeneratorFunctionLib extends FunctionValue {
|
||||
public final CodeFunction func;
|
||||
|
||||
@Override public Object call(Context ctx, Object thisArg, Object ...args) {
|
||||
@Override public Object call(Extensions ext, Object thisArg, Object ...args) {
|
||||
var handler = new GeneratorLib();
|
||||
|
||||
var newArgs = new Object[args.length + 1];
|
||||
newArgs[0] = new NativeFunction("yield", handler::yield);
|
||||
System.arraycopy(args, 0, newArgs, 1, args.length);
|
||||
|
||||
handler.frame = new Frame(ctx, thisArg, newArgs, func);
|
||||
handler.frame = new Frame(Context.of(ext), thisArg, newArgs, func);
|
||||
return handler;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ public class GeneratorLib {
|
||||
else return next(args.ctx, args.get(0), Values.NO_RETURN, null);
|
||||
}
|
||||
@Expose public ObjectValue __throw(Arguments args) {
|
||||
return next(args.ctx, Values.NO_RETURN, Values.NO_RETURN, new EngineException(args.get(0)).setCtx(args.ctx));
|
||||
return next(args.ctx, Values.NO_RETURN, Values.NO_RETURN, new EngineException(args.get(0)).setExtensions(args.ctx));
|
||||
}
|
||||
@Expose public ObjectValue __return(Arguments args) {
|
||||
return next(args.ctx, Values.NO_RETURN, args.get(0), null);
|
||||
|
||||
@@ -17,6 +17,7 @@ import me.topchetoeu.jscript.utils.interop.Expose;
|
||||
import me.topchetoeu.jscript.utils.interop.ExposeField;
|
||||
import me.topchetoeu.jscript.utils.interop.ExposeTarget;
|
||||
import me.topchetoeu.jscript.utils.interop.ExposeType;
|
||||
import me.topchetoeu.jscript.utils.interop.NativeWrapperProvider;
|
||||
import me.topchetoeu.jscript.utils.modules.ModuleRepo;
|
||||
|
||||
public class Internals {
|
||||
@@ -51,7 +52,7 @@ public class Internals {
|
||||
try { Thread.sleep(ms, ns); }
|
||||
catch (InterruptedException e) { return; }
|
||||
|
||||
args.ctx.get(EventLoop.KEY).pushMsg(() -> func.call(new Context(args.ctx.environment), null, arguments), false);
|
||||
args.ctx.get(EventLoop.KEY).pushMsg(() -> func.call(new Context(args.ctx.extensions), null, arguments), false);
|
||||
});
|
||||
|
||||
thread.start();
|
||||
@@ -79,7 +80,7 @@ public class Internals {
|
||||
}
|
||||
catch (InterruptedException e) { return; }
|
||||
|
||||
args.ctx.get(EventLoop.KEY).pushMsg(() -> func.call(new Context(args.ctx.environment), null, arguments), false);
|
||||
args.ctx.get(EventLoop.KEY).pushMsg(() -> func.call(new Context(args.ctx.extensions), null, arguments), false);
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
@@ -165,8 +166,8 @@ public class Internals {
|
||||
}
|
||||
|
||||
public static Environment apply(Environment env) {
|
||||
var wp = env.wrappers;
|
||||
var glob = env.global = new GlobalScope(wp.getNamespace(Internals.class));
|
||||
var wp = new NativeWrapperProvider();
|
||||
var glob = new GlobalScope(wp.getNamespace(Internals.class));
|
||||
|
||||
glob.define(null, "Math", false, wp.getNamespace(MathLib.class));
|
||||
glob.define(null, "JSON", false, wp.getNamespace(JSONLib.class));
|
||||
@@ -209,8 +210,12 @@ public class Internals {
|
||||
env.add(Environment.TYPE_ERR_PROTO, wp.getProto(TypeErrorLib.class));
|
||||
env.add(Environment.RANGE_ERR_PROTO, wp.getProto(RangeErrorLib.class));
|
||||
|
||||
Values.setPrototype(Context.NULL, wp.getProto(ObjectLib.class), null);
|
||||
env.add(Environment.REGEX_CONSTR, wp.getConstr(RegExpLib.class));
|
||||
Values.setPrototype(new Context(), wp.getProto(ObjectLib.class), null);
|
||||
|
||||
env.add(NativeWrapperProvider.KEY, wp);
|
||||
env.add(GlobalScope.KEY, glob);
|
||||
|
||||
|
||||
return env;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package me.topchetoeu.jscript.lib;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.values.ObjectValue;
|
||||
import me.topchetoeu.jscript.runtime.values.Values;
|
||||
import me.topchetoeu.jscript.utils.interop.Arguments;
|
||||
@@ -85,6 +87,15 @@ public class NumberLib {
|
||||
@Expose public static String __toString(Arguments args) {
|
||||
return Values.toString(args.ctx, args.self);
|
||||
}
|
||||
@Expose public static String __toFixed(Arguments args) {
|
||||
var digits = args.getInt(0, 0);
|
||||
|
||||
var nf = NumberFormat.getNumberInstance();
|
||||
nf.setMinimumFractionDigits(digits);
|
||||
nf.setMaximumFractionDigits(digits);
|
||||
|
||||
return nf.format(args.getDouble(-1));
|
||||
}
|
||||
@Expose public static double __valueOf(Arguments args) {
|
||||
if (Values.isWrapper(args.self, NumberLib.class)) return Values.wrapper(args.self, NumberLib.class).value;
|
||||
else return Values.toNumber(args.ctx, args.self);
|
||||
|
||||
@@ -65,7 +65,7 @@ public class PromiseLib {
|
||||
}
|
||||
|
||||
if (state == STATE_REJECTED && !handled) {
|
||||
Values.printError(((EngineException)val).setCtx(ctx), "(in promise)");
|
||||
Values.printError(((EngineException)val).setExtensions(ctx), "(in promise)");
|
||||
}
|
||||
|
||||
handles = null;
|
||||
@@ -207,7 +207,7 @@ public class PromiseLib {
|
||||
}
|
||||
@Expose(value = "reject", target = ExposeTarget.STATIC)
|
||||
public static PromiseLib __ofRejected(Arguments args) {
|
||||
return ofRejected(args.ctx, new EngineException(args.get(0)).setCtx(args.ctx));
|
||||
return ofRejected(args.ctx, new EngineException(args.get(0)).setExtensions(args.ctx));
|
||||
}
|
||||
|
||||
@Expose(target = ExposeTarget.STATIC)
|
||||
@@ -215,7 +215,7 @@ public class PromiseLib {
|
||||
if (!(args.get(0) instanceof ArrayValue)) throw EngineException.ofType("Expected argument for any to be an array.");
|
||||
var promises = args.convert(0, ArrayValue.class);
|
||||
|
||||
if (promises.size() == 0) return ofRejected(args.ctx, EngineException.ofError("No promises passed to 'Promise.any'.").setCtx(args.ctx));
|
||||
if (promises.size() == 0) return ofRejected(args.ctx, EngineException.ofError("No promises passed to 'Promise.any'.").setExtensions(args.ctx));
|
||||
var n = new int[] { promises.size() };
|
||||
var res = new PromiseLib();
|
||||
var errors = new ArrayValue();
|
||||
@@ -230,7 +230,7 @@ public class PromiseLib {
|
||||
public void onReject(EngineException err) {
|
||||
errors.set(args.ctx, index, err.value);
|
||||
n[0]--;
|
||||
if (n[0] <= 0) res.reject(args.ctx, new EngineException(errors).setCtx(args.ctx));
|
||||
if (n[0] <= 0) res.reject(args.ctx, new EngineException(errors).setExtensions(args.ctx));
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -390,7 +390,7 @@ public class PromiseLib {
|
||||
return null;
|
||||
}),
|
||||
new NativeFunction(null, _args -> {
|
||||
res.reject(_args.ctx, new EngineException(_args.get(0)).setCtx(_args.ctx));
|
||||
res.reject(_args.ctx, new EngineException(_args.get(0)).setExtensions(_args.ctx));
|
||||
return null;
|
||||
})
|
||||
);
|
||||
|
||||
18
src/java/me/topchetoeu/jscript/lib/ThrowableLib.java
Normal file
18
src/java/me/topchetoeu/jscript/lib/ThrowableLib.java
Normal 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);
|
||||
}
|
||||
}
|
||||
5
src/java/me/topchetoeu/jscript/runtime/Childable.java
Normal file
5
src/java/me/topchetoeu/jscript/runtime/Childable.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package me.topchetoeu.jscript.runtime;
|
||||
|
||||
public interface Childable {
|
||||
Object child();
|
||||
}
|
||||
@@ -11,58 +11,39 @@ 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();
|
||||
|
||||
public final Context parent;
|
||||
public final Environment environment;
|
||||
public final Extensions extensions;
|
||||
public final Frame frame;
|
||||
// public final Engine engine;
|
||||
public final int stackSize;
|
||||
|
||||
@Override public <T> void add(Key<T> key, T obj) {
|
||||
if (environment != null) environment.add(key, obj);
|
||||
// else if (engine != null) engine.add(key, obj);
|
||||
if (extensions != null) extensions.add(key, obj);
|
||||
}
|
||||
@Override public <T> T get(Key<T> key) {
|
||||
if (environment != null && environment.has(key)) return environment.get(key);
|
||||
// else if (engine != null && engine.has(key)) return engine.get(key);
|
||||
if (extensions != null && extensions.has(key)) return extensions.get(key);
|
||||
return null;
|
||||
}
|
||||
@Override public boolean has(Key<?> key) {
|
||||
return
|
||||
environment != null && environment.has(key);
|
||||
// engine != null && engine.has(key);
|
||||
return extensions != null && extensions.has(key);
|
||||
}
|
||||
@Override public boolean remove(Key<?> key) {
|
||||
var res = false;
|
||||
|
||||
if (environment != null) res |= environment.remove(key);
|
||||
// else if (engine != null) res |= engine.remove(key);
|
||||
|
||||
if (extensions != null) res |= extensions.remove(key);
|
||||
return res;
|
||||
}
|
||||
@Override public Iterable<Key<?>> keys() {
|
||||
if (environment == null) return List.of();
|
||||
else return environment.keys();
|
||||
|
||||
// if (engine == null && environment == null) return List.of();
|
||||
// if (engine == null) return environment.keys();
|
||||
// if (environment == null) return engine.keys();
|
||||
|
||||
// return () -> Stream.concat(
|
||||
// StreamSupport.stream(engine.keys().spliterator(), false),
|
||||
// StreamSupport.stream(environment.keys().spliterator(), false)
|
||||
// ).distinct().iterator();
|
||||
if (extensions == null) return List.of();
|
||||
else return extensions.keys();
|
||||
}
|
||||
|
||||
public FunctionValue compile(Filename filename, String raw) {
|
||||
DebugContext.get(this).onSource(filename, raw);
|
||||
var result = new CodeFunction(environment, filename.toString(), Compiler.get(this).compile(filename, raw), new ValueVariable[0]);
|
||||
var result = new CodeFunction(extensions, filename.toString(), Compiler.get(this).compile(filename, raw), new ValueVariable[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Context pushFrame(Frame frame) {
|
||||
var res = new Context(this, frame.function.environment, frame, stackSize + 1);
|
||||
var res = new Context(this, frame.function.extensions, frame, stackSize + 1);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -88,10 +69,9 @@ public class Context implements Extensions {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
private Context(Context parent, Environment environment, Frame frame, int stackSize) {
|
||||
private Context(Context parent, Extensions ext, Frame frame, int stackSize) {
|
||||
this.parent = parent;
|
||||
this.environment = environment;
|
||||
this.extensions = ext;
|
||||
this.frame = frame;
|
||||
this.stackSize = stackSize;
|
||||
|
||||
@@ -103,7 +83,16 @@ public class Context implements Extensions {
|
||||
public Context() {
|
||||
this(null, null, null, 0);
|
||||
}
|
||||
public Context(Environment env) {
|
||||
this(null, env, null, 0);
|
||||
public Context(Extensions ext) {
|
||||
this(null, clean(ext), null, 0);
|
||||
}
|
||||
|
||||
public static Context of(Extensions ext) {
|
||||
if (ext instanceof Context) return (Context)ext;
|
||||
return new Context(ext);
|
||||
}
|
||||
public static Extensions clean(Extensions ext) {
|
||||
if (ext instanceof Context) return clean(((Context)ext).extensions);
|
||||
else return ext;
|
||||
}
|
||||
}
|
||||
|
||||
5
src/java/me/topchetoeu/jscript/runtime/Copyable.java
Normal file
5
src/java/me/topchetoeu/jscript/runtime/Copyable.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package me.topchetoeu.jscript.runtime;
|
||||
|
||||
public interface Copyable {
|
||||
Object copy();
|
||||
}
|
||||
@@ -3,11 +3,9 @@ package me.topchetoeu.jscript.runtime;
|
||||
import java.util.HashMap;
|
||||
|
||||
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")
|
||||
public class Environment implements Extensions {
|
||||
@@ -31,9 +29,6 @@ public class Environment implements Extensions {
|
||||
|
||||
private HashMap<Key<?>, Object> data = new HashMap<>();
|
||||
|
||||
public GlobalScope global;
|
||||
public WrapperProvider wrappers;
|
||||
|
||||
@Override public <T> void add(Key<T> key, T obj) {
|
||||
data.put(key, obj);
|
||||
}
|
||||
@@ -56,37 +51,11 @@ public class Environment implements Extensions {
|
||||
|
||||
public static FunctionValue regexConstructor(Extensions ext) {
|
||||
return ext.init(REGEX_CONSTR, new NativeFunction("RegExp", args -> {
|
||||
throw EngineException.ofError("Regular expressions not supported.").setCtx(args.ctx);
|
||||
throw EngineException.ofError("Regular expressions not supported.").setExtensions(args.ctx);
|
||||
}));
|
||||
}
|
||||
|
||||
public Environment copy() {
|
||||
var res = new Environment(null, global);
|
||||
|
||||
res.wrappers = wrappers.fork(res);
|
||||
res.global = global;
|
||||
res.data.putAll(data);
|
||||
|
||||
return res;
|
||||
}
|
||||
public Environment child() {
|
||||
var res = copy();
|
||||
res.global = res.global.globalChild();
|
||||
return res;
|
||||
}
|
||||
|
||||
public Context context() {
|
||||
return new Context(this);
|
||||
}
|
||||
|
||||
public Environment(WrapperProvider nativeConverter, GlobalScope global) {
|
||||
if (nativeConverter == null) nativeConverter = new NativeWrapperProvider(this);
|
||||
if (global == null) global = new GlobalScope();
|
||||
|
||||
this.wrappers = nativeConverter;
|
||||
this.global = global;
|
||||
}
|
||||
public Environment() {
|
||||
this(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,15 +23,15 @@ public interface EventLoop {
|
||||
return pushMsg(() -> { runnable.run(); return null; }, micro);
|
||||
}
|
||||
|
||||
public default DataNotifier<Object> pushMsg(boolean micro, Environment env, FunctionValue func, Object thisArg, Object ...args) {
|
||||
public default DataNotifier<Object> pushMsg(boolean micro, Extensions ext, FunctionValue func, Object thisArg, Object ...args) {
|
||||
return pushMsg(() -> {
|
||||
return func.call(new Context(env), thisArg, args);
|
||||
return func.call(Context.of(ext), thisArg, args);
|
||||
}, micro);
|
||||
}
|
||||
public default DataNotifier<Object> pushMsg(boolean micro, Environment env, Filename filename, String raw, Object thisArg, Object ...args) {
|
||||
public default DataNotifier<Object> pushMsg(boolean micro, Extensions ext, 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);
|
||||
var ctx = Context.of(ext);
|
||||
return ctx.compile(filename, raw).call(Context.of(ext), thisArg, args);
|
||||
}, micro);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package me.topchetoeu.jscript.runtime;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Extensions {
|
||||
public interface Extensions extends Childable, Copyable {
|
||||
public static Extensions EMPTY = new Extensions() {
|
||||
@Override public <T> void add(Key<T> key, T obj) { }
|
||||
@Override public boolean remove(Key<?> key) { return false; }
|
||||
@@ -47,6 +47,29 @@ public interface Extensions {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
default Extensions copy() {
|
||||
var res = new Environment();
|
||||
for (var key : keys()) {
|
||||
var val = get(key);
|
||||
if (val instanceof Copyable) val = ((Copyable)val).copy();
|
||||
res.add((Key<Object>)key, val);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
default Extensions child() {
|
||||
var res = new Environment();
|
||||
for (var key : keys()) {
|
||||
var val = get(key);
|
||||
if (val instanceof Childable) val = ((Childable)val).child();
|
||||
res.add((Key<Object>)key, val);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static Extensions wrap(Extensions ext) {
|
||||
if (ext == null) return EMPTY;
|
||||
else return ext;
|
||||
|
||||
@@ -294,13 +294,13 @@ public class Frame {
|
||||
public ObjectValue getValStackScope() {
|
||||
return new ObjectValue() {
|
||||
@Override
|
||||
protected Object getField(Context ctx, Object key) {
|
||||
var i = (int)Values.toNumber(ctx, key);
|
||||
protected Object getField(Extensions ext, Object key) {
|
||||
var i = (int)Values.toNumber(ext, key);
|
||||
if (i < 0 || i >= stackPtr) return null;
|
||||
else return stack[i];
|
||||
}
|
||||
@Override
|
||||
protected boolean hasField(Context ctx, Object key) {
|
||||
protected boolean hasField(Extensions ext, Object key) {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.Collections;
|
||||
import me.topchetoeu.jscript.common.Instruction;
|
||||
import me.topchetoeu.jscript.common.Operation;
|
||||
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
|
||||
import me.topchetoeu.jscript.runtime.scope.GlobalScope;
|
||||
import me.topchetoeu.jscript.runtime.scope.ValueVariable;
|
||||
import me.topchetoeu.jscript.runtime.values.ArrayValue;
|
||||
import me.topchetoeu.jscript.runtime.values.CodeFunction;
|
||||
@@ -14,43 +15,43 @@ 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) {
|
||||
private static Object execReturn(Extensions ext, Instruction instr, Frame frame) {
|
||||
return frame.pop();
|
||||
}
|
||||
private static Object execThrow(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execThrow(Extensions ext, Instruction instr, Frame frame) {
|
||||
throw new EngineException(frame.pop());
|
||||
}
|
||||
private static Object execThrowSyntax(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execThrowSyntax(Extensions ext, Instruction instr, Frame frame) {
|
||||
throw EngineException.ofSyntax((String)instr.get(0));
|
||||
}
|
||||
|
||||
private static Object execCall(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execCall(Extensions ext, Instruction instr, Frame frame) {
|
||||
var callArgs = frame.take(instr.get(0));
|
||||
var func = frame.pop();
|
||||
var thisArg = frame.pop();
|
||||
|
||||
frame.push(Values.call(ctx, func, thisArg, callArgs));
|
||||
frame.push(Values.call(ext, func, thisArg, callArgs));
|
||||
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execCallNew(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execCallNew(Extensions ext, Instruction instr, Frame frame) {
|
||||
var callArgs = frame.take(instr.get(0));
|
||||
var funcObj = frame.pop();
|
||||
|
||||
frame.push(Values.callNew(ctx, funcObj, callArgs));
|
||||
frame.push(Values.callNew(ext, funcObj, callArgs));
|
||||
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
|
||||
private static Object execMakeVar(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execMakeVar(Extensions ext, Instruction instr, Frame frame) {
|
||||
var name = (String)instr.get(0);
|
||||
ctx.environment.global.define(ctx, name);
|
||||
GlobalScope.get(ext).define(ext, name);
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execDefProp(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execDefProp(Extensions ext, Instruction instr, Frame frame) {
|
||||
var setter = frame.pop();
|
||||
var getter = frame.pop();
|
||||
var name = frame.pop();
|
||||
@@ -59,16 +60,16 @@ public class InstructionRunner {
|
||||
if (getter != null && !(getter instanceof FunctionValue)) throw EngineException.ofType("Getter must be a function or undefined.");
|
||||
if (setter != null && !(setter instanceof FunctionValue)) throw EngineException.ofType("Setter must be a function or undefined.");
|
||||
if (!(obj instanceof ObjectValue)) throw EngineException.ofType("Property apply target must be an object.");
|
||||
((ObjectValue)obj).defineProperty(ctx, name, (FunctionValue)getter, (FunctionValue)setter, false, false);
|
||||
((ObjectValue)obj).defineProperty(ext, name, (FunctionValue)getter, (FunctionValue)setter, false, false);
|
||||
|
||||
frame.push(obj);
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execKeys(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execKeys(Extensions ext, Instruction instr, Frame frame) {
|
||||
var val = frame.pop();
|
||||
|
||||
var members = Values.getMembers(ctx, val, false, false);
|
||||
var members = Values.getMembers(ext, val, false, false);
|
||||
Collections.reverse(members);
|
||||
|
||||
frame.push(null);
|
||||
@@ -76,7 +77,7 @@ public class InstructionRunner {
|
||||
for (var el : members) {
|
||||
if (el instanceof Symbol) continue;
|
||||
var obj = new ObjectValue();
|
||||
obj.defineProperty(ctx, "value", el);
|
||||
obj.defineProperty(ext, "value", el);
|
||||
frame.push(obj);
|
||||
}
|
||||
|
||||
@@ -84,7 +85,7 @@ public class InstructionRunner {
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
|
||||
private static Object execTryStart(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execTryStart(Extensions ext, Instruction instr, Frame frame) {
|
||||
int start = frame.codePtr + 1;
|
||||
int catchStart = (int)instr.get(0);
|
||||
int finallyStart = (int)instr.get(1);
|
||||
@@ -95,12 +96,12 @@ public class InstructionRunner {
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execTryEnd(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execTryEnd(Extensions ext, Instruction instr, Frame frame) {
|
||||
frame.popTryFlag = true;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
|
||||
private static Object execDup(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execDup(Extensions ext, Instruction instr, Frame frame) {
|
||||
int count = instr.get(0);
|
||||
|
||||
for (var i = 0; i < count; i++) {
|
||||
@@ -110,7 +111,7 @@ public class InstructionRunner {
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execLoadValue(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execLoadValue(Extensions ext, Instruction instr, Frame frame) {
|
||||
switch (instr.type) {
|
||||
case PUSH_UNDEFINED: frame.push(null); break;
|
||||
case PUSH_NULL: frame.push(Values.NULL); break;
|
||||
@@ -120,33 +121,33 @@ public class InstructionRunner {
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execLoadVar(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execLoadVar(Extensions ext, Instruction instr, Frame frame) {
|
||||
var i = instr.get(0);
|
||||
|
||||
if (i instanceof String) frame.push(ctx.environment.global.get(ctx, (String)i));
|
||||
else frame.push(frame.scope.get((int)i).get(ctx));
|
||||
if (i instanceof String) frame.push(GlobalScope.get(ext).get(ext, (String)i));
|
||||
else frame.push(frame.scope.get((int)i).get(ext));
|
||||
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execLoadObj(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execLoadObj(Extensions ext, Instruction instr, Frame frame) {
|
||||
frame.push(new ObjectValue());
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execLoadGlob(Context ctx, Instruction instr, Frame frame) {
|
||||
frame.push(ctx.environment.global.obj);
|
||||
private static Object execLoadGlob(Extensions ext, Instruction instr, Frame frame) {
|
||||
frame.push(GlobalScope.get(ext).obj);
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execLoadArr(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execLoadArr(Extensions ext, Instruction instr, Frame frame) {
|
||||
var res = new ArrayValue();
|
||||
res.setSize(instr.get(0));
|
||||
frame.push(res);
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execLoadFunc(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execLoadFunc(Extensions ext, Instruction instr, Frame frame) {
|
||||
int id = instr.get(0);
|
||||
var captures = new ValueVariable[instr.params.length - 1];
|
||||
|
||||
@@ -154,19 +155,19 @@ public class InstructionRunner {
|
||||
captures[i - 1] = frame.scope.get(instr.get(i));
|
||||
}
|
||||
|
||||
var func = new CodeFunction(ctx.environment, "", frame.function.body.children[id], captures);
|
||||
var func = new CodeFunction(ext, "", frame.function.body.children[id], captures);
|
||||
|
||||
frame.push(func);
|
||||
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execLoadMember(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execLoadMember(Extensions ext, Instruction instr, Frame frame) {
|
||||
var key = frame.pop();
|
||||
var obj = frame.pop();
|
||||
|
||||
try {
|
||||
frame.push(Values.getMember(ctx, obj, key));
|
||||
frame.push(Values.getMember(ext, obj, key));
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
throw EngineException.ofType(e.getMessage());
|
||||
@@ -174,9 +175,9 @@ public class InstructionRunner {
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execLoadRegEx(Context ctx, Instruction instr, Frame frame) {
|
||||
if (ctx.hasNotNull(Environment.REGEX_CONSTR)) {
|
||||
frame.push(Values.callNew(ctx, ctx.get(Environment.REGEX_CONSTR), instr.get(0), instr.get(1)));
|
||||
private static Object execLoadRegEx(Extensions ext, Instruction instr, Frame frame) {
|
||||
if (ext.hasNotNull(Environment.REGEX_CONSTR)) {
|
||||
frame.push(Values.callNew(ext, ext.get(Environment.REGEX_CONSTR), instr.get(0), instr.get(1)));
|
||||
}
|
||||
else {
|
||||
throw EngineException.ofSyntax("Regex is not supported.");
|
||||
@@ -185,43 +186,43 @@ public class InstructionRunner {
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
|
||||
private static Object execDiscard(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execDiscard(Extensions ext, Instruction instr, Frame frame) {
|
||||
frame.pop();
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execStoreMember(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execStoreMember(Extensions ext, Instruction instr, Frame frame) {
|
||||
var val = frame.pop();
|
||||
var key = frame.pop();
|
||||
var obj = frame.pop();
|
||||
|
||||
if (!Values.setMember(ctx, obj, key, val)) throw EngineException.ofSyntax("Can't set member '" + key + "'.");
|
||||
if (!Values.setMember(ext, obj, key, val)) throw EngineException.ofSyntax("Can't set member '" + key + "'.");
|
||||
if ((boolean)instr.get(0)) frame.push(val);
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execStoreVar(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execStoreVar(Extensions ext, Instruction instr, Frame frame) {
|
||||
var val = (boolean)instr.get(1) ? frame.peek() : frame.pop();
|
||||
var i = instr.get(0);
|
||||
|
||||
if (i instanceof String) ctx.environment.global.set(ctx, (String)i, val);
|
||||
else frame.scope.get((int)i).set(ctx, val);
|
||||
if (i instanceof String) GlobalScope.get(ext).set(ext, (String)i, val);
|
||||
else frame.scope.get((int)i).set(ext, val);
|
||||
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execStoreSelfFunc(Context ctx, Instruction instr, Frame frame) {
|
||||
frame.scope.locals[(int)instr.get(0)].set(ctx, frame.function);
|
||||
private static Object execStoreSelfFunc(Extensions ext, Instruction instr, Frame frame) {
|
||||
frame.scope.locals[(int)instr.get(0)].set(ext, frame.function);
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
|
||||
private static Object execJmp(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execJmp(Extensions ext, Instruction instr, Frame frame) {
|
||||
frame.codePtr += (int)instr.get(0);
|
||||
frame.jumpFlag = true;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execJmpIf(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execJmpIf(Extensions ext, Instruction instr, Frame frame) {
|
||||
if (Values.toBoolean(frame.pop())) {
|
||||
frame.codePtr += (int)instr.get(0);
|
||||
frame.jumpFlag = true;
|
||||
@@ -229,7 +230,7 @@ public class InstructionRunner {
|
||||
else frame.codePtr ++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execJmpIfNot(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execJmpIfNot(Extensions ext, Instruction instr, Frame frame) {
|
||||
if (Values.not(frame.pop())) {
|
||||
frame.codePtr += (int)instr.get(0);
|
||||
frame.jumpFlag = true;
|
||||
@@ -238,13 +239,13 @@ public class InstructionRunner {
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
|
||||
private static Object execTypeof(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execTypeof(Extensions ext, Instruction instr, Frame frame) {
|
||||
String name = instr.get(0);
|
||||
Object obj;
|
||||
|
||||
if (name != null) {
|
||||
if (ctx.environment.global.has(ctx, name)) {
|
||||
obj = ctx.environment.global.get(ctx, name);
|
||||
if (GlobalScope.get(ext).has(ext, name)) {
|
||||
obj = GlobalScope.get(ext).get(ext, name);
|
||||
}
|
||||
else obj = null;
|
||||
}
|
||||
@@ -255,73 +256,73 @@ public class InstructionRunner {
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
private static Object execNop(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execNop(Extensions ext, Instruction instr, Frame frame) {
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
|
||||
private static Object execDelete(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execDelete(Extensions ext, Instruction instr, Frame frame) {
|
||||
var key = frame.pop();
|
||||
var val = frame.pop();
|
||||
|
||||
if (!Values.deleteMember(ctx, val, key)) throw EngineException.ofSyntax("Can't delete member '" + key + "'.");
|
||||
if (!Values.deleteMember(ext, val, key)) throw EngineException.ofSyntax("Can't delete member '" + key + "'.");
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
|
||||
private static Object execOperation(Context ctx, Instruction instr, Frame frame) {
|
||||
private static Object execOperation(Extensions ext, Instruction instr, Frame frame) {
|
||||
Operation op = instr.get(0);
|
||||
var args = new Object[op.operands];
|
||||
|
||||
for (var i = op.operands - 1; i >= 0; i--) args[i] = frame.pop();
|
||||
|
||||
frame.push(Values.operation(ctx, op, args));
|
||||
frame.push(Values.operation(ext, op, args));
|
||||
frame.codePtr++;
|
||||
return Values.NO_RETURN;
|
||||
}
|
||||
|
||||
public static Object exec(Context ctx, Instruction instr, Frame frame) {
|
||||
public static Object exec(Extensions ext, Instruction instr, Frame frame) {
|
||||
switch (instr.type) {
|
||||
case NOP: return execNop(ctx, instr, frame);
|
||||
case RETURN: return execReturn(ctx, instr, frame);
|
||||
case THROW: return execThrow(ctx, instr, frame);
|
||||
case THROW_SYNTAX: return execThrowSyntax(ctx, instr, frame);
|
||||
case CALL: return execCall(ctx, instr, frame);
|
||||
case CALL_NEW: return execCallNew(ctx, instr, frame);
|
||||
case TRY_START: return execTryStart(ctx, instr, frame);
|
||||
case TRY_END: return execTryEnd(ctx, instr, frame);
|
||||
case NOP: return execNop(ext, instr, frame);
|
||||
case RETURN: return execReturn(ext, instr, frame);
|
||||
case THROW: return execThrow(ext, instr, frame);
|
||||
case THROW_SYNTAX: return execThrowSyntax(ext, instr, frame);
|
||||
case CALL: return execCall(ext, instr, frame);
|
||||
case CALL_NEW: return execCallNew(ext, instr, frame);
|
||||
case TRY_START: return execTryStart(ext, instr, frame);
|
||||
case TRY_END: return execTryEnd(ext, instr, frame);
|
||||
|
||||
case DUP: return execDup(ctx, instr, frame);
|
||||
case DUP: return execDup(ext, instr, frame);
|
||||
case PUSH_UNDEFINED:
|
||||
case PUSH_NULL:
|
||||
case PUSH_STRING:
|
||||
case PUSH_NUMBER:
|
||||
case PUSH_BOOL:
|
||||
return execLoadValue(ctx, instr, frame);
|
||||
case LOAD_VAR: return execLoadVar(ctx, instr, frame);
|
||||
case LOAD_OBJ: return execLoadObj(ctx, instr, frame);
|
||||
case LOAD_ARR: return execLoadArr(ctx, instr, frame);
|
||||
case LOAD_FUNC: return execLoadFunc(ctx, instr, frame);
|
||||
case LOAD_MEMBER: return execLoadMember(ctx, instr, frame);
|
||||
case LOAD_REGEX: return execLoadRegEx(ctx, instr, frame);
|
||||
case LOAD_GLOB: return execLoadGlob(ctx, instr, frame);
|
||||
return execLoadValue(ext, instr, frame);
|
||||
case LOAD_VAR: return execLoadVar(ext, instr, frame);
|
||||
case LOAD_OBJ: return execLoadObj(ext, instr, frame);
|
||||
case LOAD_ARR: return execLoadArr(ext, instr, frame);
|
||||
case LOAD_FUNC: return execLoadFunc(ext, instr, frame);
|
||||
case LOAD_MEMBER: return execLoadMember(ext, instr, frame);
|
||||
case LOAD_REGEX: return execLoadRegEx(ext, instr, frame);
|
||||
case LOAD_GLOB: return execLoadGlob(ext, instr, frame);
|
||||
|
||||
case DISCARD: return execDiscard(ctx, instr, frame);
|
||||
case STORE_MEMBER: return execStoreMember(ctx, instr, frame);
|
||||
case STORE_VAR: return execStoreVar(ctx, instr, frame);
|
||||
case STORE_SELF_FUNC: return execStoreSelfFunc(ctx, instr, frame);
|
||||
case MAKE_VAR: return execMakeVar(ctx, instr, frame);
|
||||
case DISCARD: return execDiscard(ext, instr, frame);
|
||||
case STORE_MEMBER: return execStoreMember(ext, instr, frame);
|
||||
case STORE_VAR: return execStoreVar(ext, instr, frame);
|
||||
case STORE_SELF_FUNC: return execStoreSelfFunc(ext, instr, frame);
|
||||
case MAKE_VAR: return execMakeVar(ext, instr, frame);
|
||||
|
||||
case KEYS: return execKeys(ctx, instr, frame);
|
||||
case DEF_PROP: return execDefProp(ctx, instr, frame);
|
||||
case TYPEOF: return execTypeof(ctx, instr, frame);
|
||||
case DELETE: return execDelete(ctx, instr, frame);
|
||||
case KEYS: return execKeys(ext, instr, frame);
|
||||
case DEF_PROP: return execDefProp(ext, instr, frame);
|
||||
case TYPEOF: return execTypeof(ext, instr, frame);
|
||||
case DELETE: return execDelete(ext, instr, frame);
|
||||
|
||||
case JMP: return execJmp(ctx, instr, frame);
|
||||
case JMP_IF: return execJmpIf(ctx, instr, frame);
|
||||
case JMP_IFN: return execJmpIfNot(ctx, instr, frame);
|
||||
case JMP: return execJmp(ext, instr, frame);
|
||||
case JMP_IF: return execJmpIf(ext, instr, frame);
|
||||
case JMP_IFN: return execJmpIfNot(ext, instr, frame);
|
||||
|
||||
case OPERATION: return execOperation(ctx, instr, frame);
|
||||
case OPERATION: return execOperation(ext, instr, frame);
|
||||
|
||||
default: throw EngineException.ofSyntax("Invalid instruction " + instr.type.name() + ".");
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.List;
|
||||
import me.topchetoeu.jscript.common.Location;
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Environment;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.values.ObjectValue;
|
||||
import me.topchetoeu.jscript.runtime.values.Values;
|
||||
import me.topchetoeu.jscript.runtime.values.ObjectValue.PlaceholderProto;
|
||||
@@ -14,10 +15,10 @@ public class EngineException extends RuntimeException {
|
||||
public static class StackElement {
|
||||
public final Location location;
|
||||
public final String name;
|
||||
public final Context ctx;
|
||||
public final Extensions ext;
|
||||
|
||||
public boolean visible() {
|
||||
return ctx == null || !ctx.get(Environment.HIDE_STACK, false);
|
||||
return ext == null || !ext.get(Environment.HIDE_STACK, false);
|
||||
}
|
||||
public String toString() {
|
||||
var res = "";
|
||||
@@ -29,12 +30,13 @@ public class EngineException extends RuntimeException {
|
||||
return res.trim();
|
||||
}
|
||||
|
||||
public StackElement(Context ctx, Location location, String name) {
|
||||
public StackElement(Extensions ext, Location location, String name) {
|
||||
if (name != null) name = name.trim();
|
||||
if (name.equals("")) name = null;
|
||||
|
||||
if (ctx == null) this.ctx = null;
|
||||
else this.ctx = new Context(ctx.environment);
|
||||
if (ext == null) this.ext = null;
|
||||
else this.ext = Context.clean(ext);
|
||||
|
||||
this.location = location;
|
||||
this.name = name;
|
||||
}
|
||||
@@ -42,13 +44,13 @@ public class EngineException extends RuntimeException {
|
||||
|
||||
public final Object value;
|
||||
public EngineException cause;
|
||||
public Environment env = null;
|
||||
public Extensions ext = null;
|
||||
public final List<StackElement> stackTrace = new ArrayList<>();
|
||||
|
||||
public EngineException add(Context ctx, String name, Location location) {
|
||||
var el = new StackElement(ctx, location, name);
|
||||
public EngineException add(Extensions ext, String name, Location location) {
|
||||
var el = new StackElement(ext, location, name);
|
||||
if (el.name == null && el.location == null) return this;
|
||||
setCtx(ctx);
|
||||
setExtensions(ext);
|
||||
stackTrace.add(el);
|
||||
return this;
|
||||
}
|
||||
@@ -56,15 +58,15 @@ public class EngineException extends RuntimeException {
|
||||
this.cause = cause;
|
||||
return this;
|
||||
}
|
||||
public EngineException setCtx(Context ctx) {
|
||||
if (this.env == null) this.env = ctx.environment;
|
||||
public EngineException setExtensions(Extensions ext) {
|
||||
if (this.ext == null) this.ext = Context.clean(ext);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String toString(Context ctx) {
|
||||
public String toString(Extensions ext) {
|
||||
var ss = new StringBuilder();
|
||||
try {
|
||||
ss.append(Values.toString(ctx, value)).append('\n');
|
||||
ss.append(Values.toString(ext, value)).append('\n');
|
||||
}
|
||||
catch (EngineException e) {
|
||||
ss.append("[Error while stringifying]\n");
|
||||
@@ -72,7 +74,7 @@ public class EngineException extends RuntimeException {
|
||||
for (var line : stackTrace) {
|
||||
if (line.visible()) ss.append(" ").append(line.toString()).append("\n");
|
||||
}
|
||||
if (cause != null) ss.append("Caused by ").append(cause.toString(ctx)).append('\n');
|
||||
if (cause != null) ss.append("Caused by ").append(cause.toString(ext)).append('\n');
|
||||
ss.deleteCharAt(ss.length() - 1);
|
||||
return ss.toString();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ package me.topchetoeu.jscript.runtime.scope;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.Key;
|
||||
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
|
||||
import me.topchetoeu.jscript.runtime.values.FunctionValue;
|
||||
import me.topchetoeu.jscript.runtime.values.NativeFunction;
|
||||
@@ -11,47 +12,49 @@ import me.topchetoeu.jscript.runtime.values.ObjectValue;
|
||||
import me.topchetoeu.jscript.runtime.values.Values;
|
||||
|
||||
public class GlobalScope {
|
||||
public static final Key<GlobalScope> KEY = new Key<>();
|
||||
|
||||
public final ObjectValue obj;
|
||||
|
||||
public boolean has(Context ctx, String name) {
|
||||
return Values.hasMember(null, obj, name, false);
|
||||
public boolean has(Extensions ext, String name) {
|
||||
return Values.hasMember(ext, obj, name, false);
|
||||
}
|
||||
|
||||
public GlobalScope globalChild() {
|
||||
public GlobalScope child() {
|
||||
var obj = new ObjectValue();
|
||||
Values.setPrototype(null, obj, this.obj);
|
||||
return new GlobalScope(obj);
|
||||
}
|
||||
|
||||
public Object define(Context ctx, String name) {
|
||||
if (Values.hasMember(ctx, obj, name, false)) return name;
|
||||
obj.defineProperty(ctx, name, null);
|
||||
public Object define(Extensions ext, String name) {
|
||||
if (Values.hasMember(ext, obj, name, false)) return name;
|
||||
obj.defineProperty(ext, name, null);
|
||||
return name;
|
||||
}
|
||||
public void define(Context ctx, String name, Variable val) {
|
||||
obj.defineProperty(ctx, name,
|
||||
public void define(Extensions ext, String name, Variable val) {
|
||||
obj.defineProperty(ext, name,
|
||||
new NativeFunction("get " + name, args -> val.get(args.ctx)),
|
||||
new NativeFunction("set " + name, args -> { val.set(args.ctx, args.get(0)); return null; }),
|
||||
true, true
|
||||
);
|
||||
}
|
||||
public void define(Context ctx, String name, boolean readonly, Object val) {
|
||||
obj.defineProperty(ctx, name, val, readonly, true, true);
|
||||
public void define(Extensions ext, String name, boolean readonly, Object val) {
|
||||
obj.defineProperty(ext, name, val, readonly, true, true);
|
||||
}
|
||||
public void define(Context ctx, String ...names) {
|
||||
for (var n : names) define(ctx, n);
|
||||
public void define(Extensions ext, String ...names) {
|
||||
for (var n : names) define(ext, n);
|
||||
}
|
||||
public void define(Context ctx, boolean readonly, FunctionValue val) {
|
||||
define(ctx, val.name, readonly, val);
|
||||
public void define(Extensions ext, boolean readonly, FunctionValue val) {
|
||||
define(ext, val.name, readonly, val);
|
||||
}
|
||||
|
||||
public Object get(Context ctx, String name) {
|
||||
if (!Values.hasMember(ctx, obj, name, false)) throw EngineException.ofSyntax("The variable '" + name + "' doesn't exist.");
|
||||
else return Values.getMember(ctx, obj, name);
|
||||
public Object get(Extensions ext, String name) {
|
||||
if (!Values.hasMember(ext, obj, name, false)) throw EngineException.ofSyntax("The variable '" + name + "' doesn't exist.");
|
||||
else return Values.getMember(ext, obj, name);
|
||||
}
|
||||
public void set(Context ctx, String name, Object val) {
|
||||
if (!Values.hasMember(ctx, obj, name, false)) throw EngineException.ofSyntax("The variable '" + name + "' doesn't exist.");
|
||||
if (!Values.setMember(ctx, obj, name, val)) throw EngineException.ofSyntax("The global '" + name + "' is readonly.");
|
||||
public void set(Extensions ext, String name, Object val) {
|
||||
if (!Values.hasMember(ext, obj, name, false)) throw EngineException.ofSyntax("The variable '" + name + "' doesn't exist.");
|
||||
if (!Values.setMember(ext, obj, name, val)) throw EngineException.ofSyntax("The global '" + name + "' is readonly.");
|
||||
}
|
||||
|
||||
public Set<String> keys() {
|
||||
@@ -70,4 +73,9 @@ public class GlobalScope {
|
||||
public GlobalScope(ObjectValue val) {
|
||||
this.obj = val;
|
||||
}
|
||||
|
||||
public static GlobalScope get(Extensions ext) {
|
||||
if (ext.has(KEY)) return ext.get(KEY);
|
||||
else return new GlobalScope();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package me.topchetoeu.jscript.runtime.scope;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.values.Values;
|
||||
|
||||
public class ValueVariable implements Variable {
|
||||
@@ -11,14 +11,14 @@ public class ValueVariable implements Variable {
|
||||
public boolean readonly() { return readonly; }
|
||||
|
||||
@Override
|
||||
public Object get(Context ctx) {
|
||||
public Object get(Extensions ext) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(Context ctx, Object val) {
|
||||
public void set(Extensions ext, Object val) {
|
||||
if (readonly) return;
|
||||
this.value = Values.normalize(ctx, val);
|
||||
this.value = Values.normalize(ext, val);
|
||||
}
|
||||
|
||||
public ValueVariable(boolean readonly, Object val) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package me.topchetoeu.jscript.runtime.scope;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
|
||||
public interface Variable {
|
||||
Object get(Context ctx);
|
||||
Object get(Extensions ext);
|
||||
default boolean readonly() { return true; }
|
||||
default void set(Context ctx, Object val) { }
|
||||
default void set(Extensions ext, Object val) { }
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
|
||||
// TODO: Make methods generic
|
||||
public class ArrayValue extends ObjectValue implements Iterable<Object> {
|
||||
@@ -41,12 +41,12 @@ public class ArrayValue extends ObjectValue implements Iterable<Object> {
|
||||
if (res == UNDEFINED) return null;
|
||||
else return res;
|
||||
}
|
||||
public void set(Context ctx, int i, Object val) {
|
||||
public void set(Extensions ext, int i, Object val) {
|
||||
if (i < 0) return;
|
||||
|
||||
values = alloc(i);
|
||||
|
||||
val = Values.normalize(ctx, val);
|
||||
val = Values.normalize(ext, val);
|
||||
if (val == null) val = UNDEFINED;
|
||||
values[i] = val;
|
||||
if (i >= size) size = i + 1;
|
||||
@@ -99,9 +99,9 @@ public class ArrayValue extends ObjectValue implements Iterable<Object> {
|
||||
}
|
||||
}
|
||||
|
||||
public void copyFrom(Context ctx, Object[] arr, int sourceStart, int destStart, int count) {
|
||||
public void copyFrom(Extensions ext, Object[] arr, int sourceStart, int destStart, int count) {
|
||||
for (var i = 0; i < count; i++) {
|
||||
set(ctx, i + destStart, arr[i + sourceStart]);
|
||||
set(ext, i + destStart, arr[i + sourceStart]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ public class ArrayValue extends ObjectValue implements Iterable<Object> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getField(Context ctx, Object key) {
|
||||
protected Object getField(Extensions ext, Object key) {
|
||||
if (key instanceof Number) {
|
||||
var i = ((Number)key).doubleValue();
|
||||
if (i >= 0 && i - Math.floor(i) == 0) {
|
||||
@@ -139,22 +139,22 @@ public class ArrayValue extends ObjectValue implements Iterable<Object> {
|
||||
}
|
||||
}
|
||||
|
||||
return super.getField(ctx, key);
|
||||
return super.getField(ext, key);
|
||||
}
|
||||
@Override
|
||||
protected boolean setField(Context ctx, Object key, Object val) {
|
||||
protected boolean setField(Extensions ext, Object key, Object val) {
|
||||
if (key instanceof Number) {
|
||||
var i = Values.number(key);
|
||||
if (i >= 0 && i - Math.floor(i) == 0) {
|
||||
set(ctx, (int)i, val);
|
||||
set(ext, (int)i, val);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return super.setField(ctx, key, val);
|
||||
return super.setField(ext, key, val);
|
||||
}
|
||||
@Override
|
||||
protected boolean hasField(Context ctx, Object key) {
|
||||
protected boolean hasField(Extensions ext, Object key) {
|
||||
if (key instanceof Number) {
|
||||
var i = Values.number(key);
|
||||
if (i >= 0 && i - Math.floor(i) == 0) {
|
||||
@@ -162,10 +162,10 @@ public class ArrayValue extends ObjectValue implements Iterable<Object> {
|
||||
}
|
||||
}
|
||||
|
||||
return super.hasField(ctx, key);
|
||||
return super.hasField(ext, key);
|
||||
}
|
||||
@Override
|
||||
protected void deleteField(Context ctx, Object key) {
|
||||
protected void deleteField(Extensions ext, Object key) {
|
||||
if (key instanceof Number) {
|
||||
var i = Values.number(key);
|
||||
if (i >= 0 && i - Math.floor(i) == 0) {
|
||||
@@ -174,7 +174,7 @@ public class ArrayValue extends ObjectValue implements Iterable<Object> {
|
||||
}
|
||||
}
|
||||
|
||||
super.deleteField(ctx, key);
|
||||
super.deleteField(ext, key);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -213,15 +213,15 @@ public class ArrayValue extends ObjectValue implements Iterable<Object> {
|
||||
values = new Object[cap];
|
||||
size = 0;
|
||||
}
|
||||
public ArrayValue(Context ctx, Object ...values) {
|
||||
public ArrayValue(Extensions ext, Object ...values) {
|
||||
this();
|
||||
this.values = new Object[values.length];
|
||||
size = values.length;
|
||||
|
||||
for (var i = 0; i < size; i++) this.values[i] = Values.normalize(ctx, values[i]);
|
||||
for (var i = 0; i < size; i++) this.values[i] = Values.normalize(ext, values[i]);
|
||||
}
|
||||
|
||||
public static ArrayValue of(Context ctx, Collection<?> values) {
|
||||
return new ArrayValue(ctx, values.toArray(Object[]::new));
|
||||
public static ArrayValue of(Extensions ext, Collection<?> values) {
|
||||
return new ArrayValue(ext, values.toArray(Object[]::new));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ package me.topchetoeu.jscript.runtime.values;
|
||||
|
||||
import me.topchetoeu.jscript.common.FunctionBody;
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Environment;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.Frame;
|
||||
import me.topchetoeu.jscript.runtime.scope.ValueVariable;
|
||||
|
||||
public class CodeFunction extends FunctionValue {
|
||||
public final FunctionBody body;
|
||||
public final ValueVariable[] captures;
|
||||
public Environment environment;
|
||||
public Extensions extensions;
|
||||
|
||||
// public Location loc() {
|
||||
// for (var instr : body.instructions) {
|
||||
@@ -25,8 +25,8 @@ public class CodeFunction extends FunctionValue {
|
||||
// }
|
||||
|
||||
@Override
|
||||
public Object call(Context ctx, Object thisArg, Object ...args) {
|
||||
var frame = new Frame(ctx, thisArg, args, this);
|
||||
public Object call(Extensions ext, Object thisArg, Object ...args) {
|
||||
var frame = new Frame(Context.of(ext), thisArg, args, this);
|
||||
|
||||
frame.onPush();
|
||||
|
||||
@@ -41,10 +41,10 @@ public class CodeFunction extends FunctionValue {
|
||||
}
|
||||
}
|
||||
|
||||
public CodeFunction(Environment environment, String name, FunctionBody body, ValueVariable[] captures) {
|
||||
public CodeFunction(Extensions extensions, String name, FunctionBody body, ValueVariable[] captures) {
|
||||
super(name, body.argsN);
|
||||
this.captures = captures;
|
||||
this.environment = environment;
|
||||
this.extensions = Context.clean(extensions);
|
||||
this.body = body;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package me.topchetoeu.jscript.runtime.values;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
|
||||
public abstract class FunctionValue extends ObjectValue {
|
||||
public String name = "";
|
||||
@@ -13,29 +13,29 @@ public abstract class FunctionValue extends ObjectValue {
|
||||
return String.format("function %s(...)", name);
|
||||
}
|
||||
|
||||
public abstract Object call(Context ctx, Object thisArg, Object ...args);
|
||||
public Object call(Context ctx) {
|
||||
return call(ctx, null);
|
||||
public abstract Object call(Extensions ext, Object thisArg, Object ...args);
|
||||
public Object call(Extensions ext) {
|
||||
return call(ext, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getField(Context ctx, Object key) {
|
||||
protected Object getField(Extensions ext, Object key) {
|
||||
if ("name".equals(key)) return name;
|
||||
if ("length".equals(key)) return length;
|
||||
return super.getField(ctx, key);
|
||||
return super.getField(ext, key);
|
||||
}
|
||||
@Override
|
||||
protected boolean setField(Context ctx, Object key, Object val) {
|
||||
if ("name".equals(key)) name = Values.toString(ctx, val);
|
||||
else if ("length".equals(key)) length = (int)Values.toNumber(ctx, val);
|
||||
else return super.setField(ctx, key, val);
|
||||
protected boolean setField(Extensions ext, Object key, Object val) {
|
||||
if ("name".equals(key)) name = Values.toString(ext, val);
|
||||
else if ("length".equals(key)) length = (int)Values.toNumber(ext, val);
|
||||
else return super.setField(ext, key, val);
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
protected boolean hasField(Context ctx, Object key) {
|
||||
protected boolean hasField(Extensions ext, Object key) {
|
||||
if ("name".equals(key)) return true;
|
||||
if ("length".equals(key)) return true;
|
||||
return super.hasField(ctx, key);
|
||||
return super.hasField(ext, key);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.topchetoeu.jscript.runtime.values;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.utils.interop.Arguments;
|
||||
|
||||
public class NativeFunction extends FunctionValue {
|
||||
@@ -11,8 +12,8 @@ public class NativeFunction extends FunctionValue {
|
||||
public final NativeFunctionRunner action;
|
||||
|
||||
@Override
|
||||
public Object call(Context ctx, Object thisArg, Object ...args) {
|
||||
return action.run(new Arguments(ctx, thisArg, args));
|
||||
public Object call(Extensions ext, Object thisArg, Object ...args) {
|
||||
return action.run(new Arguments(Context.of(ext), thisArg, args));
|
||||
}
|
||||
|
||||
public NativeFunction(String name, NativeFunctionRunner action) {
|
||||
|
||||
@@ -1,15 +1,24 @@
|
||||
package me.topchetoeu.jscript.runtime.values;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.Key;
|
||||
import me.topchetoeu.jscript.utils.interop.NativeWrapperProvider;
|
||||
|
||||
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) return ctx.environment.wrappers.getProto(wrapped.getClass());
|
||||
else return super.getPrototype(ctx);
|
||||
public ObjectValue getPrototype(Extensions ext) {
|
||||
if (ext != null && prototype == NATIVE_PROTO) {
|
||||
var clazz = wrapped.getClass();
|
||||
var res = NativeWrapperProvider.get(ext).getProto(clazz);
|
||||
if (res != null) return res;
|
||||
}
|
||||
return super.getPrototype(ext);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -25,8 +34,25 @@ public class NativeWrapper extends ObjectValue {
|
||||
return wrapped.hashCode();
|
||||
}
|
||||
|
||||
public NativeWrapper(Object wrapped) {
|
||||
private NativeWrapper(Object wrapped) {
|
||||
this.wrapped = wrapped;
|
||||
prototype = NATIVE_PROTO;
|
||||
}
|
||||
|
||||
public static NativeWrapper of(Extensions exts, Object wrapped) {
|
||||
if (exts == null) return new NativeWrapper(wrapped);
|
||||
var wrappers = exts.get(WRAPPERS);
|
||||
|
||||
if (wrappers == null) {
|
||||
wrappers = new WeakHashMap<>();
|
||||
exts.add(WRAPPERS, wrappers);
|
||||
}
|
||||
|
||||
if (wrappers.containsKey(wrapped)) return wrappers.get(wrapped);
|
||||
|
||||
var res = new NativeWrapper(wrapped);
|
||||
wrappers.put(wrapped, res);
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Environment;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
|
||||
public class ObjectValue {
|
||||
public static enum PlaceholderProto {
|
||||
@@ -54,10 +54,10 @@ public class ObjectValue {
|
||||
public LinkedHashSet<Object> nonConfigurableSet = new LinkedHashSet<>();
|
||||
public LinkedHashSet<Object> nonEnumerableSet = new LinkedHashSet<>();
|
||||
|
||||
private Property getProperty(Context ctx, Object key) {
|
||||
private Property getProperty(Extensions ext, Object key) {
|
||||
if (properties.containsKey(key)) return properties.get(key);
|
||||
var proto = getPrototype(ctx);
|
||||
if (proto != null) return proto.getProperty(ctx, key);
|
||||
var proto = getPrototype(ext);
|
||||
if (proto != null) return proto.getProperty(ext, key);
|
||||
else return null;
|
||||
}
|
||||
|
||||
@@ -86,8 +86,8 @@ public class ObjectValue {
|
||||
state = State.FROZEN;
|
||||
}
|
||||
|
||||
public final boolean defineProperty(Context ctx, Object key, Object val, boolean writable, boolean configurable, boolean enumerable) {
|
||||
key = Values.normalize(ctx, key); val = Values.normalize(ctx, val);
|
||||
public final boolean defineProperty(Extensions ext, Object key, Object val, boolean writable, boolean configurable, boolean enumerable) {
|
||||
key = Values.normalize(ext, key); val = Values.normalize(ext, val);
|
||||
boolean reconfigured =
|
||||
writable != memberWritable(key) ||
|
||||
configurable != memberConfigurable(key) ||
|
||||
@@ -125,11 +125,11 @@ public class ObjectValue {
|
||||
values.put(key, val);
|
||||
return true;
|
||||
}
|
||||
public final boolean defineProperty(Context ctx, Object key, Object val) {
|
||||
return defineProperty(ctx, key, val, true, true, true);
|
||||
public final boolean defineProperty(Extensions ext, Object key, Object val) {
|
||||
return defineProperty(ext, key, val, true, true, true);
|
||||
}
|
||||
public final boolean defineProperty(Context ctx, Object key, FunctionValue getter, FunctionValue setter, boolean configurable, boolean enumerable) {
|
||||
key = Values.normalize(ctx, key);
|
||||
public final boolean defineProperty(Extensions ext, Object key, FunctionValue getter, FunctionValue setter, boolean configurable, boolean enumerable) {
|
||||
key = Values.normalize(ext, key);
|
||||
if (
|
||||
properties.containsKey(key) &&
|
||||
properties.get(key).getter == getter &&
|
||||
@@ -152,19 +152,19 @@ public class ObjectValue {
|
||||
return true;
|
||||
}
|
||||
|
||||
public ObjectValue getPrototype(Context ctx) {
|
||||
public ObjectValue getPrototype(Extensions ext) {
|
||||
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);
|
||||
if (prototype == ARR_PROTO) return ext.get(Environment.ARRAY_PROTO);
|
||||
if (prototype == FUNC_PROTO) return ext.get(Environment.FUNCTION_PROTO);
|
||||
if (prototype == ERR_PROTO) return ext.get(Environment.ERROR_PROTO);
|
||||
if (prototype == RANGE_ERR_PROTO) return ext.get(Environment.RANGE_ERR_PROTO);
|
||||
if (prototype == SYNTAX_ERR_PROTO) return ext.get(Environment.SYNTAX_ERR_PROTO);
|
||||
if (prototype == TYPE_ERR_PROTO) return ext.get(Environment.TYPE_ERR_PROTO);
|
||||
return ext.get(Environment.OBJECT_PROTO);
|
||||
}
|
||||
catch (NullPointerException e) { return null; }
|
||||
|
||||
return (ObjectValue)prototype;
|
||||
}
|
||||
public final boolean setPrototype(PlaceholderProto val) {
|
||||
if (!extensible()) return false;
|
||||
@@ -185,10 +185,10 @@ public class ObjectValue {
|
||||
* A method, used to get the value of a field. If a property is bound to
|
||||
* this key, but not a field, this method should return null.
|
||||
*/
|
||||
protected Object getField(Context ctx, Object key) {
|
||||
protected Object getField(Extensions ext, Object key) {
|
||||
if (values.containsKey(key)) return values.get(key);
|
||||
var proto = getPrototype(ctx);
|
||||
if (proto != null) return proto.getField(ctx, key);
|
||||
var proto = getPrototype(ext);
|
||||
if (proto != null) return proto.getField(ext, key);
|
||||
else return null;
|
||||
}
|
||||
/**
|
||||
@@ -196,9 +196,9 @@ public class ObjectValue {
|
||||
* bound to this key, a new field should be created with the given value
|
||||
* @return Whether or not the operation was successful
|
||||
*/
|
||||
protected boolean setField(Context ctx, Object key, Object val) {
|
||||
protected boolean setField(Extensions ext, Object key, Object val) {
|
||||
if (val instanceof FunctionValue && ((FunctionValue)val).name.equals("")) {
|
||||
((FunctionValue)val).name = Values.toString(ctx, key);
|
||||
((FunctionValue)val).name = Values.toString(ext, key);
|
||||
}
|
||||
|
||||
values.put(key, val);
|
||||
@@ -207,40 +207,40 @@ public class ObjectValue {
|
||||
/**
|
||||
* Deletes the field bound to the given key.
|
||||
*/
|
||||
protected void deleteField(Context ctx, Object key) {
|
||||
protected void deleteField(Extensions ext, Object key) {
|
||||
values.remove(key);
|
||||
}
|
||||
/**
|
||||
* Returns whether or not there is a field bound to the given key.
|
||||
* This must ignore properties
|
||||
*/
|
||||
protected boolean hasField(Context ctx, Object key) {
|
||||
protected boolean hasField(Extensions ext, Object key) {
|
||||
return values.containsKey(key);
|
||||
}
|
||||
|
||||
public final Object getMember(Context ctx, Object key, Object thisArg) {
|
||||
key = Values.normalize(ctx, key);
|
||||
public final Object getMember(Extensions ext, Object key, Object thisArg) {
|
||||
key = Values.normalize(ext, key);
|
||||
|
||||
if ("__proto__".equals(key)) {
|
||||
var res = getPrototype(ctx);
|
||||
var res = getPrototype(ext);
|
||||
return res == null ? Values.NULL : res;
|
||||
}
|
||||
|
||||
var prop = getProperty(ctx, key);
|
||||
var prop = getProperty(ext, key);
|
||||
|
||||
if (prop != null) {
|
||||
if (prop.getter == null) return null;
|
||||
else return prop.getter.call(ctx, Values.normalize(ctx, thisArg));
|
||||
else return prop.getter.call(ext, Values.normalize(ext, thisArg));
|
||||
}
|
||||
else return getField(ctx, key);
|
||||
else return getField(ext, key);
|
||||
}
|
||||
public final boolean setMember(Context ctx, Object key, Object val, Object thisArg, boolean onlyProps) {
|
||||
key = Values.normalize(ctx, key); val = Values.normalize(ctx, val);
|
||||
public final boolean setMember(Extensions ext, Object key, Object val, Object thisArg, boolean onlyProps) {
|
||||
key = Values.normalize(ext, key); val = Values.normalize(ext, val);
|
||||
|
||||
var prop = getProperty(ctx, key);
|
||||
var prop = getProperty(ext, key);
|
||||
if (prop != null) {
|
||||
if (prop.setter == null) return false;
|
||||
prop.setter.call(ctx, Values.normalize(ctx, thisArg), val);
|
||||
prop.setter.call(ext, Values.normalize(ext, thisArg), val);
|
||||
return true;
|
||||
}
|
||||
else if (onlyProps) return false;
|
||||
@@ -249,32 +249,32 @@ public class ObjectValue {
|
||||
values.put(key, val);
|
||||
return true;
|
||||
}
|
||||
else if ("__proto__".equals(key)) return setPrototype(ctx, val);
|
||||
else if ("__proto__".equals(key)) return setPrototype(ext, val);
|
||||
else if (nonWritableSet.contains(key)) return false;
|
||||
else return setField(ctx, key, val);
|
||||
else return setField(ext, key, val);
|
||||
}
|
||||
public final boolean hasMember(Context ctx, Object key, boolean own) {
|
||||
key = Values.normalize(ctx, key);
|
||||
public final boolean hasMember(Extensions ext, Object key, boolean own) {
|
||||
key = Values.normalize(ext, key);
|
||||
|
||||
if (key != null && "__proto__".equals(key)) return true;
|
||||
if (hasField(ctx, key)) return true;
|
||||
if (hasField(ext, key)) return true;
|
||||
if (properties.containsKey(key)) return true;
|
||||
if (own) return false;
|
||||
var proto = getPrototype(ctx);
|
||||
return proto != null && proto.hasMember(ctx, key, own);
|
||||
var proto = getPrototype(ext);
|
||||
return proto != null && proto.hasMember(ext, key, own);
|
||||
}
|
||||
public final boolean deleteMember(Context ctx, Object key) {
|
||||
key = Values.normalize(ctx, key);
|
||||
public final boolean deleteMember(Extensions ext, Object key) {
|
||||
key = Values.normalize(ext, key);
|
||||
|
||||
if (!memberConfigurable(key)) return false;
|
||||
properties.remove(key);
|
||||
nonWritableSet.remove(key);
|
||||
nonEnumerableSet.remove(key);
|
||||
deleteField(ctx, key);
|
||||
deleteField(ext, key);
|
||||
return true;
|
||||
}
|
||||
public final boolean setPrototype(Context ctx, Object val) {
|
||||
val = Values.normalize(ctx, val);
|
||||
public final boolean setPrototype(Extensions ext, Object val) {
|
||||
val = Values.normalize(ext, val);
|
||||
|
||||
if (!extensible()) return false;
|
||||
if (val == null || val == Values.NULL) {
|
||||
@@ -284,14 +284,14 @@ public class ObjectValue {
|
||||
else if (val instanceof ObjectValue) {
|
||||
var obj = (ObjectValue)val;
|
||||
|
||||
if (ctx != null) {
|
||||
if (obj == ctx.get(Environment.OBJECT_PROTO)) prototype = OBJ_PROTO;
|
||||
else if (obj == ctx.get(Environment.ARRAY_PROTO)) prototype = ARR_PROTO;
|
||||
else if (obj == ctx.get(Environment.FUNCTION_PROTO)) prototype = FUNC_PROTO;
|
||||
else if (obj == ctx.get(Environment.ERROR_PROTO)) prototype = ERR_PROTO;
|
||||
else if (obj == ctx.get(Environment.SYNTAX_ERR_PROTO)) prototype = SYNTAX_ERR_PROTO;
|
||||
else if (obj == ctx.get(Environment.TYPE_ERR_PROTO)) prototype = TYPE_ERR_PROTO;
|
||||
else if (obj == ctx.get(Environment.RANGE_ERR_PROTO)) prototype = RANGE_ERR_PROTO;
|
||||
if (ext != null) {
|
||||
if (obj == ext.get(Environment.OBJECT_PROTO)) prototype = OBJ_PROTO;
|
||||
else if (obj == ext.get(Environment.ARRAY_PROTO)) prototype = ARR_PROTO;
|
||||
else if (obj == ext.get(Environment.FUNCTION_PROTO)) prototype = FUNC_PROTO;
|
||||
else if (obj == ext.get(Environment.ERROR_PROTO)) prototype = ERR_PROTO;
|
||||
else if (obj == ext.get(Environment.SYNTAX_ERR_PROTO)) prototype = SYNTAX_ERR_PROTO;
|
||||
else if (obj == ext.get(Environment.TYPE_ERR_PROTO)) prototype = TYPE_ERR_PROTO;
|
||||
else if (obj == ext.get(Environment.RANGE_ERR_PROTO)) prototype = RANGE_ERR_PROTO;
|
||||
else prototype = obj;
|
||||
}
|
||||
else prototype = obj;
|
||||
@@ -301,22 +301,22 @@ public class ObjectValue {
|
||||
return false;
|
||||
}
|
||||
|
||||
public final ObjectValue getMemberDescriptor(Context ctx, Object key) {
|
||||
key = Values.normalize(ctx, key);
|
||||
public final ObjectValue getMemberDescriptor(Extensions ext, Object key) {
|
||||
key = Values.normalize(ext, key);
|
||||
|
||||
var prop = properties.get(key);
|
||||
var res = new ObjectValue();
|
||||
|
||||
res.defineProperty(ctx, "configurable", memberConfigurable(key));
|
||||
res.defineProperty(ctx, "enumerable", memberEnumerable(key));
|
||||
res.defineProperty(ext, "configurable", memberConfigurable(key));
|
||||
res.defineProperty(ext, "enumerable", memberEnumerable(key));
|
||||
|
||||
if (prop != null) {
|
||||
res.defineProperty(ctx, "get", prop.getter);
|
||||
res.defineProperty(ctx, "set", prop.setter);
|
||||
res.defineProperty(ext, "get", prop.getter);
|
||||
res.defineProperty(ext, "set", prop.setter);
|
||||
}
|
||||
else if (hasField(ctx, key)) {
|
||||
res.defineProperty(ctx, "value", values.get(key));
|
||||
res.defineProperty(ctx, "writable", memberWritable(key));
|
||||
else if (hasField(ext, key)) {
|
||||
res.defineProperty(ext, "value", values.get(key));
|
||||
res.defineProperty(ext, "writable", memberWritable(key));
|
||||
}
|
||||
else return null;
|
||||
return res;
|
||||
@@ -337,10 +337,10 @@ public class ObjectValue {
|
||||
return res;
|
||||
}
|
||||
|
||||
public ObjectValue(Context ctx, Map<?, ?> values) {
|
||||
public ObjectValue(Extensions ext, Map<?, ?> values) {
|
||||
this(PlaceholderProto.OBJECT);
|
||||
for (var el : values.entrySet()) {
|
||||
defineProperty(ctx, el.getKey(), el.getValue());
|
||||
defineProperty(ext, el.getKey(), el.getValue());
|
||||
}
|
||||
}
|
||||
public ObjectValue(PlaceholderProto proto) {
|
||||
|
||||
@@ -3,7 +3,7 @@ package me.topchetoeu.jscript.runtime.values;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.scope.ValueVariable;
|
||||
|
||||
public class ScopeValue extends ObjectValue {
|
||||
@@ -11,31 +11,31 @@ public class ScopeValue extends ObjectValue {
|
||||
public final HashMap<String, Integer> names = new HashMap<>();
|
||||
|
||||
@Override
|
||||
protected Object getField(Context ctx, Object key) {
|
||||
if (names.containsKey(key)) return variables[names.get(key)].get(ctx);
|
||||
return super.getField(ctx, key);
|
||||
protected Object getField(Extensions ext, Object key) {
|
||||
if (names.containsKey(key)) return variables[names.get(key)].get(ext);
|
||||
return super.getField(ext, key);
|
||||
}
|
||||
@Override
|
||||
protected boolean setField(Context ctx, Object key, Object val) {
|
||||
protected boolean setField(Extensions ext, Object key, Object val) {
|
||||
if (names.containsKey(key)) {
|
||||
variables[names.get(key)].set(ctx, val);
|
||||
variables[names.get(key)].set(ext, val);
|
||||
return true;
|
||||
}
|
||||
|
||||
var proto = getPrototype(ctx);
|
||||
if (proto != null && proto.hasMember(ctx, key, false) && proto.setField(ctx, key, val)) return true;
|
||||
var proto = getPrototype(ext);
|
||||
if (proto != null && proto.hasMember(ext, key, false) && proto.setField(ext, key, val)) return true;
|
||||
|
||||
return super.setField(ctx, key, val);
|
||||
return super.setField(ext, key, val);
|
||||
}
|
||||
@Override
|
||||
protected void deleteField(Context ctx, Object key) {
|
||||
protected void deleteField(Extensions ext, Object key) {
|
||||
if (names.containsKey(key)) return;
|
||||
super.deleteField(ctx, key);
|
||||
super.deleteField(ext, key);
|
||||
}
|
||||
@Override
|
||||
protected boolean hasField(Context ctx, Object key) {
|
||||
protected boolean hasField(Extensions ext, Object key) {
|
||||
if (names.containsKey(key)) return true;
|
||||
return super.hasField(ctx, key);
|
||||
return super.hasField(ext, key);
|
||||
}
|
||||
@Override
|
||||
public List<Object> keys(boolean includeNonEnumerable) {
|
||||
|
||||
@@ -14,12 +14,13 @@ import java.util.Map;
|
||||
|
||||
import me.topchetoeu.jscript.common.Operation;
|
||||
import me.topchetoeu.jscript.lib.PromiseLib;
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Environment;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
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;
|
||||
import me.topchetoeu.jscript.utils.interop.NativeWrapperProvider;
|
||||
|
||||
public class Values {
|
||||
public static enum CompareResult {
|
||||
@@ -58,9 +59,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;
|
||||
}
|
||||
|
||||
@@ -74,11 +74,11 @@ public class Values {
|
||||
return "object";
|
||||
}
|
||||
|
||||
private static Object tryCallConvertFunc(Context ctx, Object obj, String name) {
|
||||
var func = getMember(ctx, obj, name);
|
||||
private static Object tryCallConvertFunc(Extensions ext, Object obj, String name) {
|
||||
var func = getMember(ext, obj, name);
|
||||
|
||||
if (func instanceof FunctionValue) {
|
||||
var res = Values.call(ctx, func, obj);
|
||||
var res = Values.call(ext, func, obj);
|
||||
if (isPrimitive(res)) return res;
|
||||
}
|
||||
|
||||
@@ -95,16 +95,16 @@ public class Values {
|
||||
obj == NULL;
|
||||
}
|
||||
|
||||
public static Object toPrimitive(Context ctx, Object obj, ConvertHint hint) {
|
||||
obj = normalize(ctx, obj);
|
||||
public static Object toPrimitive(Extensions ext, Object obj, ConvertHint hint) {
|
||||
obj = normalize(ext, obj);
|
||||
if (isPrimitive(obj)) return obj;
|
||||
|
||||
var first = hint == ConvertHint.VALUEOF ? "valueOf" : "toString";
|
||||
var second = hint == ConvertHint.VALUEOF ? "toString" : "valueOf";
|
||||
|
||||
if (ctx != null) {
|
||||
try { return tryCallConvertFunc(ctx, obj, first); }
|
||||
catch (EngineException unused) { return tryCallConvertFunc(ctx, obj, second); }
|
||||
if (ext != null) {
|
||||
try { return tryCallConvertFunc(ext, obj, first); }
|
||||
catch (EngineException unused) { return tryCallConvertFunc(ext, obj, second); }
|
||||
}
|
||||
|
||||
throw EngineException.ofType("Value couldn't be converted to a primitive.");
|
||||
@@ -116,8 +116,8 @@ public class Values {
|
||||
if (obj instanceof Boolean) return (Boolean)obj;
|
||||
return true;
|
||||
}
|
||||
public static double toNumber(Context ctx, Object obj) {
|
||||
var val = toPrimitive(ctx, obj, ConvertHint.VALUEOF);
|
||||
public static double toNumber(Extensions ext, Object obj) {
|
||||
var val = toPrimitive(ext, obj, ConvertHint.VALUEOF);
|
||||
|
||||
if (val instanceof Number) return number(val);
|
||||
if (val instanceof Boolean) return ((Boolean)val) ? 1 : 0;
|
||||
@@ -127,8 +127,8 @@ public class Values {
|
||||
}
|
||||
return Double.NaN;
|
||||
}
|
||||
public static String toString(Context ctx, Object obj) {
|
||||
var val = toPrimitive(ctx, obj, ConvertHint.VALUEOF);
|
||||
public static String toString(Extensions ext, Object obj) {
|
||||
var val = toPrimitive(ext, obj, ConvertHint.VALUEOF);
|
||||
|
||||
if (val == null) return "undefined";
|
||||
if (val == NULL) return "null";
|
||||
@@ -147,63 +147,63 @@ public class Values {
|
||||
return "Unknown value";
|
||||
}
|
||||
|
||||
public static Object add(Context ctx, Object a, Object b) {
|
||||
if (a instanceof String || b instanceof String) return toString(ctx, a) + toString(ctx, b);
|
||||
else return toNumber(ctx, a) + toNumber(ctx, b);
|
||||
public static Object add(Extensions ext, Object a, Object b) {
|
||||
if (a instanceof String || b instanceof String) return toString(ext, a) + toString(ext, b);
|
||||
else return toNumber(ext, a) + toNumber(ext, b);
|
||||
}
|
||||
public static double subtract(Context ctx, Object a, Object b) {
|
||||
return toNumber(ctx, a) - toNumber(ctx, b);
|
||||
public static double subtract(Extensions ext, Object a, Object b) {
|
||||
return toNumber(ext, a) - toNumber(ext, b);
|
||||
}
|
||||
public static double multiply(Context ctx, Object a, Object b) {
|
||||
return toNumber(ctx, a) * toNumber(ctx, b);
|
||||
public static double multiply(Extensions ext, Object a, Object b) {
|
||||
return toNumber(ext, a) * toNumber(ext, b);
|
||||
}
|
||||
public static double divide(Context ctx, Object a, Object b) {
|
||||
return toNumber(ctx, a) / toNumber(ctx, b);
|
||||
public static double divide(Extensions ext, Object a, Object b) {
|
||||
return toNumber(ext, a) / toNumber(ext, b);
|
||||
}
|
||||
public static double modulo(Context ctx, Object a, Object b) {
|
||||
return toNumber(ctx, a) % toNumber(ctx, b);
|
||||
public static double modulo(Extensions ext, Object a, Object b) {
|
||||
return toNumber(ext, a) % toNumber(ext, b);
|
||||
}
|
||||
|
||||
public static double negative(Context ctx, Object obj) {
|
||||
return -toNumber(ctx, obj);
|
||||
public static double negative(Extensions ext, Object obj) {
|
||||
return -toNumber(ext, obj);
|
||||
}
|
||||
|
||||
public static int and(Context ctx, Object a, Object b) {
|
||||
return (int)toNumber(ctx, a) & (int)toNumber(ctx, b);
|
||||
public static int and(Extensions ext, Object a, Object b) {
|
||||
return (int)toNumber(ext, a) & (int)toNumber(ext, b);
|
||||
}
|
||||
public static int or(Context ctx, Object a, Object b) {
|
||||
return (int)toNumber(ctx, a) | (int)toNumber(ctx, b);
|
||||
public static int or(Extensions ext, Object a, Object b) {
|
||||
return (int)toNumber(ext, a) | (int)toNumber(ext, b);
|
||||
}
|
||||
public static int xor(Context ctx, Object a, Object b) {
|
||||
return (int)toNumber(ctx, a) ^ (int)toNumber(ctx, b);
|
||||
public static int xor(Extensions ext, Object a, Object b) {
|
||||
return (int)toNumber(ext, a) ^ (int)toNumber(ext, b);
|
||||
}
|
||||
public static int bitwiseNot(Context ctx, Object obj) {
|
||||
return ~(int)toNumber(ctx, obj);
|
||||
public static int bitwiseNot(Extensions ext, Object obj) {
|
||||
return ~(int)toNumber(ext, obj);
|
||||
}
|
||||
|
||||
public static int shiftLeft(Context ctx, Object a, Object b) {
|
||||
return (int)toNumber(ctx, a) << (int)toNumber(ctx, b);
|
||||
public static int shiftLeft(Extensions ext, Object a, Object b) {
|
||||
return (int)toNumber(ext, a) << (int)toNumber(ext, b);
|
||||
}
|
||||
public static int shiftRight(Context ctx, Object a, Object b) {
|
||||
return (int)toNumber(ctx, a) >> (int)toNumber(ctx, b);
|
||||
public static int shiftRight(Extensions ext, Object a, Object b) {
|
||||
return (int)toNumber(ext, a) >> (int)toNumber(ext, b);
|
||||
}
|
||||
public static long unsignedShiftRight(Context ctx, Object a, Object b) {
|
||||
long _a = (long)toNumber(ctx, a);
|
||||
long _b = (long)toNumber(ctx, b);
|
||||
public static long unsignedShiftRight(Extensions ext, Object a, Object b) {
|
||||
long _a = (long)toNumber(ext, a);
|
||||
long _b = (long)toNumber(ext, b);
|
||||
|
||||
if (_a < 0) _a += 0x100000000l;
|
||||
if (_b < 0) _b += 0x100000000l;
|
||||
return _a >>> _b;
|
||||
}
|
||||
|
||||
public static CompareResult compare(Context ctx, Object a, Object b) {
|
||||
a = toPrimitive(ctx, a, ConvertHint.VALUEOF);
|
||||
b = toPrimitive(ctx, b, ConvertHint.VALUEOF);
|
||||
public static CompareResult compare(Extensions ext, Object a, Object b) {
|
||||
a = toPrimitive(ext, a, ConvertHint.VALUEOF);
|
||||
b = toPrimitive(ext, b, ConvertHint.VALUEOF);
|
||||
|
||||
if (a instanceof String && b instanceof String) CompareResult.from(((String)a).compareTo((String)b));
|
||||
|
||||
var _a = toNumber(ctx, a);
|
||||
var _b = toNumber(ctx, b);
|
||||
var _a = toNumber(ext, a);
|
||||
var _b = toNumber(ext, b);
|
||||
|
||||
if (Double.isNaN(_a) || Double.isNaN(_b)) return CompareResult.NOT_EQUAL;
|
||||
|
||||
@@ -214,60 +214,60 @@ public class Values {
|
||||
return !toBoolean(obj);
|
||||
}
|
||||
|
||||
public static boolean isInstanceOf(Context ctx, Object obj, Object proto) {
|
||||
public static boolean isInstanceOf(Extensions ext, Object obj, Object proto) {
|
||||
if (obj == null || obj == NULL || proto == null || proto == NULL) return false;
|
||||
var val = getPrototype(ctx, obj);
|
||||
var val = getPrototype(ext, obj);
|
||||
|
||||
while (val != null) {
|
||||
if (val.equals(proto)) return true;
|
||||
val = val.getPrototype(ctx);
|
||||
val = val.getPrototype(ext);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Object operation(Context ctx, Operation op, Object ...args) {
|
||||
public static Object operation(Extensions ext, Operation op, Object ...args) {
|
||||
switch (op) {
|
||||
case ADD: return add(ctx, args[0], args[1]);
|
||||
case SUBTRACT: return subtract(ctx, args[0], args[1]);
|
||||
case DIVIDE: return divide(ctx, args[0], args[1]);
|
||||
case MULTIPLY: return multiply(ctx, args[0], args[1]);
|
||||
case MODULO: return modulo(ctx, args[0], args[1]);
|
||||
case ADD: return add(ext, args[0], args[1]);
|
||||
case SUBTRACT: return subtract(ext, args[0], args[1]);
|
||||
case DIVIDE: return divide(ext, args[0], args[1]);
|
||||
case MULTIPLY: return multiply(ext, args[0], args[1]);
|
||||
case MODULO: return modulo(ext, args[0], args[1]);
|
||||
|
||||
case AND: return and(ctx, args[0], args[1]);
|
||||
case OR: return or(ctx, args[0], args[1]);
|
||||
case XOR: return xor(ctx, args[0], args[1]);
|
||||
case AND: return and(ext, args[0], args[1]);
|
||||
case OR: return or(ext, args[0], args[1]);
|
||||
case XOR: return xor(ext, args[0], args[1]);
|
||||
|
||||
case EQUALS: return strictEquals(ctx, args[0], args[1]);
|
||||
case NOT_EQUALS: return !strictEquals(ctx, args[0], args[1]);
|
||||
case LOOSE_EQUALS: return looseEqual(ctx, args[0], args[1]);
|
||||
case LOOSE_NOT_EQUALS: return !looseEqual(ctx, args[0], args[1]);
|
||||
case EQUALS: return strictEquals(ext, args[0], args[1]);
|
||||
case NOT_EQUALS: return !strictEquals(ext, args[0], args[1]);
|
||||
case LOOSE_EQUALS: return looseEqual(ext, args[0], args[1]);
|
||||
case LOOSE_NOT_EQUALS: return !looseEqual(ext, args[0], args[1]);
|
||||
|
||||
case GREATER: return compare(ctx, args[0], args[1]).greater();
|
||||
case GREATER_EQUALS: return compare(ctx, args[0], args[1]).greaterOrEqual();
|
||||
case LESS: return compare(ctx, args[0], args[1]).less();
|
||||
case LESS_EQUALS: return compare(ctx, args[0], args[1]).lessOrEqual();
|
||||
case GREATER: return compare(ext, args[0], args[1]).greater();
|
||||
case GREATER_EQUALS: return compare(ext, args[0], args[1]).greaterOrEqual();
|
||||
case LESS: return compare(ext, args[0], args[1]).less();
|
||||
case LESS_EQUALS: return compare(ext, args[0], args[1]).lessOrEqual();
|
||||
|
||||
case INVERSE: return bitwiseNot(ctx, args[0]);
|
||||
case INVERSE: return bitwiseNot(ext, args[0]);
|
||||
case NOT: return not(args[0]);
|
||||
case POS: return toNumber(ctx, args[0]);
|
||||
case NEG: return negative(ctx, args[0]);
|
||||
case POS: return toNumber(ext, args[0]);
|
||||
case NEG: return negative(ext, args[0]);
|
||||
|
||||
case SHIFT_LEFT: return shiftLeft(ctx, args[0], args[1]);
|
||||
case SHIFT_RIGHT: return shiftRight(ctx, args[0], args[1]);
|
||||
case USHIFT_RIGHT: return unsignedShiftRight(ctx, args[0], args[1]);
|
||||
case SHIFT_LEFT: return shiftLeft(ext, args[0], args[1]);
|
||||
case SHIFT_RIGHT: return shiftRight(ext, args[0], args[1]);
|
||||
case USHIFT_RIGHT: return unsignedShiftRight(ext, args[0], args[1]);
|
||||
|
||||
case IN: return hasMember(ctx, args[1], args[0], false);
|
||||
case IN: return hasMember(ext, args[1], args[0], false);
|
||||
case INSTANCEOF: {
|
||||
var proto = getMember(ctx, args[1], "prototype");
|
||||
return isInstanceOf(ctx, args[0], proto);
|
||||
var proto = getMember(ext, args[1], "prototype");
|
||||
return isInstanceOf(ext, args[0], proto);
|
||||
}
|
||||
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object getMember(Context ctx, Object obj, Object key) {
|
||||
public static Object getMember(Extensions ctx, Object obj, Object key) {
|
||||
obj = normalize(ctx, obj); key = normalize(ctx, key);
|
||||
if (obj == null) throw new IllegalArgumentException("Tried to access member of undefined.");
|
||||
if (obj == NULL) throw new IllegalArgumentException("Tried to access member of null.");
|
||||
@@ -287,12 +287,12 @@ public class Values {
|
||||
else if (key != null && "__proto__".equals(key)) return proto;
|
||||
else return proto.getMember(ctx, key, obj);
|
||||
}
|
||||
public static Object getMemberPath(Context ctx, Object obj, Object ...path) {
|
||||
public static Object getMemberPath(Extensions ctx, Object obj, Object ...path) {
|
||||
var res = obj;
|
||||
for (var key : path) res = getMember(ctx, res, key);
|
||||
return res;
|
||||
}
|
||||
public static boolean setMember(Context ctx, Object obj, Object key, Object val) {
|
||||
public static boolean setMember(Extensions ctx, Object obj, Object key, Object val) {
|
||||
obj = normalize(ctx, obj); key = normalize(ctx, key); val = normalize(ctx, val);
|
||||
if (obj == null) throw EngineException.ofType("Tried to access member of undefined.");
|
||||
if (obj == NULL) throw EngineException.ofType("Tried to access member of null.");
|
||||
@@ -302,7 +302,7 @@ public class Values {
|
||||
var proto = getPrototype(ctx, obj);
|
||||
return proto.setMember(ctx, key, val, obj, true);
|
||||
}
|
||||
public static boolean hasMember(Context ctx, Object obj, Object key, boolean own) {
|
||||
public static boolean hasMember(Extensions ctx, Object obj, Object key, boolean own) {
|
||||
if (obj == null || obj == NULL) return false;
|
||||
obj = normalize(ctx, obj); key = normalize(ctx, key);
|
||||
|
||||
@@ -320,36 +320,36 @@ public class Values {
|
||||
var proto = getPrototype(ctx, obj);
|
||||
return proto != null && proto.hasMember(ctx, key, own);
|
||||
}
|
||||
public static boolean deleteMember(Context ctx, Object obj, Object key) {
|
||||
public static boolean deleteMember(Extensions ext, Object obj, Object key) {
|
||||
if (obj == null || obj == NULL) return false;
|
||||
obj = normalize(ctx, obj); key = normalize(ctx, key);
|
||||
obj = normalize(ext, obj); key = normalize(ext, key);
|
||||
|
||||
if (obj instanceof ObjectValue) return ((ObjectValue)obj).deleteMember(ctx, key);
|
||||
if (obj instanceof ObjectValue) return ((ObjectValue)obj).deleteMember(ext, key);
|
||||
else return false;
|
||||
}
|
||||
public static ObjectValue getPrototype(Context ctx, Object obj) {
|
||||
public static ObjectValue getPrototype(Extensions ext, Object obj) {
|
||||
if (obj == null || obj == NULL) return null;
|
||||
obj = normalize(ctx, obj);
|
||||
if (obj instanceof ObjectValue) return ((ObjectValue)obj).getPrototype(ctx);
|
||||
if (ctx == null) return null;
|
||||
obj = normalize(ext, obj);
|
||||
if (obj instanceof ObjectValue) return ((ObjectValue)obj).getPrototype(ext);
|
||||
if (ext == null) return null;
|
||||
|
||||
if (obj instanceof String) return ctx.get(Environment.STRING_PROTO);
|
||||
else if (obj instanceof Number) return ctx.get(Environment.NUMBER_PROTO);
|
||||
else if (obj instanceof Boolean) return ctx.get(Environment.BOOL_PROTO);
|
||||
else if (obj instanceof Symbol) return ctx.get(Environment.SYMBOL_PROTO);
|
||||
if (obj instanceof String) return ext.get(Environment.STRING_PROTO);
|
||||
else if (obj instanceof Number) return ext.get(Environment.NUMBER_PROTO);
|
||||
else if (obj instanceof Boolean) return ext.get(Environment.BOOL_PROTO);
|
||||
else if (obj instanceof Symbol) return ext.get(Environment.SYMBOL_PROTO);
|
||||
|
||||
return null;
|
||||
}
|
||||
public static boolean setPrototype(Context ctx, Object obj, Object proto) {
|
||||
obj = normalize(ctx, obj);
|
||||
return obj instanceof ObjectValue && ((ObjectValue)obj).setPrototype(ctx, proto);
|
||||
public static boolean setPrototype(Extensions ext, Object obj, Object proto) {
|
||||
obj = normalize(ext, obj);
|
||||
return obj instanceof ObjectValue && ((ObjectValue)obj).setPrototype(ext, proto);
|
||||
}
|
||||
public static void makePrototypeChain(Context ctx, Object... chain) {
|
||||
public static void makePrototypeChain(Extensions ext, Object... chain) {
|
||||
for(var i = 1; i < chain.length; i++) {
|
||||
setPrototype(ctx, chain[i], chain[i - 1]);
|
||||
setPrototype(ext, chain[i], chain[i - 1]);
|
||||
}
|
||||
}
|
||||
public static List<Object> getMembers(Context ctx, Object obj, boolean own, boolean includeNonEnumerable) {
|
||||
public static List<Object> getMembers(Extensions ext, Object obj, boolean own, boolean includeNonEnumerable) {
|
||||
List<Object> res = new ArrayList<>();
|
||||
|
||||
if (obj instanceof ObjectValue) res = ((ObjectValue)obj).keys(includeNonEnumerable);
|
||||
@@ -358,26 +358,26 @@ public class Values {
|
||||
}
|
||||
|
||||
if (!own) {
|
||||
var proto = getPrototype(ctx, obj);
|
||||
var proto = getPrototype(ext, obj);
|
||||
|
||||
while (proto != null) {
|
||||
res.addAll(proto.keys(includeNonEnumerable));
|
||||
proto = getPrototype(ctx, proto);
|
||||
proto = getPrototype(ext, proto);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
public static ObjectValue getMemberDescriptor(Context ctx, Object obj, Object key) {
|
||||
if (obj instanceof ObjectValue) return ((ObjectValue)obj).getMemberDescriptor(ctx, key);
|
||||
public static ObjectValue getMemberDescriptor(Extensions ext, Object obj, Object key) {
|
||||
if (obj instanceof ObjectValue) return ((ObjectValue)obj).getMemberDescriptor(ext, key);
|
||||
else if (obj instanceof String && key instanceof Number) {
|
||||
var i = ((Number)key).intValue();
|
||||
var _i = ((Number)key).doubleValue();
|
||||
if (i - _i != 0) return null;
|
||||
if (i < 0 || i >= ((String)obj).length()) return null;
|
||||
|
||||
return new ObjectValue(ctx, Map.of(
|
||||
return new ObjectValue(ext, Map.of(
|
||||
"value", ((String)obj).charAt(i) + "",
|
||||
"writable", false,
|
||||
"enumerable", true,
|
||||
@@ -387,17 +387,17 @@ public class Values {
|
||||
else return null;
|
||||
}
|
||||
|
||||
public static Object call(Context ctx, Object func, Object thisArg, Object ...args) {
|
||||
public static Object call(Extensions ext, Object func, Object thisArg, Object ...args) {
|
||||
if (!(func instanceof FunctionValue)) throw EngineException.ofType("Tried to call a non-function value.");
|
||||
return ((FunctionValue)func).call(ctx, thisArg, args);
|
||||
return ((FunctionValue)func).call(ext, thisArg, args);
|
||||
}
|
||||
public static Object callNew(Context ctx, Object func, Object ...args) {
|
||||
public static Object callNew(Extensions ext, Object func, Object ...args) {
|
||||
var res = new ObjectValue();
|
||||
try {
|
||||
var proto = Values.getMember(ctx, func, "prototype");
|
||||
setPrototype(ctx, res, proto);
|
||||
var proto = Values.getMember(ext, func, "prototype");
|
||||
setPrototype(ext, res, proto);
|
||||
|
||||
var ret = call(ctx, func, res, args);
|
||||
var ret = call(ext, func, res, args);
|
||||
|
||||
if (!isPrimitive(ret)) return ret;
|
||||
return res;
|
||||
@@ -407,8 +407,9 @@ public class Values {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean strictEquals(Context ctx, Object a, Object b) {
|
||||
a = normalize(ctx, a); b = normalize(ctx, b);
|
||||
public static boolean strictEquals(Extensions ext, Object a, Object b) {
|
||||
a = normalize(ext, a);
|
||||
b = normalize(ext, b);
|
||||
|
||||
if (a == null || b == null) return a == null && b == null;
|
||||
if (isNan(a) || isNan(b)) return false;
|
||||
@@ -417,8 +418,8 @@ public class Values {
|
||||
|
||||
return a == b || a.equals(b);
|
||||
}
|
||||
public static boolean looseEqual(Context ctx, Object a, Object b) {
|
||||
a = normalize(ctx, a); b = normalize(ctx, b);
|
||||
public static boolean looseEqual(Extensions ext, Object a, Object b) {
|
||||
a = normalize(ext, a); b = normalize(ext, b);
|
||||
|
||||
// In loose equality, null is equivalent to undefined
|
||||
if (a == NULL) a = null;
|
||||
@@ -429,19 +430,19 @@ public class Values {
|
||||
if (!isPrimitive(a) && !isPrimitive(b)) return a == b;
|
||||
|
||||
// Convert values to primitives
|
||||
a = toPrimitive(ctx, a, ConvertHint.VALUEOF);
|
||||
b = toPrimitive(ctx, b, ConvertHint.VALUEOF);
|
||||
a = toPrimitive(ext, a, ConvertHint.VALUEOF);
|
||||
b = toPrimitive(ext, b, ConvertHint.VALUEOF);
|
||||
|
||||
// Compare symbols by reference
|
||||
if (a instanceof Symbol || b instanceof Symbol) return a == b;
|
||||
if (a instanceof Boolean || b instanceof Boolean) return toBoolean(a) == toBoolean(b);
|
||||
if (a instanceof Number || b instanceof Number) return strictEquals(ctx, toNumber(ctx, a), toNumber(ctx, b));
|
||||
if (a instanceof Number || b instanceof Number) return strictEquals(ext, toNumber(ext, a), toNumber(ext, b));
|
||||
|
||||
// Default to strings
|
||||
return toString(ctx, a).equals(toString(ctx, b));
|
||||
return toString(ext, a).equals(toString(ext, b));
|
||||
}
|
||||
|
||||
public static Object normalize(Context ctx, Object val) {
|
||||
public static Object normalize(Extensions ext, Object val) {
|
||||
if (val instanceof Number) return number(val);
|
||||
if (isPrimitive(val) || val instanceof ObjectValue) return val;
|
||||
if (val instanceof Character) return val + "";
|
||||
@@ -450,7 +451,7 @@ public class Values {
|
||||
var res = new ObjectValue();
|
||||
|
||||
for (var entry : ((Map<?, ?>)val).entrySet()) {
|
||||
res.defineProperty(ctx, entry.getKey(), entry.getValue());
|
||||
res.defineProperty(ext, entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
return res;
|
||||
@@ -460,22 +461,22 @@ public class Values {
|
||||
var res = new ArrayValue();
|
||||
|
||||
for (var entry : ((Iterable<?>)val)) {
|
||||
res.set(ctx, res.size(), entry);
|
||||
res.set(ext, res.size(), entry);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
if (val instanceof Class) {
|
||||
if (ctx == null) return null;
|
||||
else return ctx.environment.wrappers.getConstr((Class<?>)val);
|
||||
if (ext == null) return null;
|
||||
else return NativeWrapperProvider.get(ext).getConstr((Class<?>)val);
|
||||
}
|
||||
|
||||
return new NativeWrapper(val);
|
||||
return NativeWrapper.of(ext, val);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T convert(Context ctx, Object obj, Class<T> clazz) {
|
||||
public static <T> T convert(Extensions ext, Object obj, Class<T> clazz) {
|
||||
if (clazz == Void.class) return null;
|
||||
|
||||
if (obj instanceof NativeWrapper) {
|
||||
@@ -489,19 +490,19 @@ public class Values {
|
||||
if (clazz.isAssignableFrom(ArrayList.class)) {
|
||||
var raw = ((ArrayValue)obj).toArray();
|
||||
var res = new ArrayList<>();
|
||||
for (var i = 0; i < raw.length; i++) res.add(convert(ctx, raw[i], Object.class));
|
||||
for (var i = 0; i < raw.length; i++) res.add(convert(ext, raw[i], Object.class));
|
||||
return (T)new ArrayList<>(res);
|
||||
}
|
||||
if (clazz.isAssignableFrom(HashSet.class)) {
|
||||
var raw = ((ArrayValue)obj).toArray();
|
||||
var res = new HashSet<>();
|
||||
for (var i = 0; i < raw.length; i++) res.add(convert(ctx, raw[i], Object.class));
|
||||
for (var i = 0; i < raw.length; i++) res.add(convert(ext, raw[i], Object.class));
|
||||
return (T)new HashSet<>(res);
|
||||
}
|
||||
if (clazz.isArray()) {
|
||||
var raw = ((ArrayValue)obj).toArray();
|
||||
Object res = Array.newInstance(clazz.getComponentType(), raw.length);
|
||||
for (var i = 0; i < raw.length; i++) Array.set(res, i, convert(ctx, raw[i], Object.class));
|
||||
for (var i = 0; i < raw.length; i++) Array.set(res, i, convert(ext, raw[i], Object.class));
|
||||
return (T)res;
|
||||
}
|
||||
}
|
||||
@@ -509,25 +510,25 @@ public class Values {
|
||||
if (obj instanceof ObjectValue && clazz.isAssignableFrom(HashMap.class)) {
|
||||
var res = new HashMap<>();
|
||||
for (var el : ((ObjectValue)obj).values.entrySet()) res.put(
|
||||
convert(ctx, el.getKey(), null),
|
||||
convert(ctx, el.getValue(), null)
|
||||
convert(ext, el.getKey(), null),
|
||||
convert(ext, el.getValue(), null)
|
||||
);
|
||||
return (T)res;
|
||||
}
|
||||
|
||||
if (clazz == String.class) return (T)toString(ctx, obj);
|
||||
if (clazz == String.class) return (T)toString(ext, obj);
|
||||
if (clazz == Boolean.class || clazz == Boolean.TYPE) return (T)(Boolean)toBoolean(obj);
|
||||
if (clazz == Byte.class || clazz == byte.class) return (T)(Byte)(byte)toNumber(ctx, obj);
|
||||
if (clazz == Integer.class || clazz == int.class) return (T)(Integer)(int)toNumber(ctx, obj);
|
||||
if (clazz == Long.class || clazz == long.class) return (T)(Long)(long)toNumber(ctx, obj);
|
||||
if (clazz == Short.class || clazz == short.class) return (T)(Short)(short)toNumber(ctx, obj);
|
||||
if (clazz == Float.class || clazz == float.class) return (T)(Float)(float)toNumber(ctx, obj);
|
||||
if (clazz == Double.class || clazz == double.class) return (T)(Double)toNumber(ctx, obj);
|
||||
if (clazz == Byte.class || clazz == byte.class) return (T)(Byte)(byte)toNumber(ext, obj);
|
||||
if (clazz == Integer.class || clazz == int.class) return (T)(Integer)(int)toNumber(ext, obj);
|
||||
if (clazz == Long.class || clazz == long.class) return (T)(Long)(long)toNumber(ext, obj);
|
||||
if (clazz == Short.class || clazz == short.class) return (T)(Short)(short)toNumber(ext, obj);
|
||||
if (clazz == Float.class || clazz == float.class) return (T)(Float)(float)toNumber(ext, obj);
|
||||
if (clazz == Double.class || clazz == double.class) return (T)(Double)toNumber(ext, obj);
|
||||
|
||||
if (clazz == Character.class || clazz == char.class) {
|
||||
if (obj instanceof Number) return (T)(Character)(char)number(obj);
|
||||
else {
|
||||
var res = toString(ctx, obj);
|
||||
var res = toString(ext, obj);
|
||||
if (res.length() == 0) throw new ConvertException("\"\"", "Character");
|
||||
else return (T)(Character)res.charAt(0);
|
||||
}
|
||||
@@ -536,23 +537,23 @@ 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(ext, obj);
|
||||
}
|
||||
|
||||
throw new ConvertException(type(obj), clazz.getSimpleName());
|
||||
}
|
||||
|
||||
public static Iterable<Object> fromJSIterator(Context ctx, Object obj) {
|
||||
public static Iterable<Object> fromJSIterator(Extensions ext, Object obj) {
|
||||
return () -> {
|
||||
try {
|
||||
var symbol = Symbol.get("Symbol.iterator");
|
||||
|
||||
var iteratorFunc = getMember(ctx, obj, symbol);
|
||||
var iteratorFunc = getMember(ext, obj, symbol);
|
||||
if (!(iteratorFunc instanceof FunctionValue)) return Collections.emptyIterator();
|
||||
var iterator = iteratorFunc instanceof FunctionValue ?
|
||||
((FunctionValue)iteratorFunc).call(ctx, obj, obj) :
|
||||
((FunctionValue)iteratorFunc).call(ext, obj, obj) :
|
||||
iteratorFunc;
|
||||
var nextFunc = getMember(ctx, call(ctx, iteratorFunc, obj), "next");
|
||||
var nextFunc = getMember(ext, call(ext, iteratorFunc, obj), "next");
|
||||
|
||||
if (!(nextFunc instanceof FunctionValue)) return Collections.emptyIterator();
|
||||
|
||||
@@ -564,11 +565,11 @@ public class Values {
|
||||
private void loadNext() {
|
||||
if (next == null) value = null;
|
||||
else if (consumed) {
|
||||
var curr = next.call(ctx, iterator);
|
||||
var curr = next.call(ext, iterator);
|
||||
if (curr == null) { next = null; value = null; }
|
||||
if (toBoolean(Values.getMember(ctx, curr, "done"))) { next = null; value = null; }
|
||||
if (toBoolean(Values.getMember(ext, curr, "done"))) { next = null; value = null; }
|
||||
else {
|
||||
this.value = Values.getMember(ctx, curr, "value");
|
||||
this.value = Values.getMember(ext, curr, "value");
|
||||
consumed = false;
|
||||
}
|
||||
}
|
||||
@@ -595,17 +596,17 @@ public class Values {
|
||||
};
|
||||
}
|
||||
|
||||
public static ObjectValue toJSIterator(Context ctx, Iterator<?> it) {
|
||||
public static ObjectValue toJSIterator(Extensions ext, Iterator<?> it) {
|
||||
var res = new ObjectValue();
|
||||
|
||||
try {
|
||||
var key = getMember(ctx, getMember(ctx, ctx.get(Environment.SYMBOL_PROTO), "constructor"), "iterator");
|
||||
res.defineProperty(ctx, key, new NativeFunction("", args -> args.self));
|
||||
var key = getMember(ext, getMember(ext, ext.get(Environment.SYMBOL_PROTO), "constructor"), "iterator");
|
||||
res.defineProperty(ext, key, new NativeFunction("", args -> args.self));
|
||||
}
|
||||
catch (IllegalArgumentException | NullPointerException e) { }
|
||||
|
||||
res.defineProperty(ctx, "next", new NativeFunction("", args -> {
|
||||
if (!it.hasNext()) return new ObjectValue(ctx, Map.of("done", true));
|
||||
res.defineProperty(ext, "next", new NativeFunction("", args -> {
|
||||
if (!it.hasNext()) return new ObjectValue(ext, Map.of("done", true));
|
||||
else {
|
||||
var obj = new ObjectValue();
|
||||
obj.defineProperty(args.ctx, "value", it.next());
|
||||
@@ -616,22 +617,22 @@ public class Values {
|
||||
return res;
|
||||
}
|
||||
|
||||
public static ObjectValue toJSIterator(Context ctx, Iterable<?> it) {
|
||||
return toJSIterator(ctx, it.iterator());
|
||||
public static ObjectValue toJSIterator(Extensions ext, Iterable<?> it) {
|
||||
return toJSIterator(ext, it.iterator());
|
||||
}
|
||||
|
||||
public static ObjectValue toJSAsyncIterator(Context ctx, Iterator<?> it) {
|
||||
public static ObjectValue toJSAsyncIterator(Extensions ext, Iterator<?> it) {
|
||||
var res = new ObjectValue();
|
||||
|
||||
try {
|
||||
var key = getMemberPath(ctx, ctx.get(Environment.SYMBOL_PROTO), "constructor", "asyncIterator");
|
||||
res.defineProperty(ctx, key, new NativeFunction("", args -> args.self));
|
||||
var key = getMemberPath(ext, ext.get(Environment.SYMBOL_PROTO), "constructor", "asyncIterator");
|
||||
res.defineProperty(ext, key, new NativeFunction("", args -> args.self));
|
||||
}
|
||||
catch (IllegalArgumentException | NullPointerException e) { }
|
||||
|
||||
res.defineProperty(ctx, "next", new NativeFunction("", args -> {
|
||||
res.defineProperty(ext, "next", new NativeFunction("", args -> {
|
||||
return PromiseLib.await(args.ctx, () -> {
|
||||
if (!it.hasNext()) return new ObjectValue(ctx, Map.of("done", true));
|
||||
if (!it.hasNext()) return new ObjectValue(ext, Map.of("done", true));
|
||||
else {
|
||||
var obj = new ObjectValue();
|
||||
obj.defineProperty(args.ctx, "value", it.next());
|
||||
@@ -653,14 +654,14 @@ public class Values {
|
||||
if (protoObj.values.size() + protoObj.properties.size() != 1) return false;
|
||||
return true;
|
||||
}
|
||||
private static String toReadable(Context ctx, Object val, HashSet<Object> passed, int tab) {
|
||||
private static String toReadable(Extensions ext, Object val, HashSet<Object> passed, int tab) {
|
||||
if (tab == 0 && val instanceof String) return (String)val;
|
||||
|
||||
if (passed.contains(val)) return "[circular]";
|
||||
|
||||
var printed = true;
|
||||
var res = new StringBuilder();
|
||||
var dbg = DebugContext.get(ctx);
|
||||
var dbg = DebugContext.get(ext);
|
||||
|
||||
if (val instanceof FunctionValue) {
|
||||
res.append(val.toString());
|
||||
@@ -674,7 +675,7 @@ public class Values {
|
||||
for (int i = 0; i < obj.size(); i++) {
|
||||
if (i != 0) res.append(", ");
|
||||
else res.append(" ");
|
||||
if (obj.has(i)) res.append(toReadable(ctx, obj.get(i), passed, tab));
|
||||
if (obj.has(i)) res.append(toReadable(ext, obj.get(i), passed, tab));
|
||||
else res.append("<empty>");
|
||||
}
|
||||
res.append(" ] ");
|
||||
@@ -701,14 +702,14 @@ public class Values {
|
||||
|
||||
for (var el : obj.values.entrySet()) {
|
||||
for (int i = 0; i < tab + 1; i++) res.append(" ");
|
||||
res.append(toReadable(ctx, el.getKey(), passed, tab + 1));
|
||||
res.append(toReadable(ext, el.getKey(), passed, tab + 1));
|
||||
res.append(": ");
|
||||
res.append(toReadable(ctx, el.getValue(), passed, tab + 1));
|
||||
res.append(toReadable(ext, el.getValue(), passed, tab + 1));
|
||||
res.append(",\n");
|
||||
}
|
||||
for (var el : obj.properties.entrySet()) {
|
||||
for (int i = 0; i < tab + 1; i++) res.append(" ");
|
||||
res.append(toReadable(ctx, el.getKey(), passed, tab + 1));
|
||||
res.append(toReadable(ext, el.getKey(), passed, tab + 1));
|
||||
res.append(": [prop],\n");
|
||||
}
|
||||
|
||||
@@ -721,23 +722,23 @@ public class Values {
|
||||
else if (val == null) return "undefined";
|
||||
else if (val == Values.NULL) return "null";
|
||||
else if (val instanceof String) return "'" + val + "'";
|
||||
else return Values.toString(ctx, val);
|
||||
else return Values.toString(ext, val);
|
||||
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
public static String toReadable(Context ctx, Object val) {
|
||||
return toReadable(ctx, val, new HashSet<>(), 0);
|
||||
public static String toReadable(Extensions ext, Object val) {
|
||||
return toReadable(ext, val, new HashSet<>(), 0);
|
||||
}
|
||||
public static String errorToReadable(RuntimeException err, String prefix) {
|
||||
prefix = prefix == null ? "Uncaught" : "Uncaught " + prefix;
|
||||
if (err instanceof EngineException) {
|
||||
var ee = ((EngineException)err);
|
||||
try {
|
||||
return prefix + " " + ee.toString(new Context(ee.env));
|
||||
return prefix + " " + ee.toString(ee.ext);
|
||||
}
|
||||
catch (EngineException ex) {
|
||||
return prefix + " " + toReadable(new Context(ee.env), ee.value);
|
||||
return prefix + " " + toReadable(ee.ext, ee.value);
|
||||
}
|
||||
}
|
||||
else if (err instanceof SyntaxException) {
|
||||
@@ -751,8 +752,8 @@ public class Values {
|
||||
return prefix + " internal error " + str.toString();
|
||||
}
|
||||
}
|
||||
public static void printValue(Context ctx, Object val) {
|
||||
System.out.print(toReadable(ctx, val));
|
||||
public static void printValue(Extensions ext, Object val) {
|
||||
System.out.print(toReadable(ext, val));
|
||||
}
|
||||
public static void printError(RuntimeException err, String prefix) {
|
||||
System.out.println(errorToReadable(err, prefix));
|
||||
|
||||
@@ -18,6 +18,7 @@ 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.scope.GlobalScope;
|
||||
import me.topchetoeu.jscript.runtime.values.NativeFunction;
|
||||
import me.topchetoeu.jscript.runtime.values.Values;
|
||||
import me.topchetoeu.jscript.utils.debug.DebugServer;
|
||||
@@ -28,6 +29,7 @@ import me.topchetoeu.jscript.utils.filesystem.Mode;
|
||||
import me.topchetoeu.jscript.utils.filesystem.PhysicalFilesystem;
|
||||
import me.topchetoeu.jscript.utils.filesystem.RootFilesystem;
|
||||
import me.topchetoeu.jscript.utils.filesystem.STDFilesystem;
|
||||
import me.topchetoeu.jscript.utils.interop.NativeWrapperProvider;
|
||||
import me.topchetoeu.jscript.utils.modules.ModuleRepo;
|
||||
import me.topchetoeu.jscript.utils.permissions.PermissionsManager;
|
||||
import me.topchetoeu.jscript.utils.permissions.PermissionsProvider;
|
||||
@@ -87,10 +89,12 @@ public class JScriptRepl {
|
||||
private static void initEnv() {
|
||||
environment = Internals.apply(environment);
|
||||
|
||||
environment.global.define(null, false, new NativeFunction("exit", args -> {
|
||||
var glob = GlobalScope.get(environment);
|
||||
|
||||
glob.define(null, false, new NativeFunction("exit", args -> {
|
||||
throw new InterruptException();
|
||||
}));
|
||||
environment.global.define(null, false, new NativeFunction("go", args -> {
|
||||
glob.define(null, false, new NativeFunction("go", args -> {
|
||||
try {
|
||||
var f = Path.of("do.js");
|
||||
var func = args.ctx.compile(new Filename("do", "do/" + j++ + ".js"), new String(Files.readAllBytes(f)));
|
||||
@@ -100,7 +104,7 @@ public class JScriptRepl {
|
||||
throw new EngineException("Couldn't open do.js");
|
||||
}
|
||||
}));
|
||||
environment.global.define(null, false, new NativeFunction("log", args -> {
|
||||
glob.define(null, false, new NativeFunction("log", args -> {
|
||||
for (var el : args.args) {
|
||||
Values.printValue(args.ctx, el);
|
||||
}
|
||||
|
||||
@@ -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\"}");
|
||||
|
||||
@@ -27,9 +27,11 @@ 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.Extensions;
|
||||
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;
|
||||
@@ -41,7 +43,9 @@ import me.topchetoeu.jscript.runtime.values.Values;
|
||||
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 +54,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 +88,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 +98,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))
|
||||
@@ -133,7 +150,7 @@ public class SimpleDebugger implements Debugger {
|
||||
this.frame = frame;
|
||||
this.id = id;
|
||||
|
||||
this.global = frame.function.environment.global.obj;
|
||||
this.global = GlobalScope.get(frame.ctx).obj;
|
||||
this.local = frame.getLocalScope();
|
||||
this.capture = frame.getCaptureScope();
|
||||
Values.makePrototypeChain(frame.ctx, global, capture, local);
|
||||
@@ -156,19 +173,19 @@ public class SimpleDebugger implements Debugger {
|
||||
.add(new JSONMap()
|
||||
.set("type", "global")
|
||||
.set("name", "Global Scope")
|
||||
.set("object", serializeObj(frame.ctx, global))
|
||||
.set("object", serializeObj(frame.ctx.extensions, global))
|
||||
)
|
||||
.add(new JSONMap()
|
||||
.set("type", "other")
|
||||
.set("name", "Value Stack")
|
||||
.set("object", serializeObj(frame.ctx, valstack))
|
||||
.set("object", serializeObj(frame.ctx.extensions, valstack))
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
private class ObjRef {
|
||||
public final ObjectValue obj;
|
||||
public final Context ctx;
|
||||
public final Extensions ext;
|
||||
public final HashSet<String> heldGroups = new HashSet<>();
|
||||
public boolean held = true;
|
||||
|
||||
@@ -176,19 +193,19 @@ public class SimpleDebugger implements Debugger {
|
||||
return !held && heldGroups.size() == 0;
|
||||
}
|
||||
|
||||
public ObjRef(Context ctx, ObjectValue obj) {
|
||||
this.ctx = ctx;
|
||||
public ObjRef(Extensions ext, ObjectValue obj) {
|
||||
this.ext = ext;
|
||||
this.obj = obj;
|
||||
}
|
||||
}
|
||||
|
||||
private static class RunResult {
|
||||
public final Context ctx;
|
||||
public final Extensions ext;
|
||||
public final Object result;
|
||||
public final EngineException error;
|
||||
|
||||
public RunResult(Context ctx, Object result, EngineException error) {
|
||||
this.ctx = ctx;
|
||||
public RunResult(Extensions ext, Object result, EngineException error) {
|
||||
this.ext = ext;
|
||||
this.result = result;
|
||||
this.error = error;
|
||||
}
|
||||
@@ -202,8 +219,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 +244,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 +311,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 +339,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(Extensions 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 = Context.of(env);
|
||||
|
||||
if (val == Values.NULL) {
|
||||
return new JSONMap()
|
||||
@@ -348,7 +354,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 +381,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 +392,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 (Exception e) { }
|
||||
}
|
||||
|
||||
|
||||
if (byValue) try { res.put("value", JSON.fromJs(ctx, obj)); }
|
||||
if (byValue) try { res.put("value", JSON.fromJs(env, obj)); }
|
||||
catch (Exception e) { }
|
||||
|
||||
return res;
|
||||
@@ -411,8 +427,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(Extensions ext, Object val) {
|
||||
return serializeObj(ext, val, false);
|
||||
}
|
||||
private void addObjectGroup(String name, Object val) {
|
||||
if (val instanceof ObjectValue) {
|
||||
@@ -451,11 +467,11 @@ public class SimpleDebugger implements Debugger {
|
||||
else return JSON.toJs(res);
|
||||
}
|
||||
|
||||
private JSONMap serializeException(Context ctx, EngineException err) {
|
||||
private JSONMap serializeException(Extensions ext, EngineException err) {
|
||||
String text = null;
|
||||
|
||||
try {
|
||||
text = Values.toString(ctx, err.value);
|
||||
text = Values.toString(Context.of(ext), err.value);
|
||||
}
|
||||
catch (EngineException e) {
|
||||
text = "[error while stringifying]";
|
||||
@@ -463,7 +479,7 @@ public class SimpleDebugger implements Debugger {
|
||||
|
||||
var res = new JSONMap()
|
||||
.set("exceptionId", nextId())
|
||||
.set("exception", serializeObj(ctx, err.value))
|
||||
.set("exception", serializeObj(ext, err.value))
|
||||
.set("text", text);
|
||||
|
||||
return res;
|
||||
@@ -524,30 +540,43 @@ public class SimpleDebugger implements Debugger {
|
||||
}
|
||||
}
|
||||
|
||||
private Extensions sanitizeEnvironment(Extensions ext) {
|
||||
var res = ext.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.extensions.copy();
|
||||
|
||||
env.global = new GlobalScope(codeFrame.local);
|
||||
env.remove(EventLoop.KEY);
|
||||
env.remove(DebugContext.KEY);
|
||||
env.remove(EventLoop.KEY);
|
||||
env.remove(GlobalScope.KEY);
|
||||
env.add(EventLoop.KEY, engine);
|
||||
env.add(GlobalScope.KEY, new GlobalScope(codeFrame.local));
|
||||
|
||||
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(Extensions ext, 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 = Context.of(ext);
|
||||
if (target == null) target = GlobalScope.get(ext);
|
||||
|
||||
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 +631,7 @@ public class SimpleDebugger implements Debugger {
|
||||
desc.defineProperty(ctx, "type", Values.type(val));
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
res.set(ctx, res.size(), desc);
|
||||
}
|
||||
|
||||
@@ -630,15 +659,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 +688,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 +744,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 +764,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 {
|
||||
@@ -794,8 +829,8 @@ public class SimpleDebugger implements Debugger {
|
||||
|
||||
if (group != null) addObjectGroup(group, res.result);
|
||||
|
||||
if (res.error != null) ws.send(msg.respond(new JSONMap().set("exceptionDetails", serializeException(res.ctx, res.error))));
|
||||
else ws.send(msg.respond(new JSONMap().set("result", serializeObj(res.ctx, res.result))));
|
||||
if (res.error != null) ws.send(msg.respond(new JSONMap().set("exceptionDetails", serializeException(res.ext, res.error))));
|
||||
else ws.send(msg.respond(new JSONMap().set("result", serializeObj(res.ext, res.result))));
|
||||
}
|
||||
|
||||
@Override public synchronized void releaseObjectGroup(V8Message msg) throws IOException {
|
||||
@@ -818,46 +853,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 ext = ref.ext;
|
||||
var ctx = Context.of(ext);
|
||||
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(ext, prop.getter));
|
||||
if (prop.setter != null) propDesc.set("set", serializeObj(ext, 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(ext, 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(ext, 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 +917,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 ext = thisArgRef.ext;
|
||||
var ctx = Context.of(ext);
|
||||
|
||||
while (true) {
|
||||
var start = src.lastIndexOf("//# sourceURL=");
|
||||
@@ -892,22 +936,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(ext, 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(ext, 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(ext, 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(ext, e)))); }
|
||||
}
|
||||
|
||||
@Override public synchronized void runtimeEnable(V8Message msg) throws IOException {
|
||||
@@ -958,10 +1005,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 +1081,7 @@ public class SimpleDebugger implements Debugger {
|
||||
|
||||
public SimpleDebugger attach(DebugContext ctx) {
|
||||
ctx.attachDebugger(this);
|
||||
contexts.put(ctx, ctx);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ public abstract class BaseFile<T> implements File {
|
||||
@Override public synchronized long seek(long offset, int pos) {
|
||||
try {
|
||||
if (handle == null) throw new FilesystemException(ErrorReason.CLOSED);
|
||||
if (!mode.writable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for seeking.");
|
||||
if (mode == Mode.NONE) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for seeking.");
|
||||
return onSeek(offset, pos);
|
||||
}
|
||||
catch (FilesystemException e) { throw e.setAction(ActionType.SEEK); }
|
||||
|
||||
@@ -54,16 +54,12 @@ public class FilesystemException extends RuntimeException {
|
||||
@Override public String getMessage() {
|
||||
var parts = new ArrayList<String>(10);
|
||||
|
||||
path = String.join(" ", parts).trim();
|
||||
if (path.isEmpty()) path = null;
|
||||
parts.clear();
|
||||
|
||||
parts.add(action == null ? "An action performed upon " : action.readable(reason.usePast));
|
||||
|
||||
if (entry == EntryType.FILE) parts.add("file");
|
||||
if (entry == EntryType.FOLDER) parts.add("folder");
|
||||
|
||||
if (path != null) parts.add(path);
|
||||
if (path != null && !path.isBlank()) parts.add(path.trim());
|
||||
|
||||
parts.add(reason.readable);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package me.topchetoeu.jscript.utils.filesystem;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.Files;
|
||||
@@ -35,7 +36,8 @@ public class PhysicalFilesystem implements Filesystem {
|
||||
));
|
||||
else return handles.put(new PhysicalFile(path, perms));
|
||||
}
|
||||
catch (IOException e) { throw new FilesystemException(ErrorReason.DOESNT_EXIST); }
|
||||
catch (FileNotFoundException e) { throw new FilesystemException(ErrorReason.DOESNT_EXIST); }
|
||||
catch (IOException e) { throw new FilesystemException(ErrorReason.NO_PERMISSION); }
|
||||
}
|
||||
catch (FilesystemException e) { throw e.setAction(ActionType.OPEN).setPath(_path); }
|
||||
}
|
||||
@@ -68,12 +70,7 @@ public class PhysicalFilesystem implements Filesystem {
|
||||
|
||||
if (!Files.exists(path)) return new FileStat(Mode.NONE, EntryType.NONE);
|
||||
|
||||
var perms = Mode.NONE;
|
||||
|
||||
if (Files.isReadable(path)) {
|
||||
if (Files.isWritable(path)) perms = Mode.READ_WRITE;
|
||||
else perms = Mode.READ;
|
||||
}
|
||||
var perms = Mode.of(Files.isReadable(path), Files.isWritable(path));
|
||||
|
||||
if (perms == Mode.NONE) return new FileStat(Mode.NONE, EntryType.NONE);
|
||||
|
||||
|
||||
@@ -3,14 +3,32 @@ package me.topchetoeu.jscript.utils.interop;
|
||||
import java.lang.reflect.Array;
|
||||
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.Key;
|
||||
import me.topchetoeu.jscript.runtime.values.NativeWrapper;
|
||||
import me.topchetoeu.jscript.runtime.values.Values;
|
||||
|
||||
public class Arguments {
|
||||
public class Arguments implements Extensions {
|
||||
public final Object self;
|
||||
public final Object[] args;
|
||||
public final Context ctx;
|
||||
|
||||
@Override public <T> void add(Key<T> key, T obj) {
|
||||
ctx.add(key, obj);
|
||||
}
|
||||
@Override public <T> T get(Key<T> key) {
|
||||
return ctx.get(key);
|
||||
}
|
||||
@Override public boolean has(Key<?> key) {
|
||||
return ctx.has(key);
|
||||
}
|
||||
@Override public boolean remove(Key<?> key) {
|
||||
return ctx.remove(key);
|
||||
}
|
||||
@Override public Iterable<Key<?>> keys() {
|
||||
return ctx.keys();
|
||||
}
|
||||
|
||||
public int n() {
|
||||
return args.length;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package me.topchetoeu.jscript.utils.interop;
|
||||
|
||||
public enum ExposeType {
|
||||
INIT,
|
||||
METHOD,
|
||||
GETTER,
|
||||
SETTER,
|
||||
|
||||
@@ -4,15 +4,18 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Member;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import me.topchetoeu.jscript.common.Location;
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Environment;
|
||||
import me.topchetoeu.jscript.runtime.WrapperProvider;
|
||||
import me.topchetoeu.jscript.runtime.Copyable;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.Key;
|
||||
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
|
||||
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
|
||||
import me.topchetoeu.jscript.runtime.values.FunctionValue;
|
||||
@@ -21,11 +24,16 @@ 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 {
|
||||
public class NativeWrapperProvider implements Copyable {
|
||||
public static final Key<NativeWrapperProvider> KEY = new Key<>();
|
||||
|
||||
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 HashMap<Class<?>, Class<?>> interfaceToProxy = new HashMap<>();
|
||||
private final HashSet<Class<?>> ignore = new HashSet<>();
|
||||
|
||||
private static Object call(Context ctx, String name, Method method, Object thisArg, Object... args) {
|
||||
try {
|
||||
@@ -78,10 +86,16 @@ public class NativeWrapperProvider implements WrapperProvider {
|
||||
String.join(", ", Arrays.stream(actual).map(v -> v.getName()).collect(Collectors.toList()))
|
||||
));
|
||||
}
|
||||
private static String getName(Class<?> clazz) {
|
||||
var classNat = clazz.getAnnotation(WrapperName.class);
|
||||
if (classNat != null && !classNat.value().trim().equals("")) return classNat.value().trim();
|
||||
else return clazz.getSimpleName();
|
||||
private static String getName(Class<?> ...classes) {
|
||||
String last = null;
|
||||
|
||||
for (var clazz : classes) {
|
||||
var classNat = clazz.getAnnotation(WrapperName.class);
|
||||
if (classNat != null && !classNat.value().trim().equals("")) return classNat.value().trim();
|
||||
else if (last != null) last = clazz.getSimpleName();
|
||||
}
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
private static void checkUnderscore(Member member) {
|
||||
@@ -106,14 +120,16 @@ 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)) {
|
||||
any = true;
|
||||
if (!annotation.target().shouldApply(target)) continue;
|
||||
|
||||
checkUnderscore(method);
|
||||
@@ -122,13 +138,6 @@ public class NativeWrapperProvider implements WrapperProvider {
|
||||
var repeat = false;
|
||||
|
||||
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 {
|
||||
@@ -162,6 +171,7 @@ public class NativeWrapperProvider implements WrapperProvider {
|
||||
));
|
||||
}
|
||||
for (var annotation : method.getAnnotationsByType(ExposeField.class)) {
|
||||
any = true;
|
||||
if (!annotation.target().shouldApply(target)) continue;
|
||||
|
||||
checkUnderscore(method);
|
||||
@@ -171,8 +181,13 @@ public class NativeWrapperProvider implements WrapperProvider {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -185,6 +200,7 @@ public class NativeWrapperProvider implements WrapperProvider {
|
||||
}
|
||||
for (var field : clazz.getDeclaredFields()) {
|
||||
for (var annotation : field.getAnnotationsByType(ExposeField.class)) {
|
||||
any = true;
|
||||
if (!annotation.target().shouldApply(target)) continue;
|
||||
|
||||
checkUnderscore(field);
|
||||
@@ -195,7 +211,24 @@ public class NativeWrapperProvider implements WrapperProvider {
|
||||
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,13 +243,26 @@ 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 boolean apply(ObjectValue obj, ExposeTarget target, Class<?> ...appliers) {
|
||||
var res = false;
|
||||
|
||||
for (var i = appliers.length - 1; i >= 0; i--) {
|
||||
res |= apply(obj, target, appliers[i]);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private static Method getConstructor(Environment env, Class<?> clazz) {
|
||||
for (var method : clazz.getDeclaredMethods()) {
|
||||
if (!method.isAnnotationPresent(ExposeConstructor.class)) continue;
|
||||
checkSignature(method, true, Arguments.class);
|
||||
return method;
|
||||
private static Method getConstructor(Class<?> ...appliers) {
|
||||
for (var clazz : appliers) {
|
||||
for (var method : clazz.getDeclaredMethods()) {
|
||||
if (!method.isAnnotationPresent(ExposeConstructor.class)) continue;
|
||||
checkSignature(method, true, Arguments.class);
|
||||
return method;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -228,10 +274,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<?> ...appliers) {
|
||||
var res = new ObjectValue();
|
||||
res.defineProperty(null, Symbol.get("Symbol.typeName"), getName(clazz));
|
||||
apply(res, env, ExposeTarget.PROTOTYPE, clazz);
|
||||
res.defineProperty(null, Symbol.get("Symbol.typeName"), getName(appliers));
|
||||
if (!apply(res, ExposeTarget.PROTOTYPE, appliers)) return null;
|
||||
return res;
|
||||
}
|
||||
/**
|
||||
@@ -240,14 +286,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<?> ...appliers) {
|
||||
var constr = getConstructor(appliers);
|
||||
|
||||
FunctionValue res = constr == null ?
|
||||
new NativeFunction(getName(clazz), args -> { throw EngineException.ofError("This constructor is not invokable."); }) :
|
||||
create(getName(clazz), constr);
|
||||
new NativeFunction(getName(appliers), args -> { throw EngineException.ofError("This constructor is not invokable."); }) :
|
||||
create(getName(appliers), constr);
|
||||
|
||||
apply(res, ctx, ExposeTarget.CONSTRUCTOR, clazz);
|
||||
if (!apply(res, ExposeTarget.CONSTRUCTOR, appliers) && constr == null) return null;
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -257,15 +303,48 @@ 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<?> ...appliers) {
|
||||
var res = new ObjectValue();
|
||||
res.defineProperty(null, Symbol.get("Symbol.typeName"), getName(clazz));
|
||||
apply(res, ctx, ExposeTarget.NAMESPACE, clazz);
|
||||
|
||||
if (!apply(res, ExposeTarget.NAMESPACE, appliers)) return null;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private Class<?>[] getAppliers(Class<?> clazz) {
|
||||
var res = new ArrayList<Class<?>>();
|
||||
|
||||
res.add(clazz);
|
||||
|
||||
if (classToProxy.containsKey(clazz)) res.add(classToProxy.get(clazz));
|
||||
for (var intf : interfaceToProxy.keySet()) {
|
||||
if (intf.isAssignableFrom(clazz)) res.add(interfaceToProxy.get(intf));
|
||||
}
|
||||
|
||||
return res.toArray(Class<?>[]::new);
|
||||
}
|
||||
|
||||
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(Extensions.EMPTY, proto, parentProto);
|
||||
Values.setPrototype(Extensions.EMPTY, 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 +360,12 @@ public class NativeWrapperProvider implements WrapperProvider {
|
||||
clazz.isSynthetic()
|
||||
) return;
|
||||
|
||||
if (constr == null) constr = makeConstructor(env, clazz);
|
||||
if (proto == null) proto = makeProto(env, clazz);
|
||||
var appliers = getAppliers(clazz);
|
||||
|
||||
if (constr == null) constr = makeConstructor(appliers);
|
||||
if (proto == null) proto = makeProto(appliers);
|
||||
|
||||
if (constr == null || proto == null) return;
|
||||
|
||||
proto.defineProperty(null, "constructor", constr, true, false, false);
|
||||
constr.defineProperty(null, "prototype", proto, true, false, false);
|
||||
@@ -290,59 +373,84 @@ 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);
|
||||
public NativeWrapperProvider copy() {
|
||||
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 (clazz.isInterface()) {
|
||||
if (wrapper == null || wrapper == clazz) interfaceToProxy.remove(clazz);
|
||||
else interfaceToProxy.put(clazz, wrapper);
|
||||
}
|
||||
else {
|
||||
if (wrapper == null || wrapper == clazz) classToProxy.remove(clazz);
|
||||
else classToProxy.put(clazz, wrapper);
|
||||
}
|
||||
|
||||
var classes = Stream.concat(
|
||||
Stream.of(clazz),
|
||||
prototypes.keySet().stream().filter(clazz::isAssignableFrom)
|
||||
).toArray(Class<?>[]::new);
|
||||
|
||||
for (var el : classes) {
|
||||
prototypes.remove(el);
|
||||
constructors.remove(el);
|
||||
namespaces.remove(el);
|
||||
}
|
||||
|
||||
for (var el : classes) {
|
||||
initType(el, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
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()));
|
||||
public NativeWrapperProvider() { }
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public NativeWrapperProvider(Environment env) {
|
||||
this.env = env;
|
||||
initError();
|
||||
public static NativeWrapperProvider get(Extensions ext) {
|
||||
return ext.get(KEY);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import me.topchetoeu.jscript.common.Filename;
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
import me.topchetoeu.jscript.runtime.Key;
|
||||
import me.topchetoeu.jscript.runtime.scope.GlobalScope;
|
||||
import me.topchetoeu.jscript.utils.filesystem.Filesystem;
|
||||
import me.topchetoeu.jscript.utils.filesystem.Mode;
|
||||
|
||||
@@ -25,8 +26,10 @@ public interface ModuleRepo {
|
||||
|
||||
if (modules.containsKey(name)) return modules.get(name);
|
||||
|
||||
var env = ctx.environment.child();
|
||||
var env = Context.clean(ctx.extensions).child();
|
||||
env.add(CWD, fs.normalize(name, ".."));
|
||||
var glob = env.get(GlobalScope.KEY);
|
||||
env.add(GlobalScope.KEY, glob.child());
|
||||
|
||||
var mod = new SourceModule(filename, src, env);
|
||||
modules.put(name, mod);
|
||||
|
||||
@@ -2,22 +2,22 @@ package me.topchetoeu.jscript.utils.modules;
|
||||
|
||||
import me.topchetoeu.jscript.common.Filename;
|
||||
import me.topchetoeu.jscript.runtime.Context;
|
||||
import me.topchetoeu.jscript.runtime.Environment;
|
||||
import me.topchetoeu.jscript.runtime.Extensions;
|
||||
|
||||
public class SourceModule extends Module {
|
||||
public final Filename filename;
|
||||
public final String source;
|
||||
public final Environment env;
|
||||
public final Extensions ext;
|
||||
|
||||
@Override
|
||||
protected Object onLoad(Context ctx) {
|
||||
var res = new Context(env).compile(filename, source);
|
||||
var res = new Context(ext).compile(filename, source);
|
||||
return res.call(ctx);
|
||||
}
|
||||
|
||||
public SourceModule(Filename filename, String source, Environment env) {
|
||||
public SourceModule(Filename filename, String source, Extensions ext) {
|
||||
this.filename = filename;
|
||||
this.source = source;
|
||||
this.env = env;
|
||||
this.ext = ext;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user