src/main/c/jnlua.c
static int calljavafunction(lua_State *L) {
	jobject luastate_obj_old, javastate, javafunction;
	lua_State *T;
	int nresults;
	jthrowable throwable;
	jstring where;
	jobject luaerror;

	/* Get Java state. */
	lua_getfield(L, LUA_REGISTRYINDEX, JNLUA_JAVASTATE);
	if (!lua_isuserdata(L, -1)) {
		/* Java state has been cleared as the Java VM was destroyed. Cannot call. */
		lua_pushliteral(L, "no Java state");
		return lua_error(L);
	}
	javastate = *(jobject *)lua_touserdata(L, -1);
	lua_pop(L, 1);


	/* Get Java function object. */
	lua_pushvalue(L, lua_upvalueindex(1));
	javafunction = tojavaobject(L, -1, javafunction_interface);
	lua_pop(L, 1);
	if (!javafunction) {
		/* Function was cleared from outside JNLua code. */
		lua_pushliteral(L, "no Java function");
		return lua_error(L);
	}

	/* Perform the call, handling coroutine situations. */
	luastate_obj_old = luastate_obj;
	setyield(javastate, JNI_FALSE);
	T = getluathread(javastate);
	if (T == L) {
		nresults = (*thread_env)->CallIntMethod(thread_env, javafunction, invoke_id, javastate);
	}
	else {
		setluathread(javastate, L);
		nresults = (*thread_env)->CallIntMethod(thread_env, javafunction, invoke_id, javastate);
		setluathread(javastate, T);
	}
	luastate_obj = luastate_obj_old;

	/* Handle exception */
	throwable = (*thread_env)->ExceptionOccurred(thread_env);
	if (throwable) {
		/* Push exception & clear */
		luaL_where(L, 1);
		where = tostring(L, -1);
		luaerror = (*thread_env)->NewObject(thread_env, luaerror_class, luaerror_id, where, throwable);
		if (luaerror) {
			pushjavaobject(L, luaerror);
		}
		else {
			lua_pushliteral(L, "JNI error: NewObject() failed creating Lua error");
		}
		(*thread_env)->ExceptionClear(thread_env);

		/* Error out */
		return lua_error(L);
	}

	/* Handle yield */
	if (getyield(javastate)) {
		if (nresults < 0 || nresults > lua_gettop(L)) {
			lua_pushliteral(L, "illegal return count");
			return lua_error(L);
		}
		if (L == getluastate(javastate)) {
			lua_pushliteral(L, "not in a thread");
			return lua_error(L);
		}
		return lua_yield(L, nresults);
	}

	return nresults;
}
/* Calls a Java function. If an exception is reported, store it as the cause for later use. */
Calls a Java function. If an exception is reported, store it as the cause for later use.
Calls a Java function . If an exception is reported store it as the cause for later use .
static int calljavafunction(lua_State *L) {
	jobject luastate_obj_old, javastate, javafunction;
	lua_State *T;
	int nresults;
	jthrowable throwable;
	jstring where;
	jobject luaerror;
	lua_getfield(L, LUA_REGISTRYINDEX, JNLUA_JAVASTATE);
	if (!lua_isuserdata(L, -1)) {
		lua_pushliteral(L, "no Java state");
		return lua_error(L);
	}
	javastate = *(jobject *)lua_touserdata(L, -1);
	lua_pop(L, 1);
	lua_pushvalue(L, lua_upvalueindex(1));
	javafunction = tojavaobject(L, -1, javafunction_interface);
	lua_pop(L, 1);
	if (!javafunction) {
		lua_pushliteral(L, "no Java function");
		return lua_error(L);
	}
	luastate_obj_old = luastate_obj;
	setyield(javastate, JNI_FALSE);
	T = getluathread(javastate);
	if (T == L) {
		nresults = (*thread_env)->CallIntMethod(thread_env, javafunction, invoke_id, javastate);
	}
	else {
		setluathread(javastate, L);
		nresults = (*thread_env)->CallIntMethod(thread_env, javafunction, invoke_id, javastate);
		setluathread(javastate, T);
	}
	luastate_obj = luastate_obj_old;
	throwable = (*thread_env)->ExceptionOccurred(thread_env);
	if (throwable) {
		luaL_where(L, 1);
		where = tostring(L, -1);
		luaerror = (*thread_env)->NewObject(thread_env, luaerror_class, luaerror_id, where, throwable);
		if (luaerror) {
			pushjavaobject(L, luaerror);
		}
		else {
			lua_pushliteral(L, "JNI error: NewObject() failed creating Lua error");
		}
		(*thread_env)->ExceptionClear(thread_env);
		return lua_error(L);
	}
	if (getyield(javastate)) {
		if (nresults < 0 || nresults > lua_gettop(L)) {
			lua_pushliteral(L, "illegal return count");
			return lua_error(L);
		}
		if (L == getluastate(javastate)) {
			lua_pushliteral(L, "not in a thread");
			return lua_error(L);
		}
		return lua_yield(L, nresults);
	}
	return nresults;
}
static int calljavafunction ( lua_State * L ) { jobject luastate_obj_old , javastate , javafunction ; lua_State * T ; int nresults ; jthrowable throwable ; jstring where ; jobject luaerror ; lua_getfield ( L , LUA_REGISTRYINDEX , JNLUA_JAVASTATE ) ; if ( ! lua_isuserdata ( L , -1 ) ) { lua_pushliteral ( L , " " ) ; return lua_error ( L ) ; } javastate = * ( jobject * ) lua_touserdata ( L , -1 ) ; lua_pop ( L , 1 ) ; lua_pushvalue ( L , lua_upvalueindex ( 1 ) ) ; javafunction = tojavaobject ( L , -1 , javafunction_interface ) ; lua_pop ( L , 1 ) ; if ( ! javafunction ) { lua_pushliteral ( L , " " ) ; return lua_error ( L ) ; } luastate_obj_old = luastate_obj ; setyield ( javastate , JNI_FALSE ) ; T = getluathread ( javastate ) ; if ( T == L ) { nresults = ( * thread_env ) -> CallIntMethod ( thread_env , javafunction , invoke_id , javastate ) ; } else { setluathread ( javastate , L ) ; nresults = ( * thread_env ) -> CallIntMethod ( thread_env , javafunction , invoke_id , javastate ) ; setluathread ( javastate , T ) ; } luastate_obj = luastate_obj_old ; throwable = ( * thread_env ) -> ExceptionOccurred ( thread_env ) ; if ( throwable ) { luaL_where ( L , 1 ) ; where = tostring ( L , -1 ) ; luaerror = ( * thread_env ) -> NewObject ( thread_env , luaerror_class , luaerror_id , where , throwable ) ; if ( luaerror ) { pushjavaobject ( L , luaerror ) ; } else { lua_pushliteral ( L , " " ) ; } ( * thread_env ) -> ExceptionClear ( thread_env ) ; return lua_error ( L ) ; } if ( getyield ( javastate ) ) { if ( nresults < 0 || nresults > lua_gettop ( L ) ) { lua_pushliteral ( L , " " ) ; return lua_error ( L ) ; } if ( L == getluastate ( javastate ) ) { lua_pushliteral ( L , " " ) ; return lua_error ( L ) ; } return lua_yield ( L , nresults ) ; } return nresults ; }
Calls a Java function.
Calls a Java function .
/* Get Java state. */ /* Java state has been cleared as the Java VM was destroyed. Cannot call. */ /* Get Java function object. */ /* Function was cleared from outside JNLua code. */ /* Perform the call, handling coroutine situations. */ /* Handle exception */ /* Push exception & clear */ /* Error out */ /* Handle yield */