1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

Simplify scripting api; add support for multiple extensions; support lua extensions

This commit is contained in:
gnosygnu
2016-10-15 18:21:46 -04:00
parent 7ccea7ac7a
commit c1d84249e1
18 changed files with 482 additions and 172 deletions

View File

@@ -20,11 +20,7 @@ public interface Gfh_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);
}
class Gfh_script_engine__noop implements Gfh_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 Invoke_method(Object obj, String func, Object... args) {return null;}
Object Invoke_function(String func, Object... args);
}

View 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.langs.htmls.scripts; import gplx.*; import gplx.langs.*; import gplx.langs.htmls.*;
public class Gfh_script_engine_ {
public static Gfh_script_engine New_by_key(String key) {
if (String_.Eq(key, "javascript.java")) return new Gfh_script_engine__javascript();
else if (String_.Eq(key, "lua.luaj")) return new Gfh_script_engine__luaj();
else if (String_.Eq(key, "noop")) return new Gfh_script_engine__noop();
else throw Err_.new_unhandled(key);
}
public static Gfh_script_engine New_by_ext(String ext) {
if (String_.Eq(ext, ".js")) return new Gfh_script_engine__javascript();
else if (String_.Eq(ext, ".lua")) return new Gfh_script_engine__luaj();
else throw Err_.new_unhandled(ext);
}
}

View File

@@ -1,68 +0,0 @@
/*
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.langs.htmls.scripts; import gplx.*; import gplx.langs.*; import gplx.langs.htmls.*;
import java.io.FileReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Gfh_script_engine__java implements Gfh_script_engine {
private final ScriptEngine engine;
private final Invocable invk;
public Gfh_script_engine__java() {
ScriptEngineManager manager = new ScriptEngineManager();
this.engine = manager.getEngineByName("JavaScript");
this.invk = (Invocable)engine;
}
public void Load_script(Io_url url) {
FileReader rdr = null;
try {
rdr = new FileReader(url.Xto_api());
engine.eval(rdr);
// return engine.eval(script);
} catch (Exception e) {
System.out.println(e);
}
finally {
try {
rdr.close();
} catch (Exception e) {
System.out.println(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);
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);
return null;
}
}
}

View File

@@ -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.langs.htmls.scripts; import gplx.*; import gplx.langs.*; import gplx.langs.htmls.*;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Gfh_script_engine__javascript implements Gfh_script_engine {
private final ScriptEngine engine;
private final Invocable invk;
public Gfh_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;
}
}
}

View File

@@ -0,0 +1,108 @@
/*
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.langs.htmls.scripts; import gplx.*; import gplx.langs.*; import gplx.langs.htmls.*;
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.LuaClosure;
import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
public class Gfh_script_engine__luaj implements Gfh_script_engine {
private final ScriptEngine engine;
// private final Invocable invk;
String sss = "";
public Gfh_script_engine__luaj() {
ScriptEngineManager manager = new ScriptEngineManager();
this.engine = manager.getEngineByName("luaj");
// this.invk = (Invocable)engine;
}
public void Load_script(Io_url url) {
try {
sss = Io_mgr.Instance.LoadFilStr(url);
engine.eval(sss);
}
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 {
CompiledScript script = ((Compilable) engine).compile(sss);
Bindings sb = new SimpleBindings();
script.eval(sb);
// LuaTable lfunc = (LuaTable)sb.get("xo_script");
// LuaClosure lfunc2 = (LuaClosure)lfunc.get(func);
LuaValue arg = CoerceJavaToLua.coerce(args[0]);
LuaFunction lfunc = (LuaFunction)sb.get("write_end");
lfunc.call(arg);
return null;
// return engine.eval(obj);
}
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 {
CompiledScript script = ((Compilable) engine).compile(sss);
Bindings sb = new SimpleBindings();
script.eval(sb);
// LuaTable lfunc = (LuaTable)sb.get("xo_script");
// LuaClosure lfunc2 = (LuaClosure)lfunc.get(func);
LuaValue arg = CoerceJavaToLua.coerce(args[0]);
LuaFunction lfunc = (LuaFunction)sb.get(func);
lfunc.call(arg);
return null;
// return engine.eval(obj);
}
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;
}
}
}

View 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.langs.htmls.scripts; import gplx.*; import gplx.langs.*; import gplx.langs.htmls.*;
public class Gfh_script_engine__noop implements Gfh_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;}
}