From 304665904fe13f656c52257ff0afaa452f17d7c1 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Fri, 12 Jan 2024 09:53:56 +0200 Subject: [PATCH] feat: extract log API to console --- .../jscript/core/engine/values/Values.java | 4 +-- .../me/topchetoeu/jscript/lib/ConsoleLib.java | 36 +++++++++++++++++++ .../me/topchetoeu/jscript/lib/Internals.java | 23 ------------ 3 files changed, 38 insertions(+), 25 deletions(-) create mode 100644 src/java/me/topchetoeu/jscript/lib/ConsoleLib.java diff --git a/src/java/me/topchetoeu/jscript/core/engine/values/Values.java b/src/java/me/topchetoeu/jscript/core/engine/values/Values.java index 396b1d4..39cd674 100644 --- a/src/java/me/topchetoeu/jscript/core/engine/values/Values.java +++ b/src/java/me/topchetoeu/jscript/core/engine/values/Values.java @@ -711,14 +711,14 @@ public class Values { res.append("{\n"); for (var el : obj.values.entrySet()) { - for (int i = 0; i < tab + 1; i++) System.out.print(" "); + for (int i = 0; i < tab + 1; i++) res.append(" "); res.append(toReadable(ctx, el.getKey(), passed, tab + 1)); res.append(": "); res.append(toReadable(ctx, el.getValue(), passed, tab + 1)); res.append(",\n"); } for (var el : obj.properties.entrySet()) { - for (int i = 0; i < tab + 1; i++) System.out.print(" "); + for (int i = 0; i < tab + 1; i++) res.append(" "); res.append(toReadable(ctx, el.getKey(), passed, tab + 1)); res.append(": [prop],\n"); } diff --git a/src/java/me/topchetoeu/jscript/lib/ConsoleLib.java b/src/java/me/topchetoeu/jscript/lib/ConsoleLib.java new file mode 100644 index 0000000..46a1dda --- /dev/null +++ b/src/java/me/topchetoeu/jscript/lib/ConsoleLib.java @@ -0,0 +1,36 @@ +package me.topchetoeu.jscript.lib; + +import java.io.IOException; +import java.io.OutputStream; + +import me.topchetoeu.jscript.core.engine.values.Values; +import me.topchetoeu.jscript.utils.filesystem.FilesystemException; +import me.topchetoeu.jscript.utils.filesystem.FilesystemException.FSCode; +import me.topchetoeu.jscript.utils.interop.Arguments; +import me.topchetoeu.jscript.utils.interop.Expose; +import me.topchetoeu.jscript.utils.interop.WrapperName; + +@WrapperName("Console") +public class ConsoleLib { + private final OutputStream stream; + + @Expose + public void __log(Arguments args) { + try { + var first = true; + for (var el : args.args) { + if (!first) stream.write(" ".getBytes()); + first = false; + stream.write(Values.toReadable(args.ctx, el).getBytes()); + } + stream.write((byte)'\n'); + } + catch (IOException e) { + throw new FilesystemException("stdout", FSCode.NO_PERMISSIONS_RW); + } + } + + public ConsoleLib(OutputStream stream) { + this.stream = stream; + } +} diff --git a/src/java/me/topchetoeu/jscript/lib/Internals.java b/src/java/me/topchetoeu/jscript/lib/Internals.java index 3e61832..127fa9a 100644 --- a/src/java/me/topchetoeu/jscript/lib/Internals.java +++ b/src/java/me/topchetoeu/jscript/lib/Internals.java @@ -1,9 +1,7 @@ package me.topchetoeu.jscript.lib; -import java.io.IOException; import java.util.HashMap; -import me.topchetoeu.jscript.common.Reading; import me.topchetoeu.jscript.core.engine.Context; import me.topchetoeu.jscript.core.engine.Environment; import me.topchetoeu.jscript.core.engine.scope.GlobalScope; @@ -34,27 +32,6 @@ public class Internals { else throw EngineException.ofError("Modules are not supported."); } - @Expose(target = ExposeTarget.STATIC) - public static Object __log(Arguments args) { - for (var arg : args.args) { - Values.printValue(args.ctx, arg); - System.out.print(" "); - } - System.out.println(); - - return args.get(0); - } - @Expose(target = ExposeTarget.STATIC) - public static String __readline() { - try { - return Reading.readline(); - } - catch (IOException e) { - e.printStackTrace(); - return null; - } - } - @Expose(target = ExposeTarget.STATIC) public static Thread __setTimeout(Arguments args) { var func = args.convert(0, FunctionValue.class);