virtual_machine.rst 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. .. _api_ref_virtual_machine:
  2. ===============
  3. Virtual Machine
  4. ===============
  5. .. _sq_close:
  6. .. c:function:: void sq_close(HSQUIRRELVM v)
  7. :param HSQUIRRELVM v: the target VM
  8. releases a squirrel VM and all related friend VMs
  9. .. _sq_geterrorfunc:
  10. .. c:function:: SQPRINTFUNCTION sq_geterrorfunc(HSQUIRRELVM v)
  11. :param HSQUIRRELVM v: the target VM
  12. :returns: a pointer to a SQPRINTFUNCTION, or NULL if no function has been set.
  13. returns the current error function of the given Virtual machine. (see sq_setprintfunc())
  14. .. _sq_getforeignptr:
  15. .. c:function:: SQUserPointer sq_getforeignptr(HSQUIRRELVM v)
  16. :param HSQUIRRELVM v: the target VM
  17. :returns: the current VMs foreign pointer.
  18. Returns the foreign pointer of a VM instance.
  19. .. _sq_getprintfunc:
  20. .. c:function:: SQPRINTFUNCTION sq_getprintfunc(HSQUIRRELVM v)
  21. :param HSQUIRRELVM v: the target VM
  22. :returns: a pointer to a SQPRINTFUNCTION, or NULL if no function has been set.
  23. returns the current print function of the given Virtual machine. (see sq_setprintfunc())
  24. .. _sq_getsharedforeignptr:
  25. .. c:function:: SQUserPointer sq_getsharedforeignptr(HSQUIRRELVM v)
  26. :param HSQUIRRELVM v: the target VM
  27. :returns: the current VMs shared foreign pointer
  28. Returns the shared foreign pointer of a group of friend VMs
  29. .. _sq_getsharedreleasehook:
  30. .. c:function:: SQUserPointer sq_getsharedreleasehook(HSQUIRRELVM v)
  31. :param HSQUIRRELVM v: the target VM
  32. :returns: the current VMs release hook.
  33. Returns the shared release hook of a group of friend VMs
  34. .. _sq_getversion:
  35. .. c:function:: SQInteger sq_getversion()
  36. :returns: version number of the vm(as in SQUIRREL_VERSION_NUMBER).
  37. returns the version number of the vm
  38. .. _sq_getvmreleasehook:
  39. .. c:function:: SQUserPointer sq_getvmreleasehook(HSQUIRRELVM v)
  40. :param HSQUIRRELVM v: the target VM
  41. :returns: the current VMs release hook.
  42. Returns the release hook of a VM instance
  43. .. _sq_getvmstate:
  44. .. c:function:: SQInteger sq_getvmstate(HSQUIRRELVM v)
  45. :param HSQUIRRELVM v: the target VM
  46. :returns: the state of the vm encoded as integer value. The following constants are defined: SQ_VMSTATE_IDLE, SQ_VMSTATE_RUNNING, SQ_VMSTATE_SUSPENDED.
  47. returns the execution state of a virtual machine
  48. .. _sq_move:
  49. .. c:function:: void sq_move(HSQUIRRELVM dest, HSQUIRRELVM src, SQInteger idx)
  50. :param HSQUIRRELVM dest: the destination VM
  51. :param HSQUIRRELVM src: the source VM
  52. :param SQInteger idx: the index in the source stack of the value that has to be moved
  53. pushes the object at the position 'idx' of the source vm stack in the destination vm stack
  54. .. _sq_newthread:
  55. .. c:function:: HSQUIRRELVM sq_newthread(HSQUIRRELVM friendvm, SQInteger initialstacksize)
  56. :param HSQUIRRELVM friendvm: a friend VM
  57. :param SQInteger initialstacksize: the size of the stack in slots(number of objects)
  58. :returns: a pointer to the new VM.
  59. :remarks: By default the roottable is shared with the VM passed as first parameter. The new VM lifetime is bound to the "thread" object pushed in the stack and behave like a normal squirrel object.
  60. creates a new vm friendvm of the one passed as first parmeter and pushes it in its stack as "thread" object.
  61. .. _sq_open:
  62. .. c:function:: HSQUIRRELVM sq_open(SQInteger initialstacksize)
  63. :param SQInteger initialstacksize: the size of the stack in slots(number of objects)
  64. :returns: an handle to a squirrel vm
  65. :remarks: the returned VM has to be released with sq_releasevm
  66. creates a new instance of a squirrel VM that consists in a new execution stack.
  67. .. _sq_pushconsttable:
  68. .. c:function:: void sq_pushconsttable(HSQUIRRELVM v)
  69. :param HSQUIRRELVM v: the target VM
  70. pushes the current const table in the stack
  71. .. _sq_pushregistrytable:
  72. .. c:function:: void sq_pushregistrytable(HSQUIRRELVM v)
  73. :param HSQUIRRELVM v: the target VM
  74. pushes the registry table in the stack
  75. .. _sq_pushroottable:
  76. .. c:function:: void sq_pushroottable(HSQUIRRELVM v)
  77. :param HSQUIRRELVM v: the target VM
  78. pushes the current root table in the stack
  79. .. _sq_setconsttable:
  80. .. c:function:: void sq_setconsttable(HSQUIRRELVM v)
  81. :param HSQUIRRELVM v: the target VM
  82. pops a table from the stack and set it as const table
  83. .. _sq_seterrorhandler:
  84. .. c:function:: void sq_seterrorhandler(HSQUIRRELVM v)
  85. :param HSQUIRRELVM v: the target VM
  86. :remarks: the error handler is shared by friend VMs
  87. pops from the stack a closure or native closure an sets it as runtime-error handler.
  88. .. _sq_setforeignptr:
  89. .. c:function:: void sq_setforeignptr(HSQUIRRELVM v, SQUserPointer p)
  90. :param HSQUIRRELVM v: the target VM
  91. :param SQUserPointer p: The pointer that has to be set
  92. Sets the foreign pointer of a certain VM instance. The foreign pointer is an arbitrary user defined pointer associated to a VM (by default is value id 0). This pointer is ignored by the VM.
  93. .. _sq_setprintfunc:
  94. .. c:function:: void sq_setprintfunc(HSQUIRRELVM v, SQPRINTFUNCTION printfunc, SQPRINTFUNCTION errorfunc)
  95. :param HSQUIRRELVM v: the target VM
  96. :param SQPRINTFUNCTION printfunc: a pointer to the print func or NULL to disable the output.
  97. :param SQPRINTFUNCTION errorfunc: a pointer to the error func or NULL to disable the output.
  98. :remarks: the print func has the following prototype: void printfunc(HSQUIRRELVM v,const SQChar \*s,...)
  99. sets the print function of the virtual machine. This function is used by the built-in function '::print()' to output text.
  100. .. _sq_setroottable:
  101. .. c:function:: void sq_setroottable(HSQUIRRELVM v)
  102. :param HSQUIRRELVM v: the target VM
  103. pops a table from the stack and set it as root table
  104. .. _sq_setsharedforeignptr:
  105. .. c:function:: void sq_setsharedforeignptr(HSQUIRRELVM v, SQUserPointer p)
  106. :param HSQUIRRELVM v: the target VM
  107. :param SQUserPointer p: The pointer that has to be set
  108. Sets the shared foreign pointer. The foreign pointer is an arbitrary user defined pointer associated to a group of friend VMs (by default is value id 0). After a "main" VM is created using sq_open() all friend VMs created with sq_newthread share the same shared pointer.
  109. .. _sq_setsharedreleasehook:
  110. .. c:function:: void sq_setsharedreleasehook(HSQUIRRELVM v, SQRELESEHOOK hook)
  111. :param HSQUIRRELVM v: the target VM
  112. :param SQRELESEHOOK hook: The hook that has to be set
  113. Sets the release hook of a certain VM group. The release hook is invoked when the last vm of the group vm is destroyed (usually when sq_close() is invoked). The userpointer passed to the function is the shared foreignpointer(see sq_getsharedforeignptr()). After a "main" VM is created using sq_open() all friend VMs created with sq_newthread() share the same shared release hook.
  114. .. _sq_setvmreleasehook:
  115. .. c:function:: void sq_setvmreleasehook(HSQUIRRELVM v, SQRELESEHOOK hook)
  116. :param HSQUIRRELVM v: the target VM
  117. :param SQRELESEHOOK hook: The hook that has to be set
  118. Sets the release hook of a certain VM instance. The release hook is invoked when the vm is destroyed. The userpointer passed to the function is the vm foreignpointer (see sq_setforeignpointer())
  119. .. _sq_suspendvm:
  120. .. c:function:: HRESULT sq_suspendvm(HSQUIRRELVM v)
  121. :param HSQUIRRELVM v: the target VM
  122. :returns: an SQRESULT(that has to be returned by a C function)
  123. :remarks: sq_result can only be called as return expression of a C function. The function will fail is the suspension is done through more C calls or in a metamethod.
  124. Suspends the execution of the specified vm.
  125. *.eg*
  126. ::
  127. SQInteger suspend_vm_example(HSQUIRRELVM v)
  128. {
  129. return sq_suspendvm(v);
  130. }
  131. .. _sq_wakeupvm:
  132. .. c:function:: HRESULT sq_wakeupvm(HSQUIRRELVM v, SQBool resumedret, SQBool retval, SQBool raiseerror, SQBool throwerror)
  133. :param HSQUIRRELVM v: the target VM
  134. :param SQBool resumedret: if true the function will pop a value from the stack and use it as return value for the function that has previously suspended the virtual machine.
  135. :param SQBool retval: if true the function will push the return value of the function that suspend the excution or the main function one.
  136. :param SQBool raiseerror: if true, if a runtime error occurs during the execution of the call, the vm will invoke the error handler.
  137. :param SQBool throwerror: if true, the vm will thow an exception as soon as is resumed. the exception payload must be set beforehand invoking sq_thowerror().
  138. :returns: an HRESULT.
  139. wake up the execution a previously suspended virtual machine