How Pass Parameters from Lua to Native Java

Hi,

I have a quick question regarding luaState for Android  I studied IsWiFiEnabled(), in CoronaApplication.java that was referenced in the quick start guide to enterprise for android.  I know in order to pass data from native java to lua, you would do:

luaState.pushBoolean(value);

return 1;

so that Lua can then get the value from the stack. So that’s from Java to Lua

My question is, how do you go the other direction?  How does java retrieve parameters passed in to the function from Lua?

Would you do:

luaState.toInteger(1) to get an integer passed in as the first parameter of the Lua call to a native java function that is listed as part of NamedJavaFunction[]?

And then luaState.toString(2) to get a string passed in as the 2nd parameter of the Lua call?

Thanks in advance…

Look in /Applications/CoronaEnterprise/Samples/SimpleLuaExtension 

it should dhow you how to do what you’re looking for.

Rob

Thanks Rob, Can you or someone please send me the file(s) in that folder so that I can study the syntax in them? (or are they on GITHUB?) Or at least the one that shows how to do this? I haven’t purchased Enterprise yet because I wanted to see how involved it will be to access native functions since I have no experience with Java. I’m not sure I’m up to the task yet. So I studied what I could find, but little on this. 

Usually we can read documentation before investing in a technology, but I’ve found very limited info on Corona Enterprise available online. I’ve been using Corona SDK for years and have over 10 Corona apps live and have purchased many plugins, but I’m considering taking this next step. Please advise.

Thanks again

You could ask for a 30 day trial. Email support AT coronalabs.com

Rob

Ok sounds good. Will do. Thanks.

Dear Rob,

I’m trying to call a Lua Function from Native Android.

I’m checking the SimpleLuaExtension Sample provided in Native.

But, I can’t figure where is configured the ‘myTest’,

For example, the lua code say:

function onCall() print("callLuaFunction() has succeeded Again") end myTests.callLuaFunction(onCall)

But, I’m turning crazy where and how can Lua know what to call when myTest is provided at the Lua code.

It is any internal not accesible reserved word ?

I’m trying the following:

/\*\* MY PLUGIN \*\*/ /\*\* \* The following Lua function has been called: library.pickFile( type ) \* \<p\> \* Warning! This method is not called on the main thread. \* @param L Reference to the Lua state that the Lua function was called from. \* @return Returns the number of values to be returned by the library.pickFile() function. \*/ @SuppressWarnings("WeakerAccess") public int pickFile (LuaState L) { // Fetch a reference to the Corona activity. // Note: Will be null if the end-user has just backed out of the activity. CoronaActivity activity = CoronaEnvironment.getCoronaActivity(); if (activity == null) { return 0; } // Fetch the first argument from the called Lua function. String mySetType = L.checkString(1); if (null == mySetType) { //All Files by Default. mySetType = "\*/\*"; } // Register your activity handler. int requestCode = activity.registerActivityResultHandler(new MyHandler()); int READ\_REQUEST\_CODE = requestCode; //Activity newActivity; Intent intent = new Intent .... activity.startActivityForResult(intent, requestCode); return 0; } private static class MyHandler implements CoronaActivity.OnActivityResultHandler { public MyHandler() {} @Override public void onHandleActivityResult( CoronaActivity activity, int requestCode, int resultCode, android.content.Intent data) { // Unregister this handler. activity.unregisterActivityResultHandler(this); // Handle the result... if (resultCode == Activity.RESULT\_OK) { //Call a Listener in Lua with the result parameters \<---- try { // Check if the first argument is a function. // Will throw an exception if not or if no argument is given. int luaFunctionStackIndex = 1; luaState.checkType(luaFunctionStackIndex, com.naef.jnlua.LuaType.FUNCTION); // Push the given Lua function to the top of the Lua state's stack. We need to do this because the // because the call() method below expects this Lua function to be a the top of the stack. luaState.pushValue(luaFunctionStackIndex); // Call the given Lua function. // The first argument indicates the number of arguments that we are passing to the Lua function. // The second argument indicates the number of return values to accept from the Lua function. // In this case, we are calling this Lua function with no arguments and are accepting no return values. // Note: If you want to call the Lua function with arguments, then you need to push each argument // value to the luaState object's stack. luaState.call(0, 0); } catch (Exception ex) { // An exception will occur if the following happens: // 1) No argument was given. // 2) The argument was not a Lua function. // 3) The Lua function call failed, likely because the Lua function could not be found. ex.printStackTrace(); } } // END\_INCLUDE (parse\_open\_document\_response) } } } /\*\* Implements the library.pickFile() Lua function. \*/ @SuppressWarnings("unused") private class pickFileWrapper implements NamedJavaFunction { /\*\* \* Gets the name of the Lua function as it would appear in the Lua script. \* @return Returns the name of the custom Lua function. \*/ @Override public String getName() { return "pickFile"; } /\*\* \* This method is called when the Lua function is called. \* \<p\> \* Warning! This method is not called on the main UI thread. \* @param L Reference to the Lua state. \* Needed to retrieve the Lua function's parameters and to return values back to Lua. \* @return Returns the number of values to be returned by the Lua function. \*/ @Override public int invoke(LuaState L) { return pickFile(L); } }

Dear, I don’t found the correct way to call a listener from Java to Lua. I need to return the values (uri, realPath…, ).

Can you illuminate me a little?

As mentioned, I have been checking the SimpleLuaExtension sample, and, I can’t figure how the Lua recognize the myTest without any require…

Thanks so much again,

Hi @CesarHernandez

myTests module is registered in CoronaApplication.java. You can find line

luaState.register("myTests", luaFunctions);

inside the CoronaRuntimeEventHandler class.

Hope it will help to figure out how it works.

Thanks a lot Bektur. 

Finnally I have an idea about it. Thanks so much.

Regards,

Look in /Applications/CoronaEnterprise/Samples/SimpleLuaExtension 

it should dhow you how to do what you’re looking for.

Rob

Thanks Rob, Can you or someone please send me the file(s) in that folder so that I can study the syntax in them? (or are they on GITHUB?) Or at least the one that shows how to do this? I haven’t purchased Enterprise yet because I wanted to see how involved it will be to access native functions since I have no experience with Java. I’m not sure I’m up to the task yet. So I studied what I could find, but little on this. 

Usually we can read documentation before investing in a technology, but I’ve found very limited info on Corona Enterprise available online. I’ve been using Corona SDK for years and have over 10 Corona apps live and have purchased many plugins, but I’m considering taking this next step. Please advise.

Thanks again

You could ask for a 30 day trial. Email support AT coronalabs.com

Rob

Ok sounds good. Will do. Thanks.

Dear Rob,

I’m trying to call a Lua Function from Native Android.

I’m checking the SimpleLuaExtension Sample provided in Native.

But, I can’t figure where is configured the ‘myTest’,

For example, the lua code say:

function onCall() print("callLuaFunction() has succeeded Again") end myTests.callLuaFunction(onCall)

But, I’m turning crazy where and how can Lua know what to call when myTest is provided at the Lua code.

It is any internal not accesible reserved word ?

I’m trying the following:

/\*\* MY PLUGIN \*\*/ /\*\* \* The following Lua function has been called: library.pickFile( type ) \* \<p\> \* Warning! This method is not called on the main thread. \* @param L Reference to the Lua state that the Lua function was called from. \* @return Returns the number of values to be returned by the library.pickFile() function. \*/ @SuppressWarnings("WeakerAccess") public int pickFile (LuaState L) { // Fetch a reference to the Corona activity. // Note: Will be null if the end-user has just backed out of the activity. CoronaActivity activity = CoronaEnvironment.getCoronaActivity(); if (activity == null) { return 0; } // Fetch the first argument from the called Lua function. String mySetType = L.checkString(1); if (null == mySetType) { //All Files by Default. mySetType = "\*/\*"; } // Register your activity handler. int requestCode = activity.registerActivityResultHandler(new MyHandler()); int READ\_REQUEST\_CODE = requestCode; //Activity newActivity; Intent intent = new Intent .... activity.startActivityForResult(intent, requestCode); return 0; } private static class MyHandler implements CoronaActivity.OnActivityResultHandler { public MyHandler() {} @Override public void onHandleActivityResult( CoronaActivity activity, int requestCode, int resultCode, android.content.Intent data) { // Unregister this handler. activity.unregisterActivityResultHandler(this); // Handle the result... if (resultCode == Activity.RESULT\_OK) { //Call a Listener in Lua with the result parameters \<---- try { // Check if the first argument is a function. // Will throw an exception if not or if no argument is given. int luaFunctionStackIndex = 1; luaState.checkType(luaFunctionStackIndex, com.naef.jnlua.LuaType.FUNCTION); // Push the given Lua function to the top of the Lua state's stack. We need to do this because the // because the call() method below expects this Lua function to be a the top of the stack. luaState.pushValue(luaFunctionStackIndex); // Call the given Lua function. // The first argument indicates the number of arguments that we are passing to the Lua function. // The second argument indicates the number of return values to accept from the Lua function. // In this case, we are calling this Lua function with no arguments and are accepting no return values. // Note: If you want to call the Lua function with arguments, then you need to push each argument // value to the luaState object's stack. luaState.call(0, 0); } catch (Exception ex) { // An exception will occur if the following happens: // 1) No argument was given. // 2) The argument was not a Lua function. // 3) The Lua function call failed, likely because the Lua function could not be found. ex.printStackTrace(); } } // END\_INCLUDE (parse\_open\_document\_response) } } } /\*\* Implements the library.pickFile() Lua function. \*/ @SuppressWarnings("unused") private class pickFileWrapper implements NamedJavaFunction { /\*\* \* Gets the name of the Lua function as it would appear in the Lua script. \* @return Returns the name of the custom Lua function. \*/ @Override public String getName() { return "pickFile"; } /\*\* \* This method is called when the Lua function is called. \* \<p\> \* Warning! This method is not called on the main UI thread. \* @param L Reference to the Lua state. \* Needed to retrieve the Lua function's parameters and to return values back to Lua. \* @return Returns the number of values to be returned by the Lua function. \*/ @Override public int invoke(LuaState L) { return pickFile(L); } }

Dear, I don’t found the correct way to call a listener from Java to Lua. I need to return the values (uri, realPath…, ).

Can you illuminate me a little?

As mentioned, I have been checking the SimpleLuaExtension sample, and, I can’t figure how the Lua recognize the myTest without any require…

Thanks so much again,

Hi @CesarHernandez

myTests module is registered in CoronaApplication.java. You can find line

luaState.register("myTests", luaFunctions);

inside the CoronaRuntimeEventHandler class.

Hope it will help to figure out how it works.

Thanks a lot Bektur. 

Finnally I have an idea about it. Thanks so much.

Regards,