From ce33729fc07023bda7fcbbd5e6cf38a8b2b6ef07 Mon Sep 17 00:00:00 2001 From: connerdouglass Date: Thu, 20 Jun 2024 10:37:20 -0400 Subject: [PATCH] feat(runtime): implement console.log global --- pom.xml | 4 +- .../io/customrealms/jsplugin/JsPlugin.java | 3 +- .../customrealms/runtime/globals/Console.java | 58 ++++++++++ .../runtime/globals/JSFunction.java | 109 ++++++++++++++++++ 4 files changed, 171 insertions(+), 3 deletions(-) create mode 100755 src/main/java/io/customrealms/runtime/globals/Console.java create mode 100644 src/main/java/io/customrealms/runtime/globals/JSFunction.java diff --git a/pom.xml b/pom.xml index 24c604a..e19c75a 100755 --- a/pom.xml +++ b/pom.xml @@ -25,8 +25,8 @@ org.apache.maven.plugins maven-compiler-plugin - 8 - 8 + 9 + 9 diff --git a/src/main/java/io/customrealms/jsplugin/JsPlugin.java b/src/main/java/io/customrealms/jsplugin/JsPlugin.java index 2bd50d4..9e0b757 100644 --- a/src/main/java/io/customrealms/jsplugin/JsPlugin.java +++ b/src/main/java/io/customrealms/jsplugin/JsPlugin.java @@ -38,7 +38,8 @@ private void setup() { // Add globals to the runtime this.server_commands, new BukkitEvents(this.java_plugin, logger), - new Scheduler(this.java_plugin, logger) + new Scheduler(this.java_plugin, logger), + new Console(logger) ); } diff --git a/src/main/java/io/customrealms/runtime/globals/Console.java b/src/main/java/io/customrealms/runtime/globals/Console.java new file mode 100755 index 0000000..e471866 --- /dev/null +++ b/src/main/java/io/customrealms/runtime/globals/Console.java @@ -0,0 +1,58 @@ +package io.customrealms.runtime.globals; + +import io.customrealms.runtime.Global; +import io.customrealms.runtime.Logger; + +import javax.script.Bindings; +import java.util.*; + +public class Console implements Global { + /** + * The logger for the runtime + */ + private final Logger logger; + + public Console(Logger logger) { + this.logger = logger; + } + + public void init(Bindings bindings) { + HashMap console = new HashMap<>(); + console.put("log", new JSFunction(this::jsConsoleLog)); + console.put("warn", new JSFunction(this::jsConsoleWarn)); + console.put("error", new JSFunction(this::jsConsoleError)); + + bindings.put("console", console); + } + + /** + * Releases all the values tying the runtime to the plugin + */ + public void release() {} + + private static String joinLogArgs(Object[] args) { + StringBuilder str = new StringBuilder(); + for (int i = 0; i < args.length; i++) { + if (i > 0) { + str.append("\t"); + } + str.append(args[i]); + } + return str.toString(); + } + + private Object jsConsoleLog(Object thiz, Object... args) { + this.logger.log(Logger.LogType.LOG, Console.joinLogArgs(args)); + return null; + } + + private Object jsConsoleWarn(Object thiz, Object... args) { + this.logger.log(Logger.LogType.WARNING, Console.joinLogArgs(args)); + return null; + } + + private Object jsConsoleError(Object thiz, Object... args) { + this.logger.log(Logger.LogType.ERROR, Console.joinLogArgs(args)); + return null; + } +} diff --git a/src/main/java/io/customrealms/runtime/globals/JSFunction.java b/src/main/java/io/customrealms/runtime/globals/JSFunction.java new file mode 100644 index 0000000..b861852 --- /dev/null +++ b/src/main/java/io/customrealms/runtime/globals/JSFunction.java @@ -0,0 +1,109 @@ +package io.customrealms.runtime.globals; + +import org.openjdk.nashorn.api.scripting.JSObject; + +import java.util.Collection; +import java.util.List; +import java.util.Set; + +public class JSFunction implements JSObject { + public interface Runnable { + Object call(Object thiz, Object... args); + } + + private final Runnable runnable; + + public JSFunction(Runnable runnable) { + this.runnable = runnable; + } + + @Override + public Object call(Object thiz, Object... args) { + return this.runnable.call(thiz, args); + } + + @Override + public Object newObject(Object... args) { + return null; + } + + @Override + public Object eval(String s) { + return null; + } + + @Override + public Object getMember(String name) { + return null; + } + + @Override + public Object getSlot(int index) { + return null; + } + + @Override + public boolean hasMember(String name) { + return false; + } + + @Override + public boolean hasSlot(int slot) { + return false; + } + + @Override + public void removeMember(String name) { + + } + + @Override + public void setMember(String name, Object value) { + + } + + @Override + public void setSlot(int index, Object value) { + + } + + @Override + public Set keySet() { + return Set.of(); + } + + @Override + public Collection values() { + return List.of(); + } + + @Override + public boolean isInstance(Object instance) { + return false; + } + + @Override + public boolean isInstanceOf(Object clazz) { + return false; + } + + @Override + public String getClassName() { + return "Function"; + } + + @Override + public boolean isFunction() { + return true; + } + + @Override + public boolean isStrictFunction() { + return false; + } + + @Override + public boolean isArray() { + return false; + } +}