打开如图位置的luaj.lua文件
1 local luaj = {} 2 3 local callJavaStaticMethod = LuaJavaBridge.callStaticMethod 4 5 local function checkArguments(args, sig) 6 if type(args) ~= "table" then args = {} end 7 if sig then return args, sig end 8 9 sig = {"("} 10 for i, v in ipairs(args) do 11 local t = type(v) 12 if t == "number" then 13 sig[#sig + 1] = "F" 14 elseif t == "boolean" then 15 sig[#sig + 1] = "Z" 16 elseif t == "function" then 17 sig[#sig + 1] = "I" 18 else 19 sig[#sig + 1] = "Ljava/lang/String;" 20 end 21 end 22 sig[#sig + 1] = ")V" 23 24 return args, table.concat(sig) 25 end 26 27 function luaj.callStaticMethod(className, methodName, args, sig) 28 local args, sig = checkArguments(args, sig) 29 --echoInfo("luaj.callStaticMethod("%s", "%s", args, "%s"", className, methodName, sig) 30 return callJavaStaticMethod(className, methodName, args, sig) 31 end 32 33 return luaj
luaj.callStaticMethod 方法就是调JAVA的函数
我们再看方法最后返回的东西
return callJavaStaticMethod(className, methodName, args, sig)
而文件开始就写了
local callJavaStaticMethod = LuaJavaBridge.callStaticMethod
callJavaStaticMethod 是一个函数类型,再查LuaJavaBridge到底是什么,我们用sublime ctrl+p 搜索一下
这是一个头文件,LuaJavaBridge 就是这个C++写的类,再看它下面的方法
再打开它的
LuaJavaBridge.cpp源文件查看源代码
int LuaJavaBridge::callJavaStaticMethod(lua_State *L) { if (!lua_isstring(L, -4) || !lua_isstring(L, -3) || !lua_istable(L, -2) || !lua_isstring(L, -1)) { lua_pushboolean(L, 0); lua_pushinteger(L, LUAJ_ERR_INVALID_SIGNATURES); return 2; } LOGD("%s", "LuaJavaBridge::callJavaStaticMethod(lua_State *L)"); const char *className = lua_tostring(L, -4); const char *methodName = lua_tostring(L, -3); const char *methodSig = lua_tostring(L, -1); CallInfo call(className, methodName, methodSig); // check args lua_pop(L, 1); /* L: args */ int count = fetchArrayElements(L, -1); /* L: args e1 e2 e3 e4 ... */ jvalue *args = NULL; if (count > 0) { args = new jvalue[count]; for (int i = 0; i < count; ++i) { int index = -count + i; switch (call.argumentTypeAtIndex(i)) { case TypeInteger: if (lua_isfunction(L, index)) { args[i].i = retainLuaFunction(L, index, NULL); } else { args[i].i = (int)lua_tonumber(L, index); } break; case TypeFloat: args[i].f = lua_tonumber(L, index); break; case TypeBoolean: args[i].z = lua_toboolean(L, index) != 0 ? JNI_TRUE : JNI_FALSE; break; case TypeString: default: args[i].l = call.getEnv()->NewStringUTF(lua_tostring(L, index)); break; } } lua_pop(L, count); /* L: args */ } bool success = args ? call.executeWithArgs(args) : call.execute(); if (args) delete []args; if (!success) { LOGD("LuaJavaBridge::callJavaStaticMethod("%s", "%s", args, "%s") EXECUTE FAILURE, ERROR CODE: %d", className, methodName, methodSig, call.getErrorCode()); lua_pushboolean(L, 0); lua_pushinteger(L, call.getErrorCode()); return 2; } LOGD("LuaJavaBridge::callJavaStaticMethod("%s", "%s", args, "%s") SUCCESS", className, methodName, methodSig); lua_pushboolean(L, 1); return 1 + call.pushReturnValue(L); }
细心观察LuaJavaBridge.cpp,文件开头的地方include了jni的头文件
#include "platform/android/jni/JniHelper.h"
再观察 LuaJavaBridge.h 文件开头的地方
extern "C" { #include "lua.h" }
可以发现LuaJavaBridge.cpp也是一层中间代码,负责把lua和java粘合起来
注:在c里面调用了lua.h头文件即可用C调用LUA。后续继续写JAVA调用LUA以及LUA与C++之间的互调