Advertisement
snake5

SGScript docs - object interface

Mar 11th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. # Object interface [info]
  2.  
  3. Every interface function has the type `int ObjCallback ( sgs_Context* C, sgs_VarObj* data, ... )`. Not every of them has to be implemented (none of them are required) but for all non-pointer objects it helps to have at least one of them.
  4.  
  5. Interface is a structure that contains of an array and 10 function pointers in the following order: destruct, gcmark, getindex, setindex, convert, serialize, dump, getnext, call, expr. This is how interfaces are usually defined in code:
  6.  
  7. sgs_ObjInterface object_interface[1] =
  8. {{
  9. "object_type_name",
  10. object_destruct, NULL, /* destruct, gcmark */
  11. object_getindex, NULL, /* getindex, setindex */
  12. NULL, NULL, NULL, NULL, /* convert, serialize, dump, getnext */
  13. NULL, NULL /* call, expr */
  14. }};
  15.  
  16. The interface is defined as an array with size=1 to later be able to use it in code without prepending "&" to the name.
  17.  
  18. === DESTRUCT - destruction callback
  19.  
  20. ==== When called:
  21.  
  22. - Before the object is about to be destroyed.
  23. - On @sgs_ObjCallDtor
  24.  
  25. ==== Additional arguments:
  26.  
  27. None.
  28.  
  29. ==== Stack frame:
  30.  
  31. Empty.
  32.  
  33. ==== Return values:
  34.  
  35. Non-negative value if successful, negative on error.
  36.  
  37. ==== Additional notes:
  38.  
  39. It is important to minimize the possibility of failure here. The system cannot help in any way if memory or ownership states are corrupted.
  40.  
  41. === GETINDEX - index/property retrieval callback
  42.  
  43. ==== When called:
  44.  
  45. - On A[B] (index) and A.B (property) reads in SGScript.
  46. - Index/Property/Path function families in the C API.
  47.  
  48. ==== Additional arguments:
  49.  
  50. - sgs_Variable* key -- key variable to be used to find a sub-item
  51. - int isprop -- (0/1) whether this is a property read (1) or index read (0)
  52.  
  53. ==== Stack frame:
  54.  
  55. Empty at beginning. Expected to have at least one item on stack after a successful index/property read. The topmost one is used.
  56.  
  57. ==== Return values:
  58.  
  59. - Non-negative value if successful, negative on error.
  60. - Use SGS_ENOTFND if the specified index/property was not found.
  61.  
  62. ==== Additional notes:
  63.  
  64. It is safe to use conversion functions on the key variable.
  65.  
  66. === SETINDEX - index/property setting callback
  67.  
  68. ==== When called:
  69.  
  70. - On A[B] (index) and A.B (property) writes in SGScript.
  71. - Index/Property/Path function families in the C API.
  72.  
  73. ==== Additional arguments:
  74.  
  75. - sgs_Variable* key -- key variable to be used to find a sub-item
  76. - sgs_Variable* value -- value variable to be used in setting the value of the sub-item
  77. - int isprop -- (0/1) whether this is a property read (1) or index read (0)
  78.  
  79. ==== Stack frame:
  80.  
  81. Empty.
  82.  
  83. ==== Return values:
  84.  
  85. - Non-negative value if successful, negative on error.
  86. - Use SGS_ENOTFND if the specified index/property was not found.
  87. - Use SGS_EINVAL if the given value could not be used.
  88.  
  89. ==== Additional notes:
  90.  
  91. It is safe to use conversion functions on both variables.
  92.  
  93. === CONVERT - conversion callback
  94.  
  95. ==== When called:
  96.  
  97. Depending on the argument, it is called by different sources:
  98. - type conversion: to*** function families and other conversion triggers (like operators) in SGScript and the Get/To/Parse function families in the C API.
  99. - SGS_CONVOP_CLONE: called on @clone / @sgs_Clone
  100. - SGS_CONVOP_TYPEOF: called on @typeof / @sgs_TypeOf
  101. - SGS_CONVOP_TOITER: called on foreach / @sgs_PushIterator(P) / @sgs_GetIterator(P)
  102.  
  103. ==== Additional arguments:
  104.  
  105. - int type -- one of SGS_VT_[BOOL|INT|REAL|STRING|PTR] or SGS_CONVOP_CLONE / SGS_CONVOP_TYPEOF / SGS_CONVOP_TOITER
  106.  
  107. ==== Stack frame:
  108.  
  109. Empty at beginning. Expected to have at least one item on stack after a successful conversion. The topmost one is used.
  110.  
  111. Depending on the argument, it requires different variable types on stack:
  112. - type conversions require a value of the right type.
  113. - SGS_CONVOP_CLONE should return an exact copy of the same object
  114. - SGS_CONVOP_TYPEOF should return a string containing the type name
  115. - SGS_CONVOP_TOITER should return an object with a GETNEXT callback
  116.  
  117. ==== Return values:
  118.  
  119. - Non-negative value if successful, negative on error.
  120. - Use SGS_ENOTSUP if specified conversion is not supported.
  121.  
  122. ==== Additional notes:
  123.  
  124. - It should be safe to give the wrong variable type but errors may not be triggered in such cases.
  125.  
  126. === SERIALIZE - serialization callback
  127.  
  128. ==== When called:
  129.  
  130. - @serialize in SGScript.
  131. - @sgs_Serialize in the C API.
  132.  
  133. ==== Additional arguments:
  134.  
  135. None.
  136.  
  137. ==== Stack frame:
  138.  
  139. Empty.
  140.  
  141. ==== Return values:
  142.  
  143. - Non-negative value if successful, negative on error.
  144.  
  145. ==== Additional notes:
  146.  
  147. Callback requires no data but expects that @sgs_Serialize / @sgs_SerializeObject is successfully called once. In the case of sgs_SerializeObject, the necessary number (equal to argument count, passed to the function) of sgs_Serialize calls must be made before it.
  148.  
  149. === DUMP - debug dump callback
  150.  
  151. ==== When called:
  152.  
  153. - @printvar, @dumpvar, @printvar_ext, @dumpvar_ext in SGScript.
  154. - @sgs_DumpVar in the C API.
  155.  
  156. ==== Additional arguments:
  157.  
  158. - int maxdepth -- remaining recursion depth of dump to be passed on to @sgs_DumpVar calls.
  159.  
  160. ==== Stack frame:
  161.  
  162. Empty at beginning. Expected to have at least one item on stack after a successful conversion. The topmost one is used.
  163.  
  164. ==== Return values:
  165.  
  166. - Non-negative value if successful, negative on error.
  167.  
  168. ==== Additional notes:
  169.  
  170. - Callback expects a string variable on the top of the stack.
  171. - @sgs_DumpVar with maxdepth <= 0 returns "...", so it is not necessary to handle such cases beyond passing the parameter.
  172.  
  173. === GCMARK - garbage collector marking callback
  174.  
  175. ==== When called:
  176.  
  177. - @gc_collect in SGScript.
  178. - @sgs_GCMark / @sgs_GCExecute in the C API.
  179.  
  180. ==== Additional arguments:
  181.  
  182. None.
  183.  
  184. ==== Stack frame:
  185.  
  186. Empty.
  187.  
  188. ==== Return values:
  189.  
  190. - Non-negative value if successful, negative on error.
  191.  
  192. ==== Additional notes:
  193.  
  194. - Callback expects that sgs_GCMark is called on all of the owned variables.
  195. - It is important to minimize the possibility of failure here. The system cannot help in any way if memory or ownership states are corrupted.
  196.  
  197. === GETNEXT - iterator control callback
  198.  
  199. ==== When called:
  200.  
  201. - foreach in SGScript.
  202. - @sgs_IterAdvance(P) / @sgs_IterPushData(P) / @sgs_IterGetData(P)
  203.  
  204. ==== Additional arguments:
  205.  
  206. - int act -- specifies the actions that should be taken in this call.
  207. -- if argument == 0, iterator's position must be increased by one step and the return value must contain whether iterator has not reached end (positive value if so, otherwise - 0) or an error if callback has failed
  208. -- otherwise, callback must push the data required (if SGS_GETNEXT_KEY is set, the key, and if SGS_GETNEXT_VALUE is set - the value, in exactly that order)
  209.  
  210. ==== Stack frame:
  211.  
  212. Empty at beginning. Expects at least one (if either flag is set, but not both) or two (if both flags are set) variables on success. Key first, value second.
  213.  
  214. ==== Return values:
  215.  
  216. - Non-negative value if successful, negative on error.
  217. - if argument == 0, must return whether iterator has not reached end (positive value if so, otherwise - 0)
  218.  
  219. ==== Additional notes:
  220.  
  221. None.
  222.  
  223. === CALL - the "function call" callback
  224.  
  225. ==== When called:
  226.  
  227. - when an object is used like a function
  228. -- `object_variable( .. )` in SGScript
  229. -- Call function family in the C API
  230.  
  231. ==== Additional arguments:
  232.  
  233. None.
  234.  
  235. ==== Stack frame:
  236.  
  237. Contains all the same things as any C function call would: optional `this` variable and optional argument list. Expects at least the returned number of items after the call to be used as return values.
  238.  
  239. ==== Return values:
  240.  
  241. - Non-negative value if successful, negative on error.
  242. - On success, the number returned is the number of variables returned by the function.
  243.  
  244. ==== Additional notes:
  245.  
  246. None.
  247.  
  248. === EXPR - expression callback
  249.  
  250. === When called:
  251.  
  252. - On arithmetic operations with the object as one or both operands in SGScript.
  253. - On @sgs_ArithOp in the C API.
  254.  
  255. ==== Additional arguments:
  256.  
  257. - sgs_Variable* A - the first (left side) operand or the only one in case of SGS_EOP_NEGATE
  258. - sgs_Variable* B - the second (right side) operand or NULL in case of SGS_EOP_NEGATE
  259. - int op - operation type, one of SGS_EOP_[ADD|SUB|MUL|DIV|MOD|COMPARE|NEGATE]
  260.  
  261. ==== Stack frame:
  262.  
  263. Empty at beginning. Expects at least one variable on success (the topmost one will be used).
  264.  
  265. ==== Return values:
  266.  
  267. - Non-negative value if successful, negative on error.
  268.  
  269. ==== Additional notes:
  270.  
  271. - SGS_EOP_COMPARE expects int/real value: >0 if A > B, <0 if A < B, =0 if A = B
  272.  
  273. The full list of operators triggering the operations:
  274.  
  275. - SGS_EOP_ADD: binary +, +=
  276. - SGS_EOP_SUB: binary -, -=
  277. - SGS_EOP_MUL: *, *=
  278. - SGS_EOP_DIV: /, /=
  279. - SGS_EOP_MOD: %, %=
  280. - SGS_EOP_COMPARE: <, <=, >, >=, ==, !=, ===, !==
  281. - SGS_EOP_NEGATE: unary -
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement