Compare commits

...

12 Commits

87 changed files with 575 additions and 390 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,12 +1,12 @@
package me.topchetoeu.jscript.core;
package me.topchetoeu.jscript.runtime;
import java.util.HashMap;
import me.topchetoeu.jscript.core.scope.GlobalScope;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.scope.GlobalScope;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.utils.interop.NativeWrapperProvider;
@SuppressWarnings("unchecked")

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.debug;
package me.topchetoeu.jscript.runtime.debug;
import java.util.ArrayList;
import java.util.HashMap;
@@ -10,13 +10,13 @@ import me.topchetoeu.jscript.common.FunctionBody;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.Location;
import me.topchetoeu.jscript.common.mapping.FunctionMap;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Extensions;
import me.topchetoeu.jscript.core.Frame;
import me.topchetoeu.jscript.core.Key;
import me.topchetoeu.jscript.core.values.CodeFunction;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Extensions;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.Key;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.CodeFunction;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
public class DebugContext {
public static final Key<DebugContext> KEY = new Key<>();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
package me.topchetoeu.jscript.core.values;
package me.topchetoeu.jscript.runtime.values;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.runtime.Context;
public class NativeWrapper extends ObjectValue {
private static final Object NATIVE_PROTO = new Object();
@@ -8,8 +8,11 @@ public class NativeWrapper extends ObjectValue {
@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);
if (ctx.environment != null && prototype == NATIVE_PROTO) {
var res = ctx.environment.wrappers.getProto(wrapped.getClass());
if (res != null) return res;
}
return super.getPrototype(ctx);
}
@Override

View File

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

View File

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

View File

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

View File

@@ -1,4 +1,4 @@
package me.topchetoeu.jscript.core.values;
package me.topchetoeu.jscript.runtime.values;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
@@ -13,13 +13,13 @@ import java.util.List;
import java.util.Map;
import me.topchetoeu.jscript.common.Operation;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.debug.DebugContext;
import me.topchetoeu.jscript.core.exceptions.ConvertException;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
import me.topchetoeu.jscript.lib.PromiseLib;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.ConvertException;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class Values {
public static enum CompareResult {

View File

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

View File

@@ -8,18 +8,18 @@ import java.nio.file.Path;
import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.common.Metadata;
import me.topchetoeu.jscript.common.Reading;
import me.topchetoeu.jscript.core.Compiler;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Engine;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.EventLoop;
import me.topchetoeu.jscript.core.debug.DebugContext;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.exceptions.InterruptException;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
import me.topchetoeu.jscript.lib.Internals;
import me.topchetoeu.jscript.runtime.Compiler;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Engine;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.EventLoop;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.runtime.values.Values;
import me.topchetoeu.jscript.utils.debug.DebugServer;
import me.topchetoeu.jscript.utils.debug.SimpleDebugger;
import me.topchetoeu.jscript.utils.filesystem.Filesystem;
@@ -111,7 +111,7 @@ public class JScriptRepl {
var fs = new RootFilesystem(PermissionsProvider.get(environment));
fs.protocols.put("temp", new MemoryFilesystem(Mode.READ_WRITE));
fs.protocols.put("file", new PhysicalFilesystem("."));
fs.protocols.put("std", STDFilesystem.ofStd(System.in, System.out, System.err));
fs.protocols.put("std", new STDFilesystem(System.in, System.out, System.err));
environment.add(PermissionsProvider.KEY, PermissionsManager.ALL_PERMS);
environment.add(Filesystem.KEY, fs);

View File

@@ -15,8 +15,8 @@ import me.topchetoeu.jscript.common.events.Notifier;
import me.topchetoeu.jscript.common.json.JSON;
import me.topchetoeu.jscript.common.json.JSONList;
import me.topchetoeu.jscript.common.json.JSONMap;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
import me.topchetoeu.jscript.utils.debug.WebSocketMessage.Type;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
public class DebugServer {
public static String browserDisplayName = Metadata.name() + "/" + Metadata.version();

View File

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

View File

@@ -23,19 +23,19 @@ import me.topchetoeu.jscript.common.json.JSONList;
import me.topchetoeu.jscript.common.json.JSONMap;
import me.topchetoeu.jscript.common.mapping.FunctionMap;
import me.topchetoeu.jscript.compilation.parsing.Parsing;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Engine;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.EventLoop;
import me.topchetoeu.jscript.core.Frame;
import me.topchetoeu.jscript.core.debug.DebugContext;
import me.topchetoeu.jscript.core.scope.GlobalScope;
import me.topchetoeu.jscript.core.values.ArrayValue;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Symbol;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Engine;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.EventLoop;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.scope.GlobalScope;
import me.topchetoeu.jscript.runtime.values.ArrayValue;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Symbol;
import me.topchetoeu.jscript.runtime.values.Values;
// very simple indeed
public class SimpleDebugger implements Debugger {

View File

@@ -13,7 +13,7 @@ public abstract class BaseFile<T> implements File {
protected abstract long onSeek(long offset, int pos);
protected abstract boolean onClose();
@Override public int read(byte[] buff) {
@Override public synchronized int read(byte[] buff) {
try {
if (handle == null) throw new FilesystemException(ErrorReason.CLOSED);
if (!mode.readable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for reading.");
@@ -21,7 +21,7 @@ public abstract class BaseFile<T> implements File {
}
catch (FilesystemException e) { throw e.setAction(ActionType.READ); }
}
@Override public void write(byte[] buff) {
@Override public synchronized void write(byte[] buff) {
try {
if (handle == null) throw new FilesystemException(ErrorReason.CLOSED);
if (!mode.writable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for writting.");
@@ -29,7 +29,7 @@ public abstract class BaseFile<T> implements File {
}
catch (FilesystemException e) { throw e.setAction(ActionType.WRITE); }
}
@Override public long seek(long offset, int pos) {
@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.");
@@ -37,7 +37,7 @@ public abstract class BaseFile<T> implements File {
}
catch (FilesystemException e) { throw e.setAction(ActionType.SEEK); }
}
@Override public boolean close() {
@Override public synchronized boolean close() {
if (handle != null) {
try {
var res = onClose();

View File

@@ -66,7 +66,7 @@ public interface File {
public static File ofStream(InputStream str) {
return new File() {
@Override public int read(byte[] buff) {
@Override public synchronized int read(byte[] buff) {
try {
try { return str.read(buff); }
catch (NullPointerException e) { throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, e.getMessage()); }
@@ -78,7 +78,7 @@ public interface File {
}
public static File ofStream(OutputStream str) {
return new File() {
@Override public void write(byte[] buff) {
@Override public synchronized void write(byte[] buff) {
try {
try { str.write(buff); }
catch (NullPointerException e) {throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, e.getMessage()); }
@@ -91,7 +91,7 @@ public interface File {
public static File ofLineWriter(LineWriter writer) {
var buff = new Buffer();
return new File() {
@Override public void write(byte[] val) {
@Override public synchronized void write(byte[] val) {
try {
if (val == null) throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, "Given buffer is null.");
@@ -118,7 +118,7 @@ public interface File {
private byte[] prev = new byte[0];
@Override
public int read(byte[] buff) {
public synchronized int read(byte[] buff) {
try {
if (buff == null) throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, "Given buffer is null.");
var ptr = 0;

View File

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

View File

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

View File

@@ -20,7 +20,7 @@ public class MemoryFilesystem implements Filesystem {
@Override public String normalize(String... path) {
return Paths.normalize(path);
}
@Override public File open(String _path, Mode perms) {
@Override public synchronized File open(String _path, Mode perms) {
try {
var path = realPath(_path);
var pcount = path.getNameCount();
@@ -47,7 +47,7 @@ public class MemoryFilesystem implements Filesystem {
}
catch (FilesystemException e) { throw e.setPath(_path).setAction(ActionType.OPEN); }
}
@Override public boolean create(String _path, EntryType type) {
@Override public synchronized boolean create(String _path, EntryType type) {
try {
var path = realPath(_path);
@@ -69,14 +69,14 @@ public class MemoryFilesystem implements Filesystem {
}
catch (FilesystemException e) { throw e.setPath(_path).setAction(ActionType.CREATE); }
}
@Override public FileStat stat(String _path) {
@Override public synchronized FileStat stat(String _path) {
var path = realPath(_path);
if (files.containsKey(path)) return new FileStat(mode, EntryType.FILE);
else if (folders.contains(path)) return new FileStat(mode, EntryType.FOLDER);
else return new FileStat(Mode.NONE, EntryType.NONE);
}
@Override public void close() throws FilesystemException {
@Override public synchronized void close() throws FilesystemException {
handles.close();
}

View File

@@ -24,7 +24,7 @@ public class PhysicalFilesystem implements Filesystem {
@Override public String normalize(String... paths) {
return Paths.normalize(paths);
}
@Override public File open(String _path, Mode perms) {
@Override public synchronized File open(String _path, Mode perms) {
try {
var path = realPath(normalize(_path));
checkMode(path, perms);
@@ -39,7 +39,7 @@ public class PhysicalFilesystem implements Filesystem {
}
catch (FilesystemException e) { throw e.setAction(ActionType.OPEN).setPath(_path); }
}
@Override public boolean create(String _path, EntryType type) {
@Override public synchronized boolean create(String _path, EntryType type) {
try {
var path = realPath(_path);
@@ -63,7 +63,7 @@ public class PhysicalFilesystem implements Filesystem {
return true;
}
@Override public FileStat stat(String _path) {
@Override public synchronized FileStat stat(String _path) {
var path = realPath(_path);
if (!Files.exists(path)) return new FileStat(Mode.NONE, EntryType.NONE);
@@ -82,7 +82,7 @@ public class PhysicalFilesystem implements Filesystem {
Files.isDirectory(path) ? EntryType.FOLDER : EntryType.FILE
);
}
@Override public void close() throws FilesystemException {
@Override public synchronized void close() throws FilesystemException {
try {
handles.close();
}

View File

@@ -53,7 +53,7 @@ public class RootFilesystem implements Filesystem {
else return filename.protocol + "://" + protocol.normalize(paths);
}
}
@Override public File open(String path, Mode perms) throws FilesystemException {
@Override public synchronized File open(String path, Mode perms) throws FilesystemException {
try {
var filename = Filename.parse(path);
var protocol = getProtocol(filename);
@@ -63,7 +63,7 @@ public class RootFilesystem implements Filesystem {
}
catch (FilesystemException e) { throw e.setPath(path).setAction(ActionType.OPEN); }
}
@Override public boolean create(String path, EntryType type) throws FilesystemException {
@Override public synchronized boolean create(String path, EntryType type) throws FilesystemException {
try {
var filename = Filename.parse(path);
var protocol = getProtocol(filename);
@@ -73,7 +73,7 @@ public class RootFilesystem implements Filesystem {
}
catch (FilesystemException e) { throw e.setPath(path).setAction(ActionType.CREATE); }
}
@Override public FileStat stat(String path) throws FilesystemException {
@Override public synchronized FileStat stat(String path) throws FilesystemException {
try {
var filename = Filename.parse(path);
var protocol = getProtocol(filename);
@@ -82,7 +82,7 @@ public class RootFilesystem implements Filesystem {
}
catch (FilesystemException e) { throw e.setPath(path).setAction(ActionType.STAT); }
}
@Override public void close() throws FilesystemException {
@Override public synchronized void close() throws FilesystemException {
try {
for (var protocol : protocols.values()) {
protocol.close();

View File

@@ -2,41 +2,51 @@ package me.topchetoeu.jscript.utils.filesystem;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
public class STDFilesystem implements Filesystem {
private final HashMap<String, File> handles = new HashMap<>();
private File in;
private File out;
private File err;
@Override
public String normalize(String... path) {
var res = Paths.normalize(path);
while (res.startsWith("/")) res = res.substring(1);
while (res.endsWith("/")) res = res.substring(0, res.length() - 1);
return res;
}
@Override public File open(String path, Mode mode) {
@Override public synchronized File open(String path, Mode mode) {
path = normalize(path);
if (handles.containsKey(path)) return handles.get(path);
if (in != null && path.equals("in")) return in;
else if (out != null && path.equals("out")) return out;
else if (err != null && path.equals("err")) return err;
else throw new FilesystemException(ErrorReason.DOESNT_EXIST).setAction(ActionType.OPEN).setPath(path);
}
@Override public FileStat stat(String path) {
@Override public synchronized FileStat stat(String path) {
path = normalize(path);
if (handles.containsKey(path)) return new FileStat(Mode.READ_WRITE, EntryType.FILE);
if (path.equals("in") || path.equals("out") || path.equals("err")) return new FileStat(Mode.READ_WRITE, EntryType.FILE);
else return new FileStat(Mode.NONE, EntryType.NONE);
}
@Override public void close() {
handles.clear();
@Override public synchronized void close() {
in = out = err = null;
}
public STDFilesystem add(String name, File handle) {
this.handles.put(name, handle);
return this;
public STDFilesystem(File in, File out, File err) {
this.in = in;
this.out = out;
this.err = err;
}
public static STDFilesystem ofStd(InputStream in, OutputStream out, OutputStream err) {
return new STDFilesystem()
.add("in", File.ofStream(in))
.add("out", File.ofStream(out))
.add("err", File.ofStream(err));
public STDFilesystem(InputStream in, OutputStream out, OutputStream err) {
if (in != null) this.in = File.ofStream(in);
if (out != null) this.out = File.ofStream(out);
if (err != null) this.err = File.ofStream(err);
}
public STDFilesystem(LineReader in, LineWriter out) {
if (in != null) this.in = File.ofLineReader(in);
if (out != null) {
this.out = File.ofLineWriter(out);
this.err = File.ofLineWriter(out);
}
}
}

View File

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

View File

@@ -10,21 +10,22 @@ import java.util.HashSet;
import java.util.stream.Collectors;
import me.topchetoeu.jscript.common.Location;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.Environment;
import me.topchetoeu.jscript.core.WrapperProvider;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Symbol;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
import me.topchetoeu.jscript.core.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Environment;
import me.topchetoeu.jscript.runtime.WrapperProvider;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.values.FunctionValue;
import me.topchetoeu.jscript.runtime.values.NativeFunction;
import me.topchetoeu.jscript.runtime.values.ObjectValue;
import me.topchetoeu.jscript.runtime.values.Symbol;
import me.topchetoeu.jscript.runtime.values.Values;
public class NativeWrapperProvider implements WrapperProvider {
private final HashMap<Class<?>, FunctionValue> constructors = new HashMap<>();
private final HashMap<Class<?>, ObjectValue> prototypes = new HashMap<>();
private final HashMap<Class<?>, ObjectValue> namespaces = new HashMap<>();
private final HashSet<Class<?>> ignore = new HashSet<>();
private final Environment env;
private static Object call(Context ctx, String name, Method method, Object thisArg, Object... args) {
@@ -106,11 +107,12 @@ public class NativeWrapperProvider implements WrapperProvider {
else return name;
}
private static void apply(ObjectValue obj, Environment env, ExposeTarget target, Class<?> clazz) {
private static boolean apply(ObjectValue obj, Environment env, ExposeTarget target, Class<?> clazz) {
var getters = new HashMap<Object, FunctionValue>();
var setters = new HashMap<Object, FunctionValue>();
var props = new HashSet<Object>();
var nonProps = new HashSet<Object>();
var any = false;
for (var method : clazz.getDeclaredMethods()) {
for (var annotation : method.getAnnotationsByType(Expose.class)) {
@@ -120,6 +122,7 @@ public class NativeWrapperProvider implements WrapperProvider {
var name = getName(method, annotation.value());
var key = getKey(name);
var repeat = false;
any = true;
switch (annotation.type()) {
case INIT:
@@ -168,6 +171,7 @@ public class NativeWrapperProvider implements WrapperProvider {
var name = getName(method, annotation.value());
var key = getKey(name);
var repeat = false;
any = true;
if (props.contains(key) || nonProps.contains(key)) repeat = true;
else {
@@ -191,11 +195,29 @@ public class NativeWrapperProvider implements WrapperProvider {
var name = getName(field, annotation.value());
var key = getKey(name);
var repeat = false;
any = true;
if (props.contains(key) || nonProps.contains(key)) repeat = true;
else {
try {
obj.defineProperty(null, key, Values.normalize(new Context(env), field.get(null)), true, true, false);
if (Modifier.isStatic(field.getModifiers())) {
obj.defineProperty(null, key, Values.normalize(new Context(env), 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,6 +232,8 @@ public class NativeWrapperProvider implements WrapperProvider {
}
for (var key : props) obj.defineProperty(null, key, getters.get(key), setters.get(key), true, false);
return any;
}
private static Method getConstructor(Environment env, Class<?> clazz) {
@@ -231,7 +255,7 @@ public class NativeWrapperProvider implements WrapperProvider {
public static ObjectValue makeProto(Environment env, Class<?> clazz) {
var res = new ObjectValue();
res.defineProperty(null, Symbol.get("Symbol.typeName"), getName(clazz));
apply(res, env, ExposeTarget.PROTOTYPE, clazz);
if (!apply(res, env, ExposeTarget.PROTOTYPE, clazz)) return null;
return res;
}
/**
@@ -260,7 +284,7 @@ public class NativeWrapperProvider implements WrapperProvider {
public static ObjectValue makeNamespace(Environment ctx, Class<?> clazz) {
var res = new ObjectValue();
res.defineProperty(null, Symbol.get("Symbol.typeName"), getName(clazz));
apply(res, ctx, ExposeTarget.NAMESPACE, clazz);
if (!apply(res, ctx, ExposeTarget.NAMESPACE, clazz)) return null;
return res;
}
@@ -284,20 +308,27 @@ public class NativeWrapperProvider implements WrapperProvider {
if (constr == null) constr = makeConstructor(env, clazz);
if (proto == null) proto = makeProto(env, clazz);
if (constr == null || proto == null) return;
proto.defineProperty(null, "constructor", constr, true, false, false);
constr.defineProperty(null, "prototype", proto, true, false, false);
prototypes.put(clazz, proto);
constructors.put(clazz, constr);
var parent = clazz.getSuperclass();
if (parent == null) return;
var parent = clazz;
var parentProto = getProto(parent);
var parentConstr = getConstr(parent);
while (true) {
parent = parent.getSuperclass();
if (parent == null) return;
if (parentProto != null) Values.setPrototype(Context.NULL, proto, parentProto);
if (parentConstr != null) Values.setPrototype(Context.NULL, constr, parentConstr);
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);
}
}
public ObjectValue getProto(Class<?> clazz) {
@@ -317,11 +348,23 @@ public class NativeWrapperProvider implements WrapperProvider {
return new NativeWrapperProvider(env);
}
public void setProto(Class<?> clazz, ObjectValue value) {
prototypes.put(clazz, value);
public void set(Class<?> clazz, Class<?> wrapper) {
if (wrapper == null) set(clazz, null, null);
else set(clazz, makeProto(env, wrapper), makeConstructor(env, wrapper));
}
public void setConstr(Class<?> clazz, FunctionValue value) {
constructors.put(clazz, value);
public void set(Class<?> clazz, ObjectValue proto, FunctionValue constructor) {
if (proto != null && constructor != null) {
prototypes.put(clazz, proto);
constructors.put(clazz, constructor);
ignore.remove(clazz);
}
else {
prototypes.remove(clazz);
constructors.remove(clazz);
ignore.remove(clazz);
initType(clazz, constructor, proto);
}
}
private void initError() {
@@ -337,8 +380,7 @@ public class NativeWrapperProvider implements WrapperProvider {
proto.defineProperty(null, "constructor", constr, true, false, false);
constr.defineProperty(null, "prototype", proto, true, false, false);
setProto(Throwable.class, proto);
setConstr(Throwable.class, constr);
set(Throwable.class, proto, constr);
}
public NativeWrapperProvider(Environment env) {

View File

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

View File

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

View File

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

View File

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

View File

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