Deji

SuperVars: Objective-SCM

Oct 6th, 2012
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. {********************************************}
  2. {* Objective-SCM for SB3 *}
  3. {* SuperVars required *}
  4. {* Written by Deji in SB-SCR *}
  5. {********************************************}
  6.  
  7. CONST
  8. OBJECT_HEADER_SIZE = 0x14
  9. _OBJECT_DTOR = -1@
  10. _OBJECT_GET_TYPE = -2@
  11. _OBJECT_GET_SIZE = -3@
  12. _OBJECT_GET_NUM = -4@
  13. SUPERVAR = string
  14. this = 0@
  15. END
  16. VAR
  17. THIS:array 4 of SUPERVAR
  18. END
  19.  
  20. //core functions
  21. {}:_object_init
  22. CALL 1@ 0 3@ 4@ 5@
  23. 0085: this[_OBJECT_DTOR] = 4@
  24. 0085: this[_OBJECT_GET_TYPE] = 1@
  25. 0085: this[_OBJECT_GET_SIZE] = 5@
  26. 2@ += 1
  27. 0085: this[_OBJECT_GET_NUM] = 2@
  28. this += OBJECT_HEADER_SIZE
  29. 0006: this[this] = @_object_vtbl
  30. RET 1 this
  31.  
  32. {}:_object_vtbl
  33. HEX
  34. 00000000
  35. @_object_dtor
  36. @_object_gettype
  37. @_object_getsize
  38. @_object_getnum
  39. END
  40.  
  41. {}:_object_dtor
  42. this -= OBJECT_HEADER_SIZE
  43. 8039: NOT this[_OBJECT_DTOR] == 0
  44. ELSE_GOTO @_object_dtor_end
  45. CALL this[_OBJECT_DTOR] 1 this
  46. {}:_object_dtor_end
  47. CALL @free 1 this
  48. RET 0
  49.  
  50. {}:_object_gettype
  51. this -= OBJECT_HEADER_SIZE
  52. RET 1 this[_OBJECT_GET_TYPE]
  53.  
  54. {}:_object_getsize
  55. this -= OBJECT_HEADER_SIZE
  56. RET 1 this[_OBJECT_GET_SIZE]
  57.  
  58. {}:_object_getnum
  59. this -= OBJECT_HEADER_SIZE
  60. RET 1 this[_OBJECT_GET_NUM]
  61.  
  62. {}:_object_getbase
  63. WHILE 8039: NOT this[this] == 0
  64. 0085: this = this[this]
  65. END
  66. RET 1 this
  67.  
  68. :GetObjectBase{\__(obj)__}
  69. CALL @_object_getbase 1 this this
  70. RET 1 this
  71.  
  72. :new{\__[pObj]__(type(),_nNum)__}
  73. {
  74. Description:
  75. Similar to new[] in C++. This creates an object or an array of objects of a certain type().
  76. Emulation of type's can be created using a type() function, for example:
  77. :type
  78. ret 3 @constructor_call @destructor_call 32
  79. In this example, 32 is the type()'s size. Use 0 to not use a constructor/destructor call.
  80. You can use a constructor function to set a virtual table, but the first VTBL call MUST
  81. be a pointer to the default VTBL (@_object_vtbl), unless you wish to replace the default
  82. behaviours (not recommended).
  83.  
  84. The num parameter is the number of elements of type() to allocate in a new array.
  85. Equivalent to using: new type[32]; in C++
  86.  
  87. On success, the script condition result is set to TRUE and the object/array pointer is
  88. returned.
  89. On failure, the script condition result is set to FALSE and NULL is returned.
  90. }
  91. CALL 0@ 0 2@ 3@ 4@
  92. 0A90: 5@ = 4@ * 1@
  93. 5@ += OBJECT_HEADER_SIZE
  94. CALL @malloc 1 5@ 6@
  95. ELSE_GOTO @new_fail
  96. WRITE_MEMORY 6@ 5@ 0 OFF
  97. CALL @_object_init 3 6@ 0@ 1@ 6@
  98. NOT 2@ == 0
  99. ELSE_GOTO @new_end
  100. NOT 1@ == 0
  101. ELSE_GOTO @new_1
  102. CALL @array_walk 2 6@ 2@
  103. GOTO @new_end
  104. {}:new_1
  105. CALL 2@ 1 6@
  106. {}:new_end
  107. RETURN_TRUE
  108. RET 1 6@
  109. {}:new_fail
  110. RET 1 0
  111.  
  112. :delete{\__(pObj)__}
  113. {
  114. Description:
  115. Similar to delete in C++. This deletes an object or an array of objects and frees the
  116. memory allocated to it.
  117. }
  118. CALL this[_OBJECT_DTOR] 1 this
  119. RET 0
  120.  
  121. :malloc{\__[pMem]__(nSize)__}
  122. {
  123. Description:
  124. Basically just a wrapper for CLEO4's ALLOCATE_MEMORY.
  125.  
  126. On success, the script condition result is set to TRUE and the memory pointer is
  127. returned.
  128. On failure, the script condition result is set to FALSE and NULL is returned.
  129. }
  130. ALLOCATE_MEMORY 0@ 1@
  131. ELSE_GOTO @malloc_fail
  132. RET 1 1@
  133. {}:malloc_fail
  134. PRINTSTRING "ALLOC_ERROR"
  135. RET 1 0
  136.  
  137. :free{\__(pMem)__}
  138. {
  139. Description:
  140. Free's the memory allocated with malloc.
  141. }
  142. NOT 0@ == 0
  143. ELSE_GOTO @_free_END
  144. FREE_MEMORY 0@
  145. {}:_free_END
  146. RET 0
  147.  
  148. :calloc{\__[pMem]__(nSize,_nCount)__}
  149. {
  150. Description:
  151. C-style calloc wrapper for malloc. The allocated memory is initialised to zero.
  152.  
  153. On success, the script condition result is set to TRUE and the memory pointer is
  154. returned.
  155. On failure, the script condition result is set to FALSE and NULL is returned.
  156. }
  157. NOT 1@ == 0
  158. ELSE_GOTO @calloc_1
  159. 006A: 0@ *= 1@
  160. {}:calloc_1
  161. CALL @malloc 1 0@ 2@
  162. ELSE_GOTO @calloc_end
  163. WRITE_MEMORY 2@ 0@ 0 OFF
  164. RET 1 2@
  165. {}:calloc_end
  166. RET 1 0
  167.  
  168. :sizeof{\__[nSize]__(pObj)__}
  169. {
  170. Description:
  171. Returns the size of the type(), object or array of objects in bytes.
  172. }
  173. 0@ >= 0
  174. ELSE_GOTO @sizeof_1
  175. 0085: 1@ = 0@[0@]
  176. CALL 1@(_OBJECT_GET_SIZE,4s) 1 0@ 2@
  177. CALL 1@(_OBJECT_GET_NUM,4s) 1 0@ 3@
  178. 006A: 2@ *= 3@
  179. GOTO @sizeof_end
  180. {}:sizeof_1
  181. CALL 0@ 0 0@ 1@ 2@
  182. {}:sizeof_end
  183. RET 1 2@
  184.  
  185. :count{\__[nCount]__(Obj[])__}
  186. {
  187. Description:
  188. Returns the number of objects in an array.
  189. If used on a single object, the result will be 1.
  190. }
  191. 0085: 1@ = this[this]
  192. CALL 1@(_OBJECT_GET_NUM,4s) 1 this 2@
  193. RET 1 2@
  194.  
  195. :gettype{\__[type()]__(pObj)__}
  196. {
  197. Description:
  198. Returns the objects type().
  199. }
  200. 0085: 1@ = this[this]
  201. CALL 1@(_OBJECT_GET_TYPE,4s) 1 this 2@
  202. RET 1 2@
  203.  
  204. :array_walk{\__(Obj[],_Func())__}
  205. {
  206. Description:
  207. Iterates over the array and calls the specified method on each object.
  208. }
  209. CALL @gettype 1 0@ 2@
  210. CALL @sizeof 1 2@ 3@
  211. CALL @sizeof 1 0@ 4@
  212. 0A8E: 5@ = 0@ + 4@
  213. 0062: 5@ -= 3@
  214. FOR 0@ = 0@ TO 5@ STEP 3@
  215. CALL 1@ 1 0@
  216. END
  217. RET 0
Advertisement
Add Comment
Please, Sign In to add comment