gudzpoz / luajava

Lua for Java on Windows, Mac OS X, Linux, Android. 5.1, 5.2, 5.3, 5.4, LuaJ or LuaJIT.
https://gudzpoz.github.io/luajava/
Other
141 stars 17 forks source link

Vararg JFunction #102

Closed slava110 closed 1 year ago

slava110 commented 1 year ago

Hi. Is it possible to get amount of args in vararg JFunction? I don't see number of arguments anywhere on the stack. Is it not possible to create Java vararg function in such case?

slava110 commented 1 year ago

If I understand correctly, ignored parameter here is the thing I need https://github.com/gudzpoz/luajava/blob/d17aa85f0587f5aa608167de77dc995d2e2105e0/luajava/src/main/java/party/iroiro/luajava/JuaAPI.java#L330 I'm not sure what's the best way to provide it to users without breaking compatibility with previous versions of LuaJava. Maybe something like that?

public interface JFunction {

    int __call(Lua L);

    default int __call(Lua L, int paramCount) {
        return __call(L);
    }
}

Or an additional interface. Or maybe you'll have a better idea. Or just break compatibility with older versions :I

gudzpoz commented 1 year ago

Each function call in Lua gets its own stack, so the number of elements on stack ([Lua::getTop](https://gudzpoz.github.io/luajava/javadoc/party/iroiro/luajava/Lua.html#getTop())) is just the number of parameters. (See lua_CFunction.) Actually I don't remember why I wrote juaFunctionCall like this. (Probably I just wanted to reduce the number of JNI calls, which I don't think is worth it anymore. Maybe I will do some pruning afterwards.) Anyway, using getTop should do the job.

slava110 commented 1 year ago

Yes, it works! getTop is the solution. Didn't know that each function call gets its own stack (maybe missed it in the docs). Thank you very much!