-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runtime): implement console.log global
feat(runtime): implement console.log global
- Loading branch information
Showing
4 changed files
with
171 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/io/customrealms/runtime/globals/Console.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
109
src/main/java/io/customrealms/runtime/globals/JSFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |