public int luaCB_length(Lua l, LuaPlugin plugin) { try { l.pushNumber((double) object.length()); } catch (IOException e) { l.pushNil(); l.pushString("IOException: " + e.getMessage()); return 2; } return 1; }
public int luaCB_read(Lua l, LuaPlugin plugin) { l.pushNil(); l.pushNil(); try { long offset = (long) l.checkNumber(2); int length = (int) l.checkNumber(3); if (length <= 0) { l.pushNil(); l.pushString("Length is not positive"); return 2; } byte[] tmp = new byte[length]; object.seek(offset); length = object.read(tmp); if (length < 0) { l.pushNil(); l.pushNil(); return 2; } StringBuffer buf = new StringBuffer(length); for (byte x : tmp) buf.appendCodePoint((int) x & 0xFF); l.push(buf.toString()); } catch (IOException e) { l.pushNil(); l.pushString("IOException: " + e.getMessage()); return 2; } return 1; }
public int luaCB_close(Lua l, LuaPlugin plugin) { try { plugin.destroyLuaObject(l); l.pushBoolean(true); } catch (IOException e) { l.pushBoolean(false); l.pushString("IOException: " + e.getMessage()); return 2; } return 1; }
public int luaCB_set_length(Lua l, LuaPlugin plugin) { l.pushNil(); try { object.setLength((long) l.checkNumber(2)); } catch (IOException e) { l.pushNil(); l.pushString("IOException: " + e.getMessage()); return 2; } return 1; }
public int luaCB_four_to_five(Lua l, LuaPlugin plugin) { try { plugin.generateLuaClass(l, new BinaryOutFile(plugin, new FourToFiveEncoder(object))); } catch (IOException e) { l.pushNil(); l.pushString("IOException: " + e.getMessage()); return 2; } catch (IllegalArgumentException e) { l.pushNil(); l.pushString("Illegal argument: " + e.getMessage()); return 2; } return 1; }
public int luaCB_deflate(Lua l, LuaPlugin plugin) { try { plugin.generateLuaClass(l, new BinaryOutFile(plugin, new DeflaterOutputStream(object))); } catch (IOException e) { l.pushNil(); l.pushString("IOException: " + e.getMessage()); return 2; } catch (IllegalArgumentException e) { l.pushNil(); l.pushString("Illegal argument: " + e.getMessage()); return 2; } return 1; }
public int luaCB_text(Lua l, LuaPlugin plugin) { try { plugin.generateLuaClass(l, new TextOutFile(plugin, object)); } catch (IOException e) { l.pushNil(); l.pushString("IOException: " + e.getMessage()); return 2; } catch (IllegalArgumentException e) { l.pushNil(); l.pushString("Illegal argument: " + e.getMessage()); return 2; } return 1; }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); state = savedInstanceState; Intent intent = getIntent(); modName = intent.getStringExtra("LUA_MODULE"); argRef = intent.getIntExtra("LUA_MODULE_ARG", 0); Lua.bind(this, this); }
@Override public boolean onOptionsItemSelected(MenuItem item) { Object res = service.invokeMethod(modTable, "onOptionsItemSelected", item); if (res == null) { return super.onOptionsItemSelected(item); } else { return true; } }
public void testModel() { Model model = null; File file = new File("models/simple.lua"); try { model = Lua.newModel(file); } catch (ModelException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace(); throw e; } System.out.println("name: " + model.getName()); System.out.println("number of parameters: " + model.getNPar()); System.out.println("number of variables: " + model.getNVar()); System.out.println("description: " + model.getDescription()); }
public void testLuaModel() { LuaModel model = null; File file = new File("models/simple.lua"); try { model = (LuaModel) Lua.newModel(file); } catch (ModelException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace(); throw e; } assertFalse(model.getHas_inverse() != 0); double[] result = new double[2]; model.f(new double[] {1}, new double[] {0.5, 0.5}, result); assertTrue(0.25 == result[0]); assertTrue(0.25 == result[1]); }
public static int luaCB_open(Lua l, LuaPlugin plugin) { l.pushNil(); String name = l.checkString(1); try { plugin.generateLuaClass(l, new BinaryOutFile(plugin, new FileOutputStream(name))); } catch (IOException e) { l.pushNil(); l.pushString("IOException: " + e.getMessage()); return 2; } catch (IllegalArgumentException e) { l.pushNil(); l.pushString("Illegal argument: " + e.getMessage()); return 2; } return 1; }
public void onServiceConnected(ComponentName name, IBinder iservice) { Log.d("lua", "setting activity"); service = ((Lua.LocalBinder) iservice).getService(); modTable = service.require(modName); if (modTable == null) { finish(); return; } service.setGlobal("current_activity", this); Object res; Object arg = null; if (argRef != 0) { arg = LuaObject.fromReference(Lua.L, argRef); } try { if (modTable.isFunction()) { LuaObject android = service.require("android"); LuaObject aNew = android.getField("new"); res = aNew.call(new Object[] {modTable}); modTable = (LuaObject) res; } res = service.invokeMethod(modTable, "onCreate", this, arg, state); } catch (LuaException e) { log("onCreate " + e.getMessage()); res = null; } if (res == null) { finish(); return; } if (res instanceof View) { setContentView((View) res); } else if (!(res instanceof Boolean)) { log("onCreate must return a View"); finish(); return; } service.invokeMethod(modTable, "onStart"); if (state != null) { super.onRestoreInstanceState(state); } service.invokeMethod(modTable, "onResume"); }
public int luaCB_write(Lua l, LuaPlugin plugin) { l.pushNil(); l.pushNil(); try { long offset = (long) l.checkNumber(2); String towrite = l.checkString(3); if (towrite == "") { l.pushBoolean(true); return 1; } byte[] tmp = new byte[towrite.length()]; object.seek(offset); for (int i = 0; i < towrite.length(); i++) tmp[i] = (byte) towrite.charAt(i); object.write(tmp); l.pushBoolean(true); } catch (IOException e) { l.pushBoolean(false); l.pushString("IOException: " + e.getMessage()); return 2; } return 1; }
@Override public void onDestroy() { super.onDestroy(); service.invokeMethod(modTable, "onDestroy"); Lua.unbind(this, this); }
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); service.invokeMethod(modTable, "onCreateContextMenu", menu, v, menuInfo); }
public void log(String msg) { Lua.log(msg); }
@Override protected void onActivityResult(int request, int result, Intent data) { service.invokeMethod(modTable, "onActivityResult", request, result, data); }
@Override public void onPause() { super.onPause(); service.invokeMethod(modTable, "onPause"); }
@Override public void onStart() { super.onStart(); if (service != null) service.invokeMethod(modTable, "onStart"); }
@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (service != null) service.invokeMethod(modTable, "onSaveInstanceState", outState); }
@Override public boolean onCreateOptionsMenu(Menu menu) { Object res = service.invokeMethod(modTable, "onCreateOptionsMenu", menu); return res != null ? (Boolean) res : false; }
@Override public void onStop() { super.onStop(); service.invokeMethod(modTable, "onStop"); }