Cocos2d-x  v3.10
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups
ScriptingCore Class Reference

ScriptingCore is the main class which manages interaction with JavaScript environment It creates the JavaScript context and its global object. More...

Inherits ScriptEngineProtocol.

Public Member Functions

virtual cocos2d::ccScriptType getScriptType () override
 Gets the script type, for ScriptingCore, it will return cocos2d::kScriptTypeJavascript
 
local getScriptType ()
 Gets the script type, for ScriptingCore, it will return cocos2d::kScriptTypeJavascript
 
virtual void removeScriptObjectByObject (cocos2d::Ref *obj) override
 Removes the C++ object's linked JavaScript proxy object from JavaScript context Object to be removed.
 
local removeScriptObjectByObject ( local obj)
 Removes the C++ object's linked JavaScript proxy object from JavaScript context Object to be removed.
 
virtual int executeString (const char *codes) override
 Useless in ScriptingCore, please use evalString.
 
void pauseSchedulesAndActions (js_proxy_t *p)
 Pause scheduled tasks and actions for an object proxy. More...
 
var pauseSchedulesAndActions ( var p)
 Pause scheduled tasks and actions for an object proxy. More...
 
local pauseSchedulesAndActions ( local p)
 Pause scheduled tasks and actions for an object proxy. More...
 
void resumeSchedulesAndActions (js_proxy_t *p)
 Resume scheduled tasks and actions for an object proxy. More...
 
void cleanupSchedulesAndActions (js_proxy_t *p)
 Cleanup scheduled tasks and actions for an object proxy. More...
 
virtual int executeScriptFile (const char *filename) override
 Useless in ScriptingCore, please use runScript.
 
virtual int executeGlobalFunction (const char *functionName) override
 Useless in ScriptingCore, please use executeFunctionWithOwner.
 
virtual int sendEvent (cocos2d::ScriptEvent *message) override
 When some events triggered in the c++ also needs to pass into script ,call this func.The ScriptEvent contains the data need to passed into the script.
 
local sendEvent ( local message)
 When some events triggered in the c++ also needs to pass into script ,call this func.The ScriptEvent contains the data need to passed into the script.
 
virtual bool parseConfig (ConfigType type, const std::string &str) override
 Parse configuration file. More...
 
local parseConfig ( local type, local str)
 Parse configuration file. More...
 
virtual bool handleAssert (const char *msg) override
 Useless in ScriptingCore false.
 
var handleAssert ( var msg)
 Useless in ScriptingCore false.
 
local handleAssert ( local msg)
 Useless in ScriptingCore false.
 
virtual void setCalledFromScript (bool callFromScript) override
 Set whether the call comes from the script or not, only use for the js. More...
 
virtual bool isCalledFromScript () override
 Get the flag that represents whether the call comes from the script or not,only use for the js. More...
 
bool executeFunctionWithObjectData (void *nativeObj, const char *name, JSObject *obj)
 Execute a js function with a JavaScript object as parameter. More...
 
bool executeFunctionWithOwner (jsval owner, const char *name, uint32_t argc, jsval *vp)
 Execute a js function with a JavaScript caller, function name, arguments count and arguments. More...
 
bool executeFunctionWithOwner (jsval owner, const char *name, uint32_t argc, jsval *vp, JS::MutableHandleValue retVal)
 Execute a js function with a JavaScript caller, function name, arguments count, arguments and a return value. More...
 
bool executeFunctionWithOwner (jsval owner, const char *name, const JS::HandleValueArray &args)
 Execute a js function with a JavaScript caller, function name, arguments array. More...
 
bool executeFunctionWithOwner (jsval owner, const char *name, const JS::HandleValueArray &args, JS::MutableHandleValue retVal)
 Execute a js function with a JavaScript caller, function name, arguments array and a return value. More...
 
void executeJSFunctionWithThisObj (JS::HandleValue thisObj, JS::HandleValue callback)
 Execute a js function with a js this object and the js function object. More...
 
void executeJSFunctionWithThisObj (JS::HandleValue thisObj, JS::HandleValue callback, const JS::HandleValueArray &vp, JS::MutableHandleValue retVal)
 Execute a js function with a js this object, the js function object, arguments and a return value. More...
 
bool evalString (const char *string, JS::MutableHandleValue outVal, const char *filename, JSContext *cx, JS::HandleObject global)
 Evaluate the specified js code string The string with the javascript code to be evaluated The jsval that will hold the return value of the evaluation. More...
 
bool evalString (const char *string, JS::MutableHandleValue outVal)
 Evaluate the specified js code string. More...
 
bool evalString (const char *string)
 Evaluate the specified js code string. More...
 
JSScript * getScript (const char *path)
 Get script object for the given path The script file path Script object.
 
void compileScript (const char *path, JS::HandleObject global, JSContext *cx=NULL)
 Compile the specified js file The path of the script to to compiled The js global object The js context.
 
bool runScript (const char *path)
 Run the specified js file The path of the script to be executed Return true if succeed, otherwise return false.
 
bool runScript (const char *path, JS::HandleObject global, JSContext *cx=NULL)
 Run the specified js file The path of the script to be executed The global object to execute the script The context to execute the script Return true if succeed, otherwise return false.
 
local runScript ( local path, local global, local NULL)
 Run the specified js file The path of the script to be executed The global object to execute the script The context to execute the script Return true if succeed, otherwise return false.
 
bool requireScript (const char *path, JS::MutableHandleValue jsvalRet)
 Require the specified js file The difference between run and require is that require returns the export object of the script. More...
 
var requireScript ( var path, var jsvalRet)
 Require the specified js file The difference between run and require is that require returns the export object of the script. More...
 
local requireScript ( local path, local jsvalRet)
 Require the specified js file The difference between run and require is that require returns the export object of the script. More...
 
bool requireScript (const char *path, JS::HandleObject global, JSContext *cx, JS::MutableHandleValue jsvalRet)
 Require the specified js file The difference between run and require is that require returns the export object of the script. More...
 
void cleanScript (const char *path)
 Clean script object for the specified js file The path of the js file to be cleaned.
 
std::unordered_map
< std::string, JSScript * > & 
getFileScript ()
 Gets the cached script objects for all executed js file The cached script object map.
 
local getFileScript ()
 Gets the cached script objects for all executed js file The cached script object map.
 
void cleanAllScript ()
 Clean all script objects.
 
void start ()
 Initialize everything, including the js context, js global object etc.
 
void cleanup ()
 Cleanup everything, including script cache, js context, global object etc.
 
void reset ()
 Cleanup everything then initialize everything.
 
void addRegisterCallback (sc_register_sth callback)
 Add the register_sth callback to the list of functions that need to be called after the creation of the context. More...
 
void createGlobalContext ()
 Create a new context. More...
 
int executeCustomTouchEvent (cocos2d::EventTouch::EventCode eventType, cocos2d::Touch *pTouch, JSObject *obj, JS::MutableHandleValue retval)
 Simulate a touch event and dispatch it to a js object. More...
 
int executeCustomTouchEvent (cocos2d::EventTouch::EventCode eventType, cocos2d::Touch *pTouch, JSObject *obj)
 Simulate a touch event and dispatch it to a js object. More...
 
int executeCustomTouchesEvent (cocos2d::EventTouch::EventCode eventType, const std::vector< cocos2d::Touch * > &touches, JSObject *obj)
 Simulate a multi touch event and dispatch it to a js object. More...
 
JSContext * getGlobalContext ()
 Gets the current global context. More...
 
bool setReservedSpot (uint32_t i, JSObject *obj, jsval value)
 Sets a js value to the targeted js obejct's reserved slot, which is not exposed to script environment. More...
 
void debugProcessInput (const std::string &str)
 Log a string to the debug environment. More...
 
void enableDebugger (unsigned int port=5086)
 Enable the debug environment, mozilla Firefox's remote debugger or Code IDE can connect to it. More...
 
JSObject * getDebugGlobal ()
 Gets the debug environment's global object The debug environment's global object.
 
JSObject * getGlobalObject ()
 Gets the global object The global object.
 
bool isFunctionOverridedInJS (JS::HandleObject obj, const std::string &name, JSNative native)
 Checks whether a C++ function is overrided in js prototype chain The js object The function name The native function The global object.
 
virtual void rootObject (cocos2d::Ref *ref) override
 Roots the associated JSObj. More...
 
var rootObject ( var ref)
 Roots the associated JSObj. More...
 
local rootObject ( local ref)
 Roots the associated JSObj. More...
 
virtual void unrootObject (cocos2d::Ref *ref) override
 Unroots the associated JSObj. More...
 
local unrootObject ( local ref)
 Unroots the associated JSObj. More...
 
- Public Member Functions inherited from ScriptEngineProtocol
 ScriptEngineProtocol ()
 Constructor of ScriptEngineProtocol.
 
virtual ~ScriptEngineProtocol ()
 Destructor of ScriptEngineProtocol.
 
virtual void removeScriptHandler (int handler)
 Remove script function handler, only LuaEngine class need to implement this function.
 
virtual int reallocateScriptHandler (int handler)
 Reallocate script function handler, only LuaEngine class need to implement this function.
 

Static Public Member Functions

static ScriptingCoregetInstance ()
 ScriptingCore is a singleton class, you can retrieve its instance with this function. More...
 
static void removeAllRoots (JSContext *cx)
 Removes all rooted object in the given js context, rooted object won't be garbage collected. More...
 
static void reportError (JSContext *cx, const char *message, JSErrorReport *report)
 Report an error in the js context The js context The error message The js error report object.
 
static bool log (JSContext *cx, uint32_t argc, jsval *vp)
 Log something to the js context using CCLog. More...
 
static bool executeScript (JSContext *cx, uint32_t argc, jsval *vp)
 Runs a script from script environment, it should be invoked from script environment Bound to __jsc__.executeScript and window.executeScript The js context The arguments count The arguments Return true if succeed, otherwise return false.
 
local executeScript ( local cx, local argc, local vp)
 Runs a script from script environment, it should be invoked from script environment Bound to __jsc__.executeScript and window.executeScript The js context The arguments count The arguments Return true if succeed, otherwise return false.
 
static bool forceGC (JSContext *cx, uint32_t argc, jsval *vp)
 Forces a cycle of garbage collection, it should be invoked from script environment Bound to __jsc__.garbageCollect and window.garbageCollect The js context The arguments count The arguments.
 
var forceGC ( var cx, var argc, var vp)
 Forces a cycle of garbage collection, it should be invoked from script environment Bound to __jsc__.garbageCollect and window.garbageCollect The js context The arguments count The arguments.
 
local forceGC ( local cx, local argc, local vp)
 Forces a cycle of garbage collection, it should be invoked from script environment Bound to __jsc__.garbageCollect and window.garbageCollect The js context The arguments count The arguments.
 
static bool dumpRoot (JSContext *cx, uint32_t argc, jsval *vp)
 Dump all named rooted objects, it should be invoked from script environment Bound to __jsc__.dumpRoot The js context The arguments count The arguments.
 
static bool addRootJS (JSContext *cx, uint32_t argc, jsval *vp)
 Adds a js object to root so that it won't be touched by the garbage collection, it should be invoked from script environment Bound to __jsc__.addGCRootObject The js context The arguments count The arguments.
 
static bool removeRootJS (JSContext *cx, uint32_t argc, jsval *vp)
 Removes a js object from root, it should be invoked from script environment Bound to __jsc__.removeGCRootObject The js context The arguments count The arguments.
 
static bool isObjectValid (JSContext *cx, uint32_t argc, jsval *vp)
 Check whether a js object's C++ proxy is still valid, it should be invoked from script environment Bound to window.__isObjectValid The js context The arguments count The arguments.
 

Additional Inherited Members

- Public Types inherited from ScriptEngineProtocol
enum  ConfigType
 ConfigType enum.
 

Detailed Description

ScriptingCore is the main class which manages interaction with JavaScript environment It creates the JavaScript context and its global object.

It also manages object bindings between C++ scope and JavaScript scope, for most cocos2d classes, if you create an object in JavaScript scope, it will also create a C++ object, ScriptingCore will manage a proxy between them and synchronize the life cycle. It can:

  • Execute JavaScript functions in different ways with different parameters
  • Evaluate JavaScript commands or string
  • Execute a JavaScript file
  • Clean a JavaScript file cache
  • Clean all JavaScript files
  • Cleanup or reset the JavaScript context
  • Invoke garbage collection of JavaScript context
  • etc...

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