Skip to content

Commit

Permalink
feat(runtime): implement console.log global
Browse files Browse the repository at this point in the history
feat(runtime): implement console.log global
  • Loading branch information
connerdouglass authored Jun 20, 2024
2 parents f8199cf + ce33729 commit 7f01414
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
<plugin>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/customrealms/jsplugin/JsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/java/io/customrealms/runtime/globals/Console.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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;
}
}
109 changes: 109 additions & 0 deletions src/main/java/io/customrealms/runtime/globals/JSFunction.java
Original file line number Diff line number Diff line change
@@ -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<String> keySet() {
return Set.of();
}

@Override
public Collection<Object> 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;
}
}

0 comments on commit 7f01414

Please sign in to comment.