2

So, I have a script that goes like so:

function testfunction()
    print("Test from testfunction");
end

I am able to call Java functions from Lua, but how do I accomplish the opposite? How do I call a Lua function from Java using LuaJ?

PuppyKevin
  • 2,967
  • 7
  • 25
  • 27

4 Answers4

12

I was looking around to solve this same problem and I came up with something very similar to Profetylen's answer

test.java:

import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;

public class test
{
    public static void main(String[] args)
    {
        //run the lua script defining your function
        LuaValue _G = JsePlatform.standardGlobals();
        _G.get("dofile").call( LuaValue.valueOf("./test.lua"));

        //call the function MyAdd with two parameters 5, and 5
        LuaValue MyAdd = _G.get("MyAdd");
        LuaValue retvals = MyAdd.call(LuaValue.valueOf(5), LuaValue.valueOf(5));

        //print out the result from the lua function
        System.out.println(retvals.tojstring(1));
    }
}

test.lua:

function MyAdd( num1, num2 )
    return num1 + num2
end
demon_ds1
  • 164
  • 1
  • 7
2

I don't know, maybe this is what you want to do: (This will get you the return values inside Scala)

Main.scala:

import org.luaj.vm2._
import org.luaj.vm2.lib.jse._

object Main extends App {
    val global = JsePlatform.standardGlobals()
    global.get("dofile").call(LuaValue.valueOf("test.lua"))

    val args: Array[LuaValue] = Array(LuaValue.valueof(1), LuaValue.valueof(2))
    val retVals: LuaValue.Vararg = global.get("add").invoke(LuaValue.varargsOf(args))

    println(retVals.arg(1))
}

test.lua:

function add(a, b)
    a = a or 0
    b = b or 0
    return a + b
end

Note: I didn't test the code, but it think something like that should work.

edit: While googling around, I didn't realize this question had really nothing to do with Scala (I was myself looking to solve the problem in Scala when I got here). However, demon_ds1 seems to have found a solution in Java based on my answer and now he gave his own answer, so I'm glad something good came out of this confusing answer!

nijoakim
  • 930
  • 10
  • 25
1

Taking a quick look at the LuaJ docs it seems to me that you have to get the org.luaj.vm2.LuaFunction object, representing the Lua function then apply the invoke method on it.

jpjacobs
  • 9,359
  • 36
  • 45
  • Mind being more precise? I don't see any way to grab a LuaFunction object, other than when the script is loaded (which wouldn't work, how would I invoke the method itself?) – PuppyKevin Jun 29 '12 at 20:38
  • Also, upon searching more, [this question](http://stackoverflow.com/questions/8956564/luaj-import-lua-methods) is the exact same question as I have, but the asker didn't leave his method of getting the job done. – PuppyKevin Jun 29 '12 at 20:57
  • I don't think this is the right answer since the LuaFunction is for internal use, for a Java developer to interpret/execute a lua library function. – Splash Aug 16 '14 at 22:26
0

The examples are really helpful. It is very simple to call Lua code from java anyways, since you can always do it via something like loadstring.

https://github.com/sylvanaar/luaj/blob/master/luaj-vm/examples/jse/SampleJseMain.java

https://github.com/sylvanaar/luaj/blob/master/luaj-vm/examples/jse/ScriptEngineSample.java

sylvanaar
  • 8,096
  • 37
  • 59