mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Clean up luaj scripting engine; allow multiple lua library files
This commit is contained in:
26
400_xowa/src/gplx/core/scripts/Gfo_script_engine.java
Normal file
26
400_xowa/src/gplx/core/scripts/Gfo_script_engine.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.scripts; import gplx.*; import gplx.core.*;
|
||||
public interface Gfo_script_engine {
|
||||
void Load_script(Io_url url);
|
||||
Object Get_object(String obj_name);
|
||||
void Put_object(String name, Object obj);
|
||||
Object Eval_script(String script);
|
||||
Object Invoke_method(Object obj, String func, Object... args);
|
||||
Object Invoke_function(String func, Object... args);
|
||||
}
|
||||
31
400_xowa/src/gplx/core/scripts/Gfo_script_engine_.java
Normal file
31
400_xowa/src/gplx/core/scripts/Gfo_script_engine_.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.scripts; import gplx.*; import gplx.core.*;
|
||||
public class Gfo_script_engine_ {
|
||||
public static Gfo_script_engine New_by_key(String key) {
|
||||
if (String_.Eq(key, "javascript.java")) return new Gfo_script_engine__javascript();
|
||||
else if (String_.Eq(key, "lua.luaj")) return new Gfo_script_engine__luaj();
|
||||
else if (String_.Eq(key, "noop")) return new Gfo_script_engine__noop();
|
||||
else throw Err_.new_unhandled(key);
|
||||
}
|
||||
public static Gfo_script_engine New_by_ext(String ext) {
|
||||
if (String_.Eq(ext, ".js")) return new Gfo_script_engine__javascript();
|
||||
else if (String_.Eq(ext, ".lua")) return new Gfo_script_engine__luaj();
|
||||
else throw Err_.new_unhandled(ext);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.scripts; import gplx.*; import gplx.core.*;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
public class Gfo_script_engine__javascript implements Gfo_script_engine {
|
||||
private final ScriptEngine engine;
|
||||
private final Invocable invk;
|
||||
public Gfo_script_engine__javascript() {
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
this.engine = manager.getEngineByName("JavaScript");
|
||||
this.invk = (Invocable)engine;
|
||||
}
|
||||
public void Load_script(Io_url url) {
|
||||
try {engine.eval(Io_mgr.Instance.LoadFilStr(url));}
|
||||
catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to load_script; url=~{0} err=~{1}", url, Err_.Message_lang(e));
|
||||
}
|
||||
}
|
||||
public void Put_object(String key, Object val) {
|
||||
engine.put(key, val);
|
||||
}
|
||||
public Object Get_object(String obj_name) {
|
||||
try {return engine.get(obj_name);}
|
||||
catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to get object; obj_name=~{0} err=~{1}", obj_name, Err_.Message_lang(e));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public Object Eval_script(String script) {
|
||||
try {return engine.eval(script);}
|
||||
catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to eval; script=~{0} err=~{1}", script, Err_.Message_lang(e));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public Object Invoke_method(Object obj, String func, Object... args) {
|
||||
try {return invk.invokeMethod(obj, func, args);}
|
||||
catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to invoke method; method=~{0} err=~{1}", func, Err_.Message_lang(e));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public Object Invoke_function(String func, Object... args) {
|
||||
try {return invk.invokeFunction(func, args);}
|
||||
catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to invoke method; method=~{0} err=~{1}", func, Err_.Message_lang(e));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
99
400_xowa/src/gplx/core/scripts/Gfo_script_engine__luaj.java
Normal file
99
400_xowa/src/gplx/core/scripts/Gfo_script_engine__luaj.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.scripts; import gplx.*; import gplx.core.*;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
|
||||
import javax.script.SimpleBindings;
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
|
||||
import org.luaj.vm2.LuaFunction;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
|
||||
import org.luaj.vm2.lib.jse.CoerceLuaToJava;
|
||||
public class Gfo_script_engine__luaj implements Gfo_script_engine {
|
||||
private final ScriptEngine engine;
|
||||
private final List_adp script_list = List_adp_.New();
|
||||
private SimpleBindings bindings;
|
||||
public Gfo_script_engine__luaj() {
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
this.engine = manager.getEngineByName("luaj");
|
||||
}
|
||||
public void Load_script(Io_url url) {
|
||||
try {
|
||||
// create itm and add it to the list of all scripts; list is not used now, but may be used in future
|
||||
String src = Io_mgr.Instance.LoadFilStr(url);
|
||||
script_list.Add(new Gfo_script_itm__luaj(url, src));
|
||||
|
||||
// compile the item into the "bindings" object; this allows scripts to share one "sandbox-space", allowing funcs in one script to call funcs in another
|
||||
CompiledScript compiled = ((Compilable) engine).compile(src);
|
||||
if (bindings == null) bindings = new SimpleBindings();
|
||||
compiled.eval(bindings);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Warn("failed to load_script; url=~{0} err=~{1}", url, Err_.Message_lang(e));
|
||||
}
|
||||
}
|
||||
public void Put_object(String key, Object val) {
|
||||
engine.put(key, val);
|
||||
}
|
||||
public Object Get_object(String obj_name) {
|
||||
try {return engine.get(obj_name);}
|
||||
catch (Exception e) {
|
||||
Warn("failed to get object; obj_name=~{0} err=~{1}", obj_name, Err_.Message_lang(e));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public Object Eval_script(String script) {
|
||||
try {return engine.eval(script);}
|
||||
catch (Exception e) {
|
||||
Warn("", "", "failed to eval; script=~{0} err=~{1}", script, Err_.Message_lang(e));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public Object Invoke_method(Object obj, String func, Object... args) {
|
||||
throw Err_.new_unimplemented_w_msg("luaj does not support invocable interface"); // NOTE: cannot support with Get_object, b/c Get_object needs obj_name, and only "obj" exists
|
||||
}
|
||||
public Object Invoke_function(String func, Object... args) {
|
||||
try {
|
||||
LuaValue arg = CoerceJavaToLua.coerce(args[0]);
|
||||
LuaFunction lfunc = (LuaFunction)bindings.get(func);
|
||||
LuaValue rv = lfunc.call(arg);
|
||||
return CoerceLuaToJava.coerce(rv, Object.class);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Gfo_script_engine__luaj.Warn("", "", "failed to invoke method; method=~{0} err=~{1}", func, Err_.Message_lang(e));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static void Warn(String fmt, Object... args) {
|
||||
String msg = Gfo_usr_dlg_.Instance.Warn_many("", "", fmt, args);
|
||||
System.out.println(msg);
|
||||
}
|
||||
}
|
||||
class Gfo_script_itm__luaj {
|
||||
public Gfo_script_itm__luaj(Io_url url, String src) {
|
||||
this.url = url;
|
||||
this.src = src;
|
||||
}
|
||||
public Io_url Url() {return url;} private final Io_url url;
|
||||
public String Src() {return src;} private final String src;
|
||||
}
|
||||
26
400_xowa/src/gplx/core/scripts/Gfo_script_engine__noop.java
Normal file
26
400_xowa/src/gplx/core/scripts/Gfo_script_engine__noop.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.scripts; import gplx.*; import gplx.core.*;
|
||||
public class Gfo_script_engine__noop implements Gfo_script_engine {
|
||||
public void Load_script(Io_url url) {}
|
||||
public Object Get_object(String obj_name) {return null;}
|
||||
public void Put_object(String name, Object obj) {}
|
||||
public Object Eval_script(String script) {return null;}
|
||||
public Object Invoke_method(Object obj, String func, Object... args) {return null;}
|
||||
public Object Invoke_function(String func, Object... args) {return null;}
|
||||
}
|
||||
Reference in New Issue
Block a user