ZFFramework
 
Loading...
Searching...
No Matches
ZFImpl_ZFLua.h File Reference

global header for lua impl More...

#include "ZFLua.h"
#include "../ZFImpl_ZFLua_impl.h"

Go to the source code of this file.

Classes

class  ZFImpl_ZFLua_ImplSetupHelper
 see ZFImpl_ZFLua_luaStateOpen More...
 
class  ZFImpl_ZFLuaValue
 raw lua value created by ZFImpl_ZFLua_toGeneric, and can be converted by ZFImpl_ZFLua_toLuaValue More...
 

Macros

#define ZFImpl_ZFLua_implSetupCallback_DEFINE(SetupSig, setupAttachAction, setupDetachAction, setupClassDataUpdate)
 for impl to add attach and detach actions
 
#define ZFImpl_ZFLua_luaErrorPrepare(L)
 util for impl
 
#define ZFImpl_ZFLua_luaError(L, fmt, ...)
 see ZFImpl_ZFLua_luaErrorPrepare
 
#define ZFImpl_ZFLua_DEBUG_luaStackChecker(name, L, stackChange)
 util to debug lua stack, assert fail if stack messed up
 

Functions

void * ZFImpl_ZFLua_luaStateOpen (void)
 create new lua state
 
void ZFImpl_ZFLua_luaStateClose (lua_State *L)
 see ZFImpl_ZFLua_luaStateOpen
 
void ZFImpl_ZFLua_luaStateAttach (lua_State *L)
 see ZFImpl_ZFLua_luaStateOpen
 
void ZFImpl_ZFLua_luaStateDetach (lua_State *L)
 see ZFImpl_ZFLua_luaStateAttach
 
void ZFImpl_ZFLua_classDataUpdate (lua_State *L, const ZFClassDataUpdateData &data)
 called when class data changed, for each lua state of each thread
 
void ZFImpl_ZFLua_implSetupMetatable (lua_State *L, int metatableIndex=-1)
 setup metatable for zfauto in lua env
 
zfbool ZFImpl_ZFLua_execute (lua_State *L, const zfchar *buf, zfindex bufLen=((zfindex) -1), zfauto *luaResult=zft_zfnull, const ZFCoreArray< zfauto > *luaParams=zft_zfnull, zfstring *errHint=zft_zfnull, const zfchar *chunkInfo=zft_zfnull, const zfchar *srcInfo=zft_zfnull)
 run lua code, L must be first initialized by ZFImpl_ZFLua_luaStateAttach
 
void ZFImpl_ZFLua_execute_errorHandle (lua_State *L, int error, zfstring *errHint=zft_zfnull, const zfchar *chunkInfo=zft_zfnull, zfindex errorLine=((zfindex) -1))
 error handler for ZFImpl_ZFLua_execute, lua_pcall, etc
 
zfbool ZFImpl_ZFLua_stacktraceT (lua_State *L, zfstring &ret)
 get lua stacktrace
 
zfstring ZFImpl_ZFLua_stacktrace (lua_State *L)
 get lua stacktrace
 
void ZFImpl_ZFLua_luaObjectInfoT (zfstring &ret, lua_State *L, int luaStackOffset, zfbool printLuaType=_ZFT_t_zffalse)
 see ZFImpl_ZFLua_luaObjectInfo
 
zfstring ZFImpl_ZFLua_luaObjectInfo (lua_State *L, int luaStackOffset, zfbool printLuaType=_ZFT_t_zffalse)
 get raw lua object info
 
zfbool ZFImpl_ZFLua_toObject (zfauto &param, lua_State *L, int luaStackOffset)
 get params from lua
 
zfbool ZFImpl_ZFLua_toGeneric (zfauto &param, lua_State *L, int luaStackOffset, zfstring *errorHint=zft_zfnull)
 get params from lua
 
zfbool ZFImpl_ZFLua_toCallback (zfauto &ret, lua_State *L, int luaStackOffset, zfstring *errorHint=zft_zfnull)
 get params from lua
 
zfbool ZFImpl_ZFLua_toStringT (zfstring &s, lua_State *L, int luaStackOffset, zfbool allowEmpty=_ZFT_t_zffalse, const ZFClass **holderCls=zft_zfnull)
 see ZFImpl_ZFLua_toString
 
zfstring ZFImpl_ZFLua_toString (lua_State *L, int luaStackOffset, zfbool allowEmpty=_ZFT_t_zffalse, const ZFClass **holderCls=zft_zfnull)
 get params from lua
 
zfbool ZFImpl_ZFLua_toStringT (zfstring &s, ZFObject *obj, zfbool allowEmpty=_ZFT_t_zffalse, const ZFClass **holderCls=zft_zfnull)
 see ZFImpl_ZFLua_toString
 
zfstring ZFImpl_ZFLua_toString (ZFObject *obj, zfbool allowEmpty=_ZFT_t_zffalse, const ZFClass **holderCls=zft_zfnull)
 see ZFImpl_ZFLua_toString
 
zfbool ZFImpl_ZFLua_toNumberT (zfauto &ret, lua_State *L, int luaStackOffset, zfbool allowEmpty=_ZFT_t_zffalse, const ZFClass **holderCls=zft_zfnull)
 see ZFImpl_ZFLua_toNumber
 
zfauto ZFImpl_ZFLua_toNumber (lua_State *L, int luaStackOffset, zfbool allowEmpty=_ZFT_t_zffalse, const ZFClass **holderCls=zft_zfnull)
 get params from lua
 
zfbool ZFImpl_ZFLua_toNumberT (zfauto &ret, ZFObject *obj, zfbool allowEmpty=_ZFT_t_zffalse, const ZFClass **holderCls=zft_zfnull)
 see ZFImpl_ZFLua_toNumber
 
zfbool ZFImpl_ZFLua_toLuaValue (lua_State *L, ZFObject *obj, zfbool allowEmpty=_ZFT_t_zffalse)
 convert native type to lua type
 
zfbool ZFImpl_ZFLua_zfstringAppend (lua_State *L, zfstring &s, int luaParamOffset=1)
 append with va_arg, see ZFLuaExecute
 
zfstring ZFImpl_ZFLua_luaStackInfo (lua_State *L, int luaStackOffset=1)
 return info of current stack
 
lua_State * ZFImpl_ZFLua_luaOpen (void)
 util for impl
 
void ZFImpl_ZFLua_luaClose (lua_State *L)
 util for impl
 
template<typename T>
void ZFImpl_ZFLua_luaClassRegister (lua_State *L, const zfchar *name)
 util for impl
 
template<typename F>
void ZFImpl_ZFLua_luaFunctionRegister (lua_State *L, const zfchar *name, F f)
 util for impl
 
void ZFImpl_ZFLua_luaCFunctionRegister (lua_State *L, const zfchar *name, int(*f)(lua_State *))
 util for impl
 
void ZFImpl_ZFLua_luaPush (lua_State *L, zfauto &v)
 util for impl
 
void ZFImpl_ZFLua_luaPush (lua_State *L, const zfauto &v)
 util for impl
 
void ZFImpl_ZFLua_luaPush (lua_State *L, ZFObject *v)
 util for impl
 
zfautoZFImpl_ZFLua_luaGet (lua_State *L, int luaStackOffset)
 util for impl
 

Detailed Description

global header for lua impl

Macro Definition Documentation

◆ ZFImpl_ZFLua_implSetupCallback_DEFINE

#define ZFImpl_ZFLua_implSetupCallback_DEFINE ( SetupSig,
setupAttachAction,
setupDetachAction,
setupClassDataUpdate )

for impl to add attach and detach actions

usage:

// your own attach action, callback proto type:
// void implSetupAttach(
// ZF_IN_OUT lua_State *L
// , ZF_IN_OUT ZFImpl_ZFLua_ImplSetupHelper &helper
// );
}, {
// your own detach action, callback proto type:
// void implSetupDetach(ZF_IN_OUT lua_State *L);
}, {
// your own class data change action, callback proto type:
// void implSetupClassDataUpdate(
// ZF_IN_OUT lua_State *L
// , ZF_IN const ZFClassDataUpdateData &data
// , ZF_IN_OUT ZFImpl_ZFLua_ImplSetupHelper &helper
// );
})
#define ZFImpl_ZFLua_implSetupCallback_DEFINE(SetupSig, setupAttachAction, setupDetachAction, setupClassDataUpdate)
for impl to add attach and detach actions
Definition ZFImpl_ZFLua.h:145

◆ ZFImpl_ZFLua_luaErrorPrepare

#define ZFImpl_ZFLua_luaErrorPrepare ( L)

util for impl

used to solve this problem: when raise lua error without ZF_ENV_ZFLUA_USE_EXCEPTION defined, you must ensure any cpp object has been properly cleaned up (from nearest lua c function registered by ZFImpl_ZFLua_luaCFunctionRegister, to luaL_error), otherwise all of cpp objects' destructor would be skipped due to usage of longjmp instead of exception handler

a typical recommended use case:

static int myLuaCallback(ZF_IN lua_State *L) {
// must placed at top of lua c function registered by ZFImpl_ZFLua_luaCFunctionRegister
... // your code
if(error) {
return ZFImpl_ZFLua_luaError(L, xxx);
}
...
}
ZFImpl_ZFLua_luaCFunctionRegister(L, "myLuaCallback", myLuaCallback);
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
#define ZFImpl_ZFLua_luaErrorPrepare(L)
util for impl
Definition ZFImpl_ZFLua.h:536
void ZFImpl_ZFLua_luaCFunctionRegister(lua_State *L, const zfchar *name, int(*f)(lua_State *))
util for impl
Definition ZFImpl_ZFLua.h:467
#define ZFImpl_ZFLua_luaError(L, fmt,...)
see ZFImpl_ZFLua_luaErrorPrepare
Definition ZFImpl_ZFLua.h:587

Function Documentation

◆ ZFImpl_ZFLua_luaStateOpen()

void * ZFImpl_ZFLua_luaStateOpen ( void )
extern

create new lua state

how the impl works:

◆ ZFImpl_ZFLua_luaStateAttach()

void ZFImpl_ZFLua_luaStateAttach ( lua_State * L)
extern

see ZFImpl_ZFLua_luaStateOpen

must not be attached more than one time

◆ ZFImpl_ZFLua_luaStateDetach()

void ZFImpl_ZFLua_luaStateDetach ( lua_State * L)
extern

see ZFImpl_ZFLua_luaStateAttach

note, this method only detach L from ZFFramework, all methods registered to L can not be undo

◆ ZFImpl_ZFLua_execute()

zfbool ZFImpl_ZFLua_execute ( lua_State * L,
const zfchar * buf,
zfindex bufLen = ((zfindex) -1),
zfauto * luaResult = zft_zfnull,
const ZFCoreArray< zfauto > * luaParams = zft_zfnull,
zfstring * errHint = zft_zfnull,
const zfchar * chunkInfo = zft_zfnull,
const zfchar * srcInfo = zft_zfnull )
extern

run lua code, L must be first initialized by ZFImpl_ZFLua_luaStateAttach

NOTE: the lua code can return any number of values (any type that ZFImpl_ZFLua_toGeneric supports), when more than one value returned, the luaResult would holds a v_ZFCoreArray

◆ ZFImpl_ZFLua_toGeneric()

zfbool ZFImpl_ZFLua_toGeneric ( zfauto & param,
lua_State * L,
int luaStackOffset,
zfstring * errorHint = zft_zfnull )
extern

get params from lua

supports these types:

◆ ZFImpl_ZFLua_toCallback()

zfbool ZFImpl_ZFLua_toCallback ( zfauto & ret,
lua_State * L,
int luaStackOffset,
zfstring * errorHint = zft_zfnull )
extern

get params from lua

supports these types:

  • v_ZFCallback
  • lua function, converted by ZFCallbackForLua (available in lua code only)

◆ ZFImpl_ZFLua_toString()

zfstring ZFImpl_ZFLua_toString ( lua_State * L,
int luaStackOffset,
zfbool allowEmpty = _ZFT_t_zffalse,
const ZFClass ** holderCls = zft_zfnull )
inline

get params from lua

support types:

◆ ZFImpl_ZFLua_toNumber()

zfauto ZFImpl_ZFLua_toNumber ( lua_State * L,
int luaStackOffset,
zfbool allowEmpty = _ZFT_t_zffalse,
const ZFClass ** holderCls = zft_zfnull )
inline

get params from lua

support types:

return proper v_zfdouble if success, or empty if fail

◆ ZFImpl_ZFLua_toLuaValue()

zfbool ZFImpl_ZFLua_toLuaValue ( lua_State * L,
ZFObject * obj,
zfbool allowEmpty = _ZFT_t_zffalse )
extern

convert native type to lua type

support all types that can be converted by:

if success, push result value to top of lua's stack and return true, otherwise, push nothing and return false
if allowEmpty, push 0 number if obj is null