lua - Loading java library functions to Luaj -


i stuck loading java functions can called lua file using luaj.

what create :

in some_package/aif.java :

package some_package;  public class aif extends twoargfunction {      public aif() {     }      @override         public luavalue call(luavalue modname, luavalue env) {         luavalue library = tableof();         library.set("foo", new foo());         env.set("aif", library);         return library;     }      //the rest contains implementations of java functions } 

and in lua file :

require "some_package/aif" --etc ... 

and in main.java file :

public static void main(string[] args) {     string script = "lib/some_lua_file.lua";     globals = jseplatform.standardglobals();     luavalue chunk = globals.loadfile(script);     chunk.call( luavalue.valueof(script) );          } 

this code works , want in lua file dont have use "require". have achieved in c++ using line :

lual_requiref(l, "aif", luaopen_aiflib, 1); 

can in luaj? tried :

globals.load(new aif()); 

but gets exception in thread "main" org.luaj.vm2.luaerror: index expected, got nil (variable env in call function of aif class nil)

anybody knows how setup aif lua libary use luaj?

you can write myxargimpl following:

package mypackage;  import org.luaj.vm2.luavalue; import org.luaj.vm2.lib.zeroargfunction;  public class myzeroargimpl extends zeroargfunction {     public luavalue call() {         return valueof("my 0 arg implementation");     } } 

and add lua following:

luavalue globals = jseplatform.standardglobals(); globals.get("dofile").call( luavalue.valueof(yourscriptfile)); globals.set("callmyfunction", new myzeroargimpl()); 

now can call function inside lua script without require('...'):

print(callmyfunction()) 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -