collections_glue.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*************************************************************************/
  2. /* collections_glue.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "collections_glue.h"
  31. #ifdef MONO_GLUE_ENABLED
  32. #include <mono/metadata/exception.h>
  33. #include "../mono_gd/gd_mono_class.h"
  34. #include "../mono_gd/gd_mono_utils.h"
  35. Array *godot_icall_Array_Ctor() {
  36. return memnew(Array);
  37. }
  38. void godot_icall_Array_Dtor(Array *ptr) {
  39. memdelete(ptr);
  40. }
  41. MonoObject *godot_icall_Array_At(Array *ptr, int index) {
  42. if (index < 0 || index > ptr->size()) {
  43. GDMonoUtils::set_pending_exception(mono_get_exception_index_out_of_range());
  44. return NULL;
  45. }
  46. return GDMonoMarshal::variant_to_mono_object(ptr->operator[](index));
  47. }
  48. MonoObject *godot_icall_Array_At_Generic(Array *ptr, int index, uint32_t type_encoding, GDMonoClass *type_class) {
  49. if (index < 0 || index > ptr->size()) {
  50. GDMonoUtils::set_pending_exception(mono_get_exception_index_out_of_range());
  51. return NULL;
  52. }
  53. return GDMonoMarshal::variant_to_mono_object(ptr->operator[](index), ManagedType(type_encoding, type_class));
  54. }
  55. void godot_icall_Array_SetAt(Array *ptr, int index, MonoObject *value) {
  56. if (index < 0 || index > ptr->size()) {
  57. GDMonoUtils::set_pending_exception(mono_get_exception_index_out_of_range());
  58. return;
  59. }
  60. ptr->operator[](index) = GDMonoMarshal::mono_object_to_variant(value);
  61. }
  62. int godot_icall_Array_Count(Array *ptr) {
  63. return ptr->size();
  64. }
  65. void godot_icall_Array_Add(Array *ptr, MonoObject *item) {
  66. ptr->append(GDMonoMarshal::mono_object_to_variant(item));
  67. }
  68. void godot_icall_Array_Clear(Array *ptr) {
  69. ptr->clear();
  70. }
  71. bool godot_icall_Array_Contains(Array *ptr, MonoObject *item) {
  72. return ptr->find(GDMonoMarshal::mono_object_to_variant(item)) != -1;
  73. }
  74. void godot_icall_Array_CopyTo(Array *ptr, MonoArray *array, int array_index) {
  75. unsigned int count = ptr->size();
  76. if (mono_array_length(array) < (array_index + count)) {
  77. MonoException *exc = mono_get_exception_argument("", "Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
  78. GDMonoUtils::set_pending_exception(exc);
  79. return;
  80. }
  81. for (unsigned int i = 0; i < count; i++) {
  82. MonoObject *boxed = GDMonoMarshal::variant_to_mono_object(ptr->operator[](i));
  83. mono_array_setref(array, array_index, boxed);
  84. array_index++;
  85. }
  86. }
  87. int godot_icall_Array_IndexOf(Array *ptr, MonoObject *item) {
  88. return ptr->find(GDMonoMarshal::mono_object_to_variant(item));
  89. }
  90. void godot_icall_Array_Insert(Array *ptr, int index, MonoObject *item) {
  91. if (index < 0 || index > ptr->size()) {
  92. GDMonoUtils::set_pending_exception(mono_get_exception_index_out_of_range());
  93. return;
  94. }
  95. ptr->insert(index, GDMonoMarshal::mono_object_to_variant(item));
  96. }
  97. bool godot_icall_Array_Remove(Array *ptr, MonoObject *item) {
  98. int idx = ptr->find(GDMonoMarshal::mono_object_to_variant(item));
  99. if (idx >= 0) {
  100. ptr->remove(idx);
  101. return true;
  102. }
  103. return false;
  104. }
  105. void godot_icall_Array_RemoveAt(Array *ptr, int index) {
  106. if (index < 0 || index > ptr->size()) {
  107. GDMonoUtils::set_pending_exception(mono_get_exception_index_out_of_range());
  108. return;
  109. }
  110. ptr->remove(index);
  111. }
  112. Error godot_icall_Array_Resize(Array *ptr, int new_size) {
  113. return ptr->resize(new_size);
  114. }
  115. void godot_icall_Array_Generic_GetElementTypeInfo(MonoReflectionType *refltype, uint32_t *type_encoding, GDMonoClass **type_class) {
  116. MonoType *elem_type = mono_reflection_type_get_type(refltype);
  117. *type_encoding = mono_type_get_type(elem_type);
  118. MonoClass *type_class_raw = mono_class_from_mono_type(elem_type);
  119. *type_class = GDMono::get_singleton()->get_class(type_class_raw);
  120. }
  121. Dictionary *godot_icall_Dictionary_Ctor() {
  122. return memnew(Dictionary);
  123. }
  124. void godot_icall_Dictionary_Dtor(Dictionary *ptr) {
  125. memdelete(ptr);
  126. }
  127. MonoObject *godot_icall_Dictionary_GetValue(Dictionary *ptr, MonoObject *key) {
  128. Variant *ret = ptr->getptr(GDMonoMarshal::mono_object_to_variant(key));
  129. if (ret == NULL) {
  130. MonoObject *exc = mono_object_new(mono_domain_get(), CACHED_CLASS(KeyNotFoundException)->get_mono_ptr());
  131. #ifdef DEBUG_ENABLED
  132. CRASH_COND(!exc);
  133. #endif
  134. GDMonoUtils::runtime_object_init(exc);
  135. GDMonoUtils::set_pending_exception((MonoException *)exc);
  136. return NULL;
  137. }
  138. return GDMonoMarshal::variant_to_mono_object(ret);
  139. }
  140. MonoObject *godot_icall_Dictionary_GetValue_Generic(Dictionary *ptr, MonoObject *key, uint32_t type_encoding, GDMonoClass *type_class) {
  141. Variant *ret = ptr->getptr(GDMonoMarshal::mono_object_to_variant(key));
  142. if (ret == NULL) {
  143. MonoObject *exc = mono_object_new(mono_domain_get(), CACHED_CLASS(KeyNotFoundException)->get_mono_ptr());
  144. #ifdef DEBUG_ENABLED
  145. CRASH_COND(!exc);
  146. #endif
  147. GDMonoUtils::runtime_object_init(exc);
  148. GDMonoUtils::set_pending_exception((MonoException *)exc);
  149. return NULL;
  150. }
  151. return GDMonoMarshal::variant_to_mono_object(ret, ManagedType(type_encoding, type_class));
  152. }
  153. void godot_icall_Dictionary_SetValue(Dictionary *ptr, MonoObject *key, MonoObject *value) {
  154. ptr->operator[](GDMonoMarshal::mono_object_to_variant(key)) = GDMonoMarshal::mono_object_to_variant(value);
  155. }
  156. Array *godot_icall_Dictionary_Keys(Dictionary *ptr) {
  157. return memnew(Array(ptr->keys()));
  158. }
  159. Array *godot_icall_Dictionary_Values(Dictionary *ptr) {
  160. return memnew(Array(ptr->values()));
  161. }
  162. int godot_icall_Dictionary_Count(Dictionary *ptr) {
  163. return ptr->size();
  164. }
  165. void godot_icall_Dictionary_Add(Dictionary *ptr, MonoObject *key, MonoObject *value) {
  166. Variant varKey = GDMonoMarshal::mono_object_to_variant(key);
  167. Variant *ret = ptr->getptr(varKey);
  168. if (ret != NULL) {
  169. GDMonoUtils::set_pending_exception(mono_get_exception_argument("key", "An element with the same key already exists"));
  170. return;
  171. }
  172. ptr->operator[](varKey) = GDMonoMarshal::mono_object_to_variant(value);
  173. }
  174. void godot_icall_Dictionary_Clear(Dictionary *ptr) {
  175. ptr->clear();
  176. }
  177. bool godot_icall_Dictionary_Contains(Dictionary *ptr, MonoObject *key, MonoObject *value) {
  178. // no dupes
  179. Variant *ret = ptr->getptr(GDMonoMarshal::mono_object_to_variant(key));
  180. return ret != NULL && *ret == GDMonoMarshal::mono_object_to_variant(value);
  181. }
  182. bool godot_icall_Dictionary_ContainsKey(Dictionary *ptr, MonoObject *key) {
  183. return ptr->has(GDMonoMarshal::mono_object_to_variant(key));
  184. }
  185. bool godot_icall_Dictionary_RemoveKey(Dictionary *ptr, MonoObject *key) {
  186. return ptr->erase(GDMonoMarshal::mono_object_to_variant(key));
  187. }
  188. bool godot_icall_Dictionary_Remove(Dictionary *ptr, MonoObject *key, MonoObject *value) {
  189. Variant varKey = GDMonoMarshal::mono_object_to_variant(key);
  190. // no dupes
  191. Variant *ret = ptr->getptr(varKey);
  192. if (ret != NULL && *ret == GDMonoMarshal::mono_object_to_variant(value)) {
  193. ptr->erase(varKey);
  194. return true;
  195. }
  196. return false;
  197. }
  198. bool godot_icall_Dictionary_TryGetValue(Dictionary *ptr, MonoObject *key, MonoObject **value) {
  199. Variant *ret = ptr->getptr(GDMonoMarshal::mono_object_to_variant(key));
  200. if (ret == NULL) {
  201. *value = NULL;
  202. return false;
  203. }
  204. *value = GDMonoMarshal::variant_to_mono_object(ret);
  205. return true;
  206. }
  207. bool godot_icall_Dictionary_TryGetValue_Generic(Dictionary *ptr, MonoObject *key, MonoObject **value, uint32_t type_encoding, GDMonoClass *type_class) {
  208. Variant *ret = ptr->getptr(GDMonoMarshal::mono_object_to_variant(key));
  209. if (ret == NULL) {
  210. *value = NULL;
  211. return false;
  212. }
  213. *value = GDMonoMarshal::variant_to_mono_object(ret, ManagedType(type_encoding, type_class));
  214. return true;
  215. }
  216. void godot_icall_Dictionary_Generic_GetValueTypeInfo(MonoReflectionType *refltype, uint32_t *type_encoding, GDMonoClass **type_class) {
  217. MonoType *value_type = mono_reflection_type_get_type(refltype);
  218. *type_encoding = mono_type_get_type(value_type);
  219. MonoClass *type_class_raw = mono_class_from_mono_type(value_type);
  220. *type_class = GDMono::get_singleton()->get_class(type_class_raw);
  221. }
  222. void godot_register_collections_icalls() {
  223. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_Ctor", (void *)godot_icall_Array_Ctor);
  224. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_Dtor", (void *)godot_icall_Array_Dtor);
  225. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_At", (void *)godot_icall_Array_At);
  226. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_At_Generic", (void *)godot_icall_Array_At_Generic);
  227. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_SetAt", (void *)godot_icall_Array_SetAt);
  228. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_Count", (void *)godot_icall_Array_Count);
  229. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_Add", (void *)godot_icall_Array_Add);
  230. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_Clear", (void *)godot_icall_Array_Clear);
  231. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_Contains", (void *)godot_icall_Array_Contains);
  232. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_CopyTo", (void *)godot_icall_Array_CopyTo);
  233. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_IndexOf", (void *)godot_icall_Array_IndexOf);
  234. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_Insert", (void *)godot_icall_Array_Insert);
  235. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_Remove", (void *)godot_icall_Array_Remove);
  236. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_RemoveAt", (void *)godot_icall_Array_RemoveAt);
  237. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_Resize", (void *)godot_icall_Array_Resize);
  238. mono_add_internal_call("Godot.Collections.Array::godot_icall_Array_Generic_GetElementTypeInfo", (void *)godot_icall_Array_Generic_GetElementTypeInfo);
  239. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_Ctor", (void *)godot_icall_Dictionary_Ctor);
  240. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_Dtor", (void *)godot_icall_Dictionary_Dtor);
  241. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_GetValue", (void *)godot_icall_Dictionary_GetValue);
  242. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_GetValue_Generic", (void *)godot_icall_Dictionary_GetValue_Generic);
  243. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_SetValue", (void *)godot_icall_Dictionary_SetValue);
  244. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_Keys", (void *)godot_icall_Dictionary_Keys);
  245. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_Values", (void *)godot_icall_Dictionary_Values);
  246. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_Count", (void *)godot_icall_Dictionary_Count);
  247. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_Add", (void *)godot_icall_Dictionary_Add);
  248. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_Clear", (void *)godot_icall_Dictionary_Clear);
  249. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_Contains", (void *)godot_icall_Dictionary_Contains);
  250. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_ContainsKey", (void *)godot_icall_Dictionary_ContainsKey);
  251. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_RemoveKey", (void *)godot_icall_Dictionary_RemoveKey);
  252. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_Remove", (void *)godot_icall_Dictionary_Remove);
  253. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_TryGetValue", (void *)godot_icall_Dictionary_TryGetValue);
  254. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_TryGetValue_Generic", (void *)godot_icall_Dictionary_TryGetValue_Generic);
  255. mono_add_internal_call("Godot.Collections.Dictionary::godot_icall_Dictionary_Generic_GetValueTypeInfo", (void *)godot_icall_Dictionary_Generic_GetValueTypeInfo);
  256. }
  257. #endif // MONO_GLUE_ENABLED