Compare commits

..

2 Commits

Author SHA1 Message Date
1548938537
small fixes
Some checks failed
tagged-release / Tagged Release (push) Has been cancelled
2025-01-24 22:46:51 +02:00
e14d85e7a8
whitespaces 2025-01-24 22:45:14 +02:00
6 changed files with 57 additions and 22 deletions

View File

@ -59,6 +59,8 @@ public class Compilers {
public static Compiler transpilerFromSource(Compiler prev, Environment target, Filename compilerName, String compilerSrc) { public static Compiler transpilerFromSource(Compiler prev, Environment target, Filename compilerName, String compilerSrc) {
var env = StdLib.apply(null); var env = StdLib.apply(null);
// var handler = new SimpleDebugHandler();
// env.add(DebugHandler.KEY, handler);
var glob = Value.global(env); var glob = Value.global(env);
var compilerFactory = new FunctionValue[1]; var compilerFactory = new FunctionValue[1];
@ -83,9 +85,19 @@ public class Compilers {
})); }));
var compiled = JavaScript.compile(compilerName, compilerSrc, false); var compiled = JavaScript.compile(compilerName, compilerSrc, false);
new CodeFunction(env, "intializer", compiled.body(), new Value[0][]).apply(env, Value.UNDEFINED);
return wrap(prev, env, target, compilerFactory[0]); // for (var el : compiled.all()) {
// handler.onFunctionLoad(el.body(), el.map());
// }
try {
new CodeFunction(env, "intializer", compiled.body(), new Value[0][]).apply(env, Value.UNDEFINED);
return wrap(prev, env, target, compilerFactory[0]);
}
catch (EngineException e) {
System.out.println(Value.errorToReadable(env, e, "in transpiler initializer"));
return prev;
}
} }
public static Compiler babelCompiler(Compiler prev, Environment target) { public static Compiler babelCompiler(Compiler prev, Environment target) {
@ -111,7 +123,7 @@ public class Compilers {
Compiler create(Compiler prev, Environment target); Compiler create(Compiler prev, Environment target);
} }
public static Compiler chainTranspilers(Environment target, Compiler base, TranspilerFactory ...factories) { public static Compiler chainTranspilers(Compiler base, Environment target, TranspilerFactory ...factories) {
var res = base; var res = base;
for (var el : factories) { for (var el : factories) {

View File

@ -690,5 +690,4 @@ public class Primordials {
return res; return res;
} }
} }

View File

@ -6,24 +6,36 @@ import me.topchetoeu.j2s.common.Metadata;
import me.topchetoeu.j2s.common.Reading; import me.topchetoeu.j2s.common.Reading;
import me.topchetoeu.j2s.compilation.CompileResult; import me.topchetoeu.j2s.compilation.CompileResult;
import me.topchetoeu.j2s.compilation.JavaScript; import me.topchetoeu.j2s.compilation.JavaScript;
import me.topchetoeu.j2s.runtime.exceptions.EngineException;
import me.topchetoeu.j2s.runtime.values.Value; import me.topchetoeu.j2s.runtime.values.Value;
import me.topchetoeu.j2s.runtime.values.functions.CodeFunction; import me.topchetoeu.j2s.runtime.values.functions.CodeFunction;
public class StdLib { public class StdLib {
private static final CompileResult RUNNER = JavaScript.compile(new Filename(Metadata.name(), "init.js"), Reading.resourceToString("lib/stdlib.js"), false); private static final CompileResult RUNNER = JavaScript.compile(
new Filename(Metadata.name(), "init.js"),
Reading.resourceToString("lib/stdlib.js"), false
);
public static Environment apply(Environment env) { public static Environment apply(Environment env, CompileResult body) {
if (env == null) { env = new Environment();
env = new Environment();
}
var stubEnv = new Environment(); var stubEnv = new Environment();
Value.global(stubEnv).defineOwnField(stubEnv, "target", Value.global(env)); Value.global(stubEnv).defineOwnField(stubEnv, "target", Value.global(env));
Value.global(stubEnv).defineOwnField(stubEnv, "primordials", Primordials.create(env)); Value.global(stubEnv).defineOwnField(stubEnv, "primordials", Primordials.create(env));
var func = new CodeFunction(stubEnv, "intializer", RUNNER.body(), new Value[0][]); var func = new CodeFunction(stubEnv, "intializer", body.body(), new Value[0][]);
func.apply(stubEnv, Value.UNDEFINED); try {
func.apply(stubEnv, Value.UNDEFINED);
}
catch (EngineException e) {
System.out.println(Value.errorToReadable(env, e, "in environment initializer"));
}
return env; return env;
} }
public static Environment apply(Environment env) {
if (env == null) env = new Environment();
return apply(env, RUNNER);
}
} }

View File

@ -2,6 +2,7 @@ package me.topchetoeu.j2s.repl;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -17,10 +18,10 @@ import me.topchetoeu.j2s.common.Reading;
import me.topchetoeu.j2s.common.SyntaxException; import me.topchetoeu.j2s.common.SyntaxException;
import me.topchetoeu.j2s.lib.Compilers; import me.topchetoeu.j2s.lib.Compilers;
import me.topchetoeu.j2s.lib.StdLib; import me.topchetoeu.j2s.lib.StdLib;
import me.topchetoeu.j2s.repl.debug.SimpleDebugHandler; import me.topchetoeu.j2s.lib.debug.DebugServer;
import me.topchetoeu.j2s.repl.debug.DebugServer; import me.topchetoeu.j2s.lib.debug.Debugger;
import me.topchetoeu.j2s.repl.debug.Debugger; import me.topchetoeu.j2s.lib.debug.SimpleDebugHandler;
import me.topchetoeu.j2s.repl.debug.SimpleDebugger; import me.topchetoeu.j2s.lib.debug.SimpleDebugger;
import me.topchetoeu.j2s.runtime.Compiler; import me.topchetoeu.j2s.runtime.Compiler;
import me.topchetoeu.j2s.runtime.Engine; import me.topchetoeu.j2s.runtime.Engine;
import me.topchetoeu.j2s.runtime.EventLoop; import me.topchetoeu.j2s.runtime.EventLoop;
@ -104,11 +105,10 @@ public class SimpleRepl {
} }
private static Environment createESEnv() { private static Environment createESEnv() {
var env = new Environment(); var env = StdLib.apply(null);
env.add(EventLoop.KEY, engine); env.add(EventLoop.KEY, engine);
env.add(DebugHandler.KEY, new SimpleDebugHandler()); env.add(DebugHandler.KEY, new SimpleDebugHandler());
env.add(Compiler.KEY, Compilers.chainTranspilers(environment, Compilers.jsCompiler(), Compilers::babelCompiler, Compilers::coffeescriptCompiler)); env.add(Compiler.KEY, Compilers.chainTranspilers(Compilers.jsCompiler(), env, Compilers::babelCompiler));
StdLib.apply(env);
var glob = Value.global(env); var glob = Value.global(env);
@ -116,6 +116,18 @@ public class SimpleRepl {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new CancellationException(); throw new CancellationException();
})); }));
glob.defineOwnField(null, "dofile", new NativeFunction("dofile", args -> {
var file = args.get(0).toString(args.env);
var filename = new Filename("file", new File(file).getAbsolutePath());
try {
var src = Reading.streamToString(new FileInputStream(file));
return Compiler.get(args.env).compile(args.env, filename, src, v -> v).apply(args.env, Value.UNDEFINED);
}
catch (FileNotFoundException e) {
throw EngineException.ofError("IOException", e.getMessage());
}
}));
return env; return env;
} }

View File

@ -209,7 +209,7 @@ public final class Frame {
returnValue = InstructionRunner.exec(env, instr, this); returnValue = InstructionRunner.exec(env, instr, this);
} }
catch (EngineException e) { catch (EngineException e) {
error = e.add(env, function.name, dbg.getMapOrEmpty(env, function).toLocation(codePtr)); error = e.add(env, function.name, dbg.getMapOrEmpty(env, function).toLocation(codePtr, true));
} }
} }
} }

View File

@ -26,7 +26,7 @@ public final class CodeFunction extends FunctionValue {
} }
@Override protected Value onApply(Environment env, Value self, Value... args) { @Override protected Value onApply(Environment env, Value self, Value... args) {
var frame = new Frame(env, false, null, self, args, this); var frame = new Frame(this.env, false, null, self, args, this);
var res = onCall(frame); var res = onCall(frame);
return res; return res;
} }
@ -37,7 +37,7 @@ public final class CodeFunction extends FunctionValue {
if (proto instanceof ObjectValue) self.setPrototype(env, (ObjectValue)proto); if (proto instanceof ObjectValue) self.setPrototype(env, (ObjectValue)proto);
else if (proto == Value.NULL) self.setPrototype(env, null); else if (proto == Value.NULL) self.setPrototype(env, null);
var frame = new Frame(env, true, target, self, args, this); var frame = new Frame(this.env, true, target, self, args, this);
var ret = onCall(frame); var ret = onCall(frame);