My Project
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
ParaScripting::CNPLScriptingState Class Reference

a NPL scripting state (wrapper of lua State), for binding c++ classes to lua. More...

#include <NPLScriptingState.h>

Inheritance diagram for ParaScripting::CNPLScriptingState:
NPL::CNPLRuntimeState

Public Types

enum  NPL_MemAllocatorType { MEM_ALLOC_TYPE_SYS_MALLOC, MEM_ALLOC_TYPE_POOL_MALLOC, MEM_ALLOC_TYPE_DL_MALLOC }
 NPL runs in its own thread. More...
 

Public Member Functions

 CNPLScriptingState (bool bCreateState=true)
 split registration to save compiling time. More...
 
int GetLastReturnValue () const
 get the last return value from lua_pcall. More...
 
void SetLastReturnValue (int val)
 
lua_StateGetLuaState ()
 get the lua state.
 
bool IsValid ()
 return true if the runtime state is valid
 
void LoadNPLLib ()
 load only NPL related functions. More...
 
void LoadParaLib ()
 load only Para related functions. More...
 
void LoadHAPI_Globals ()
 load functions for scene management
 
void LoadHAPI_SceneManager ()
 load functions for scene management
 
void LoadHAPI_ResourceManager ()
 load functions for resource or assets management
 
void LoadHAPI_UI ()
 load functions for GUI controls and sounds
 
void LoadHAPI_UI_Extension ()
 this function is called by LoadHAPI_UI()
 
void LoadHAPI_Audio ()
 load functions for Audio Engine More...
 
void LoadHAPI_Network ()
 load functions for NPL Network Layer
 
void LoadHAPI_AI ()
 load functions for AI
 
void LoadHAPI_NPL ()
 load functions for neural parallel language
 
void LoadHAPI_Jabber ()
 load jabber related functions
 
bool IsScriptFileLoaded (const string &filepath)
 whether a given script file is loaded. More...
 
bool LoadFile (const string &filePath, bool bReload, lua_State *L=0, bool bNoReturn=false)
 load a new NPL script file without running it. More...
 
int DoString (const char *sCode, int nLength=0, const char *sFileName=NULL, bool bPopReturnValue=true)
 do string in the current state. More...
 
NPL::NPLReturnCode ActivateFile (const string &filepath, const char *code=NULL, int nLength=0)
 Activate a local file. More...
 
bool BindFileActivateFunc (const object &funcActivate, const std::string &filename)
 bind the activation function. More...
 
const string & GetFileName ()
 get current file name which is being processed now. More...
 
const char * GetCurrentFileName (lua_State *L=0)
 get current file that is being loaded or where the current code is defined. More...
 
bool CreateSetState (lua_State *pLuaState=NULL)
 load or restore the runtime state no need to call LoadLuabind(), if the script runtime is created by this function More...
 
void SetOwnLuaState (bool bOwn)
 if true, we will delete the luastate when this class is destroyed. More...
 
int NPL_export (lua_State *L=0)
 set/get exported file module. More...
 
const std::map< std::string, int32 > & GetLoadedFiles ()
 NOT thread-safe: all loaded files mapping from filename to number of cached objects. More...
 

Static Public Member Functions

static int GetNPLCodeFromFile (ParaEngine::CParaFile *pFile, char **pBuffer, int *pBufferSize)
 get the NPL code buffer and size according to a given file. More...
 
static void AddSearchPath (const char *sSearchPath, bool bIsAdding=true)
 static function: More...
 
static uint32 GetScriptDiskPath (const string &filePath, string &sFileName)
 we will first find if there is an up-to-date compiled version in the script/bin directory. More...
 
static NPL::NPLRuntimeState_ptr GetRuntimeStateFromLuaObject (const object &obj)
 get pointer to NPLRuntimeState from the lua state object. More...
 
static NPL::NPLRuntimeState_ptr GetRuntimeStateFromLuaState (lua_State *L)
 

Protected Member Functions

void DestroyState ()
 destroy the runtime state careful with this function, it will make the state invalid.
 
void LoadLuabind ()
 the function must be called for the lua state before binding anything to it
 
void SetRuntimeState (NPL::NPLRuntimeState_ptr runtime_state)
 set the current runtime state. More...
 
void ProcessResult (int nResult, lua_State *L=0)
 Process return result after calling a function or loading a file in Lua. More...
 
int CacheFileModule (const std::string &filename, int nResult, lua_State *L=0)
 save nResult objects on stack to file modules More...
 
int PopFileModule (const std::string &filename, lua_State *L=0)
 pop file module to stack for a given file. More...
 
std::string GetModuleFilePath (const std::string &modulename, lua_State *L=0)
 get module file path by module name
 
int GetFileLoadStatus (const string &filepath)
 wrapping the m_loaded_files
 
void SetFileLoadStatus (const string &filepath, int nStatus)
 

Detailed Description

a NPL scripting state (wrapper of lua State), for binding c++ classes to lua.

Note
: not thread safe, but can use in multiple thread with a lock. In a multi-threaded environment, the number of states loaded are limited by system memory. Each state can cost over 1MB memory.

Then call Load*() functions to load ParaEngine classes and APIs to the specified runtime, so that scripts in the runtime can use these classes and functions

Member Enumeration Documentation

§ NPL_MemAllocatorType

NPL runs in its own thread.

and constantly reallocate 32, 64, 128, 256, 512 sized objects.

Constructor & Destructor Documentation

§ CNPLScriptingState()

ParaScripting::CNPLScriptingState::CNPLScriptingState ( bool  bCreateState = true)

split registration to save compiling time.

Parameters
bCreateStateif false, no interface state is created. this is usually the case when we wants to create an empty scripting state for DLL runtime state.

void register_part1(class_<X>& x){ x.def(...); } void register_part2(class_<X>& x){ x.def(...); } void register_(lua_State* L){ class_<X> x("x"); register_part1(x); register_part2(x); module(L) [ x ]; }

Member Function Documentation

§ ActivateFile()

NPL::NPLReturnCode ParaScripting::CNPLScriptingState::ActivateFile ( const string &  filepath,
const char *  code = NULL,
int  nLength = 0 
)

Activate a local file.

The file should be loaded already.

Parameters
filepathpointer to the file path.
Returns
: NPLReturnCode

the activation function is stored in __glia global variable, indexed by the the file name. There activation function may be nil.

get the "__act" global table in the runtime

get activation function of the file name

§ AddSearchPath()

void ParaScripting::CNPLScriptingState::AddSearchPath ( const char *  sSearchPath,
bool  bIsAdding = true 
)
static

static function:

Parameters
sSearchPathfor example "src/" in android, etc.
bIsAddingdefault to true.

§ BindFileActivateFunc()

bool ParaScripting::CNPLScriptingState::BindFileActivateFunc ( const object funcActivate,
const std::string &  filename 
)

bind the activation function.

Usually, it is for the script function NPL.this(funcActivate).

Parameters
nPreemptiveInstructionCountoptional parameter. if omitted, the activate function will run non-preemptive (it is the programmer's job to let the function finish in short time). If a number is specified here, the activate function will be preemptive like in Erlang. When this number of instructions are executed, the activate function will be paused.

create the activate table, if it does not exist

add the current file name to the __activate table

§ CacheFileModule()

int ParaScripting::CNPLScriptingState::CacheFileModule ( const std::string &  filename,
int  nResult,
lua_State L = 0 
)
protected

save nResult objects on stack to file modules

Returns
the number of new result pushed on stack. usually 1 or 0

§ CreateSetState()

bool ParaScripting::CNPLScriptingState::CreateSetState ( lua_State pLuaState = NULL)

load or restore the runtime state no need to call LoadLuabind(), if the script runtime is created by this function

Parameters
pLuaStatedefault to NULL, if not, it will be an external luastate (that we do not own, calling SetOwnLuaState(false) internally)

make this a reasonable size

§ DoString()

int ParaScripting::CNPLScriptingState::DoString ( const char *  sCode,
int  nLength = 0,
const char *  sFileName = NULL,
bool  bPopReturnValue = true 
)

do string in the current state.

This function is usually called from the scripting interface.

Parameters
sCodethe string to executed.
nLengthlength in bytes.if this is 0, length will be calculated, but must be smaller than a predefined safe length. If this is positive. any string length is allowed.
sFileNameNULL or a file name string that is associated with the code chunk. This is mostly used for debugging.
bPopReturnValuedefault to true, whether to pop any return values from the sCode. default to true.
Returns
0 or the first int return value in the code.

do sCall as a string Assume caller is authorized, since it is from the local environment. This may lack security, and is only valid for authorized callers.

Load and execute the a buffer of code in protected mode ( lua_pcall() )

Output messages through log interface

§ GetCurrentFileName()

const char * ParaScripting::CNPLScriptingState::GetCurrentFileName ( lua_State L = 0)

get current file that is being loaded or where the current code is defined.

Parameters
Ljust in case the NPL.load is called from a different coroutine thread, which is different lua_state(stack) than the default one.

§ GetFileName()

const string & ParaScripting::CNPLScriptingState::GetFileName ( )

get current file name which is being processed now.

§ GetLastReturnValue()

int ParaScripting::CNPLScriptingState::GetLastReturnValue ( ) const

get the last return value from lua_pcall.

§ GetLoadedFiles()

const std::map< std::string, int32 > & ParaScripting::CNPLScriptingState::GetLoadedFiles ( )

NOT thread-safe: all loaded files mapping from filename to number of cached objects.

If -1, it means that file is being loaded or something went wrong. 0 means no cached object.

§ GetNPLCodeFromFile()

int ParaScripting::CNPLScriptingState::GetNPLCodeFromFile ( ParaEngine::CParaFile pFile,
char **  pBuffer,
int *  pBufferSize 
)
static

get the NPL code buffer and size according to a given file.

Please note that BOM is read from the beginning of the file. if it is a utf8 encoded file with BOM byte mask, only the data section is returned

Returns
: return the encoding if any. 0 is returned is no BOM encoding found at the beginning of the file. CP_UTF8 is returned if utf8 is found.

§ GetRuntimeStateFromLuaObject()

NPL::NPLRuntimeState_ptr ParaScripting::CNPLScriptingState::GetRuntimeStateFromLuaObject ( const object obj)
static

get pointer to NPLRuntimeState from the lua state object.

it just retrieves from a secret lua_status variable.

§ GetScriptDiskPath()

uint32 ParaScripting::CNPLScriptingState::GetScriptDiskPath ( const string &  filePath,
string &  sFileName 
)
static

we will first find if there is an up-to-date compiled version in the script/bin directory.

if there is, we will load the compiled version, otherwise we will use the text version.

Parameters
filePaththe logic file path
sFileNamethe output file path. return uint32 of FileLocation enumeration. 0 if not found.

§ IsScriptFileLoaded()

bool ParaScripting::CNPLScriptingState::IsScriptFileLoaded ( const string &  filepath)

whether a given script file is loaded.

Parameters
filePaththe local file path in the following format: [dir0]/[dir1]/[dir2]/[filename.lua]
Returns
: return true if file is already loaded in the current state.

§ LoadFile()

bool ParaScripting::CNPLScriptingState::LoadFile ( const string &  filePath,
bool  bReload,
lua_State L = 0,
bool  bNoReturn = false 
)

load a new NPL script file without running it.

If the file is already loaded, it will not be loaded again.

Parameters
filePaththe local NPL script file path
bReloadif true, the file will be reloaded even if it is already loaded. otherwise, the file will only be loaded if it is not loaded yet.
Ljust in case the NPL.load is called from a different coroutine thread, which is different lua_state(stack) than the default one.
bNoReturngenerate no return on lua_state's stack.
Returns
: return the GliaFile reference.

for standard lua file, Load and execute the a buffer of code in protected mode ( lua_pcall() ) Output messages through log interface

§ LoadHAPI_Audio()

void CNPLScriptingState::LoadHAPI_Audio ( )

load functions for Audio Engine

for luabind, The main drawback of this approach is that the compilation time will increase for the file that does the registration, it is therefore recommended that you register everything in the same cpp-file.

§ LoadNPLLib()

void ParaScripting::CNPLScriptingState::LoadNPLLib ( )

load only NPL related functions.

load NPL

§ LoadParaLib()

void ParaScripting::CNPLScriptingState::LoadParaLib ( )

load only Para related functions.

load all Para libs

§ NPL_export()

int ParaScripting::CNPLScriptingState::NPL_export ( lua_State L = 0)

set/get exported file module.

§ PopFileModule()

int ParaScripting::CNPLScriptingState::PopFileModule ( const std::string &  filename,
lua_State L = 0 
)
protected

pop file module to stack for a given file.

Return true, if file is loaded before or false if not.

Returns
the number of result pushed on stack. usually 1 or 0

§ ProcessResult()

void ParaScripting::CNPLScriptingState::ProcessResult ( int  nResult,
lua_State L = 0 
)
protected

Process return result after calling a function or loading a file in Lua.

Parameters
nResultreturn result returned by luaL_loadbuffer() or lua_pcall().
Ljust in case the NPL.load is called from a different coroutine thread, which is different lua_state(stack) than the default one.

report error to log;

§ SetOwnLuaState()

void ParaScripting::CNPLScriptingState::SetOwnLuaState ( bool  bOwn)

if true, we will delete the luastate when this class is destroyed.

This function should rarely be called.

§ SetRuntimeState()

void ParaScripting::CNPLScriptingState::SetRuntimeState ( NPL::NPLRuntimeState_ptr  runtime_state)
protected

set the current runtime state.

this function should be called from the parent class immediately after LoadNPLLib().


The documentation for this class was generated from the following files: