/xbmc/visualizations/Vortex/angelscript/docs/doxygen/source/doc_call_script_func.h

http://github.com/xbmc/xbmc · C++ Header · 192 lines · 0 code · 0 blank · 192 comment · 0 complexity · 878c10c92523784693f875ec4d6243cc MD5 · raw file

  1. /**
  2. \page doc_call_script_func Calling a script function
  3. \section doc_call_script_1 Preparing context and executing the function
  4. Normally a script function is executed in a few steps:
  5. <ol>
  6. <li>Prepare the context
  7. <li>Set the function arguments
  8. <li>Execute the function
  9. <li>Retrieve the return value
  10. </ol>
  11. The code for this might look something like this:
  12. \code
  13. // Get a script context instance. Usually you'll want to reuse a previously
  14. // created instance to avoid the overhead of allocating the instance with
  15. // each call.
  16. asIScriptContext *ctx = engine->CreateContext();
  17. // Obtain the function id from the module. This value should preferrably
  18. // be cached if the same function is called multiple times.
  19. int funcId = engine->GetModule(module_name)->GetFunctionIdByDecl(function_declaration);
  20. // Prepare() must be called to allow the context to prepare the stack
  21. ctx->Prepare(funcId);
  22. // Set the function arguments
  23. ctx->SetArgDWord(...);
  24. int r = ctx->Execute();
  25. if( r == asEXECUTION_FINISHED )
  26. {
  27. // The return value is only valid if the execution finished successfully
  28. asDWORD ret = ctx->GetReturnDWord();
  29. }
  30. // Release the context when you're done with it
  31. ctx->Release();
  32. \endcode
  33. If your application allow the execution to be suspended, either by using
  34. the callback function or registering a function that allow the script to
  35. manually suspend the execution, then the execution function may return
  36. before finishing with the return code asEXECUTION_SUSPENDED. In that case you
  37. can later resume the execution by simply calling the execution function again.
  38. Note that the return value retrieved with GetReturnValue() is only valid
  39. if the script function returned successfully, i.e. if Execute() returned
  40. asEXECUTION_FINISHED.
  41. \section doc_call_script_2 Passing and returning primitives
  42. When calling script functions that take arguments, the values of these
  43. arguments must be set after the call to Prepare() and before Execute().
  44. The arguments are set using a group of SetArg methods:
  45. \code
  46. int SetArgDWord(int arg, asDWORD value);
  47. int SetArgQWord(int arg, asQWORD value);
  48. int SetArgFloat(int arg, float value);
  49. int SetArgDouble(int arg, double value);
  50. \endcode
  51. <code>arg</code> is the argument number, where the first argument is on 0, the second
  52. on 1, and so on. <code>value</code> is the value of the argument. What method to use is
  53. determined by the type of the parameter. For primitive types you can use any of these.
  54. If the parameter type is a reference to a primitive type it is recommended to use the
  55. SetArgDWord() method and pass the pointer as the value. For non-primitive types the
  56. method SetArgObject() should be used, which will be described in the next section.
  57. \code
  58. // The context has been prepared for a script
  59. // function with the following signature:
  60. // int function(int, double, int&in)
  61. // Put the arguments on the context stack, starting with the first one
  62. ctx->SetArgDWord(0, 1);
  63. ctx->SetArgDouble(1, 3.141592);
  64. int val;
  65. ctx->SetArgDWord(2, (asDWORD)&val);
  66. \endcode
  67. Once the script function has been executed the return value is retrieved in
  68. a similar way using the group of GetReturn methods:
  69. \code
  70. asDWORD GetReturnDWord();
  71. asQWORD GetReturnQWord();
  72. float GetReturnFloat();
  73. double GetReturnDouble();
  74. \endcode
  75. Note that you must make sure the returned value is in fact valid, for
  76. example if the script function was interrupted by a script exception the value
  77. would not be valid. You do this by verifying the return code from Execute() or
  78. GetState(), where the return code should be asEXECUTION_FINISHED.
  79. \section doc_call_script_3 Passing and returning objects
  80. Passing registered object types to a script function is done in a similar
  81. way to how primitive types are passed. The function to use is SetArgObject():
  82. \code
  83. int SetArgObject(int arg, void *object);
  84. \endcode
  85. <code>arg</code> is the argument number, just like the other SetArg methods.
  86. <code>object</code> is a pointer to the object you wish to pass.
  87. This same method is used both for parameters passed by value and for those
  88. passed by reference. The library will automatically make a copy of the object
  89. if the parameter is defined to be passed by value.
  90. \code
  91. // The complex object we wish to pass to the script function
  92. CObject obj;
  93. // Pass the object to the function
  94. ctx->SetArgObject(0, &obj);
  95. \endcode
  96. Getting an object returned by a script function is done in a similar way, using
  97. GetReturnObject():
  98. \code
  99. void *GetReturnObject();
  100. \endcode
  101. This method will return a pointer to the object returned by the script function.
  102. The library will still hold a reference to the object, which will only be freed as
  103. the context is released.
  104. \code
  105. // The object where we want to store the return value
  106. CObject obj;
  107. // Execute the function
  108. int r = ctx->Execute();
  109. if( r == asEXECUTION_FINISHED )
  110. {
  111. // Get a pointer to the returned object and copy it to our object
  112. obj = *(CObject*)ctx->GetReturnObject();
  113. }
  114. \endcode
  115. It is important to make a copy of the returned object, or if it is managed by
  116. reference counting add a reference to it. If this is not done the pointer obtained
  117. with GetReturnObject() will be invalidated as the context is released, or reused for
  118. another script function call.
  119. \section doc_call_script_4 Exception handling
  120. If the script performs an illegal action, e.g. calling a method on a null handle, then
  121. the script engine will throw a script exception. The virtual machine will then abort
  122. the execution and the \ref asIScriptContext::Execute "Execute" method will return with the value
  123. \ref asEXECUTION_EXCEPTION.
  124. At this time it is possible to obtain information about the exception through the
  125. \ref asIScriptContext's methods. Example:
  126. \code
  127. void PrintExceptionInfo(asIScriptContext *ctx)
  128. {
  129. asIScriptEngine *engine = ctx->GetEngine();
  130. // Determine the exception that occurred
  131. printf("desc: %s\n", ctx->GetExceptionString());
  132. // Determine the function where the exception occurred
  133. int funcId = ctx->GetExceptionFunction();
  134. const asIScriptFunction *function = engine->GetFunctionDescriptorById(funcId);
  135. printf("func: %s\n", function->GetDeclaration());
  136. printf("modl: %s\n", function->GetModuleName());
  137. printf("sect: %s\n", function->GetScriptSectionName());
  138. // Determine the line number where the exception occurred
  139. printf("line: %d\n", ctx->GetExceptionLineNumber());
  140. }
  141. \endcode
  142. If desired, it is also possible to \ref asIScriptContext::SetExceptionCallback "register a callback function"
  143. that will be called at the moment the exception occurred, before the \ref asIScriptContext::Execute "Execute" method returns.
  144. \see \ref doc_debug for information on examining the callstack
  145. */