2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __LUA_BRIDGE_H_
|
|
|
|
#define __LUA_BRIDGE_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "lua.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
/**
|
|
|
|
* @addtogroup lua
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
/** LuaBridge Error enum, with inline docs. */
|
2021-12-25 10:04:45 +08:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
kLuaBridgeErrorOk = 0, /** < value 0 */
|
|
|
|
kLuaBridgeErrorInvalidParameters = -1, /** < value -1 */
|
|
|
|
kLuaBridgeErrorClassNotFound = -2, /** < value -2 */
|
|
|
|
kLuaBridgeErrorMethodNotFound = -3, /** < value -3 */
|
|
|
|
kLuaBridgeErrorExceptionOccurred = -4, /** < value -4 */
|
|
|
|
kLuaBridgeErrorMethodSignature = -5, /** < value -5 */
|
|
|
|
kLuaBridgeErrorJavaVMError = -6, /** < value -6 */
|
2019-11-23 20:27:39 +08:00
|
|
|
} LuaBridgeError;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#define LUA_BRIDGE_REGISTRY_FUNCTION "lua_bridge_function_id" // table[function] = id
|
|
|
|
#define LUA_BRIDGE_REGISTRY_RETAIN "lua_bridge_function_id_retain" // table[id] = retain count
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build bridge between ObjC and Lua.
|
|
|
|
* It makes Lua and ObjC can call each other conveniently.
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
class LuaBridge
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Get the LuaStack of LuaEngine.
|
|
|
|
*
|
|
|
|
* @return the LuaStack object.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
static LuaStack* getStack();
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Push the function pointer corresponding to functionId on the top of lua stack by searching the
|
|
|
|
* `lua_bridge_function_id` table. If it don't find the function pointer corresponding to functionId, it will reset
|
|
|
|
* stack top index to the index before searching.
|
2019-11-23 20:27:39 +08:00
|
|
|
*
|
|
|
|
* @param functionId the value used to search the `lua_bridge_function_id` table.
|
|
|
|
*/
|
|
|
|
static int pushLuaFunctionById(int functionId);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* The retain count would be increase by 1 corresponding to functionId in the `lua_bridge_function_id_retain` table
|
|
|
|
* if it could be found. If `lua_bridge_function_id_retain` table is not existed or the type of
|
|
|
|
* lua_bridge_function_id_retain[functionId] isn't LUA_TNUMBER, It would return 0. The top index of lua stack the
|
|
|
|
* same as before calling this function.
|
2019-11-23 20:27:39 +08:00
|
|
|
*
|
|
|
|
* @param functionId the value used to search the `lua_bridge_function_id_retain` table.
|
|
|
|
* @return the retain count or 0.
|
|
|
|
*/
|
|
|
|
static int retainLuaFunctionById(int functionId);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
*
|
2021-12-25 10:04:45 +08:00
|
|
|
* The retain count would be reduced by 1 corresponding to functionId in the `lua_bridge_function_id_retain` table
|
|
|
|
* if it could be found. If `lua_bridge_function_id` table or `lua_bridge_function_id_retain` aren't found, it would
|
|
|
|
* return 0. If the value of retain count is 0 after reducing, it would update the `lua_bridge_function_id_retain`
|
|
|
|
* table and `lua_bridge_function_id_retain` table to remove the reference corresponding to this functionId
|
2019-11-23 20:27:39 +08:00
|
|
|
*
|
|
|
|
* @param functionId the value used to search the `lua_bridge_function_id` table and `lua_bridge_function_id` table.
|
|
|
|
* @return the retain count or 0.
|
|
|
|
*/
|
|
|
|
static int releaseLuaFunctionById(int functionId);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
2021-12-25 10:04:45 +08:00
|
|
|
static int retainLuaFunction(lua_State* L, int functionIndex, int* retainCountReturn);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static lua_State* s_luaState;
|
|
|
|
static int s_newFunctionId;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
// end group
|
|
|
|
/// @}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // __LUA_BRIDGE_H_
|