Compare commits

..

1 Commits

Author SHA1 Message Date
f3d419085c
whitespaces 2025-01-24 22:43:49 +02:00
6 changed files with 22 additions and 57 deletions

View File

@ -59,8 +59,6 @@ public class Compilers {
public static Compiler transpilerFromSource(Compiler prev, Environment target, Filename compilerName, String compilerSrc) {
var env = StdLib.apply(null);
// var handler = new SimpleDebugHandler();
// env.add(DebugHandler.KEY, handler);
var glob = Value.global(env);
var compilerFactory = new FunctionValue[1];
@ -85,19 +83,9 @@ public class Compilers {
}));
var compiled = JavaScript.compile(compilerName, compilerSrc, false);
new CodeFunction(env, "intializer", compiled.body(), new Value[0][]).apply(env, Value.UNDEFINED);
// 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;
}
return wrap(prev, env, target, compilerFactory[0]);
}
public static Compiler babelCompiler(Compiler prev, Environment target) {
@ -123,7 +111,7 @@ public class Compilers {
Compiler create(Compiler prev, Environment target);
}
public static Compiler chainTranspilers(Compiler base, Environment target, TranspilerFactory ...factories) {
public static Compiler chainTranspilers(Environment target, Compiler base, TranspilerFactory ...factories) {
var res = base;
for (var el : factories) {

View File

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

View File

@ -6,36 +6,24 @@ import me.topchetoeu.j2s.common.Metadata;
import me.topchetoeu.j2s.common.Reading;
import me.topchetoeu.j2s.compilation.CompileResult;
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.functions.CodeFunction;
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, CompileResult body) {
env = new Environment();
public static Environment apply(Environment env) {
if (env == null) {
env = new Environment();
}
var stubEnv = new Environment();
Value.global(stubEnv).defineOwnField(stubEnv, "target", Value.global(env));
Value.global(stubEnv).defineOwnField(stubEnv, "primordials", Primordials.create(env));
var func = new CodeFunction(stubEnv, "intializer", body.body(), new Value[0][]);
try {
func.apply(stubEnv, Value.UNDEFINED);
}
catch (EngineException e) {
System.out.println(Value.errorToReadable(env, e, "in environment initializer"));
}
var func = new CodeFunction(stubEnv, "intializer", RUNNER.body(), new Value[0][]);
func.apply(stubEnv, Value.UNDEFINED);
return env;
}
public static Environment apply(Environment env) {
if (env == null) env = new Environment();
return apply(env, RUNNER);
}
}

View File

@ -2,7 +2,6 @@ package me.topchetoeu.j2s.repl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -18,10 +17,10 @@ import me.topchetoeu.j2s.common.Reading;
import me.topchetoeu.j2s.common.SyntaxException;
import me.topchetoeu.j2s.lib.Compilers;
import me.topchetoeu.j2s.lib.StdLib;
import me.topchetoeu.j2s.lib.debug.DebugServer;
import me.topchetoeu.j2s.lib.debug.Debugger;
import me.topchetoeu.j2s.lib.debug.SimpleDebugHandler;
import me.topchetoeu.j2s.lib.debug.SimpleDebugger;
import me.topchetoeu.j2s.repl.debug.SimpleDebugHandler;
import me.topchetoeu.j2s.repl.debug.DebugServer;
import me.topchetoeu.j2s.repl.debug.Debugger;
import me.topchetoeu.j2s.repl.debug.SimpleDebugger;
import me.topchetoeu.j2s.runtime.Compiler;
import me.topchetoeu.j2s.runtime.Engine;
import me.topchetoeu.j2s.runtime.EventLoop;
@ -105,10 +104,11 @@ public class SimpleRepl {
}
private static Environment createESEnv() {
var env = StdLib.apply(null);
var env = new Environment();
env.add(EventLoop.KEY, engine);
env.add(DebugHandler.KEY, new SimpleDebugHandler());
env.add(Compiler.KEY, Compilers.chainTranspilers(Compilers.jsCompiler(), env, Compilers::babelCompiler));
env.add(Compiler.KEY, Compilers.chainTranspilers(environment, Compilers.jsCompiler(), Compilers::babelCompiler, Compilers::coffeescriptCompiler));
StdLib.apply(env);
var glob = Value.global(env);
@ -116,18 +116,6 @@ public class SimpleRepl {
Thread.currentThread().interrupt();
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;
}

View File

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

View File

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