cpgf library documentation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
document:script-binding.html [2017/04/07 05:26]
qi
document:script-binding.html [2017/04/07 06:50] (current)
qi
Line 3: Line 3:
 ===== Overview ===== ===== Overview =====
  
-In this documentation,​ the phrase "​script engine"​ represents any supported script engine, which is Lua, Google V8 Javascript, Python or Mozilla SpiderMonkey.+In this documentation,​ the phrase "​script engine"​ represents any supported script engine, which is Lua, Google V8 JavaScript, Python or Mozilla SpiderMonkey.
  
 The binding engine uses reflection meta data as the source. It doesn'​t require any external tools. The binding engine uses reflection meta data as the source. It doesn'​t require any external tools.
Line 9: Line 9:
 What can the binding engine do What can the binding engine do
   * Bind C++ class to the script engine. Then script engine can create instance, invoke class member function, get/set class field or property, and more.   * Bind C++ class to the script engine. Then script engine can create instance, invoke class member function, get/set class field or property, and more.
-  * Bind C++ enum type to script engine. Then script engine can access the enum value by name.+  * Bind C++ enumerator ​type to script engine. Then script engine can access the enum value by name.
   * Bind C++ functions (global or member) to the script engine.   * Bind C++ functions (global or member) to the script engine.
   * Bind C++ values to script engine variable.   * Bind C++ values to script engine variable.
Line 20: Line 20:
 ===== Get start ===== ===== Get start =====
  
-To binding Lua, include ​the header, \\ +To binding Lua, #include ​"cpgf/​scriptbind/​gluabind.h"
-cpgf/​scriptbind/​gluabind.h ​\\ +
-For V8, include the header, \\ +
-cpgf/​scriptbind/​gv8bind.h \\ +
-For Python, include the header, \\ +
-cpgf/​scriptbind/​gpythonbind.h \\ +
-For Mozilla SpiderMonkey,​ include the header, \\ +
-cpgf/​scriptbind/​gspidermonkeybind.h+
  
-First, we need a GScriptObject or IScriptObject (the interface version) ​object.+For V8, #include "​cpgf/​scriptbind/​gv8bind.h"​ 
 + 
 +For Python, #include "​cpgf/​scriptbind/​gpythonbind.h"​ 
 + 
 +For Mozilla SpiderMonkey,​ #include "​cpgf/​scriptbind/​gspidermonkeybind.h"​ 
 + 
 +First, we need a global script object, ​GScriptObject or IScriptObject (the interface version).
  
 Lua version: Lua version:
Line 58: Line 57:
  
 The first parameter is a IMetaService. You can call createDefaultMetaService() or createMetaService() to create the service. \\ The first parameter is a IMetaService. You can call createDefaultMetaService() or createMetaService() to create the service. \\
-The second parameter is the Python object. You can use the object ​\_\_main\_\_ ​as the root object. \\+The second parameter is the Python object. You can use the object ​<​nowiki>​__main__</​nowiki> ​as the root object. \\
 The third one is a config. Just call GScriptConfig() to construct default one. The third one is a config. Just call GScriptConfig() to construct default one.
  
Line 79: Line 78:
  
 After getting the GScriptObject object, we get everything ready to bind the meta data to the script engine. After getting the GScriptObject object, we get everything ready to bind the meta data to the script engine.
 +
 +===== Global script object VS. nested script object =====
 +
 +The APIs shows above create global script object. A global script object represent the whole scripting environment,​ it's the root object of every other objects. Anything that is set to a global object is accessed by the name directly. For example, if we have set a variable "​myObj"​ to global object, in script we can write "​myObj.foo = bar" to use myObj"​.
 +
 +A nested script object is a kind of object that embeded in another object. It can be created by GScriptObject::​createScriptObject. Assume we created a nested object named "​myNested",​ and then set a variable "​myOtherObj"​ to "​myNested",​ then in script we can access myOtherObj as "​myNested.myOtherObj.foo = bar".
  
 ===== Script object API ===== ===== Script object API =====
Line 104: Line 109:
 On failure the function will fail and return GScriptValue(). \\ On failure the function will fail and return GScriptValue(). \\
 To get the IScriptObject pointer, call toScriptObject on the returned value. To get the IScriptObject pointer, call toScriptObject on the returned value.
- 
-<code cpp> 
-virtual GScriptValue getScriptFunction(const char * name) = 0; 
-</​code>​ 
-Get existing script function in current script object. \\ 
-The script function can be invoked later. \\ 
-On failure the function will fail and return GScriptValue(). \\ 
-To get the IScriptFunction pointer, call toScriptFunction on the returned value. 
  
 <code cpp> <code cpp>