Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b47d1a7
refactor: remove StackData and Data usage
TopchetoEU Nov 6, 2023
df99328
feat: remove unnececeary @NativeInit directives
TopchetoEU Nov 6, 2023
1acd78e
refactor: clean up Main class
TopchetoEU Nov 13, 2023
1eeac3a
fix: replace templates in Metadata class with placeholder data
TopchetoEU Nov 13, 2023
455f5a6
feat: implement simple permission matching
TopchetoEU Nov 13, 2023
e7dbe91
refactor: clean up protocol.json
TopchetoEU Nov 13, 2023
30f5d61
fix: errors now have the right prototype and name
TopchetoEU Nov 14, 2023
0a4149b
fix: remove double space in "Uncaught ..."
TopchetoEU Nov 14, 2023
ed08041
fix: internal error when trying to use key of "undefined"
TopchetoEU Nov 14, 2023
488deea
fix: improve performance of typescript by caching separate declarations
TopchetoEU Nov 14, 2023
7ecb8bf
feat: implement permissions
TopchetoEU Nov 25, 2023
3e25068
feat: implement bulk of fs
TopchetoEU Nov 25, 2023
55e3d46
feat: implement memory and root fs
TopchetoEU Nov 25, 2023
987f8b8
fix: handle native errors properly-ish
TopchetoEU Nov 25, 2023
4b0dcff
feat: add fs declarations in lib.d.ts
TopchetoEU Nov 25, 2023
829bea7
fix: CodeFrame didnt't handle out of bounds jumps well
TopchetoEU Nov 25, 2023
f5a0b6e
fix: some improvements of compiled bytecode
TopchetoEU Nov 25, 2023
f2b33d0
feat: add support for async iterators
TopchetoEU Nov 25, 2023
1666682
refactor: get rid of data parent hierarchy
TopchetoEU Nov 25, 2023
4111dbf
fix: functions now print their name when stringified
TopchetoEU Nov 25, 2023
4a1473c
fix: sorting with no comparator now works
TopchetoEU Nov 25, 2023
f0ad936
refactor: use new iterable names
TopchetoEU Nov 25, 2023
86d205e
fix: parsing files won't modify last instruction to RET, instead will…
TopchetoEU Nov 25, 2023
e1ce384
feat: add parse function to Filename
TopchetoEU Nov 25, 2023
8b743f4
feat: add toString, equals and hashCode overrides to wrappers and obj…
TopchetoEU Nov 25, 2023
6af3c70
feat: add filesystem libs
TopchetoEU Nov 25, 2023
e107dd3
fix: promise doesn't use microtasks in some cases
TopchetoEU Nov 25, 2023
443dc0f
feat: add await function to promise
TopchetoEU Nov 25, 2023
b6eaff6
style: remove unnececeary import
TopchetoEU Nov 25, 2023
b127aad
fix: promise was sending macro tasks instead of micro
TopchetoEU Nov 25, 2023
4b1ec67
fix: micro tasks not handled properly
TopchetoEU Nov 25, 2023
2cfdd8e
feat: add utf8 encoding and decoding
TopchetoEU Nov 25, 2023
567eaa8
fix: incorrect declarations
TopchetoEU Nov 25, 2023
f52f47c
feat: properly implement filesystems
TopchetoEU Nov 25, 2023
eb14bb0
fix: debugger breakpoints not registered
TopchetoEU Nov 25, 2023
ab56908
fix: faulty insufficient permissions error
TopchetoEU Nov 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,329 changes: 163 additions & 1,166 deletions src/assets/protocol.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/me/topchetoeu/jscript/Filename.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ public Filename(String protocol, String path) {
this.protocol = protocol;
this.path = path;
}

public static Filename parse(String val) {
var i = val.indexOf("://");
if (i >= 0) return new Filename(val.substring(0, i).trim(), val.substring(i + 3).trim());
else return new Filename("file", val.trim());
}
}
144 changes: 89 additions & 55 deletions src/me/topchetoeu/jscript/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.exceptions.InterruptException;
import me.topchetoeu.jscript.exceptions.SyntaxException;
import me.topchetoeu.jscript.filesystem.MemoryFilesystem;
import me.topchetoeu.jscript.filesystem.Mode;
import me.topchetoeu.jscript.filesystem.PhysicalFilesystem;
import me.topchetoeu.jscript.lib.Internals;

public class Main {
static Thread engineTask, debugTask;
static Engine engine;
static Environment env;
static int j = 0;

private static Observer<Object> valuePrinter = new Observer<Object>() {
public class Main {
public static class Printer implements Observer<Object> {
public void next(Object data) {
Values.printValue(null, data);
System.out.println();
Expand All @@ -34,27 +32,80 @@ public void error(RuntimeException err) {
Values.printError(err, null);
}

@Override
public void finish() {
engineTask.interrupt();
}
};
}

public static void main(String args[]) {
System.out.println(String.format("Running %s v%s by %s", Metadata.NAME, Metadata.VERSION, Metadata.AUTHOR));
engine = new Engine(true);
static Thread engineTask, debugTask;
static Engine engine = new Engine(true);
static DebugServer debugServer = new DebugServer();
static Environment environment = new Environment(null, null, null);

var exited = new boolean[1];
var server = new DebugServer();
server.targets.put("target", (ws, req) -> new SimpleDebugger(ws, engine));

env = Internals.apply(new Environment(null, null, null));
static int j = 0;
static boolean exited = false;
static String[] args;

env.global.define("exit", _ctx -> {
exited[0] = true;
private static void reader() {
try {
for (var arg : args) {
try {
if (arg.equals("--ts")) initTypescript();
else {
var file = Path.of(arg);
var raw = Files.readString(file);
var res = engine.pushMsg(
false, new Context(engine, environment),
Filename.fromFile(file.toFile()),
raw, null
).await();
Values.printValue(null, res);
System.out.println();
}
}
catch (EngineException e) { Values.printError(e, null); }
}
for (var i = 0; ; i++) {
try {
var raw = Reading.read();

if (raw == null) break;
var res = engine.pushMsg(
false, new Context(engine, environment),
new Filename("jscript", "repl/" + i + ".js"),
raw, null
).await();
Values.printValue(null, res);
System.out.println();
}
catch (EngineException e) { Values.printError(e, null); }
catch (SyntaxException e) { Values.printError(e, null); }
}
}
catch (IOException e) {
System.out.println(e.toString());
exited = true;
}
catch (RuntimeException ex) {
if (!exited) {
System.out.println("Internal error ocurred:");
ex.printStackTrace();
}
}
if (exited) {
debugTask.interrupt();
engineTask.interrupt();
}
}

private static void initEnv() {
environment = Internals.apply(environment);

environment.global.define("exit", _ctx -> {
exited = true;
throw new InterruptException();
});
env.global.define("go", _ctx -> {
environment.global.define("go", _ctx -> {
try {
var f = Path.of("do.js");
var func = _ctx.compile(new Filename("do", "do/" + j++ + ".js"), new String(Files.readAllBytes(f)));
Expand All @@ -65,9 +116,15 @@ public static void main(String args[]) {
}
});

environment.filesystem.protocols.put("temp", new MemoryFilesystem(Mode.READ_WRITE));
environment.filesystem.protocols.put("file", new PhysicalFilesystem(Path.of(".").toAbsolutePath()));
}
private static void initEngine() {
debugServer.targets.put("target", (ws, req) -> new SimpleDebugger(ws, engine));
engineTask = engine.start();
debugTask = server.start(new InetSocketAddress("127.0.0.1", 9229), true);

debugTask = debugServer.start(new InetSocketAddress("127.0.0.1", 9229), true);
}
private static void initTypescript() {
try {
var tsEnv = Internals.apply(new Environment(null, null, null));
var bsEnv = Internals.apply(new Environment(null, null, null));
Expand All @@ -84,46 +141,23 @@ false, new Context(engine, tsEnv),
engine.pushMsg(
false, ctx,
new Filename("jscript", "internals/bootstrap.js"), Reading.resourceToString("js/bootstrap.js"), null,
tsEnv.global.get(ctx, "ts"), env, new ArrayValue(null, Reading.resourceToString("js/lib.d.ts"))
tsEnv.global.get(ctx, "ts"), environment, new ArrayValue(null, Reading.resourceToString("js/lib.d.ts"))
).await();
}
catch (EngineException e) {
Values.printError(e, "(while initializing TS)");
}
}

var reader = new Thread(() -> {
try {
for (var arg : args) {
try {
var file = Path.of(arg);
var raw = Files.readString(file);
valuePrinter.next(engine.pushMsg(false, new Context(engine).pushEnv(env), Filename.fromFile(file.toFile()), raw, null).await());
}
catch (EngineException e) { Values.printError(e, ""); }
}
for (var i = 0; ; i++) {
try {
var raw = Reading.read();
public static void main(String args[]) {
System.out.println(String.format("Running %s v%s by %s", Metadata.name(), Metadata.version(), Metadata.author()));

Main.args = args;
var reader = new Thread(Main::reader);

initEnv();
initEngine();

if (raw == null) break;
valuePrinter.next(engine.pushMsg(false, new Context(engine).pushEnv(env), new Filename("jscript", "repl/" + i + ".js"), raw, null).await());
}
catch (EngineException e) { Values.printError(e, ""); }
}
}
catch (IOException e) { exited[0] = true; }
catch (SyntaxException ex) {
if (exited[0]) return;
System.out.println("Syntax error:" + ex.msg);
}
catch (RuntimeException ex) {
if (!exited[0]) {
System.out.println("Internal error ocurred:");
ex.printStackTrace();
}
}
if (exited[0]) debugTask.interrupt();
});
reader.setDaemon(true);
reader.setName("STD Reader");
reader.start();
Expand Down
19 changes: 16 additions & 3 deletions src/me/topchetoeu/jscript/Metadata.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package me.topchetoeu.jscript;

public class Metadata {
public static final String VERSION = "${VERSION}";
public static final String AUTHOR = "${AUTHOR}";
public static final String NAME = "${NAME}";
private static final String VERSION = "${VERSION}";
private static final String AUTHOR = "${AUTHOR}";
private static final String NAME = "${NAME}";

public static String version() {
if (VERSION.equals("$" + "{VERSION}")) return "1337-devel";
else return VERSION;
}
public static String author() {
if (AUTHOR.equals("$" + "{AUTHOR}")) return "anonymous";
else return AUTHOR;
}
public static String name() {
if (NAME.equals("$" + "{NAME}")) return "some-product";
else return NAME;
}
}
27 changes: 15 additions & 12 deletions src/me/topchetoeu/jscript/compilation/control/SwitchStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public void declare(ScopeRecord varsScope) {

@Override
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
var caseMap = new HashMap<Integer, Integer>();
var stmIndexMap = new HashMap<Integer, Integer>();
var caseToStatement = new HashMap<Integer, Integer>();
var statementToIndex = new HashMap<Integer, Integer>();

value.compile(target, scope, true);

for (var ccase : cases) {
target.add(Instruction.dup().locate(loc()));
ccase.value.compile(target, scope, true);
target.add(Instruction.operation(Operation.EQUALS).locate(loc()));
caseMap.put(target.size(), ccase.statementI);
caseToStatement.put(target.size(), ccase.statementI);
target.add(Instruction.nop().locate(ccase.value.loc()));
}

Expand All @@ -51,28 +51,31 @@ public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
target.add(Instruction.nop());

for (var stm : body) {
stmIndexMap.put(stmIndexMap.size(), target.size());
statementToIndex.put(statementToIndex.size(), target.size());
stm.compileWithDebug(target, scope, false);
}

if (defaultI < 0 || defaultI >= body.length) target.set(start, Instruction.jmp(target.size() - start).locate(loc()));
else target.set(start, Instruction.jmp(stmIndexMap.get(defaultI) - start)).locate(loc());
int end = target.size();
target.add(Instruction.discard().locate(loc()));
if (pollute) target.add(Instruction.loadValue(null));

if (defaultI < 0 || defaultI >= body.length) target.set(start, Instruction.jmp(end - start).locate(loc()));
else target.set(start, Instruction.jmp(statementToIndex.get(defaultI) - start)).locate(loc());

for (int i = start; i < target.size(); i++) {
for (int i = start; i < end; i++) {
var instr = target.get(i);
if (instr.type == Type.NOP && instr.is(0, "break") && instr.get(1) == null) {
target.set(i, Instruction.jmp(target.size() - i).locate(instr.location));
target.set(i, Instruction.jmp(end - i).locate(instr.location));
}
}
for (var el : caseMap.entrySet()) {
for (var el : caseToStatement.entrySet()) {
var loc = target.get(el.getKey()).location;
var i = stmIndexMap.get(el.getValue());
if (i == null) i = target.size();
var i = statementToIndex.get(el.getValue());
if (i == null) i = end;
target.set(el.getKey(), Instruction.jmpIf(i - el.getKey()).locate(loc));
target.setDebug(el.getKey());
}

target.add(Instruction.discard().locate(loc()));
}

public SwitchStatement(Location loc, Statement value, int defaultI, SwitchCase[] cases, Statement[] body) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CommaStatement extends Statement {
@Override
public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) {
for (var i = 0; i < values.length; i++) {
values[i].compile(target, scope, i == values.length - 1 && pollute);
values[i].compileWithDebug(target, scope, i == values.length - 1 && pollute);
}
}

Expand Down
Loading