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

ScriptingCore is the main class which manages interaction with JavaScript environment. More...

Inherits ScriptEngineProtocol.

Public Member Functions

virtual cocos2d::ccScriptType getScriptType () override
 Gets the script type, for ScriptingCore, it will return cocos2d::kScriptTypeJavascript More...
 
local getScriptType ()
 Gets the script type, for ScriptingCore, it will return cocos2d::kScriptTypeJavascript More...
 
virtual void retainScriptObject (cocos2d::Ref *owner, cocos2d::Ref *target) override
 Reflect the retain relationship to script scope.
 
var retainScriptObject ( var owner, var target)
 Reflect the retain relationship to script scope.
 
local retainScriptObject ( local owner, local target)
 Reflect the retain relationship to script scope.
 
virtual void rootScriptObject (cocos2d::Ref *target) override
 Add the script object to root object.
 
virtual void releaseScriptObject (cocos2d::Ref *owner, cocos2d::Ref *target) override
 Reflect the release relationship to script scope.
 
var releaseScriptObject ( var owner, var target)
 Reflect the release relationship to script scope.
 
local releaseScriptObject ( local owner, local target)
 Reflect the release relationship to script scope.
 
virtual void unrootScriptObject (cocos2d::Ref *target) override
 Remove the script object from root object.
 
virtual void releaseAllChildrenRecursive (cocos2d::Node *node) override
 Release all children in script scope.
 
virtual void releaseAllNativeRefs (cocos2d::Ref *owner) override
 Release all native refs for the given owner in script scope.
 
local releaseAllNativeRefs ( local owner)
 Release all native refs for the given owner in script scope.
 
virtual void removeScriptObjectByObject (cocos2d::Ref *obj) override
 Removes the C++ object's linked JavaScript proxy object from JavaScript context. More...
 
virtual int executeString (const char *codes) override
 Useless in ScriptingCore, please use evalString. More...
 
void pauseSchedulesAndActions (js_proxy_t *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. More...
 
virtual int executeGlobalFunction (const char *functionName) override
 Useless in ScriptingCore, please use executeFunctionWithOwner. More...
 
virtual int sendEvent (cocos2d::ScriptEvent *message) override
 When trigger a script event ,call this func,add params needed into ScriptEvent object.nativeObject is object triggering the event, can be nullptr in Lua.
 
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. More...
 
virtual void setCalledFromScript (bool callFromScript) override
 Useless for Lua.
 
virtual bool isCalledFromScript () override
 Useless for Lua.
 
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...
 
var executeFunctionWithOwner ( var owner, var name, var argc, var vp)
 Execute a js function with a JavaScript caller, function name, arguments count and arguments. More...
 
local executeFunctionWithOwner ( local owner, local name, local argc, local 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...
 
var executeFunctionWithOwner ( var owner, var name, var argc, var vp, var retVal)
 Execute a js function with a JavaScript caller, function name, arguments count, arguments and a return value. More...
 
local executeFunctionWithOwner ( local owner, local name, local argc, local vp, local 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. 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...
 
JS::PersistentRootedScript * getScript (const std::string &path)
 Get script object for the given path. More...
 
local getScript ( local path)
 Get script object for the given path. More...
 
JS::PersistentRootedScript * compileScript (const std::string &path, JS::HandleObject global, JSContext *cx=nullptr)
 Compile the specified js file. More...
 
local compileScript ( local path, local global, local nullptr)
 Compile the specified js file. More...
 
bool runScript (const std::string &path)
 Run the specified js file. More...
 
bool runScript (const std::string &path, JS::HandleObject global, JSContext *cx=NULL)
 Run the specified js file. More...
 
local runScript ( local path, local global, local NULL)
 Run the specified js file. More...
 
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...
 
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...
 
local requireScript ( local path, local global, local cx, local 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. More...
 
std::unordered_map
< std::string,
JS::PersistentRootedScript * > & 
getFileScript ()
 Gets the cached script objects for all executed js file. More...
 
void cleanAllScript ()
 Clean all script objects.
 
std::chrono::steady_clock::time_point getEngineStartTime () const
 Gets the time that the ScriptingCore was initalized.
 
local getEngineStartTime ()
 Gets the time that the ScriptingCore was initalized.
 
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 object'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. More...
 
JSObject * getGlobalObject ()
 Gets the global object. More...
 
bool isFunctionOverridedInJS (JS::HandleObject obj, const std::string &name, JSNative native)
 Checks whether a C++ function is overridden in js prototype chain. More...
 
virtual void rootObject (cocos2d::Ref *ref) override
 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...
 
virtual void removeObjectProxy (cocos2d::Ref *obj) override
 Remove proxy for a native object.
 
local removeObjectProxy ( local obj)
 Remove proxy for a native object.
 
virtual void garbageCollect () override
 Calls the Garbage Collector.
 
void setFinalizing (JSObject *finalizing)
 Sets the js object that is being finalizing in the script engine, internal use only, please do not call this function.
 
JSObject * getFinalizing ()
 Gets the js object that is being finalizing in the script engine.
 
- Public Member Functions inherited from ScriptEngineProtocol
 ScriptEngineProtocol ()
 Constructor of ScriptEngineProtocol.
 
virtual ~ScriptEngineProtocol ()
 Destructor of ScriptEngineProtocol.
 
virtual void removeScriptHandler (int)
 Remove script function handler, only LuaEngine class need to implement this function. More...
 
virtual int reallocateScriptHandler (int)
 Reallocate script function handler, only LuaEngine class need to implement this function. More...
 

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. More...
 
var reportError ( var cx, var message, var report)
 Report an error in the js context. More...
 
local reportError ( local cx, local message, local report)
 Report an error in the js context. More...
 
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 More...
 
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 More...
 
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 More...
 
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 More...
 
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 More...
 
local dumpRoot ( local cx, local argc, local vp)
 Dump all named rooted objects, it should be invoked from script environment Bound to __jsc__.dumpRoot More...
 
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 More...
 
local isObjectValid ( local cx, local argc, local vp)
 Check whether a js object's C++ proxy is still valid, it should be invoked from script environment Bound to window.__isObjectValid More...
 

Additional Inherited Members

- Public Types inherited from ScriptEngineProtocol
enum  ConfigType
 ConfigType enum. More...
 

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: