object_creation_and_handling.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. .. _api_ref_object_creation_and_handling:
  2. ============================
  3. Object creation and handling
  4. ============================
  5. .. _sq_bindenv:
  6. .. c:function:: SQRESULT sq_bindenv(HSQUIRRELVM v, SQInteger idx)
  7. :param HSQUIRRELVM v: the target VM
  8. :param SQInteger idx: index of the target closure
  9. :returns: a SQRESULT
  10. :remarks: the cloned closure holds the environment object as weak reference
  11. pops an object from the stack (must be a table, instance, or class); clones the closure at position idx in the stack and sets the popped object as environment of the cloned closure. Then pushes the new cloned closure on top of the stack.
  12. .. _sq_createinstance:
  13. .. c:function:: SQRESULT sq_createinstance(HSQUIRRELVM v, SQInteger idx)
  14. :param HSQUIRRELVM v: the target VM
  15. :param SQInteger idx: index of the target class
  16. :returns: a SQRESULT
  17. :remarks: the function doesn't invoke the instance contructor. To create an instance and automatically invoke its contructor, sq_call must be used instead.
  18. creates an instance of the class at 'idx' position in the stack. The new class instance is pushed on top of the stack.
  19. .. _sq_getbool:
  20. .. c:function:: SQRESULT sq_getbool(HSQUIRRELVM v, SQInteger idx, SQBool * b)
  21. :param HSQUIRRELVM v: the target VM
  22. :param SQInteger idx: an index in the stack
  23. :param SQBool * b: A pointer to the bool that will store the value
  24. :returns: a SQRESULT
  25. gets the value of the bool at the idx position in the stack.
  26. .. _sq_getbyhandle:
  27. .. c:function:: SQRESULT sq_getbyhandle(HSQUIRRELVM v, SQInteger idx, HSQMEMBERHANDLE* handle)
  28. :param HSQUIRRELVM v: the target VM
  29. :param SQInteger idx: an index in the stack pointing to the class or instance
  30. :param HSQMEMBERHANDLE* handle: a pointer to the member handle
  31. :returns: a SQRESULT
  32. pushes the value of a class or instance member using a member handle (see sq_getmemberhandle)
  33. .. _sq_getclosureinfo:
  34. .. c:function:: SQRESULT sq_getclosureinfo(HSQUIRRELVM v, SQInteger idx, SQUnsignedInteger * nparams, SQUnsignedInteger * nfreevars)
  35. :param HSQUIRRELVM v: the target VM
  36. :param SQInteger idx: index of the target closure
  37. :param SQUnsignedInteger * nparams: a pointer to an unsigned integer that will store the number of parameters
  38. :param SQUnsignedInteger * nfreevars: a pointer to an unsigned integer that will store the number of free variables
  39. :returns: an SQRESULT
  40. retrieves number of parameters and number of freevariables from a squirrel closure.
  41. .. _sq_getclosurename:
  42. .. c:function:: SQRESULT sq_getclosurename(HSQUIRRELVM v, SQInteger idx)
  43. :param HSQUIRRELVM v: the target VM
  44. :param SQInteger idx: index of the target closure
  45. :returns: an SQRESULT
  46. pushes the name of the closure at position idx in the stack. Note that the name can be a string or null if the closure is anonymous or a native closure with no name assigned to it.
  47. .. _sq_getclosureroot:
  48. .. c:function:: SQRESULT sq_getclosureroot(HSQUIRRELVM v, SQInteger idx)
  49. :param HSQUIRRELVM v: the target VM
  50. :param SQInteger idx: index of the target closure
  51. :returns: an SQRESULT
  52. pushes the root table of the closure at position idx in the stack
  53. .. _sq_getfloat:
  54. .. c:function:: SQRESULT sq_getfloat(HSQUIRRELVM v, SQInteger idx, SQFloat * f)
  55. :param HSQUIRRELVM v: the target VM
  56. :param SQInteger idx: an index in the stack
  57. :param SQFloat * f: A pointer to the float that will store the value
  58. :returns: a SQRESULT
  59. gets the value of the float at the idx position in the stack.
  60. .. _sq_gethash:
  61. .. c:function:: SQHash sq_gethash(HSQUIRRELVM v, SQInteger idx)
  62. :param HSQUIRRELVM v: the target VM
  63. :param SQInteger idx: an index in the stack
  64. :returns: the hash key of the value at the position idx in the stack
  65. :remarks: the hash value function is the same used by the VM.
  66. returns the hash key of a value at the idx position in the stack.
  67. .. _sq_getinstanceup:
  68. .. c:function:: SQRESULT sq_getinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer * up, SQUSerPointer typetag)
  69. :param HSQUIRRELVM v: the target VM
  70. :param SQInteger idx: an index in the stack
  71. :param SQUserPointer * up: a pointer to the userpointer that will store the result
  72. :param SQUSerPointer typetag: the typetag that has to be checked, if this value is set to 0 the typetag is ignored.
  73. :returns: a SQRESULT
  74. gets the userpointer of the class instance at position idx in the stack. if the parameter 'typetag' is different than 0, the function checks that the class or a base class of the instance is tagged with the specified tag; if not the function fails. If 'typetag' is 0 the function will ignore the tag check.
  75. .. _sq_getinteger:
  76. .. c:function:: SQRESULT sq_getinteger(HSQUIRRELVM v, SQInteger idx, SQInteger * i)
  77. :param HSQUIRRELVM v: the target VM
  78. :param SQInteger idx: an index in the stack
  79. :param SQInteger * i: A pointer to the integer that will store the value
  80. :returns: a SQRESULT
  81. gets the value of the integer at the idx position in the stack.
  82. .. _sq_getmemberhandle:
  83. .. c:function:: SQRESULT sq_getmemberhandle(HSQUIRRELVM v, SQInteger idx, HSQMEMBERHANDLE* handle)
  84. :param HSQUIRRELVM v: the target VM
  85. :param SQInteger idx: an index in the stack pointing to the class
  86. :param HSQMEMBERHANDLE* handle: a pointer to the variable that will store the handle
  87. :returns: a SQRESULT
  88. :remarks: This method works only with classes. A handle retrieved through a class can be later used to set or get values from one of the class instances. Handles retrieved from base classes are still valid in derived classes and respect inheritance rules.
  89. pops a value from the stack and uses it as index to fetch the handle of a class member. The handle can be later used to set or get the member value using sq_getbyhandle(), sq_setbyhandle().
  90. .. _sq_getreleasehook:
  91. .. c:function:: SQRELEASEHOOK sq_getreleasehook(HSQUIRRELVM v, SQInteger idx)
  92. :param HSQUIRRELVM v: the target VM
  93. :param SQInteger idx: an index in the stack
  94. :remarks: if the object that position idx is not an userdata, class instance or class the function returns NULL.
  95. gets the release hook of the userdata, class instance or class at position idx in the stack.
  96. .. _sq_getscratchpad:
  97. .. c:function:: SQChar * sq_getscratchpad(HSQUIRRELVM v, SQInteger minsize)
  98. :param HSQUIRRELVM v: the target VM
  99. :param SQInteger minsize: the requested size for the scratchpad buffer
  100. :remarks: the buffer is valid until the next call to sq_getscratchpad
  101. returns a pointer to a memory buffer that is at least as big as minsize.
  102. .. _sq_getsize:
  103. .. c:function:: SQObjectType sq_getsize(HSQUIRRELVM v, SQInteger idx)
  104. :param HSQUIRRELVM v: the target VM
  105. :param SQInteger idx: an index in the stack
  106. :returns: the size of the value at the position idx in the stack
  107. :remarks: this function only works with strings, arrays, tables, classes, instances, and userdata if the value is not a valid type, the function will return -1.
  108. returns the size of a value at the idx position in the stack. If the value is a class or a class instance the size returned is the size of the userdata buffer (see sq_setclassudsize).
  109. .. _sq_getstring:
  110. .. c:function:: SQRESULT sq_getstring(HSQUIRRELVM v, SQInteger idx, const SQChar ** c)
  111. :param HSQUIRRELVM v: the target VM
  112. :param SQInteger idx: an index in the stack
  113. :param const SQChar ** c: a pointer to the pointer that will point to the string
  114. :returns: a SQRESULT
  115. gets a pointer to the string at the idx position in the stack.
  116. .. _sq_getthread:
  117. .. c:function:: SQRESULT sq_getthread(HSQUIRRELVM v, SQInteger idx, HSQUIRRELVM* v)
  118. :param HSQUIRRELVM v: the target VM
  119. :param SQInteger idx: an index in the stack
  120. :param HSQUIRRELVM* v: A pointer to the variable that will store the thread pointer
  121. :returns: a SQRESULT
  122. gets a pointer to the thread the idx position in the stack.
  123. .. _sq_gettype:
  124. .. c:function:: SQObjectType sq_gettype(HSQUIRRELVM v, SQInteger idx)
  125. :param HSQUIRRELVM v: the target VM
  126. :param SQInteger idx: an index in the stack
  127. :returns: the type of the value at the position idx in the stack
  128. returns the type of the value at the position idx in the stack
  129. .. _sq_gettypetag:
  130. .. c:function:: SQRESULT sq_gettypetag(HSQUIRRELVM v, SQInteger idx, SQUserPointer * typetag)
  131. :param HSQUIRRELVM v: the target VM
  132. :param SQInteger idx: an index in the stack
  133. :param SQUserPointer * typetag: a pointer to the variable that will store the tag
  134. :returns: a SQRESULT
  135. :remarks: the function works also with instances. if the taget object is an instance, the typetag of it's base class is fetched.
  136. gets the typetag of the object (userdata or class) at position idx in the stack.
  137. .. _sq_getuserdata:
  138. .. c:function:: SQRESULT sq_getuserdata(HSQUIRRELVM v, SQInteger idx, SQUserPointer * p, SQUserPointer * typetag)
  139. :param HSQUIRRELVM v: the target VM
  140. :param SQInteger idx: an index in the stack
  141. :param SQUserPointer * p: A pointer to the userpointer that will point to the userdata's payload
  142. :param SQUserPointer * typetag: A pointer to a SQUserPointer that will store the userdata tag(see sq_settypetag). The parameter can be NULL.
  143. :returns: a SQRESULT
  144. gets a pointer to the value of the userdata at the idx position in the stack.
  145. .. _sq_getuserpointer:
  146. .. c:function:: SQRESULT sq_getuserpointer(HSQUIRRELVM v, SQInteger idx, SQUserPointer * p)
  147. :param HSQUIRRELVM v: the target VM
  148. :param SQInteger idx: an index in the stack
  149. :param SQUserPointer * p: A pointer to the userpointer that will store the value
  150. :returns: a SQRESULT
  151. gets the value of the userpointer at the idx position in the stack.
  152. .. _sq_newarray:
  153. .. c:function:: void sq_newarray(HSQUIRRELVM v, SQInteger size)
  154. :param HSQUIRRELVM v: the target VM
  155. :param SQInteger size: the size of the array that as to be created
  156. creates a new array and pushes it in the stack
  157. .. _sq_newclass:
  158. .. c:function:: SQRESULT sq_newclass(HSQUIRRELVM v, SQBool hasbase)
  159. :param HSQUIRRELVM v: the target VM
  160. :param SQBool hasbase: if the parameter is true the function expects a base class on top of the stack.
  161. :returns: a SQRESULT
  162. creates a new class object. If the parameter 'hasbase' is different than 0, the function pops a class from the stack and inherits the new created class from it.
  163. .. _sq_newclosure:
  164. .. c:function:: void sq_newclosure(HSQUIRRELVM v, HSQFUNCTION func, SQInteger nfreevars)
  165. :param HSQUIRRELVM v: the target VM
  166. :param HSQFUNCTION func: a pointer to a native-function
  167. :param SQInteger nfreevars: number of free variables(can be 0)
  168. create a new native closure, pops n values set those as free variables of the new closure, and push the new closure in the stack.
  169. .. _sq_newtable:
  170. .. c:function:: void sq_newtable(HSQUIRRELVM v)
  171. :param HSQUIRRELVM v: the target VM
  172. creates a new table and pushes it in the stack
  173. .. _sq_newtableex:
  174. .. c:function:: void sq_newtableex(HSQUIRRELVM v, SQInteger initialcapacity)
  175. :param HSQUIRRELVM v: the target VM
  176. :param SQInteger initialcapacity: number of key/value pairs to preallocate
  177. creates a new table and pushes it in the stack. This function allows you to specify the initial capacity of the table to prevent unnecessary rehashing when the number of slots required is known at creation-time.
  178. .. _sq_newuserdata:
  179. .. c:function:: SQUserPointer sq_newuserdata(HSQUIRRELVM v, SQUnsignedInteger size)
  180. :param HSQUIRRELVM v: the target VM
  181. :param SQUnsignedInteger size: the size of the userdata that as to be created in bytes
  182. creates a new userdata and pushes it in the stack
  183. .. _sq_pushbool:
  184. .. c:function:: void sq_pushbool(HSQUIRRELVM v, SQBool b)
  185. :param HSQUIRRELVM v: the target VM
  186. :param SQBool b: the bool that has to be pushed(SQTrue or SQFalse)
  187. pushes a bool into the stack
  188. .. _sq_pushfloat:
  189. .. c:function:: void sq_pushfloat(HSQUIRRELVM v, SQFloat f)
  190. :param HSQUIRRELVM v: the target VM
  191. :param SQFloat f: the float that has to be pushed
  192. pushes a float into the stack
  193. .. _sq_pushinteger:
  194. .. c:function:: void sq_pushinteger(HSQUIRRELVM v, SQInteger n)
  195. :param HSQUIRRELVM v: the target VM
  196. :param SQInteger n: the integer that has to be pushed
  197. pushes an integer into the stack
  198. .. _sq_pushnull:
  199. .. c:function:: void sq_pushnull(HSQUIRRELVM v)
  200. :param HSQUIRRELVM v: the target VM
  201. pushes a null value into the stack
  202. .. _sq_pushstring:
  203. .. c:function:: void sq_pushstring(HSQUIRRELVM v, const SQChar * s, SQInteger len)
  204. :param HSQUIRRELVM v: the target VM
  205. :param const SQChar * s: pointer to the string that has to be pushed
  206. :param SQInteger len: length of the string pointed by s
  207. :remarks: if the parameter len is less than 0 the VM will calculate the length using strlen(s)
  208. pushes a string in the stack
  209. .. _sq_pushuserpointer:
  210. .. c:function:: void sq_pushuserpointer(HSQUIRRELVM v, SQUserPointer p)
  211. :param HSQUIRRELVM v: the target VM
  212. :param SQUserPointer p: the pointer that as to be pushed
  213. pushes a userpointer into the stack
  214. .. _sq_setbyhandle:
  215. .. c:function:: SQRESULT sq_setbyhandle(HSQUIRRELVM v, SQInteger idx, HSQMEMBERHANDLE* handle)
  216. :param HSQUIRRELVM v: the target VM
  217. :param SQInteger idx: an index in the stack pointing to the class
  218. :param HSQMEMBERHANDLE* handle: a pointer the member handle
  219. :returns: a SQRESULT
  220. pops a value from the stack and sets it to a class or instance member using a member handle (see sq_getmemberhandle)
  221. .. _sq_setclassudsize:
  222. .. c:function:: SQRESULT sq_setclassudsize(HSQUIRRELVM v, SQInteger idx, SQInteger udsize)
  223. :param HSQUIRRELVM v: the target VM
  224. :param SQInteger idx: an index in the stack pointing to the class
  225. :param SQInteger udsize: size in bytes reserved for user data
  226. :returns: a SQRESULT
  227. Sets the user data size of a class. If a class 'user data size' is greater than 0. When an instance of the class is created additional space will be reserved at the end of the memory chunk where the instance is stored. The userpointer of the instance will also be automatically set to this memory area. This allows you to minimize allocations in applications that have to carry data along with the class instance.
  228. .. _sq_setclosureroot:
  229. .. c:function:: SQRESULT sq_setclosureroot(HSQUIRRELVM v, SQInteger idx)
  230. :param HSQUIRRELVM v: the target VM
  231. :param SQInteger idx: index of the target closure
  232. :returns: an SQRESULT
  233. pops a table from the stack and sets it as root of the closure at position idx in the stack
  234. .. _sq_setinstanceup:
  235. .. c:function:: SQRESULT sq_setinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer up)
  236. :param HSQUIRRELVM v: the target VM
  237. :param SQInteger idx: an index in the stack
  238. :param SQUserPointer up: an arbitrary user pointer
  239. :returns: a SQRESULT
  240. sets the userpointer of the class instance at position idx in the stack.
  241. .. _sq_setnativeclosurename:
  242. .. c:function:: SQRESULT sq_setnativeclosurename(HSQUIRRELVM v, SQInteger idx, const SQChar * name)
  243. :param HSQUIRRELVM v: the target VM
  244. :param SQInteger idx: index of the target native closure
  245. :param const SQChar * name: the name that has to be set
  246. :returns: an SQRESULT
  247. sets the name of the native closure at the position idx in the stack. The name of a native closure is purely for debug purposes. The name is retrieved trough the function sq_stackinfos() while the closure is in the call stack.
  248. .. _sq_setparamscheck:
  249. .. c:function:: SQRESULT sq_setparamscheck(HSQUIRRELVM v, SQInteger nparamscheck, const SQChar * typemask)
  250. :param HSQUIRRELVM v: the target VM
  251. :param SQInteger nparamscheck: defines the parameters number check policy (0 disables the param checking). If nparamscheck is greater than 0, the VM ensures that the number of parameters is exactly the number specified in nparamscheck (eg. if nparamscheck == 3 the function can only be called with 3 parameters). If nparamscheck is less than 0 the VM ensures that the closure is called with at least the absolute value of the number specified in nparamcheck (eg. nparamscheck == -3 will check that the function is called with at least 3 parameters). The hidden parameter 'this' is included in this number; free variables aren't. If SQ_MATCHTYPEMASKSTRING is passed instead of the number of parameters, the function will automatically infer the number of parameters to check from the typemask (eg. if the typemask is ".sn", it is like passing 3).
  252. :param const SQChar * typemask: defines a mask to validate the parametes types passed to the function. If the parameter is NULL, no typechecking is applied (default).
  253. :remarks: The typemask consists in a zero terminated string that represent the expected parameter type. The types are expressed as follows: 'o' null, 'i' integer, 'f' float, 'n' integer or float, 's' string, 't' table, 'a' array, 'u' userdata, 'c' closure and nativeclosure, 'g' generator, 'p' userpointer, 'v' thread, 'x' instance(class instance), 'y' class, 'b' bool. and '.' any type. The symbol '|' can be used as 'or' to accept multiple types on the same parameter. There isn't any limit on the number of 'or' that can be used. Spaces are ignored so can be inserted between types to increase readability. For instance to check a function that expect a table as 'this' a string as first parameter and a number or a userpointer as second parameter, the string would be "tsn|p" (table,string,number or userpointer). If the parameters mask is contains fewer parameters than 'nparamscheck', the remaining parameters will not be typechecked.
  254. Sets the parameter validation scheme for the native closure at the top position in the stack. Allows you to validate the number of parameters accepted by the function and optionally their types. If the function call does not comply with the parameter schema set by sq_setparamscheck, an exception is thrown.
  255. *.eg*
  256. ::
  257. //example
  258. SQInteger testy(HSQUIRRELVM v)
  259. {
  260. SQUserPointer p;
  261. const SQChar *s;
  262. SQInteger i;
  263. //no type checking, if the call complies with the mask
  264. //surely the functions will succeed.
  265. sq_getuserdata(v,1,&p,NULL);
  266. sq_getstring(v,2,&s);
  267. sq_getinteger(v,3,&i);
  268. //... do something
  269. return 0;
  270. }
  271. //the reg code
  272. //....stuff
  273. sq_newclosure(v,testy,0);
  274. //expects exactly 3 parameters(userdata,string,number)
  275. sq_setparamscheck(v,3,_SC("usn"));
  276. //....stuff
  277. .. _sq_setreleasehook:
  278. .. c:function:: void sq_setreleasehook(HSQUIRRELVM v, SQInteger idx, SQRELEASEHOOK hook)
  279. :param HSQUIRRELVM v: the target VM
  280. :param SQInteger idx: an index in the stack
  281. :param SQRELEASEHOOK hook: a function pointer to the hook(see sample below)
  282. :remarks: the function hook is called by the VM before the userdata memory is deleted.
  283. sets the release hook of the userdata, class instance, or class at position idx in the stack.
  284. *.eg*
  285. ::
  286. /* tyedef SQInteger (*SQRELEASEHOOK)(SQUserPointer,SQInteger size); */
  287. SQInteger my_release_hook(SQUserPointer p,SQInteger size)
  288. {
  289. /* do something here */
  290. return 1;
  291. }
  292. .. _sq_settypetag:
  293. .. c:function:: SQRESULT sq_settypetag(HSQUIRRELVM v, SQInteger idx, SQUserPointer typetag)
  294. :param HSQUIRRELVM v: the target VM
  295. :param SQInteger idx: an index in the stack
  296. :param SQUserPointer typetag: an arbitrary SQUserPointer
  297. :returns: a SQRESULT
  298. sets the typetag of the object (userdata or class) at position idx in the stack.
  299. .. _sq_tobool:
  300. .. c:function:: void sq_tobool(HSQUIRRELVM v, SQInteger idx, SQBool * b)
  301. :param HSQUIRRELVM v: the target VM
  302. :param SQInteger idx: an index in the stack
  303. :param SQBool * b: A pointer to the bool that will store the value
  304. :remarks: if the object is not a bool the function converts the value to bool according to squirrel's rules. For instance the number 1 will result in true, and the number 0 in false.
  305. gets the value at position idx in the stack as bool.
  306. .. _sq_tostring:
  307. .. c:function:: void sq_tostring(HSQUIRRELVM v, SQInteger idx)
  308. :param HSQUIRRELVM v: the target VM
  309. :param SQInteger idx: an index in the stack
  310. converts the object at position idx in the stack to string and pushes the resulting string in the stack.
  311. .. _sq_typeof:
  312. .. c:function:: SQObjectType sq_typeof(HSQUIRRELVM v, SQInteger idx)
  313. :param HSQUIRRELVM v: the target VM
  314. :param SQInteger idx: an index in the stack
  315. :returns: a SQRESULT
  316. pushes the type name of the value at the position idx in the stack. It also invokes the _typeof metamethod for tables and class instances that implement it; in that case the pushed object could be something other than a string (is up to the _typeof implementation).