Compare commits
2 Commits
f3d419085c
...
1548938537
Author | SHA1 | Date | |
---|---|---|---|
1548938537 | |||
e14d85e7a8 |
@ -59,6 +59,8 @@ 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];
|
||||
@ -83,10 +85,20 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
public static Compiler babelCompiler(Compiler prev, Environment target) {
|
||||
return transpilerFromSource(prev, target,
|
||||
@ -111,7 +123,7 @@ public class Compilers {
|
||||
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;
|
||||
|
||||
for (var el : factories) {
|
||||
|
@ -690,5 +690,4 @@ public class Primordials {
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,24 +6,36 @@ 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) {
|
||||
if (env == null) {
|
||||
public static Environment apply(Environment env, CompileResult body) {
|
||||
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", RUNNER.body(), new Value[0][]);
|
||||
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"));
|
||||
}
|
||||
|
||||
return env;
|
||||
}
|
||||
|
||||
public static Environment apply(Environment env) {
|
||||
if (env == null) env = new Environment();
|
||||
return apply(env, RUNNER);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ 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;
|
||||
@ -17,10 +18,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.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.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.runtime.Compiler;
|
||||
import me.topchetoeu.j2s.runtime.Engine;
|
||||
import me.topchetoeu.j2s.runtime.EventLoop;
|
||||
@ -104,11 +105,10 @@ public class SimpleRepl {
|
||||
}
|
||||
|
||||
private static Environment createESEnv() {
|
||||
var env = new Environment();
|
||||
var env = StdLib.apply(null);
|
||||
env.add(EventLoop.KEY, engine);
|
||||
env.add(DebugHandler.KEY, new SimpleDebugHandler());
|
||||
env.add(Compiler.KEY, Compilers.chainTranspilers(environment, Compilers.jsCompiler(), Compilers::babelCompiler, Compilers::coffeescriptCompiler));
|
||||
StdLib.apply(env);
|
||||
env.add(Compiler.KEY, Compilers.chainTranspilers(Compilers.jsCompiler(), env, Compilers::babelCompiler));
|
||||
|
||||
var glob = Value.global(env);
|
||||
|
||||
@ -116,6 +116,18 @@ 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;
|
||||
}
|
||||
|
@ -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));
|
||||
error = e.add(env, function.name, dbg.getMapOrEmpty(env, function).toLocation(codePtr, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ public final class CodeFunction extends FunctionValue {
|
||||
}
|
||||
|
||||
@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);
|
||||
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(env, true, target, self, args, this);
|
||||
var frame = new Frame(this.env, true, target, self, args, this);
|
||||
|
||||
var ret = onCall(frame);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user