83 lines
2.8 KiB
Java
83 lines
2.8 KiB
Java
|
|
package org.openautonomousconnection.luascript.tables;
|
||
|
|
|
||
|
|
import org.luaj.vm2.LuaValue;
|
||
|
|
import org.luaj.vm2.lib.*;
|
||
|
|
import org.openautonomousconnection.luascript.events.JavaToLua;
|
||
|
|
import org.openautonomousconnection.luascript.hosts.HostServices;
|
||
|
|
import org.openautonomousconnection.luascript.hosts.UtilHost;
|
||
|
|
import org.openautonomousconnection.luascript.utils.ScriptTable;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Lua table: util
|
||
|
|
*
|
||
|
|
* <p>Functions:</p>
|
||
|
|
* <ul>
|
||
|
|
* <li>util.base64Encode(text)</li>
|
||
|
|
* <li>util.base64Decode(b64)</li>
|
||
|
|
* <li>util.randomHex(bytes)</li>
|
||
|
|
* <li>util.parseUrl(url) -> map</li>
|
||
|
|
* <li>util.parseQuery(query) -> map(key->array)</li>
|
||
|
|
* <li>util.jsonStringifyExpr(elementId, jsExpr) -> jsonString</li>
|
||
|
|
* <li>util.jsonNormalize(elementId, json) -> jsonString</li>
|
||
|
|
* </ul>
|
||
|
|
*/
|
||
|
|
public final class UtilTable extends ScriptTable {
|
||
|
|
|
||
|
|
public UtilTable() {
|
||
|
|
super("util");
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
protected void define(HostServices services) {
|
||
|
|
UtilHost host = services.util().orElseThrow(() -> new IllegalStateException("UtilHost not provided"));
|
||
|
|
|
||
|
|
table().set("base64Encode", new OneArgFunction() {
|
||
|
|
@Override
|
||
|
|
public LuaValue call(LuaValue text) {
|
||
|
|
return LuaValue.valueOf(host.base64Encode(text.isnil() ? "" : text.tojstring()));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
table().set("base64Decode", new OneArgFunction() {
|
||
|
|
@Override
|
||
|
|
public LuaValue call(LuaValue b64) {
|
||
|
|
return LuaValue.valueOf(host.base64Decode(b64.isnil() ? "" : b64.tojstring()));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
table().set("randomHex", new OneArgFunction() {
|
||
|
|
@Override
|
||
|
|
public LuaValue call(LuaValue bytes) {
|
||
|
|
return LuaValue.valueOf(host.randomHex(bytes.checkint()));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
table().set("parseUrl", new OneArgFunction() {
|
||
|
|
@Override
|
||
|
|
public LuaValue call(LuaValue url) {
|
||
|
|
return JavaToLua.coerce(host.parseUrl(url.checkjstring()));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
table().set("parseQuery", new OneArgFunction() {
|
||
|
|
@Override
|
||
|
|
public LuaValue call(LuaValue q) {
|
||
|
|
return JavaToLua.coerce(host.parseQuery(q.isnil() ? "" : q.tojstring()));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
table().set("jsonStringifyExpr", new TwoArgFunction() {
|
||
|
|
@Override
|
||
|
|
public LuaValue call(LuaValue elementId, LuaValue jsExpr) {
|
||
|
|
return LuaValue.valueOf(host.jsonStringifyExpr(elementId.checkjstring(), jsExpr.checkjstring()));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
table().set("jsonNormalize", new TwoArgFunction() {
|
||
|
|
@Override
|
||
|
|
public LuaValue call(LuaValue elementId, LuaValue json) {
|
||
|
|
return LuaValue.valueOf(host.jsonNormalize(elementId.checkjstring(), json.checkjstring()));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|