feat: extract log API to console

This commit is contained in:
TopchetoEU 2024-01-12 09:53:56 +02:00
parent 56ae3a85a6
commit 304665904f
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
3 changed files with 38 additions and 25 deletions

View File

@ -711,14 +711,14 @@ public class Values {
res.append("{\n"); res.append("{\n");
for (var el : obj.values.entrySet()) { 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(toReadable(ctx, el.getKey(), passed, tab + 1));
res.append(": "); res.append(": ");
res.append(toReadable(ctx, el.getValue(), passed, tab + 1)); res.append(toReadable(ctx, el.getValue(), passed, tab + 1));
res.append(",\n"); res.append(",\n");
} }
for (var el : obj.properties.entrySet()) { 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(toReadable(ctx, el.getKey(), passed, tab + 1));
res.append(": [prop],\n"); res.append(": [prop],\n");
} }

View File

@ -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;
}
}

View File

@ -1,9 +1,7 @@
package me.topchetoeu.jscript.lib; package me.topchetoeu.jscript.lib;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import me.topchetoeu.jscript.common.Reading;
import me.topchetoeu.jscript.core.engine.Context; import me.topchetoeu.jscript.core.engine.Context;
import me.topchetoeu.jscript.core.engine.Environment; import me.topchetoeu.jscript.core.engine.Environment;
import me.topchetoeu.jscript.core.engine.scope.GlobalScope; import me.topchetoeu.jscript.core.engine.scope.GlobalScope;
@ -34,27 +32,6 @@ public class Internals {
else throw EngineException.ofError("Modules are not supported."); 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) @Expose(target = ExposeTarget.STATIC)
public static Thread __setTimeout(Arguments args) { public static Thread __setTimeout(Arguments args) {
var func = args.convert(0, FunctionValue.class); var func = args.convert(0, FunctionValue.class);