From 29b0c91c5d778d4149f3daef6df30ca93db465b3 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Wed, 15 Jan 2025 20:12:52 +0200 Subject: [PATCH] fix: function mappings were registered incorrectly --- README.md | 4 ++-- .../j2s/compilation/CompileResult.java | 19 +++++++++++-------- .../me/topchetoeu/j2s/repl/SimpleRepl.java | 6 +++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 589fb8c..ab56bb3 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,8 @@ env.add(Compiler.KEY, (_env, filename, raw, mapper) -> { // We'll register the source and function source mappings for debugging DebugContext.get(env).onSource(filename, raw); - for (var el : res.bodies()) { - DebugContext.get(env).onFunctionLoad(el, res.map(mapper)); + for (var el : res.all()) { + DebugContext.get(env).onFunctionLoad(el.body(), el.map(mapper)); } // Finally, we will construct the function diff --git a/compilation/src/main/java/me/topchetoeu/j2s/compilation/CompileResult.java b/compilation/src/main/java/me/topchetoeu/j2s/compilation/CompileResult.java index e1f8c7c..e55e956 100644 --- a/compilation/src/main/java/me/topchetoeu/j2s/compilation/CompileResult.java +++ b/compilation/src/main/java/me/topchetoeu/j2s/compilation/CompileResult.java @@ -25,6 +25,7 @@ import java.util.LinkedList; public final class CompileResult { public static final Key DEBUG_LOG = new Key<>(); + private FunctionBody body; public final List instructions; public final List children; public final Map childrenMap = new HashMap<>(); @@ -71,17 +72,17 @@ public final class CompileResult { setLocationAndDebug(instructions.size() - 1, loc, type); } - public Iterable bodies() { - var stack = new Stack(); - stack.push(body()); + public Iterable all() { + var stack = new Stack(); + stack.push(this); - return () -> new Iterator() { - @Override public FunctionBody next() { + return () -> new Iterator() { + @Override public CompileResult next() { if (stack.empty()) return null; else { var res = stack.pop(); - for (var el : res.children) { - stack.push(el); + for (var child : res.children) { + stack.push(child); } return res; } @@ -110,6 +111,8 @@ public final class CompileResult { return map.build(scope.localNames(), scope.capturableNames(), scope.captureNames()); } public FunctionBody body() { + if (body != null) return body; + var builtChildren = new FunctionBody[children.size()]; for (var i = 0; i < children.size(); i++) builtChildren[i] = children.get(i).body(); @@ -124,7 +127,7 @@ public final class CompileResult { for (var instr : instrRes) System.out.println(instr); } - return new FunctionBody( + return body = new FunctionBody( scope.localsCount(), scope.capturablesCount(), scope.capturesCount(), length, instrRes, builtChildren ); diff --git a/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java b/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java index 680426f..7807517 100644 --- a/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java +++ b/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java @@ -62,8 +62,8 @@ public class SimpleRepl { var body = res.body(); DebugContext.get(env).onSource(filename, raw); - for (var el : res.bodies()) { - DebugContext.get(env).onFunctionLoad(el, res.map(mapper)); + for (var el : res.all()) { + DebugContext.get(env).onFunctionLoad(el.body(), el.map(mapper)); } return new CodeFunction(env, filename.toString(), body, new Value[0][]); @@ -708,7 +708,7 @@ public class SimpleRepl { res.defineOwnField(env, "construct", new NativeFunction(args -> { var func = (FunctionValue)args.get(0); var target = args.get(1); - var funcArgs = (ArrayValue)args.get(2); + var funcArgs = (ArrayLikeValue)args.get(2); if (target == Value.UNDEFINED) return func.constructNoSelf(env, funcArgs.toArray()); else return func.construct(env, target, funcArgs.toArray());