gdscript_vm.cpp 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918
  1. /*************************************************************************/
  2. /* gdscript_vm.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "gdscript_function.h"
  31. #include "core/core_string_names.h"
  32. #include "core/os/os.h"
  33. #include "gdscript.h"
  34. Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_instance, GDScript *p_script, Variant &self, Variant &static_ref, Variant *p_stack, String &r_error) const {
  35. int address = p_address & ADDR_MASK;
  36. //sequential table (jump table generated by compiler)
  37. switch ((p_address & ADDR_TYPE_MASK) >> ADDR_BITS) {
  38. case ADDR_TYPE_SELF: {
  39. #ifdef DEBUG_ENABLED
  40. if (unlikely(!p_instance)) {
  41. r_error = "Cannot access self without instance.";
  42. return nullptr;
  43. }
  44. #endif
  45. return &self;
  46. } break;
  47. case ADDR_TYPE_CLASS: {
  48. return &static_ref;
  49. } break;
  50. case ADDR_TYPE_MEMBER: {
  51. #ifdef DEBUG_ENABLED
  52. if (unlikely(!p_instance)) {
  53. r_error = "Cannot access member without instance.";
  54. return nullptr;
  55. }
  56. #endif
  57. //member indexing is O(1)
  58. return &p_instance->members.write[address];
  59. } break;
  60. case ADDR_TYPE_CLASS_CONSTANT: {
  61. //todo change to index!
  62. GDScript *s = p_script;
  63. #ifdef DEBUG_ENABLED
  64. ERR_FAIL_INDEX_V(address, _global_names_count, nullptr);
  65. #endif
  66. const StringName *sn = &_global_names_ptr[address];
  67. while (s) {
  68. GDScript *o = s;
  69. while (o) {
  70. Map<StringName, Variant>::Element *E = o->constants.find(*sn);
  71. if (E) {
  72. return &E->get();
  73. }
  74. o = o->_owner;
  75. }
  76. s = s->_base;
  77. }
  78. ERR_FAIL_V_MSG(nullptr, "GDScriptCompiler bug.");
  79. } break;
  80. case ADDR_TYPE_LOCAL_CONSTANT: {
  81. #ifdef DEBUG_ENABLED
  82. ERR_FAIL_INDEX_V(address, _constant_count, nullptr);
  83. #endif
  84. return &_constants_ptr[address];
  85. } break;
  86. case ADDR_TYPE_STACK:
  87. case ADDR_TYPE_STACK_VARIABLE: {
  88. #ifdef DEBUG_ENABLED
  89. ERR_FAIL_INDEX_V(address, _stack_size, nullptr);
  90. #endif
  91. return &p_stack[address];
  92. } break;
  93. case ADDR_TYPE_GLOBAL: {
  94. #ifdef DEBUG_ENABLED
  95. ERR_FAIL_INDEX_V(address, GDScriptLanguage::get_singleton()->get_global_array_size(), nullptr);
  96. #endif
  97. return &GDScriptLanguage::get_singleton()->get_global_array()[address];
  98. } break;
  99. #ifdef TOOLS_ENABLED
  100. case ADDR_TYPE_NAMED_GLOBAL: {
  101. #ifdef DEBUG_ENABLED
  102. ERR_FAIL_INDEX_V(address, _global_names_count, nullptr);
  103. #endif
  104. StringName id = _global_names_ptr[address];
  105. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(id)) {
  106. return (Variant *)&GDScriptLanguage::get_singleton()->get_named_globals_map()[id];
  107. } else {
  108. r_error = "Autoload singleton '" + String(id) + "' has been removed.";
  109. return nullptr;
  110. }
  111. } break;
  112. #endif
  113. case ADDR_TYPE_NIL: {
  114. return &nil;
  115. } break;
  116. }
  117. ERR_FAIL_V_MSG(nullptr, "Bad code! (unknown addressing mode).");
  118. return nullptr;
  119. }
  120. #ifdef DEBUG_ENABLED
  121. static String _get_var_type(const Variant *p_var) {
  122. String basestr;
  123. if (p_var->get_type() == Variant::OBJECT) {
  124. bool was_freed;
  125. Object *bobj = p_var->get_validated_object_with_check(was_freed);
  126. if (!bobj) {
  127. if (was_freed) {
  128. basestr = "null instance";
  129. } else {
  130. basestr = "previously freed";
  131. }
  132. } else {
  133. if (bobj->get_script_instance()) {
  134. basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  135. } else {
  136. basestr = bobj->get_class();
  137. }
  138. }
  139. } else {
  140. basestr = Variant::get_type_name(p_var->get_type());
  141. }
  142. return basestr;
  143. }
  144. #endif // DEBUG_ENABLED
  145. String GDScriptFunction::_get_call_error(const Callable::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  146. String err_text;
  147. if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  148. int errorarg = p_err.argument;
  149. // Handle the Object to Object case separately as we don't have further class details.
  150. #ifdef DEBUG_ENABLED
  151. if (p_err.expected == Variant::OBJECT && argptrs[errorarg]->get_type() == p_err.expected) {
  152. err_text = "Invalid type in " + p_where + ". The Object-derived class of argument " + itos(errorarg + 1) + " (" + _get_var_type(argptrs[errorarg]) + ") is not a subclass of the expected argument class.";
  153. } else
  154. #endif // DEBUG_ENABLED
  155. {
  156. err_text = "Invalid type in " + p_where + ". Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(p_err.expected)) + ".";
  157. }
  158. } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  159. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  160. } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  161. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  162. } else if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  163. err_text = "Invalid call. Nonexistent " + p_where + ".";
  164. } else if (p_err.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  165. err_text = "Attempt to call " + p_where + " on a null instance.";
  166. } else {
  167. err_text = "Bug, call error: #" + itos(p_err.error);
  168. }
  169. return err_text;
  170. }
  171. #if defined(__GNUC__)
  172. #define OPCODES_TABLE \
  173. static const void *switch_table_ops[] = { \
  174. &&OPCODE_OPERATOR, \
  175. &&OPCODE_OPERATOR_VALIDATED, \
  176. &&OPCODE_EXTENDS_TEST, \
  177. &&OPCODE_IS_BUILTIN, \
  178. &&OPCODE_SET_KEYED, \
  179. &&OPCODE_SET_KEYED_VALIDATED, \
  180. &&OPCODE_SET_INDEXED_VALIDATED, \
  181. &&OPCODE_GET_KEYED, \
  182. &&OPCODE_GET_KEYED_VALIDATED, \
  183. &&OPCODE_GET_INDEXED_VALIDATED, \
  184. &&OPCODE_SET_NAMED, \
  185. &&OPCODE_SET_NAMED_VALIDATED, \
  186. &&OPCODE_GET_NAMED, \
  187. &&OPCODE_GET_NAMED_VALIDATED, \
  188. &&OPCODE_SET_MEMBER, \
  189. &&OPCODE_GET_MEMBER, \
  190. &&OPCODE_ASSIGN, \
  191. &&OPCODE_ASSIGN_TRUE, \
  192. &&OPCODE_ASSIGN_FALSE, \
  193. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  194. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  195. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  196. &&OPCODE_CAST_TO_BUILTIN, \
  197. &&OPCODE_CAST_TO_NATIVE, \
  198. &&OPCODE_CAST_TO_SCRIPT, \
  199. &&OPCODE_CONSTRUCT, \
  200. &&OPCODE_CONSTRUCT_VALIDATED, \
  201. &&OPCODE_CONSTRUCT_ARRAY, \
  202. &&OPCODE_CONSTRUCT_DICTIONARY, \
  203. &&OPCODE_CALL, \
  204. &&OPCODE_CALL_RETURN, \
  205. &&OPCODE_CALL_ASYNC, \
  206. &&OPCODE_CALL_UTILITY, \
  207. &&OPCODE_CALL_UTILITY_VALIDATED, \
  208. &&OPCODE_CALL_GDSCRIPT_UTILITY, \
  209. &&OPCODE_CALL_BUILTIN_TYPE_VALIDATED, \
  210. &&OPCODE_CALL_SELF_BASE, \
  211. &&OPCODE_CALL_METHOD_BIND, \
  212. &&OPCODE_CALL_METHOD_BIND_RET, \
  213. &&OPCODE_CALL_PTRCALL_NO_RETURN, \
  214. &&OPCODE_CALL_PTRCALL_BOOL, \
  215. &&OPCODE_CALL_PTRCALL_INT, \
  216. &&OPCODE_CALL_PTRCALL_FLOAT, \
  217. &&OPCODE_CALL_PTRCALL_STRING, \
  218. &&OPCODE_CALL_PTRCALL_VECTOR2, \
  219. &&OPCODE_CALL_PTRCALL_VECTOR2I, \
  220. &&OPCODE_CALL_PTRCALL_RECT2, \
  221. &&OPCODE_CALL_PTRCALL_RECT2I, \
  222. &&OPCODE_CALL_PTRCALL_VECTOR3, \
  223. &&OPCODE_CALL_PTRCALL_VECTOR3I, \
  224. &&OPCODE_CALL_PTRCALL_TRANSFORM2D, \
  225. &&OPCODE_CALL_PTRCALL_PLANE, \
  226. &&OPCODE_CALL_PTRCALL_QUAT, \
  227. &&OPCODE_CALL_PTRCALL_AABB, \
  228. &&OPCODE_CALL_PTRCALL_BASIS, \
  229. &&OPCODE_CALL_PTRCALL_TRANSFORM, \
  230. &&OPCODE_CALL_PTRCALL_COLOR, \
  231. &&OPCODE_CALL_PTRCALL_STRING_NAME, \
  232. &&OPCODE_CALL_PTRCALL_NODE_PATH, \
  233. &&OPCODE_CALL_PTRCALL_RID, \
  234. &&OPCODE_CALL_PTRCALL_OBJECT, \
  235. &&OPCODE_CALL_PTRCALL_CALLABLE, \
  236. &&OPCODE_CALL_PTRCALL_SIGNAL, \
  237. &&OPCODE_CALL_PTRCALL_DICTIONARY, \
  238. &&OPCODE_CALL_PTRCALL_ARRAY, \
  239. &&OPCODE_CALL_PTRCALL_PACKED_BYTE_ARRAY, \
  240. &&OPCODE_CALL_PTRCALL_PACKED_INT32_ARRAY, \
  241. &&OPCODE_CALL_PTRCALL_PACKED_INT64_ARRAY, \
  242. &&OPCODE_CALL_PTRCALL_PACKED_FLOAT32_ARRAY, \
  243. &&OPCODE_CALL_PTRCALL_PACKED_FLOAT64_ARRAY, \
  244. &&OPCODE_CALL_PTRCALL_PACKED_STRING_ARRAY, \
  245. &&OPCODE_CALL_PTRCALL_PACKED_VECTOR2_ARRAY, \
  246. &&OPCODE_CALL_PTRCALL_PACKED_VECTOR3_ARRAY, \
  247. &&OPCODE_CALL_PTRCALL_PACKED_COLOR_ARRAY, \
  248. &&OPCODE_AWAIT, \
  249. &&OPCODE_AWAIT_RESUME, \
  250. &&OPCODE_JUMP, \
  251. &&OPCODE_JUMP_IF, \
  252. &&OPCODE_JUMP_IF_NOT, \
  253. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  254. &&OPCODE_RETURN, \
  255. &&OPCODE_ITERATE_BEGIN, \
  256. &&OPCODE_ITERATE_BEGIN_INT, \
  257. &&OPCODE_ITERATE_BEGIN_FLOAT, \
  258. &&OPCODE_ITERATE_BEGIN_VECTOR2, \
  259. &&OPCODE_ITERATE_BEGIN_VECTOR2I, \
  260. &&OPCODE_ITERATE_BEGIN_VECTOR3, \
  261. &&OPCODE_ITERATE_BEGIN_VECTOR3I, \
  262. &&OPCODE_ITERATE_BEGIN_STRING, \
  263. &&OPCODE_ITERATE_BEGIN_DICTIONARY, \
  264. &&OPCODE_ITERATE_BEGIN_ARRAY, \
  265. &&OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY, \
  266. &&OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY, \
  267. &&OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY, \
  268. &&OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY, \
  269. &&OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY, \
  270. &&OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY, \
  271. &&OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY, \
  272. &&OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY, \
  273. &&OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY, \
  274. &&OPCODE_ITERATE_BEGIN_OBJECT, \
  275. &&OPCODE_ITERATE, \
  276. &&OPCODE_ITERATE_INT, \
  277. &&OPCODE_ITERATE_FLOAT, \
  278. &&OPCODE_ITERATE_VECTOR2, \
  279. &&OPCODE_ITERATE_VECTOR2I, \
  280. &&OPCODE_ITERATE_VECTOR3, \
  281. &&OPCODE_ITERATE_VECTOR3I, \
  282. &&OPCODE_ITERATE_STRING, \
  283. &&OPCODE_ITERATE_DICTIONARY, \
  284. &&OPCODE_ITERATE_ARRAY, \
  285. &&OPCODE_ITERATE_PACKED_BYTE_ARRAY, \
  286. &&OPCODE_ITERATE_PACKED_INT32_ARRAY, \
  287. &&OPCODE_ITERATE_PACKED_INT64_ARRAY, \
  288. &&OPCODE_ITERATE_PACKED_FLOAT32_ARRAY, \
  289. &&OPCODE_ITERATE_PACKED_FLOAT64_ARRAY, \
  290. &&OPCODE_ITERATE_PACKED_STRING_ARRAY, \
  291. &&OPCODE_ITERATE_PACKED_VECTOR2_ARRAY, \
  292. &&OPCODE_ITERATE_PACKED_VECTOR3_ARRAY, \
  293. &&OPCODE_ITERATE_PACKED_COLOR_ARRAY, \
  294. &&OPCODE_ITERATE_OBJECT, \
  295. &&OPCODE_ASSERT, \
  296. &&OPCODE_BREAKPOINT, \
  297. &&OPCODE_LINE, \
  298. &&OPCODE_END \
  299. }; \
  300. static_assert((sizeof(switch_table_ops) / sizeof(switch_table_ops[0]) == (OPCODE_END + 1)), "Opcodes in jump table aren't the same as opcodes in enum.");
  301. #define OPCODE(m_op) \
  302. m_op:
  303. #define OPCODE_WHILE(m_test) \
  304. OPSWHILE:
  305. #define OPCODES_END \
  306. OPSEXIT:
  307. #define OPCODES_OUT \
  308. OPSOUT:
  309. #define DISPATCH_OPCODE goto OPSWHILE
  310. #define OPCODE_SWITCH(m_test) goto *switch_table_ops[m_test];
  311. #define OPCODE_BREAK goto OPSEXIT
  312. #define OPCODE_OUT goto OPSOUT
  313. #else
  314. #define OPCODES_TABLE
  315. #define OPCODE(m_op) case m_op:
  316. #define OPCODE_WHILE(m_test) while (m_test)
  317. #define OPCODES_END
  318. #define OPCODES_OUT
  319. #define DISPATCH_OPCODE continue
  320. #define OPCODE_SWITCH(m_test) switch (m_test)
  321. #define OPCODE_BREAK break
  322. #define OPCODE_OUT break
  323. #endif
  324. // Helpers for VariantInternal methods in macros.
  325. #define OP_GET_BOOL get_bool
  326. #define OP_GET_INT get_int
  327. #define OP_GET_FLOAT get_float
  328. #define OP_GET_VECTOR2 get_vector2
  329. #define OP_GET_VECTOR2I get_vector2i
  330. #define OP_GET_VECTOR3 get_vector3
  331. #define OP_GET_VECTOR3I get_vector3i
  332. #define OP_GET_RECT2 get_rect2
  333. #define OP_GET_RECT2I get_rect2i
  334. #define OP_GET_QUAT get_quat
  335. #define OP_GET_COLOR get_color
  336. #define OP_GET_STRING get_string
  337. #define OP_GET_STRING_NAME get_string_name
  338. #define OP_GET_NODE_PATH get_node_path
  339. #define OP_GET_CALLABLE get_callable
  340. #define OP_GET_SIGNAL get_signal
  341. #define OP_GET_ARRAY get_array
  342. #define OP_GET_DICTIONARY get_dictionary
  343. #define OP_GET_PACKED_BYTE_ARRAY get_byte_array
  344. #define OP_GET_PACKED_INT32_ARRAY get_int32_array
  345. #define OP_GET_PACKED_INT64_ARRAY get_int64_array
  346. #define OP_GET_PACKED_FLOAT32_ARRAY get_float32_array
  347. #define OP_GET_PACKED_FLOAT64_ARRAY get_float64_array
  348. #define OP_GET_PACKED_STRING_ARRAY get_string_array
  349. #define OP_GET_PACKED_VECTOR2_ARRAY get_vector2_array
  350. #define OP_GET_PACKED_VECTOR3_ARRAY get_vector3_array
  351. #define OP_GET_PACKED_COLOR_ARRAY get_color_array
  352. #define OP_GET_TRANSFORM get_transform
  353. #define OP_GET_TRANSFORM2D get_transform2d
  354. #define OP_GET_PLANE get_plane
  355. #define OP_GET_AABB get_aabb
  356. #define OP_GET_BASIS get_basis
  357. #define OP_GET_RID get_rid
  358. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state) {
  359. OPCODES_TABLE;
  360. if (!_code_ptr) {
  361. return Variant();
  362. }
  363. r_err.error = Callable::CallError::CALL_OK;
  364. Variant self;
  365. Variant static_ref;
  366. Variant retvalue;
  367. Variant *stack = nullptr;
  368. Variant **instruction_args;
  369. const void **call_args_ptr = nullptr;
  370. int defarg = 0;
  371. #ifdef DEBUG_ENABLED
  372. //GDScriptLanguage::get_singleton()->calls++;
  373. #endif
  374. uint32_t alloca_size = 0;
  375. GDScript *script;
  376. int ip = 0;
  377. int line = _initial_line;
  378. if (p_state) {
  379. //use existing (supplied) state (awaited)
  380. stack = (Variant *)p_state->stack.ptr();
  381. instruction_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  382. line = p_state->line;
  383. ip = p_state->ip;
  384. alloca_size = p_state->stack.size();
  385. script = p_state->script;
  386. p_instance = p_state->instance;
  387. defarg = p_state->defarg;
  388. self = p_state->self;
  389. } else {
  390. if (p_argcount != _argument_count) {
  391. if (p_argcount > _argument_count) {
  392. r_err.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  393. r_err.argument = _argument_count;
  394. return Variant();
  395. } else if (p_argcount < _argument_count - _default_arg_count) {
  396. r_err.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  397. r_err.argument = _argument_count - _default_arg_count;
  398. return Variant();
  399. } else {
  400. defarg = _argument_count - p_argcount;
  401. }
  402. }
  403. alloca_size = sizeof(Variant *) * _instruction_args_size + sizeof(Variant) * _stack_size;
  404. if (alloca_size) {
  405. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  406. if (_stack_size) {
  407. stack = (Variant *)aptr;
  408. for (int i = 0; i < p_argcount; i++) {
  409. if (!argument_types[i].has_type) {
  410. memnew_placement(&stack[i], Variant(*p_args[i]));
  411. continue;
  412. }
  413. if (!argument_types[i].is_type(*p_args[i], true)) {
  414. r_err.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  415. r_err.argument = i;
  416. r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT;
  417. return Variant();
  418. }
  419. if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
  420. Variant arg;
  421. Variant::construct(argument_types[i].builtin_type, arg, &p_args[i], 1, r_err);
  422. memnew_placement(&stack[i], Variant(arg));
  423. } else {
  424. memnew_placement(&stack[i], Variant(*p_args[i]));
  425. }
  426. }
  427. for (int i = p_argcount; i < _stack_size; i++) {
  428. memnew_placement(&stack[i], Variant);
  429. }
  430. } else {
  431. stack = nullptr;
  432. }
  433. if (_instruction_args_size) {
  434. instruction_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  435. } else {
  436. instruction_args = nullptr;
  437. }
  438. } else {
  439. stack = nullptr;
  440. instruction_args = nullptr;
  441. }
  442. if (p_instance) {
  443. self = p_instance->owner;
  444. script = p_instance->script.ptr();
  445. } else {
  446. script = _script;
  447. }
  448. }
  449. if (_ptrcall_args_size) {
  450. call_args_ptr = (const void **)alloca(_ptrcall_args_size * sizeof(void *));
  451. } else {
  452. call_args_ptr = nullptr;
  453. }
  454. static_ref = script;
  455. String err_text;
  456. #ifdef DEBUG_ENABLED
  457. if (EngineDebugger::is_active()) {
  458. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  459. }
  460. #define GD_ERR_BREAK(m_cond) \
  461. { \
  462. if (unlikely(m_cond)) { \
  463. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  464. OPCODE_BREAK; \
  465. } \
  466. }
  467. #define CHECK_SPACE(m_space) \
  468. GD_ERR_BREAK((ip + m_space) > _code_size)
  469. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  470. Variant *m_v; \
  471. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text); \
  472. if (unlikely(!m_v)) \
  473. OPCODE_BREAK;
  474. #else
  475. #define GD_ERR_BREAK(m_cond)
  476. #define CHECK_SPACE(m_space)
  477. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  478. Variant *m_v; \
  479. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, static_ref, stack, err_text);
  480. #endif
  481. #define GET_INSTRUCTION_ARG(m_v, m_idx) \
  482. Variant *m_v = instruction_args[m_idx]
  483. #ifdef DEBUG_ENABLED
  484. uint64_t function_start_time = 0;
  485. uint64_t function_call_time = 0;
  486. if (GDScriptLanguage::get_singleton()->profiling) {
  487. function_start_time = OS::get_singleton()->get_ticks_usec();
  488. function_call_time = 0;
  489. profile.call_count++;
  490. profile.frame_call_count++;
  491. }
  492. bool exit_ok = false;
  493. bool awaited = false;
  494. #endif
  495. #ifdef DEBUG_ENABLED
  496. OPCODE_WHILE(ip < _code_size) {
  497. int last_opcode = _code_ptr[ip];
  498. #else
  499. OPCODE_WHILE(true) {
  500. #endif
  501. // Load arguments for the instruction before each instruction.
  502. int instr_arg_count = ((_code_ptr[ip]) & INSTR_ARGS_MASK) >> INSTR_BITS;
  503. for (int i = 0; i < instr_arg_count; i++) {
  504. GET_VARIANT_PTR(v, i + 1);
  505. instruction_args[i] = v;
  506. }
  507. OPCODE_SWITCH(_code_ptr[ip] & INSTR_MASK) {
  508. OPCODE(OPCODE_OPERATOR) {
  509. CHECK_SPACE(5);
  510. bool valid;
  511. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 4];
  512. GD_ERR_BREAK(op >= Variant::OP_MAX);
  513. GET_INSTRUCTION_ARG(a, 0);
  514. GET_INSTRUCTION_ARG(b, 1);
  515. GET_INSTRUCTION_ARG(dst, 2);
  516. #ifdef DEBUG_ENABLED
  517. Variant ret;
  518. Variant::evaluate(op, *a, *b, ret, valid);
  519. #else
  520. Variant::evaluate(op, *a, *b, *dst, valid);
  521. #endif
  522. #ifdef DEBUG_ENABLED
  523. if (!valid) {
  524. if (ret.get_type() == Variant::STRING) {
  525. //return a string when invalid with the error
  526. err_text = ret;
  527. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  528. } else {
  529. err_text = "Invalid operands '" + Variant::get_type_name(a->get_type()) + "' and '" + Variant::get_type_name(b->get_type()) + "' in operator '" + Variant::get_operator_name(op) + "'.";
  530. }
  531. OPCODE_BREAK;
  532. }
  533. *dst = ret;
  534. #endif
  535. ip += 5;
  536. }
  537. DISPATCH_OPCODE;
  538. OPCODE(OPCODE_OPERATOR_VALIDATED) {
  539. CHECK_SPACE(5);
  540. int operator_idx = _code_ptr[ip + 4];
  541. GD_ERR_BREAK(operator_idx < 0 || operator_idx >= _operator_funcs_count);
  542. Variant::ValidatedOperatorEvaluator operator_func = _operator_funcs_ptr[operator_idx];
  543. GET_INSTRUCTION_ARG(a, 0);
  544. GET_INSTRUCTION_ARG(b, 1);
  545. GET_INSTRUCTION_ARG(dst, 2);
  546. operator_func(a, b, dst);
  547. ip += 5;
  548. }
  549. DISPATCH_OPCODE;
  550. OPCODE(OPCODE_EXTENDS_TEST) {
  551. CHECK_SPACE(4);
  552. GET_INSTRUCTION_ARG(a, 0);
  553. GET_INSTRUCTION_ARG(b, 1);
  554. GET_INSTRUCTION_ARG(dst, 2);
  555. #ifdef DEBUG_ENABLED
  556. if (b->get_type() != Variant::OBJECT || b->operator Object *() == nullptr) {
  557. err_text = "Right operand of 'is' is not a class.";
  558. OPCODE_BREAK;
  559. }
  560. #endif
  561. bool extends_ok = false;
  562. if (a->get_type() == Variant::OBJECT && a->operator Object *() != nullptr) {
  563. #ifdef DEBUG_ENABLED
  564. bool was_freed;
  565. Object *obj_A = a->get_validated_object_with_check(was_freed);
  566. if (was_freed) {
  567. err_text = "Left operand of 'is' is a previously freed instance.";
  568. OPCODE_BREAK;
  569. }
  570. Object *obj_B = b->get_validated_object_with_check(was_freed);
  571. if (was_freed) {
  572. err_text = "Right operand of 'is' is a previously freed instance.";
  573. OPCODE_BREAK;
  574. }
  575. #else
  576. Object *obj_A = *a;
  577. Object *obj_B = *b;
  578. #endif // DEBUG_ENABLED
  579. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  580. if (scr_B) {
  581. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  582. //in other situation, this should return false.
  583. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  584. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  585. //bool found=false;
  586. while (cmp) {
  587. if (cmp == scr_B) {
  588. //inherits from script, all ok
  589. extends_ok = true;
  590. break;
  591. }
  592. cmp = cmp->_base;
  593. }
  594. }
  595. } else {
  596. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  597. #ifdef DEBUG_ENABLED
  598. if (!nc) {
  599. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  600. OPCODE_BREAK;
  601. }
  602. #endif
  603. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  604. }
  605. }
  606. *dst = extends_ok;
  607. ip += 4;
  608. }
  609. DISPATCH_OPCODE;
  610. OPCODE(OPCODE_IS_BUILTIN) {
  611. CHECK_SPACE(4);
  612. GET_INSTRUCTION_ARG(value, 0);
  613. GET_INSTRUCTION_ARG(dst, 1);
  614. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
  615. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  616. *dst = value->get_type() == var_type;
  617. ip += 4;
  618. }
  619. DISPATCH_OPCODE;
  620. OPCODE(OPCODE_SET_KEYED) {
  621. CHECK_SPACE(3);
  622. GET_INSTRUCTION_ARG(dst, 0);
  623. GET_INSTRUCTION_ARG(index, 1);
  624. GET_INSTRUCTION_ARG(value, 2);
  625. bool valid;
  626. dst->set(*index, *value, &valid);
  627. #ifdef DEBUG_ENABLED
  628. if (!valid) {
  629. String v = index->operator String();
  630. if (v != "") {
  631. v = "'" + v + "'";
  632. } else {
  633. v = "of type '" + _get_var_type(index) + "'";
  634. }
  635. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  636. OPCODE_BREAK;
  637. }
  638. #endif
  639. ip += 4;
  640. }
  641. DISPATCH_OPCODE;
  642. OPCODE(OPCODE_SET_KEYED_VALIDATED) {
  643. CHECK_SPACE(4);
  644. GET_INSTRUCTION_ARG(dst, 0);
  645. GET_INSTRUCTION_ARG(index, 1);
  646. GET_INSTRUCTION_ARG(value, 2);
  647. int index_setter = _code_ptr[ip + 4];
  648. GD_ERR_BREAK(index_setter < 0 || index_setter >= _keyed_setters_count);
  649. const Variant::ValidatedKeyedSetter setter = _keyed_setters_ptr[index_setter];
  650. bool valid;
  651. setter(dst, index, value, &valid);
  652. #ifdef DEBUG_ENABLED
  653. if (!valid) {
  654. String v = index->operator String();
  655. if (v != "") {
  656. v = "'" + v + "'";
  657. } else {
  658. v = "of type '" + _get_var_type(index) + "'";
  659. }
  660. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  661. OPCODE_BREAK;
  662. }
  663. #endif
  664. ip += 5;
  665. }
  666. DISPATCH_OPCODE;
  667. OPCODE(OPCODE_SET_INDEXED_VALIDATED) {
  668. CHECK_SPACE(4);
  669. GET_INSTRUCTION_ARG(dst, 0);
  670. GET_INSTRUCTION_ARG(index, 1);
  671. GET_INSTRUCTION_ARG(value, 2);
  672. int index_setter = _code_ptr[ip + 4];
  673. GD_ERR_BREAK(index_setter < 0 || index_setter >= _indexed_setters_count);
  674. const Variant::ValidatedIndexedSetter setter = _indexed_setters_ptr[index_setter];
  675. int64_t int_index = *VariantInternal::get_int(index);
  676. bool oob;
  677. setter(dst, int_index, value, &oob);
  678. #ifdef DEBUG_ENABLED
  679. if (oob) {
  680. String v = index->operator String();
  681. if (v != "") {
  682. v = "'" + v + "'";
  683. } else {
  684. v = "of type '" + _get_var_type(index) + "'";
  685. }
  686. err_text = "Out of bounds set index " + v + " (on base: '" + _get_var_type(dst) + "')";
  687. OPCODE_BREAK;
  688. }
  689. #endif
  690. ip += 5;
  691. }
  692. DISPATCH_OPCODE;
  693. OPCODE(OPCODE_GET_KEYED) {
  694. CHECK_SPACE(3);
  695. GET_INSTRUCTION_ARG(src, 0);
  696. GET_INSTRUCTION_ARG(index, 1);
  697. GET_INSTRUCTION_ARG(dst, 2);
  698. bool valid;
  699. #ifdef DEBUG_ENABLED
  700. // Allow better error message in cases where src and dst are the same stack position.
  701. Variant ret = src->get(*index, &valid);
  702. #else
  703. *dst = src->get(*index, &valid);
  704. #endif
  705. #ifdef DEBUG_ENABLED
  706. if (!valid) {
  707. String v = index->operator String();
  708. if (v != "") {
  709. v = "'" + v + "'";
  710. } else {
  711. v = "of type '" + _get_var_type(index) + "'";
  712. }
  713. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  714. OPCODE_BREAK;
  715. }
  716. *dst = ret;
  717. #endif
  718. ip += 4;
  719. }
  720. DISPATCH_OPCODE;
  721. OPCODE(OPCODE_GET_KEYED_VALIDATED) {
  722. CHECK_SPACE(4);
  723. GET_INSTRUCTION_ARG(src, 0);
  724. GET_INSTRUCTION_ARG(key, 1);
  725. GET_INSTRUCTION_ARG(dst, 2);
  726. int index_getter = _code_ptr[ip + 4];
  727. GD_ERR_BREAK(index_getter < 0 || index_getter >= _keyed_getters_count);
  728. const Variant::ValidatedKeyedGetter getter = _keyed_getters_ptr[index_getter];
  729. bool valid;
  730. #ifdef DEBUG_ENABLED
  731. // Allow better error message in cases where src and dst are the same stack position.
  732. Variant ret;
  733. getter(src, key, &ret, &valid);
  734. #else
  735. getter(src, key, dst, &valid);
  736. #endif
  737. #ifdef DEBUG_ENABLED
  738. if (!valid) {
  739. String v = key->operator String();
  740. if (v != "") {
  741. v = "'" + v + "'";
  742. } else {
  743. v = "of type '" + _get_var_type(key) + "'";
  744. }
  745. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  746. OPCODE_BREAK;
  747. }
  748. *dst = ret;
  749. #endif
  750. ip += 5;
  751. }
  752. DISPATCH_OPCODE;
  753. OPCODE(OPCODE_GET_INDEXED_VALIDATED) {
  754. CHECK_SPACE(4);
  755. GET_INSTRUCTION_ARG(src, 0);
  756. GET_INSTRUCTION_ARG(index, 1);
  757. GET_INSTRUCTION_ARG(dst, 2);
  758. int index_getter = _code_ptr[ip + 4];
  759. GD_ERR_BREAK(index_getter < 0 || index_getter >= _indexed_getters_count);
  760. const Variant::ValidatedIndexedGetter getter = _indexed_getters_ptr[index_getter];
  761. int64_t int_index = *VariantInternal::get_int(index);
  762. bool oob;
  763. getter(src, int_index, dst, &oob);
  764. #ifdef DEBUG_ENABLED
  765. if (oob) {
  766. String v = index->operator String();
  767. if (v != "") {
  768. v = "'" + v + "'";
  769. } else {
  770. v = "of type '" + _get_var_type(index) + "'";
  771. }
  772. err_text = "Out of bounds get index " + v + " (on base: '" + _get_var_type(src) + "')";
  773. OPCODE_BREAK;
  774. }
  775. #endif
  776. ip += 5;
  777. }
  778. DISPATCH_OPCODE;
  779. OPCODE(OPCODE_SET_NAMED) {
  780. CHECK_SPACE(3);
  781. GET_INSTRUCTION_ARG(dst, 0);
  782. GET_INSTRUCTION_ARG(value, 1);
  783. int indexname = _code_ptr[ip + 3];
  784. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  785. const StringName *index = &_global_names_ptr[indexname];
  786. bool valid;
  787. dst->set_named(*index, *value, valid);
  788. #ifdef DEBUG_ENABLED
  789. if (!valid) {
  790. String err_type;
  791. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  792. OPCODE_BREAK;
  793. }
  794. #endif
  795. ip += 4;
  796. }
  797. DISPATCH_OPCODE;
  798. OPCODE(OPCODE_SET_NAMED_VALIDATED) {
  799. CHECK_SPACE(3);
  800. GET_INSTRUCTION_ARG(dst, 0);
  801. GET_INSTRUCTION_ARG(value, 1);
  802. int index_setter = _code_ptr[ip + 3];
  803. GD_ERR_BREAK(index_setter < 0 || index_setter >= _setters_count);
  804. const Variant::ValidatedSetter setter = _setters_ptr[index_setter];
  805. setter(dst, value);
  806. ip += 4;
  807. }
  808. DISPATCH_OPCODE;
  809. OPCODE(OPCODE_GET_NAMED) {
  810. CHECK_SPACE(4);
  811. GET_INSTRUCTION_ARG(src, 0);
  812. GET_INSTRUCTION_ARG(dst, 1);
  813. int indexname = _code_ptr[ip + 3];
  814. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  815. const StringName *index = &_global_names_ptr[indexname];
  816. bool valid;
  817. #ifdef DEBUG_ENABLED
  818. //allow better error message in cases where src and dst are the same stack position
  819. Variant ret = src->get_named(*index, valid);
  820. #else
  821. *dst = src->get_named(*index, valid);
  822. #endif
  823. #ifdef DEBUG_ENABLED
  824. if (!valid) {
  825. if (src->has_method(*index)) {
  826. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "'). Did you mean '." + index->operator String() + "()' or funcref(obj, \"" + index->operator String() + "\") ?";
  827. } else {
  828. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  829. }
  830. OPCODE_BREAK;
  831. }
  832. *dst = ret;
  833. #endif
  834. ip += 4;
  835. }
  836. DISPATCH_OPCODE;
  837. OPCODE(OPCODE_GET_NAMED_VALIDATED) {
  838. CHECK_SPACE(3);
  839. GET_INSTRUCTION_ARG(src, 0);
  840. GET_INSTRUCTION_ARG(dst, 1);
  841. int index_getter = _code_ptr[ip + 3];
  842. GD_ERR_BREAK(index_getter < 0 || index_getter >= _getters_count);
  843. const Variant::ValidatedGetter getter = _getters_ptr[index_getter];
  844. getter(src, dst);
  845. ip += 4;
  846. }
  847. DISPATCH_OPCODE;
  848. OPCODE(OPCODE_SET_MEMBER) {
  849. CHECK_SPACE(3);
  850. GET_INSTRUCTION_ARG(src, 0);
  851. int indexname = _code_ptr[ip + 2];
  852. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  853. const StringName *index = &_global_names_ptr[indexname];
  854. bool valid;
  855. #ifndef DEBUG_ENABLED
  856. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  857. #else
  858. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  859. if (!ok) {
  860. err_text = "Internal error setting property: " + String(*index);
  861. OPCODE_BREAK;
  862. } else if (!valid) {
  863. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  864. OPCODE_BREAK;
  865. }
  866. #endif
  867. ip += 3;
  868. }
  869. DISPATCH_OPCODE;
  870. OPCODE(OPCODE_GET_MEMBER) {
  871. CHECK_SPACE(3);
  872. GET_INSTRUCTION_ARG(dst, 0);
  873. int indexname = _code_ptr[ip + 2];
  874. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  875. const StringName *index = &_global_names_ptr[indexname];
  876. #ifndef DEBUG_ENABLED
  877. ClassDB::get_property(p_instance->owner, *index, *dst);
  878. #else
  879. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  880. if (!ok) {
  881. err_text = "Internal error getting property: " + String(*index);
  882. OPCODE_BREAK;
  883. }
  884. #endif
  885. ip += 3;
  886. }
  887. DISPATCH_OPCODE;
  888. OPCODE(OPCODE_ASSIGN) {
  889. CHECK_SPACE(3);
  890. GET_INSTRUCTION_ARG(dst, 0);
  891. GET_INSTRUCTION_ARG(src, 1);
  892. *dst = *src;
  893. ip += 3;
  894. }
  895. DISPATCH_OPCODE;
  896. OPCODE(OPCODE_ASSIGN_TRUE) {
  897. CHECK_SPACE(2);
  898. GET_INSTRUCTION_ARG(dst, 0);
  899. *dst = true;
  900. ip += 2;
  901. }
  902. DISPATCH_OPCODE;
  903. OPCODE(OPCODE_ASSIGN_FALSE) {
  904. CHECK_SPACE(2);
  905. GET_INSTRUCTION_ARG(dst, 0);
  906. *dst = false;
  907. ip += 2;
  908. }
  909. DISPATCH_OPCODE;
  910. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  911. CHECK_SPACE(4);
  912. GET_INSTRUCTION_ARG(dst, 0);
  913. GET_INSTRUCTION_ARG(src, 1);
  914. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
  915. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  916. if (src->get_type() != var_type) {
  917. #ifdef DEBUG_ENABLED
  918. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  919. #endif // DEBUG_ENABLED
  920. Callable::CallError ce;
  921. Variant::construct(var_type, *dst, const_cast<const Variant **>(&src), 1, ce);
  922. } else {
  923. #ifdef DEBUG_ENABLED
  924. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  925. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  926. OPCODE_BREAK;
  927. }
  928. } else {
  929. #endif // DEBUG_ENABLED
  930. *dst = *src;
  931. }
  932. ip += 4;
  933. }
  934. DISPATCH_OPCODE;
  935. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  936. CHECK_SPACE(4);
  937. GET_INSTRUCTION_ARG(dst, 0);
  938. GET_INSTRUCTION_ARG(src, 1);
  939. #ifdef DEBUG_ENABLED
  940. GET_INSTRUCTION_ARG(type, 2);
  941. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  942. GD_ERR_BREAK(!nc);
  943. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  944. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  945. "' to a variable of type '" + nc->get_name() + "'.";
  946. OPCODE_BREAK;
  947. }
  948. Object *src_obj = src->operator Object *();
  949. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  950. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  951. "' to a variable of type '" + nc->get_name() + "'.";
  952. OPCODE_BREAK;
  953. }
  954. #endif // DEBUG_ENABLED
  955. *dst = *src;
  956. ip += 4;
  957. }
  958. DISPATCH_OPCODE;
  959. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  960. CHECK_SPACE(4);
  961. GET_INSTRUCTION_ARG(dst, 0);
  962. GET_INSTRUCTION_ARG(src, 1);
  963. #ifdef DEBUG_ENABLED
  964. GET_INSTRUCTION_ARG(type, 2);
  965. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  966. GD_ERR_BREAK(!base_type);
  967. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  968. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  969. OPCODE_BREAK;
  970. }
  971. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  972. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  973. if (!scr_inst) {
  974. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  975. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  976. OPCODE_BREAK;
  977. }
  978. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  979. bool valid = false;
  980. while (src_type) {
  981. if (src_type == base_type) {
  982. valid = true;
  983. break;
  984. }
  985. src_type = src_type->get_base_script().ptr();
  986. }
  987. if (!valid) {
  988. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  989. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  990. OPCODE_BREAK;
  991. }
  992. }
  993. #endif // DEBUG_ENABLED
  994. *dst = *src;
  995. ip += 4;
  996. }
  997. DISPATCH_OPCODE;
  998. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  999. CHECK_SPACE(4);
  1000. GET_INSTRUCTION_ARG(src, 0);
  1001. GET_INSTRUCTION_ARG(dst, 1);
  1002. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 3];
  1003. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  1004. Callable::CallError err;
  1005. Variant::construct(to_type, *dst, (const Variant **)&src, 1, err);
  1006. #ifdef DEBUG_ENABLED
  1007. if (err.error != Callable::CallError::CALL_OK) {
  1008. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  1009. OPCODE_BREAK;
  1010. }
  1011. #endif
  1012. ip += 4;
  1013. }
  1014. DISPATCH_OPCODE;
  1015. OPCODE(OPCODE_CAST_TO_NATIVE) {
  1016. CHECK_SPACE(4);
  1017. GET_INSTRUCTION_ARG(src, 0);
  1018. GET_INSTRUCTION_ARG(dst, 1);
  1019. GET_INSTRUCTION_ARG(to_type, 2);
  1020. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  1021. GD_ERR_BREAK(!nc);
  1022. #ifdef DEBUG_ENABLED
  1023. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1024. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  1025. OPCODE_BREAK;
  1026. }
  1027. #endif
  1028. Object *src_obj = src->operator Object *();
  1029. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  1030. *dst = Variant(); // invalid cast, assign NULL
  1031. } else {
  1032. *dst = *src;
  1033. }
  1034. ip += 4;
  1035. }
  1036. DISPATCH_OPCODE;
  1037. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  1038. CHECK_SPACE(4);
  1039. GET_INSTRUCTION_ARG(src, 0);
  1040. GET_INSTRUCTION_ARG(dst, 1);
  1041. GET_INSTRUCTION_ARG(to_type, 2);
  1042. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  1043. GD_ERR_BREAK(!base_type);
  1044. #ifdef DEBUG_ENABLED
  1045. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1046. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  1047. OPCODE_BREAK;
  1048. }
  1049. #endif
  1050. bool valid = false;
  1051. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  1052. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  1053. if (scr_inst) {
  1054. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  1055. while (src_type) {
  1056. if (src_type == base_type) {
  1057. valid = true;
  1058. break;
  1059. }
  1060. src_type = src_type->get_base_script().ptr();
  1061. }
  1062. }
  1063. }
  1064. if (valid) {
  1065. *dst = *src; // Valid cast, copy the source object
  1066. } else {
  1067. *dst = Variant(); // invalid cast, assign NULL
  1068. }
  1069. ip += 4;
  1070. }
  1071. DISPATCH_OPCODE;
  1072. OPCODE(OPCODE_CONSTRUCT) {
  1073. CHECK_SPACE(2 + instr_arg_count);
  1074. ip += instr_arg_count;
  1075. int argc = _code_ptr[ip + 1];
  1076. Variant::Type t = Variant::Type(_code_ptr[ip + 2]);
  1077. Variant **argptrs = instruction_args;
  1078. GET_INSTRUCTION_ARG(dst, argc);
  1079. Callable::CallError err;
  1080. Variant::construct(t, *dst, (const Variant **)argptrs, argc, err);
  1081. #ifdef DEBUG_ENABLED
  1082. if (err.error != Callable::CallError::CALL_OK) {
  1083. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  1084. OPCODE_BREAK;
  1085. }
  1086. #endif
  1087. ip += 3;
  1088. }
  1089. DISPATCH_OPCODE;
  1090. OPCODE(OPCODE_CONSTRUCT_VALIDATED) {
  1091. CHECK_SPACE(2 + instr_arg_count);
  1092. ip += instr_arg_count;
  1093. int argc = _code_ptr[ip + 1];
  1094. int constructor_idx = _code_ptr[ip + 2];
  1095. GD_ERR_BREAK(constructor_idx < 0 || constructor_idx >= _constructors_count);
  1096. Variant::ValidatedConstructor constructor = _constructors_ptr[constructor_idx];
  1097. Variant **argptrs = instruction_args;
  1098. GET_INSTRUCTION_ARG(dst, argc);
  1099. constructor(dst, (const Variant **)argptrs);
  1100. ip += 3;
  1101. }
  1102. DISPATCH_OPCODE;
  1103. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  1104. CHECK_SPACE(1 + instr_arg_count);
  1105. ip += instr_arg_count;
  1106. int argc = _code_ptr[ip + 1];
  1107. Array array;
  1108. array.resize(argc);
  1109. for (int i = 0; i < argc; i++) {
  1110. array[i] = *(instruction_args[i]);
  1111. }
  1112. GET_INSTRUCTION_ARG(dst, argc);
  1113. *dst = array;
  1114. ip += 2;
  1115. }
  1116. DISPATCH_OPCODE;
  1117. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  1118. CHECK_SPACE(2 + instr_arg_count);
  1119. ip += instr_arg_count;
  1120. int argc = _code_ptr[ip + 1];
  1121. Dictionary dict;
  1122. for (int i = 0; i < argc; i++) {
  1123. GET_INSTRUCTION_ARG(k, i * 2 + 0);
  1124. GET_INSTRUCTION_ARG(v, i * 2 + 1);
  1125. dict[*k] = *v;
  1126. }
  1127. GET_INSTRUCTION_ARG(dst, argc * 2);
  1128. *dst = dict;
  1129. ip += 2;
  1130. }
  1131. DISPATCH_OPCODE;
  1132. OPCODE(OPCODE_CALL_ASYNC)
  1133. OPCODE(OPCODE_CALL_RETURN)
  1134. OPCODE(OPCODE_CALL) {
  1135. CHECK_SPACE(3 + instr_arg_count);
  1136. bool call_ret = (_code_ptr[ip] & INSTR_MASK) != OPCODE_CALL;
  1137. #ifdef DEBUG_ENABLED
  1138. bool call_async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC;
  1139. #endif
  1140. ip += instr_arg_count;
  1141. int argc = _code_ptr[ip + 1];
  1142. GD_ERR_BREAK(argc < 0);
  1143. int methodname_idx = _code_ptr[ip + 2];
  1144. GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
  1145. const StringName *methodname = &_global_names_ptr[methodname_idx];
  1146. GET_INSTRUCTION_ARG(base, argc);
  1147. Variant **argptrs = instruction_args;
  1148. #ifdef DEBUG_ENABLED
  1149. uint64_t call_time = 0;
  1150. if (GDScriptLanguage::get_singleton()->profiling) {
  1151. call_time = OS::get_singleton()->get_ticks_usec();
  1152. }
  1153. #endif
  1154. Callable::CallError err;
  1155. if (call_ret) {
  1156. GET_INSTRUCTION_ARG(ret, argc + 1);
  1157. base->call(*methodname, (const Variant **)argptrs, argc, *ret, err);
  1158. #ifdef DEBUG_ENABLED
  1159. if (!call_async && ret->get_type() == Variant::OBJECT) {
  1160. // Check if getting a function state without await.
  1161. bool was_freed = false;
  1162. Object *obj = ret->get_validated_object_with_check(was_freed);
  1163. if (was_freed) {
  1164. err_text = "Got a freed object as a result of the call.";
  1165. OPCODE_BREAK;
  1166. }
  1167. if (obj && obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1168. err_text = R"(Trying to call an async function without "await".)";
  1169. OPCODE_BREAK;
  1170. }
  1171. }
  1172. #endif
  1173. } else {
  1174. Variant ret;
  1175. base->call(*methodname, (const Variant **)argptrs, argc, ret, err);
  1176. }
  1177. #ifdef DEBUG_ENABLED
  1178. if (GDScriptLanguage::get_singleton()->profiling) {
  1179. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1180. }
  1181. if (err.error != Callable::CallError::CALL_OK) {
  1182. String methodstr = *methodname;
  1183. String basestr = _get_var_type(base);
  1184. if (methodstr == "call") {
  1185. if (argc >= 1) {
  1186. methodstr = String(*argptrs[0]) + " (via call)";
  1187. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1188. err.argument += 1;
  1189. }
  1190. }
  1191. } else if (methodstr == "free") {
  1192. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1193. if (base->is_ref()) {
  1194. err_text = "Attempted to free a reference.";
  1195. OPCODE_BREAK;
  1196. } else if (base->get_type() == Variant::OBJECT) {
  1197. err_text = "Attempted to free a locked object (calling or emitting).";
  1198. OPCODE_BREAK;
  1199. }
  1200. }
  1201. } else if (methodstr == "call_recursive" && basestr == "TreeItem") {
  1202. if (argc >= 1) {
  1203. methodstr = String(*argptrs[0]) + " (via TreeItem.call_recursive)";
  1204. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1205. err.argument += 1;
  1206. }
  1207. }
  1208. }
  1209. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  1210. OPCODE_BREAK;
  1211. }
  1212. #endif
  1213. ip += 3;
  1214. }
  1215. DISPATCH_OPCODE;
  1216. OPCODE(OPCODE_CALL_METHOD_BIND)
  1217. OPCODE(OPCODE_CALL_METHOD_BIND_RET) {
  1218. CHECK_SPACE(3 + instr_arg_count);
  1219. bool call_ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_METHOD_BIND_RET;
  1220. ip += instr_arg_count;
  1221. int argc = _code_ptr[ip + 1];
  1222. GD_ERR_BREAK(argc < 0);
  1223. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1224. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1225. GET_INSTRUCTION_ARG(base, argc);
  1226. #ifdef DEBUG_ENABLED
  1227. bool freed = false;
  1228. Object *base_obj = base->get_validated_object_with_check(freed);
  1229. if (freed) {
  1230. err_text = "Trying to call a function on a previously freed instance.";
  1231. OPCODE_BREAK;
  1232. } else if (!base_obj) {
  1233. err_text = "Trying to call a function on a null value.";
  1234. OPCODE_BREAK;
  1235. }
  1236. #else
  1237. Object *base_obj = base->operator Object *();
  1238. #endif
  1239. Variant **argptrs = instruction_args;
  1240. #ifdef DEBUG_ENABLED
  1241. uint64_t call_time = 0;
  1242. if (GDScriptLanguage::get_singleton()->profiling) {
  1243. call_time = OS::get_singleton()->get_ticks_usec();
  1244. }
  1245. #endif
  1246. Callable::CallError err;
  1247. if (call_ret) {
  1248. GET_INSTRUCTION_ARG(ret, argc + 1);
  1249. *ret = method->call(base_obj, (const Variant **)argptrs, argc, err);
  1250. } else {
  1251. method->call(base_obj, (const Variant **)argptrs, argc, err);
  1252. }
  1253. #ifdef DEBUG_ENABLED
  1254. if (GDScriptLanguage::get_singleton()->profiling) {
  1255. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1256. }
  1257. if (err.error != Callable::CallError::CALL_OK) {
  1258. String methodstr = method->get_name();
  1259. String basestr = _get_var_type(base);
  1260. if (methodstr == "call") {
  1261. if (argc >= 1) {
  1262. methodstr = String(*argptrs[0]) + " (via call)";
  1263. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1264. err.argument += 1;
  1265. }
  1266. }
  1267. } else if (methodstr == "free") {
  1268. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1269. if (base->is_ref()) {
  1270. err_text = "Attempted to free a reference.";
  1271. OPCODE_BREAK;
  1272. } else if (base->get_type() == Variant::OBJECT) {
  1273. err_text = "Attempted to free a locked object (calling or emitting).";
  1274. OPCODE_BREAK;
  1275. }
  1276. }
  1277. }
  1278. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  1279. OPCODE_BREAK;
  1280. }
  1281. #endif
  1282. ip += 3;
  1283. }
  1284. DISPATCH_OPCODE;
  1285. #ifdef DEBUG_ENABLED
  1286. #define OPCODE_CALL_PTR(m_type) \
  1287. OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
  1288. CHECK_SPACE(3 + instr_arg_count); \
  1289. ip += instr_arg_count; \
  1290. int argc = _code_ptr[ip + 1]; \
  1291. GD_ERR_BREAK(argc < 0); \
  1292. GET_INSTRUCTION_ARG(base, argc); \
  1293. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count); \
  1294. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
  1295. bool freed = false; \
  1296. Object *base_obj = base->get_validated_object_with_check(freed); \
  1297. if (freed) { \
  1298. err_text = "Trying to call a function on a previously freed instance."; \
  1299. OPCODE_BREAK; \
  1300. } else if (!base_obj) { \
  1301. err_text = "Trying to call a function on a null value."; \
  1302. OPCODE_BREAK; \
  1303. } \
  1304. const void **argptrs = call_args_ptr; \
  1305. for (int i = 0; i < argc; i++) { \
  1306. GET_INSTRUCTION_ARG(v, i); \
  1307. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
  1308. } \
  1309. uint64_t call_time = 0; \
  1310. if (GDScriptLanguage::get_singleton()->profiling) { \
  1311. call_time = OS::get_singleton()->get_ticks_usec(); \
  1312. } \
  1313. GET_INSTRUCTION_ARG(ret, argc + 1); \
  1314. VariantInternal::initialize(ret, Variant::m_type); \
  1315. void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
  1316. method->ptrcall(base_obj, argptrs, ret_opaque); \
  1317. if (GDScriptLanguage::get_singleton()->profiling) { \
  1318. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time; \
  1319. } \
  1320. ip += 3; \
  1321. } \
  1322. DISPATCH_OPCODE
  1323. #else
  1324. #define OPCODE_CALL_PTR(m_type) \
  1325. OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
  1326. CHECK_SPACE(3 + instr_arg_count); \
  1327. int argc = _code_ptr[ip + 1]; \
  1328. GET_INSTRUCTION_ARG(base, argc); \
  1329. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
  1330. Object *base_obj = *VariantInternal::get_object(base); \
  1331. const void **argptrs = call_args_ptr; \
  1332. for (int i = 0; i < argc; i++) { \
  1333. GET_INSTRUCTION_ARG(v, i); \
  1334. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
  1335. } \
  1336. GET_INSTRUCTION_ARG(ret, argc + 1); \
  1337. VariantInternal::initialize(ret, Variant::m_type); \
  1338. void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
  1339. method->ptrcall(base_obj, argptrs, ret_opaque); \
  1340. ip += 3; \
  1341. } \
  1342. DISPATCH_OPCODE
  1343. #endif
  1344. OPCODE_CALL_PTR(BOOL);
  1345. OPCODE_CALL_PTR(INT);
  1346. OPCODE_CALL_PTR(FLOAT);
  1347. OPCODE_CALL_PTR(STRING);
  1348. OPCODE_CALL_PTR(VECTOR2);
  1349. OPCODE_CALL_PTR(VECTOR2I);
  1350. OPCODE_CALL_PTR(RECT2);
  1351. OPCODE_CALL_PTR(RECT2I);
  1352. OPCODE_CALL_PTR(VECTOR3);
  1353. OPCODE_CALL_PTR(VECTOR3I);
  1354. OPCODE_CALL_PTR(TRANSFORM2D);
  1355. OPCODE_CALL_PTR(PLANE);
  1356. OPCODE_CALL_PTR(QUAT);
  1357. OPCODE_CALL_PTR(AABB);
  1358. OPCODE_CALL_PTR(BASIS);
  1359. OPCODE_CALL_PTR(TRANSFORM);
  1360. OPCODE_CALL_PTR(COLOR);
  1361. OPCODE_CALL_PTR(STRING_NAME);
  1362. OPCODE_CALL_PTR(NODE_PATH);
  1363. OPCODE_CALL_PTR(RID);
  1364. OPCODE_CALL_PTR(CALLABLE);
  1365. OPCODE_CALL_PTR(SIGNAL);
  1366. OPCODE_CALL_PTR(DICTIONARY);
  1367. OPCODE_CALL_PTR(ARRAY);
  1368. OPCODE_CALL_PTR(PACKED_BYTE_ARRAY);
  1369. OPCODE_CALL_PTR(PACKED_INT32_ARRAY);
  1370. OPCODE_CALL_PTR(PACKED_INT64_ARRAY);
  1371. OPCODE_CALL_PTR(PACKED_FLOAT32_ARRAY);
  1372. OPCODE_CALL_PTR(PACKED_FLOAT64_ARRAY);
  1373. OPCODE_CALL_PTR(PACKED_STRING_ARRAY);
  1374. OPCODE_CALL_PTR(PACKED_VECTOR2_ARRAY);
  1375. OPCODE_CALL_PTR(PACKED_VECTOR3_ARRAY);
  1376. OPCODE_CALL_PTR(PACKED_COLOR_ARRAY);
  1377. OPCODE(OPCODE_CALL_PTRCALL_OBJECT) {
  1378. CHECK_SPACE(3 + instr_arg_count);
  1379. ip += instr_arg_count;
  1380. int argc = _code_ptr[ip + 1];
  1381. GD_ERR_BREAK(argc < 0);
  1382. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1383. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1384. GET_INSTRUCTION_ARG(base, argc);
  1385. #ifdef DEBUG_ENABLED
  1386. bool freed = false;
  1387. Object *base_obj = base->get_validated_object_with_check(freed);
  1388. if (freed) {
  1389. err_text = "Trying to call a function on a previously freed instance.";
  1390. OPCODE_BREAK;
  1391. } else if (!base_obj) {
  1392. err_text = "Trying to call a function on a null value.";
  1393. OPCODE_BREAK;
  1394. }
  1395. #else
  1396. Object *base_obj = *VariantInternal::get_object(base);
  1397. #endif
  1398. const void **argptrs = call_args_ptr;
  1399. for (int i = 0; i < argc; i++) {
  1400. GET_INSTRUCTION_ARG(v, i);
  1401. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
  1402. }
  1403. #ifdef DEBUG_ENABLED
  1404. uint64_t call_time = 0;
  1405. if (GDScriptLanguage::get_singleton()->profiling) {
  1406. call_time = OS::get_singleton()->get_ticks_usec();
  1407. }
  1408. #endif
  1409. GET_INSTRUCTION_ARG(ret, argc + 1);
  1410. VariantInternal::initialize(ret, Variant::OBJECT);
  1411. Object **ret_opaque = VariantInternal::get_object(ret);
  1412. method->ptrcall(base_obj, argptrs, ret_opaque);
  1413. VariantInternal::object_assign(ret, *ret_opaque); // Set so ID is correct too.
  1414. #ifdef DEBUG_ENABLED
  1415. if (GDScriptLanguage::get_singleton()->profiling) {
  1416. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1417. }
  1418. #endif
  1419. ip += 3;
  1420. }
  1421. DISPATCH_OPCODE;
  1422. OPCODE(OPCODE_CALL_PTRCALL_NO_RETURN) {
  1423. CHECK_SPACE(3 + instr_arg_count);
  1424. ip += instr_arg_count;
  1425. int argc = _code_ptr[ip + 1];
  1426. GD_ERR_BREAK(argc < 0);
  1427. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1428. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1429. GET_INSTRUCTION_ARG(base, argc);
  1430. #ifdef DEBUG_ENABLED
  1431. bool freed = false;
  1432. Object *base_obj = base->get_validated_object_with_check(freed);
  1433. if (freed) {
  1434. err_text = "Trying to call a function on a previously freed instance.";
  1435. OPCODE_BREAK;
  1436. } else if (!base_obj) {
  1437. err_text = "Trying to call a function on a null value.";
  1438. OPCODE_BREAK;
  1439. }
  1440. #else
  1441. Object *base_obj = *VariantInternal::get_object(base);
  1442. #endif
  1443. const void **argptrs = call_args_ptr;
  1444. for (int i = 0; i < argc; i++) {
  1445. GET_INSTRUCTION_ARG(v, i);
  1446. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
  1447. }
  1448. #ifdef DEBUG_ENABLED
  1449. uint64_t call_time = 0;
  1450. if (GDScriptLanguage::get_singleton()->profiling) {
  1451. call_time = OS::get_singleton()->get_ticks_usec();
  1452. }
  1453. #endif
  1454. GET_INSTRUCTION_ARG(ret, argc + 1);
  1455. VariantInternal::initialize(ret, Variant::NIL);
  1456. method->ptrcall(base_obj, argptrs, nullptr);
  1457. #ifdef DEBUG_ENABLED
  1458. if (GDScriptLanguage::get_singleton()->profiling) {
  1459. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1460. }
  1461. #endif
  1462. ip += 3;
  1463. }
  1464. DISPATCH_OPCODE;
  1465. OPCODE(OPCODE_CALL_BUILTIN_TYPE_VALIDATED) {
  1466. CHECK_SPACE(3 + instr_arg_count);
  1467. ip += instr_arg_count;
  1468. int argc = _code_ptr[ip + 1];
  1469. GD_ERR_BREAK(argc < 0);
  1470. GET_INSTRUCTION_ARG(base, argc);
  1471. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _builtin_methods_count);
  1472. Variant::ValidatedBuiltInMethod method = _builtin_methods_ptr[_code_ptr[ip + 2]];
  1473. Variant **argptrs = instruction_args;
  1474. #ifdef DEBUG_ENABLED
  1475. uint64_t call_time = 0;
  1476. if (GDScriptLanguage::get_singleton()->profiling) {
  1477. call_time = OS::get_singleton()->get_ticks_usec();
  1478. }
  1479. #endif
  1480. GET_INSTRUCTION_ARG(ret, argc + 1);
  1481. method(base, (const Variant **)argptrs, argc, ret);
  1482. #ifdef DEBUG_ENABLED
  1483. if (GDScriptLanguage::get_singleton()->profiling) {
  1484. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1485. }
  1486. #endif
  1487. ip += 3;
  1488. }
  1489. DISPATCH_OPCODE;
  1490. OPCODE(OPCODE_CALL_UTILITY) {
  1491. CHECK_SPACE(3 + instr_arg_count);
  1492. ip += instr_arg_count;
  1493. int argc = _code_ptr[ip + 1];
  1494. GD_ERR_BREAK(argc < 0);
  1495. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _global_names_count);
  1496. StringName function = _global_names_ptr[_code_ptr[ip + 2]];
  1497. Variant **argptrs = instruction_args;
  1498. GET_INSTRUCTION_ARG(dst, argc);
  1499. Callable::CallError err;
  1500. Variant::call_utility_function(function, dst, (const Variant **)argptrs, argc, err);
  1501. #ifdef DEBUG_ENABLED
  1502. if (err.error != Callable::CallError::CALL_OK) {
  1503. String methodstr = function;
  1504. if (dst->get_type() == Variant::STRING) {
  1505. // Call provided error string.
  1506. err_text = "Error calling utility function '" + methodstr + "': " + String(*dst);
  1507. } else {
  1508. err_text = _get_call_error(err, "utility function '" + methodstr + "'", (const Variant **)argptrs);
  1509. }
  1510. OPCODE_BREAK;
  1511. }
  1512. #endif
  1513. ip += 3;
  1514. }
  1515. DISPATCH_OPCODE;
  1516. OPCODE(OPCODE_CALL_UTILITY_VALIDATED) {
  1517. CHECK_SPACE(3 + instr_arg_count);
  1518. ip += instr_arg_count;
  1519. int argc = _code_ptr[ip + 1];
  1520. GD_ERR_BREAK(argc < 0);
  1521. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _utilities_count);
  1522. Variant::ValidatedUtilityFunction function = _utilities_ptr[_code_ptr[ip + 2]];
  1523. Variant **argptrs = instruction_args;
  1524. GET_INSTRUCTION_ARG(dst, argc);
  1525. function(dst, (const Variant **)argptrs, argc);
  1526. ip += 3;
  1527. }
  1528. DISPATCH_OPCODE;
  1529. OPCODE(OPCODE_CALL_GDSCRIPT_UTILITY) {
  1530. CHECK_SPACE(3 + instr_arg_count);
  1531. ip += instr_arg_count;
  1532. int argc = _code_ptr[ip + 1];
  1533. GD_ERR_BREAK(argc < 0);
  1534. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _gds_utilities_count);
  1535. GDScriptUtilityFunctions::FunctionPtr function = _gds_utilities_ptr[_code_ptr[ip + 2]];
  1536. Variant **argptrs = instruction_args;
  1537. GET_INSTRUCTION_ARG(dst, argc);
  1538. Callable::CallError err;
  1539. function(dst, (const Variant **)argptrs, argc, err);
  1540. #ifdef DEBUG_ENABLED
  1541. if (err.error != Callable::CallError::CALL_OK) {
  1542. // TODO: Add this information in debug.
  1543. String methodstr = "<unkown function>";
  1544. if (dst->get_type() == Variant::STRING) {
  1545. // Call provided error string.
  1546. err_text = "Error calling GDScript utility function '" + methodstr + "': " + String(*dst);
  1547. } else {
  1548. err_text = _get_call_error(err, "GDScript utility function '" + methodstr + "'", (const Variant **)argptrs);
  1549. }
  1550. OPCODE_BREAK;
  1551. }
  1552. #endif
  1553. ip += 3;
  1554. }
  1555. DISPATCH_OPCODE;
  1556. OPCODE(OPCODE_CALL_SELF_BASE) {
  1557. CHECK_SPACE(3 + instr_arg_count);
  1558. ip += instr_arg_count;
  1559. int self_fun = _code_ptr[ip + 1];
  1560. #ifdef DEBUG_ENABLED
  1561. if (self_fun < 0 || self_fun >= _global_names_count) {
  1562. err_text = "compiler bug, function name not found";
  1563. OPCODE_BREAK;
  1564. }
  1565. #endif
  1566. const StringName *methodname = &_global_names_ptr[self_fun];
  1567. int argc = _code_ptr[ip + 2];
  1568. GD_ERR_BREAK(argc < 0);
  1569. Variant **argptrs = instruction_args;
  1570. GET_INSTRUCTION_ARG(dst, argc);
  1571. const GDScript *gds = _script;
  1572. const Map<StringName, GDScriptFunction *>::Element *E = nullptr;
  1573. while (gds->base.ptr()) {
  1574. gds = gds->base.ptr();
  1575. E = gds->member_functions.find(*methodname);
  1576. if (E) {
  1577. break;
  1578. }
  1579. }
  1580. Callable::CallError err;
  1581. if (E) {
  1582. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  1583. } else if (gds->native.ptr()) {
  1584. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1585. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  1586. if (!mb) {
  1587. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1588. } else {
  1589. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  1590. }
  1591. } else {
  1592. err.error = Callable::CallError::CALL_OK;
  1593. }
  1594. } else {
  1595. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1596. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1597. } else {
  1598. err.error = Callable::CallError::CALL_OK;
  1599. }
  1600. }
  1601. if (err.error != Callable::CallError::CALL_OK) {
  1602. String methodstr = *methodname;
  1603. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  1604. OPCODE_BREAK;
  1605. }
  1606. ip += 3;
  1607. }
  1608. DISPATCH_OPCODE;
  1609. OPCODE(OPCODE_AWAIT) {
  1610. CHECK_SPACE(2);
  1611. // Do the oneshot connect.
  1612. GET_INSTRUCTION_ARG(argobj, 0);
  1613. Signal sig;
  1614. bool is_signal = true;
  1615. {
  1616. Variant result = *argobj;
  1617. if (argobj->get_type() == Variant::OBJECT) {
  1618. bool was_freed = false;
  1619. Object *obj = argobj->get_validated_object_with_check(was_freed);
  1620. if (was_freed) {
  1621. err_text = "Trying to await on a freed object.";
  1622. OPCODE_BREAK;
  1623. }
  1624. // Is this even possible to be null at this point?
  1625. if (obj) {
  1626. if (obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1627. static StringName completed = _scs_create("completed");
  1628. result = Signal(obj, completed);
  1629. }
  1630. }
  1631. }
  1632. if (result.get_type() != Variant::SIGNAL) {
  1633. ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
  1634. // The stack pointer should be the same, so we don't need to set a return value.
  1635. is_signal = false;
  1636. } else {
  1637. sig = result;
  1638. }
  1639. }
  1640. if (is_signal) {
  1641. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  1642. gdfs->function = this;
  1643. gdfs->state.stack.resize(alloca_size);
  1644. //copy variant stack
  1645. for (int i = 0; i < _stack_size; i++) {
  1646. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  1647. }
  1648. gdfs->state.stack_size = _stack_size;
  1649. gdfs->state.self = self;
  1650. gdfs->state.alloca_size = alloca_size;
  1651. gdfs->state.ip = ip + 2;
  1652. gdfs->state.line = line;
  1653. gdfs->state.script = _script;
  1654. {
  1655. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1656. _script->pending_func_states.add(&gdfs->scripts_list);
  1657. if (p_instance) {
  1658. gdfs->state.instance = p_instance;
  1659. p_instance->pending_func_states.add(&gdfs->instances_list);
  1660. } else {
  1661. gdfs->state.instance = nullptr;
  1662. }
  1663. }
  1664. #ifdef DEBUG_ENABLED
  1665. gdfs->state.function_name = name;
  1666. gdfs->state.script_path = _script->get_path();
  1667. #endif
  1668. gdfs->state.defarg = defarg;
  1669. gdfs->function = this;
  1670. retvalue = gdfs;
  1671. Error err = sig.connect(Callable(gdfs.ptr(), "_signal_callback"), varray(gdfs), Object::CONNECT_ONESHOT);
  1672. if (err != OK) {
  1673. err_text = "Error connecting to signal: " + sig.get_name() + " during await.";
  1674. OPCODE_BREAK;
  1675. }
  1676. #ifdef DEBUG_ENABLED
  1677. exit_ok = true;
  1678. awaited = true;
  1679. #endif
  1680. OPCODE_BREAK;
  1681. }
  1682. }
  1683. DISPATCH_OPCODE; // Needed for synchronous calls (when result is immediately available).
  1684. OPCODE(OPCODE_AWAIT_RESUME) {
  1685. CHECK_SPACE(2);
  1686. #ifdef DEBUG_ENABLED
  1687. if (!p_state) {
  1688. err_text = ("Invalid Resume (bug?)");
  1689. OPCODE_BREAK;
  1690. }
  1691. #endif
  1692. GET_INSTRUCTION_ARG(result, 0);
  1693. *result = p_state->result;
  1694. ip += 2;
  1695. }
  1696. DISPATCH_OPCODE;
  1697. OPCODE(OPCODE_JUMP) {
  1698. CHECK_SPACE(2);
  1699. int to = _code_ptr[ip + 1];
  1700. GD_ERR_BREAK(to < 0 || to > _code_size);
  1701. ip = to;
  1702. }
  1703. DISPATCH_OPCODE;
  1704. OPCODE(OPCODE_JUMP_IF) {
  1705. CHECK_SPACE(3);
  1706. GET_INSTRUCTION_ARG(test, 0);
  1707. bool result = test->booleanize();
  1708. if (result) {
  1709. int to = _code_ptr[ip + 2];
  1710. GD_ERR_BREAK(to < 0 || to > _code_size);
  1711. ip = to;
  1712. } else {
  1713. ip += 3;
  1714. }
  1715. }
  1716. DISPATCH_OPCODE;
  1717. OPCODE(OPCODE_JUMP_IF_NOT) {
  1718. CHECK_SPACE(3);
  1719. GET_INSTRUCTION_ARG(test, 0);
  1720. bool result = test->booleanize();
  1721. if (!result) {
  1722. int to = _code_ptr[ip + 2];
  1723. GD_ERR_BREAK(to < 0 || to > _code_size);
  1724. ip = to;
  1725. } else {
  1726. ip += 3;
  1727. }
  1728. }
  1729. DISPATCH_OPCODE;
  1730. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1731. CHECK_SPACE(2);
  1732. ip = _default_arg_ptr[defarg];
  1733. }
  1734. DISPATCH_OPCODE;
  1735. OPCODE(OPCODE_RETURN) {
  1736. CHECK_SPACE(2);
  1737. GET_INSTRUCTION_ARG(r, 0);
  1738. retvalue = *r;
  1739. #ifdef DEBUG_ENABLED
  1740. exit_ok = true;
  1741. #endif
  1742. OPCODE_BREAK;
  1743. }
  1744. OPCODE(OPCODE_ITERATE_BEGIN) {
  1745. CHECK_SPACE(8); // Space for this and a regular iterate.
  1746. GET_INSTRUCTION_ARG(counter, 0);
  1747. GET_INSTRUCTION_ARG(container, 1);
  1748. bool valid;
  1749. if (!container->iter_init(*counter, valid)) {
  1750. #ifdef DEBUG_ENABLED
  1751. if (!valid) {
  1752. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1753. OPCODE_BREAK;
  1754. }
  1755. #endif
  1756. int jumpto = _code_ptr[ip + 4];
  1757. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1758. ip = jumpto;
  1759. } else {
  1760. GET_INSTRUCTION_ARG(iterator, 2);
  1761. *iterator = container->iter_get(*counter, valid);
  1762. #ifdef DEBUG_ENABLED
  1763. if (!valid) {
  1764. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  1765. OPCODE_BREAK;
  1766. }
  1767. #endif
  1768. ip += 5; // Skip regular iterate which is always next.
  1769. }
  1770. }
  1771. DISPATCH_OPCODE;
  1772. OPCODE(OPCODE_ITERATE_BEGIN_INT) {
  1773. CHECK_SPACE(8); // Check space for iterate instruction too.
  1774. GET_INSTRUCTION_ARG(counter, 0);
  1775. GET_INSTRUCTION_ARG(container, 1);
  1776. int64_t size = *VariantInternal::get_int(container);
  1777. VariantInternal::initialize(counter, Variant::INT);
  1778. *VariantInternal::get_int(counter) = 0;
  1779. if (size > 0) {
  1780. GET_INSTRUCTION_ARG(iterator, 2);
  1781. VariantInternal::initialize(iterator, Variant::INT);
  1782. *VariantInternal::get_int(iterator) = 0;
  1783. // Skip regular iterate.
  1784. ip += 5;
  1785. } else {
  1786. // Jump to end of loop.
  1787. int jumpto = _code_ptr[ip + 4];
  1788. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1789. ip = jumpto;
  1790. }
  1791. }
  1792. DISPATCH_OPCODE;
  1793. OPCODE(OPCODE_ITERATE_BEGIN_FLOAT) {
  1794. CHECK_SPACE(8); // Check space for iterate instruction too.
  1795. GET_INSTRUCTION_ARG(counter, 0);
  1796. GET_INSTRUCTION_ARG(container, 1);
  1797. double size = *VariantInternal::get_float(container);
  1798. VariantInternal::initialize(counter, Variant::FLOAT);
  1799. *VariantInternal::get_float(counter) = 0.0;
  1800. if (size > 0) {
  1801. GET_INSTRUCTION_ARG(iterator, 2);
  1802. VariantInternal::initialize(iterator, Variant::FLOAT);
  1803. *VariantInternal::get_float(iterator) = 0;
  1804. // Skip regular iterate.
  1805. ip += 5;
  1806. } else {
  1807. // Jump to end of loop.
  1808. int jumpto = _code_ptr[ip + 4];
  1809. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1810. ip = jumpto;
  1811. }
  1812. }
  1813. DISPATCH_OPCODE;
  1814. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2) {
  1815. CHECK_SPACE(8); // Check space for iterate instruction too.
  1816. GET_INSTRUCTION_ARG(counter, 0);
  1817. GET_INSTRUCTION_ARG(container, 1);
  1818. Vector2 *bounds = VariantInternal::get_vector2(container);
  1819. VariantInternal::initialize(counter, Variant::FLOAT);
  1820. *VariantInternal::get_float(counter) = bounds->x;
  1821. if (bounds->x < bounds->y) {
  1822. GET_INSTRUCTION_ARG(iterator, 2);
  1823. VariantInternal::initialize(iterator, Variant::FLOAT);
  1824. *VariantInternal::get_float(iterator) = bounds->x;
  1825. // Skip regular iterate.
  1826. ip += 5;
  1827. } else {
  1828. // Jump to end of loop.
  1829. int jumpto = _code_ptr[ip + 4];
  1830. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1831. ip = jumpto;
  1832. }
  1833. }
  1834. DISPATCH_OPCODE;
  1835. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2I) {
  1836. CHECK_SPACE(8); // Check space for iterate instruction too.
  1837. GET_INSTRUCTION_ARG(counter, 0);
  1838. GET_INSTRUCTION_ARG(container, 1);
  1839. Vector2i *bounds = VariantInternal::get_vector2i(container);
  1840. VariantInternal::initialize(counter, Variant::FLOAT);
  1841. *VariantInternal::get_int(counter) = bounds->x;
  1842. if (bounds->x < bounds->y) {
  1843. GET_INSTRUCTION_ARG(iterator, 2);
  1844. VariantInternal::initialize(iterator, Variant::INT);
  1845. *VariantInternal::get_int(iterator) = bounds->x;
  1846. // Skip regular iterate.
  1847. ip += 5;
  1848. } else {
  1849. // Jump to end of loop.
  1850. int jumpto = _code_ptr[ip + 4];
  1851. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1852. ip = jumpto;
  1853. }
  1854. }
  1855. DISPATCH_OPCODE;
  1856. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3) {
  1857. CHECK_SPACE(8); // Check space for iterate instruction too.
  1858. GET_INSTRUCTION_ARG(counter, 0);
  1859. GET_INSTRUCTION_ARG(container, 1);
  1860. Vector3 *bounds = VariantInternal::get_vector3(container);
  1861. double from = bounds->x;
  1862. double to = bounds->y;
  1863. double step = bounds->z;
  1864. VariantInternal::initialize(counter, Variant::FLOAT);
  1865. *VariantInternal::get_float(counter) = from;
  1866. bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0);
  1867. if (do_continue) {
  1868. GET_INSTRUCTION_ARG(iterator, 2);
  1869. VariantInternal::initialize(iterator, Variant::FLOAT);
  1870. *VariantInternal::get_float(iterator) = from;
  1871. // Skip regular iterate.
  1872. ip += 5;
  1873. } else {
  1874. // Jump to end of loop.
  1875. int jumpto = _code_ptr[ip + 4];
  1876. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1877. ip = jumpto;
  1878. }
  1879. }
  1880. DISPATCH_OPCODE;
  1881. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3I) {
  1882. CHECK_SPACE(8); // Check space for iterate instruction too.
  1883. GET_INSTRUCTION_ARG(counter, 0);
  1884. GET_INSTRUCTION_ARG(container, 1);
  1885. Vector3i *bounds = VariantInternal::get_vector3i(container);
  1886. int64_t from = bounds->x;
  1887. int64_t to = bounds->y;
  1888. int64_t step = bounds->z;
  1889. VariantInternal::initialize(counter, Variant::INT);
  1890. *VariantInternal::get_int(counter) = from;
  1891. bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0);
  1892. if (do_continue) {
  1893. GET_INSTRUCTION_ARG(iterator, 2);
  1894. VariantInternal::initialize(iterator, Variant::INT);
  1895. *VariantInternal::get_int(iterator) = from;
  1896. // Skip regular iterate.
  1897. ip += 5;
  1898. } else {
  1899. // Jump to end of loop.
  1900. int jumpto = _code_ptr[ip + 4];
  1901. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1902. ip = jumpto;
  1903. }
  1904. }
  1905. DISPATCH_OPCODE;
  1906. OPCODE(OPCODE_ITERATE_BEGIN_STRING) {
  1907. CHECK_SPACE(8); // Check space for iterate instruction too.
  1908. GET_INSTRUCTION_ARG(counter, 0);
  1909. GET_INSTRUCTION_ARG(container, 1);
  1910. String *str = VariantInternal::get_string(container);
  1911. VariantInternal::initialize(counter, Variant::INT);
  1912. *VariantInternal::get_int(counter) = 0;
  1913. if (!str->is_empty()) {
  1914. GET_INSTRUCTION_ARG(iterator, 2);
  1915. VariantInternal::initialize(iterator, Variant::STRING);
  1916. *VariantInternal::get_string(iterator) = str->substr(0, 1);
  1917. // Skip regular iterate.
  1918. ip += 5;
  1919. } else {
  1920. // Jump to end of loop.
  1921. int jumpto = _code_ptr[ip + 4];
  1922. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1923. ip = jumpto;
  1924. }
  1925. }
  1926. DISPATCH_OPCODE;
  1927. OPCODE(OPCODE_ITERATE_BEGIN_DICTIONARY) {
  1928. CHECK_SPACE(8); // Check space for iterate instruction too.
  1929. GET_INSTRUCTION_ARG(counter, 0);
  1930. GET_INSTRUCTION_ARG(container, 1);
  1931. Dictionary *dict = VariantInternal::get_dictionary(container);
  1932. const Variant *next = dict->next(nullptr);
  1933. if (!dict->is_empty()) {
  1934. GET_INSTRUCTION_ARG(iterator, 2);
  1935. *counter = *next;
  1936. *iterator = *next;
  1937. // Skip regular iterate.
  1938. ip += 5;
  1939. } else {
  1940. // Jump to end of loop.
  1941. int jumpto = _code_ptr[ip + 4];
  1942. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1943. ip = jumpto;
  1944. }
  1945. }
  1946. DISPATCH_OPCODE;
  1947. OPCODE(OPCODE_ITERATE_BEGIN_ARRAY) {
  1948. CHECK_SPACE(8); // Check space for iterate instruction too.
  1949. GET_INSTRUCTION_ARG(counter, 0);
  1950. GET_INSTRUCTION_ARG(container, 1);
  1951. Array *array = VariantInternal::get_array(container);
  1952. VariantInternal::initialize(counter, Variant::INT);
  1953. *VariantInternal::get_int(counter) = 0;
  1954. if (!array->is_empty()) {
  1955. GET_INSTRUCTION_ARG(iterator, 2);
  1956. *iterator = array->get(0);
  1957. // Skip regular iterate.
  1958. ip += 5;
  1959. } else {
  1960. // Jump to end of loop.
  1961. int jumpto = _code_ptr[ip + 4];
  1962. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1963. ip = jumpto;
  1964. }
  1965. }
  1966. DISPATCH_OPCODE;
  1967. #define OPCODE_ITERATE_BEGIN_PACKED_ARRAY(m_var_type, m_elem_type, m_get_func, m_var_ret_type, m_ret_type, m_ret_get_func) \
  1968. OPCODE(OPCODE_ITERATE_BEGIN_PACKED_##m_var_type##_ARRAY) { \
  1969. CHECK_SPACE(8); \
  1970. GET_INSTRUCTION_ARG(counter, 0); \
  1971. GET_INSTRUCTION_ARG(container, 1); \
  1972. Vector<m_elem_type> *array = VariantInternal::m_get_func(container); \
  1973. VariantInternal::initialize(counter, Variant::INT); \
  1974. *VariantInternal::get_int(counter) = 0; \
  1975. if (!array->is_empty()) { \
  1976. GET_INSTRUCTION_ARG(iterator, 2); \
  1977. VariantInternal::initialize(iterator, Variant::m_var_ret_type); \
  1978. m_ret_type *it = VariantInternal::m_ret_get_func(iterator); \
  1979. *it = array->get(0); \
  1980. ip += 5; \
  1981. } else { \
  1982. int jumpto = _code_ptr[ip + 4]; \
  1983. GD_ERR_BREAK(jumpto<0 || jumpto> _code_size); \
  1984. ip = jumpto; \
  1985. } \
  1986. } \
  1987. DISPATCH_OPCODE
  1988. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(BYTE, uint8_t, get_byte_array, INT, int64_t, get_int);
  1989. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(INT32, int32_t, get_int32_array, INT, int64_t, get_int);
  1990. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(INT64, int64_t, get_int64_array, INT, int64_t, get_int);
  1991. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(FLOAT32, float, get_float32_array, FLOAT, double, get_float);
  1992. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(FLOAT64, double, get_float64_array, FLOAT, double, get_float);
  1993. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(STRING, String, get_string_array, STRING, String, get_string);
  1994. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(VECTOR2, Vector2, get_vector2_array, VECTOR2, Vector2, get_vector2);
  1995. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(VECTOR3, Vector3, get_vector3_array, VECTOR3, Vector3, get_vector3);
  1996. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(COLOR, Color, get_color_array, COLOR, Color, get_color);
  1997. OPCODE(OPCODE_ITERATE_BEGIN_OBJECT) {
  1998. CHECK_SPACE(4);
  1999. GET_INSTRUCTION_ARG(counter, 0);
  2000. GET_INSTRUCTION_ARG(container, 1);
  2001. #ifdef DEBUG_ENABLED
  2002. bool freed = false;
  2003. Object *obj = container->get_validated_object_with_check(freed);
  2004. if (freed) {
  2005. err_text = "Trying to iterate on a previously freed object.";
  2006. OPCODE_BREAK;
  2007. } else if (!obj) {
  2008. err_text = "Trying to iterate on a null value.";
  2009. OPCODE_BREAK;
  2010. }
  2011. #else
  2012. Object *obj = *VariantInternal::get_object(container);
  2013. #endif
  2014. Array ref;
  2015. ref.push_back(*counter);
  2016. Variant vref;
  2017. VariantInternal::initialize(&vref, Variant::ARRAY);
  2018. *VariantInternal::get_array(&vref) = ref;
  2019. Variant **args = instruction_args; // Overriding an instruction argument, but we don't need access to that anymore.
  2020. args[0] = &vref;
  2021. Callable::CallError ce;
  2022. Variant has_next = obj->call(CoreStringNames::get_singleton()->_iter_init, (const Variant **)args, 1, ce);
  2023. #ifdef DEBUG_ENABLED
  2024. if (ce.error != Callable::CallError::CALL_OK) {
  2025. err_text = vformat(R"(There was an error calling "_iter_next" on iterator object of type %s.)", *container);
  2026. OPCODE_BREAK;
  2027. }
  2028. #endif
  2029. if (!has_next.booleanize()) {
  2030. int jumpto = _code_ptr[ip + 4];
  2031. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2032. ip = jumpto;
  2033. } else {
  2034. GET_INSTRUCTION_ARG(iterator, 2);
  2035. *iterator = obj->call(CoreStringNames::get_singleton()->_iter_get, (const Variant **)args, 1, ce);
  2036. #ifdef DEBUG_ENABLED
  2037. if (ce.error != Callable::CallError::CALL_OK) {
  2038. err_text = vformat(R"(There was an error calling "_iter_get" on iterator object of type %s.)", *container);
  2039. OPCODE_BREAK;
  2040. }
  2041. #endif
  2042. ip += 5; // Loop again.
  2043. }
  2044. }
  2045. DISPATCH_OPCODE;
  2046. OPCODE(OPCODE_ITERATE) {
  2047. CHECK_SPACE(4);
  2048. GET_INSTRUCTION_ARG(counter, 0);
  2049. GET_INSTRUCTION_ARG(container, 1);
  2050. bool valid;
  2051. if (!container->iter_next(*counter, valid)) {
  2052. #ifdef DEBUG_ENABLED
  2053. if (!valid) {
  2054. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  2055. OPCODE_BREAK;
  2056. }
  2057. #endif
  2058. int jumpto = _code_ptr[ip + 4];
  2059. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2060. ip = jumpto;
  2061. } else {
  2062. GET_INSTRUCTION_ARG(iterator, 2);
  2063. *iterator = container->iter_get(*counter, valid);
  2064. #ifdef DEBUG_ENABLED
  2065. if (!valid) {
  2066. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  2067. OPCODE_BREAK;
  2068. }
  2069. #endif
  2070. ip += 5; //loop again
  2071. }
  2072. }
  2073. DISPATCH_OPCODE;
  2074. OPCODE(OPCODE_ITERATE_INT) {
  2075. CHECK_SPACE(4);
  2076. GET_INSTRUCTION_ARG(counter, 0);
  2077. GET_INSTRUCTION_ARG(container, 1);
  2078. int64_t size = *VariantInternal::get_int(container);
  2079. int64_t *count = VariantInternal::get_int(counter);
  2080. (*count)++;
  2081. if (*count >= size) {
  2082. int jumpto = _code_ptr[ip + 4];
  2083. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2084. ip = jumpto;
  2085. } else {
  2086. GET_INSTRUCTION_ARG(iterator, 2);
  2087. *VariantInternal::get_int(iterator) = *count;
  2088. ip += 5; // Loop again.
  2089. }
  2090. }
  2091. DISPATCH_OPCODE;
  2092. OPCODE(OPCODE_ITERATE_FLOAT) {
  2093. CHECK_SPACE(4);
  2094. GET_INSTRUCTION_ARG(counter, 0);
  2095. GET_INSTRUCTION_ARG(container, 1);
  2096. double size = *VariantInternal::get_float(container);
  2097. double *count = VariantInternal::get_float(counter);
  2098. (*count)++;
  2099. if (*count >= size) {
  2100. int jumpto = _code_ptr[ip + 4];
  2101. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2102. ip = jumpto;
  2103. } else {
  2104. GET_INSTRUCTION_ARG(iterator, 2);
  2105. *VariantInternal::get_float(iterator) = *count;
  2106. ip += 5; // Loop again.
  2107. }
  2108. }
  2109. DISPATCH_OPCODE;
  2110. OPCODE(OPCODE_ITERATE_VECTOR2) {
  2111. CHECK_SPACE(4);
  2112. GET_INSTRUCTION_ARG(counter, 0);
  2113. GET_INSTRUCTION_ARG(container, 1);
  2114. const Vector2 *bounds = VariantInternal::get_vector2((const Variant *)container);
  2115. double *count = VariantInternal::get_float(counter);
  2116. (*count)++;
  2117. if (*count >= bounds->y) {
  2118. int jumpto = _code_ptr[ip + 4];
  2119. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2120. ip = jumpto;
  2121. } else {
  2122. GET_INSTRUCTION_ARG(iterator, 2);
  2123. *VariantInternal::get_float(iterator) = *count;
  2124. ip += 5; // Loop again.
  2125. }
  2126. }
  2127. DISPATCH_OPCODE;
  2128. OPCODE(OPCODE_ITERATE_VECTOR2I) {
  2129. CHECK_SPACE(4);
  2130. GET_INSTRUCTION_ARG(counter, 0);
  2131. GET_INSTRUCTION_ARG(container, 1);
  2132. const Vector2i *bounds = VariantInternal::get_vector2i((const Variant *)container);
  2133. int64_t *count = VariantInternal::get_int(counter);
  2134. (*count)++;
  2135. if (*count >= bounds->y) {
  2136. int jumpto = _code_ptr[ip + 4];
  2137. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2138. ip = jumpto;
  2139. } else {
  2140. GET_INSTRUCTION_ARG(iterator, 2);
  2141. *VariantInternal::get_int(iterator) = *count;
  2142. ip += 5; // Loop again.
  2143. }
  2144. }
  2145. DISPATCH_OPCODE;
  2146. OPCODE(OPCODE_ITERATE_VECTOR3) {
  2147. CHECK_SPACE(4);
  2148. GET_INSTRUCTION_ARG(counter, 0);
  2149. GET_INSTRUCTION_ARG(container, 1);
  2150. const Vector3 *bounds = VariantInternal::get_vector3((const Variant *)container);
  2151. double *count = VariantInternal::get_float(counter);
  2152. *count += bounds->z;
  2153. if ((bounds->z < 0 && *count <= bounds->y) || (bounds->z > 0 && *count >= bounds->y)) {
  2154. int jumpto = _code_ptr[ip + 4];
  2155. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2156. ip = jumpto;
  2157. } else {
  2158. GET_INSTRUCTION_ARG(iterator, 2);
  2159. *VariantInternal::get_float(iterator) = *count;
  2160. ip += 5; // Loop again.
  2161. }
  2162. }
  2163. DISPATCH_OPCODE;
  2164. OPCODE(OPCODE_ITERATE_VECTOR3I) {
  2165. CHECK_SPACE(4);
  2166. GET_INSTRUCTION_ARG(counter, 0);
  2167. GET_INSTRUCTION_ARG(container, 1);
  2168. const Vector3i *bounds = VariantInternal::get_vector3i((const Variant *)container);
  2169. int64_t *count = VariantInternal::get_int(counter);
  2170. *count += bounds->z;
  2171. if ((bounds->z < 0 && *count <= bounds->y) || (bounds->z > 0 && *count >= bounds->y)) {
  2172. int jumpto = _code_ptr[ip + 4];
  2173. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2174. ip = jumpto;
  2175. } else {
  2176. GET_INSTRUCTION_ARG(iterator, 2);
  2177. *VariantInternal::get_int(iterator) = *count;
  2178. ip += 5; // Loop again.
  2179. }
  2180. }
  2181. DISPATCH_OPCODE;
  2182. OPCODE(OPCODE_ITERATE_STRING) {
  2183. CHECK_SPACE(4);
  2184. GET_INSTRUCTION_ARG(counter, 0);
  2185. GET_INSTRUCTION_ARG(container, 1);
  2186. const String *str = VariantInternal::get_string((const Variant *)container);
  2187. int64_t *idx = VariantInternal::get_int(counter);
  2188. (*idx)++;
  2189. if (*idx >= str->length()) {
  2190. int jumpto = _code_ptr[ip + 4];
  2191. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2192. ip = jumpto;
  2193. } else {
  2194. GET_INSTRUCTION_ARG(iterator, 2);
  2195. *VariantInternal::get_string(iterator) = str->substr(*idx, 1);
  2196. ip += 5; // Loop again.
  2197. }
  2198. }
  2199. DISPATCH_OPCODE;
  2200. OPCODE(OPCODE_ITERATE_DICTIONARY) {
  2201. CHECK_SPACE(4);
  2202. GET_INSTRUCTION_ARG(counter, 0);
  2203. GET_INSTRUCTION_ARG(container, 1);
  2204. const Dictionary *dict = VariantInternal::get_dictionary((const Variant *)container);
  2205. const Variant *next = dict->next(counter);
  2206. if (!next) {
  2207. int jumpto = _code_ptr[ip + 4];
  2208. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2209. ip = jumpto;
  2210. } else {
  2211. GET_INSTRUCTION_ARG(iterator, 2);
  2212. *counter = *next;
  2213. *iterator = *next;
  2214. ip += 5; // Loop again.
  2215. }
  2216. }
  2217. DISPATCH_OPCODE;
  2218. OPCODE(OPCODE_ITERATE_ARRAY) {
  2219. CHECK_SPACE(4);
  2220. GET_INSTRUCTION_ARG(counter, 0);
  2221. GET_INSTRUCTION_ARG(container, 1);
  2222. const Array *array = VariantInternal::get_array((const Variant *)container);
  2223. int64_t *idx = VariantInternal::get_int(counter);
  2224. (*idx)++;
  2225. if (*idx >= array->size()) {
  2226. int jumpto = _code_ptr[ip + 4];
  2227. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2228. ip = jumpto;
  2229. } else {
  2230. GET_INSTRUCTION_ARG(iterator, 2);
  2231. *iterator = array->get(*idx);
  2232. ip += 5; // Loop again.
  2233. }
  2234. }
  2235. DISPATCH_OPCODE;
  2236. #define OPCODE_ITERATE_PACKED_ARRAY(m_var_type, m_elem_type, m_get_func, m_ret_get_func) \
  2237. OPCODE(OPCODE_ITERATE_PACKED_##m_var_type##_ARRAY) { \
  2238. CHECK_SPACE(4); \
  2239. GET_INSTRUCTION_ARG(counter, 0); \
  2240. GET_INSTRUCTION_ARG(container, 1); \
  2241. const Vector<m_elem_type> *array = VariantInternal::m_get_func((const Variant *)container); \
  2242. int64_t *idx = VariantInternal::get_int(counter); \
  2243. (*idx)++; \
  2244. if (*idx >= array->size()) { \
  2245. int jumpto = _code_ptr[ip + 4]; \
  2246. GD_ERR_BREAK(jumpto<0 || jumpto> _code_size); \
  2247. ip = jumpto; \
  2248. } else { \
  2249. GET_INSTRUCTION_ARG(iterator, 2); \
  2250. *VariantInternal::m_ret_get_func(iterator) = array->get(*idx); \
  2251. ip += 5; \
  2252. } \
  2253. } \
  2254. DISPATCH_OPCODE
  2255. OPCODE_ITERATE_PACKED_ARRAY(BYTE, uint8_t, get_byte_array, get_int);
  2256. OPCODE_ITERATE_PACKED_ARRAY(INT32, int32_t, get_int32_array, get_int);
  2257. OPCODE_ITERATE_PACKED_ARRAY(INT64, int64_t, get_int64_array, get_int);
  2258. OPCODE_ITERATE_PACKED_ARRAY(FLOAT32, float, get_float32_array, get_float);
  2259. OPCODE_ITERATE_PACKED_ARRAY(FLOAT64, double, get_float64_array, get_float);
  2260. OPCODE_ITERATE_PACKED_ARRAY(STRING, String, get_string_array, get_string);
  2261. OPCODE_ITERATE_PACKED_ARRAY(VECTOR2, Vector2, get_vector2_array, get_vector2);
  2262. OPCODE_ITERATE_PACKED_ARRAY(VECTOR3, Vector3, get_vector3_array, get_vector3);
  2263. OPCODE_ITERATE_PACKED_ARRAY(COLOR, Color, get_color_array, get_color);
  2264. OPCODE(OPCODE_ITERATE_OBJECT) {
  2265. CHECK_SPACE(4);
  2266. GET_INSTRUCTION_ARG(counter, 0);
  2267. GET_INSTRUCTION_ARG(container, 1);
  2268. #ifdef DEBUG_ENABLED
  2269. bool freed = false;
  2270. Object *obj = container->get_validated_object_with_check(freed);
  2271. if (freed) {
  2272. err_text = "Trying to iterate on a previously freed object.";
  2273. OPCODE_BREAK;
  2274. } else if (!obj) {
  2275. err_text = "Trying to iterate on a null value.";
  2276. OPCODE_BREAK;
  2277. }
  2278. #else
  2279. Object *obj = *VariantInternal::get_object(container);
  2280. #endif
  2281. Array ref;
  2282. ref.push_back(*counter);
  2283. Variant vref;
  2284. VariantInternal::initialize(&vref, Variant::ARRAY);
  2285. *VariantInternal::get_array(&vref) = ref;
  2286. Variant **args = instruction_args; // Overriding an instruction argument, but we don't need access to that anymore.
  2287. args[0] = &vref;
  2288. Callable::CallError ce;
  2289. Variant has_next = obj->call(CoreStringNames::get_singleton()->_iter_next, (const Variant **)args, 1, ce);
  2290. #ifdef DEBUG_ENABLED
  2291. if (ce.error != Callable::CallError::CALL_OK) {
  2292. err_text = vformat(R"(There was an error calling "_iter_next" on iterator object of type %s.)", *container);
  2293. OPCODE_BREAK;
  2294. }
  2295. #endif
  2296. if (!has_next.booleanize()) {
  2297. int jumpto = _code_ptr[ip + 4];
  2298. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2299. ip = jumpto;
  2300. } else {
  2301. GET_INSTRUCTION_ARG(iterator, 2);
  2302. *iterator = obj->call(CoreStringNames::get_singleton()->_iter_get, (const Variant **)args, 1, ce);
  2303. #ifdef DEBUG_ENABLED
  2304. if (ce.error != Callable::CallError::CALL_OK) {
  2305. err_text = vformat(R"(There was an error calling "_iter_get" on iterator object of type %s.)", *container);
  2306. OPCODE_BREAK;
  2307. }
  2308. #endif
  2309. ip += 5; // Loop again.
  2310. }
  2311. }
  2312. DISPATCH_OPCODE;
  2313. OPCODE(OPCODE_ASSERT) {
  2314. CHECK_SPACE(3);
  2315. #ifdef DEBUG_ENABLED
  2316. GET_INSTRUCTION_ARG(test, 0);
  2317. bool result = test->booleanize();
  2318. if (!result) {
  2319. String message_str;
  2320. if (_code_ptr[ip + 2] != 0) {
  2321. GET_INSTRUCTION_ARG(message, 1);
  2322. message_str = *message;
  2323. }
  2324. if (message_str.is_empty()) {
  2325. err_text = "Assertion failed.";
  2326. } else {
  2327. err_text = "Assertion failed: " + message_str;
  2328. }
  2329. OPCODE_BREAK;
  2330. }
  2331. #endif
  2332. ip += 3;
  2333. }
  2334. DISPATCH_OPCODE;
  2335. OPCODE(OPCODE_BREAKPOINT) {
  2336. #ifdef DEBUG_ENABLED
  2337. if (EngineDebugger::is_active()) {
  2338. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  2339. }
  2340. #endif
  2341. ip += 1;
  2342. }
  2343. DISPATCH_OPCODE;
  2344. OPCODE(OPCODE_LINE) {
  2345. CHECK_SPACE(2);
  2346. line = _code_ptr[ip + 1];
  2347. ip += 2;
  2348. if (EngineDebugger::is_active()) {
  2349. // line
  2350. bool do_break = false;
  2351. if (EngineDebugger::get_script_debugger()->get_lines_left() > 0) {
  2352. if (EngineDebugger::get_script_debugger()->get_depth() <= 0) {
  2353. EngineDebugger::get_script_debugger()->set_lines_left(EngineDebugger::get_script_debugger()->get_lines_left() - 1);
  2354. }
  2355. if (EngineDebugger::get_script_debugger()->get_lines_left() <= 0) {
  2356. do_break = true;
  2357. }
  2358. }
  2359. if (EngineDebugger::get_script_debugger()->is_breakpoint(line, source)) {
  2360. do_break = true;
  2361. }
  2362. if (do_break) {
  2363. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  2364. }
  2365. EngineDebugger::get_singleton()->line_poll();
  2366. }
  2367. }
  2368. DISPATCH_OPCODE;
  2369. OPCODE(OPCODE_END) {
  2370. #ifdef DEBUG_ENABLED
  2371. exit_ok = true;
  2372. #endif
  2373. OPCODE_BREAK;
  2374. }
  2375. #if 0 // Enable for debugging.
  2376. default: {
  2377. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  2378. OPCODE_BREAK;
  2379. }
  2380. #endif
  2381. }
  2382. OPCODES_END
  2383. #ifdef DEBUG_ENABLED
  2384. if (exit_ok) {
  2385. OPCODE_OUT;
  2386. }
  2387. //error
  2388. // function, file, line, error, explanation
  2389. String err_file;
  2390. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->path != "") {
  2391. err_file = p_instance->script->path;
  2392. } else if (script) {
  2393. err_file = script->path;
  2394. }
  2395. if (err_file == "") {
  2396. err_file = "<built-in>";
  2397. }
  2398. String err_func = name;
  2399. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->name != "") {
  2400. err_func = p_instance->script->name + "." + err_func;
  2401. }
  2402. int err_line = line;
  2403. if (err_text == "") {
  2404. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  2405. }
  2406. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  2407. // debugger break did not happen
  2408. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  2409. }
  2410. #endif
  2411. OPCODE_OUT;
  2412. }
  2413. OPCODES_OUT
  2414. #ifdef DEBUG_ENABLED
  2415. if (GDScriptLanguage::get_singleton()->profiling) {
  2416. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  2417. profile.total_time += time_taken;
  2418. profile.self_time += time_taken - function_call_time;
  2419. profile.frame_total_time += time_taken;
  2420. profile.frame_self_time += time_taken - function_call_time;
  2421. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  2422. }
  2423. // Check if this is the last time the function is resuming from await
  2424. // Will be true if never awaited as well
  2425. // When it's the last resume it will postpone the exit from stack,
  2426. // so the debugger knows which function triggered the resume of the next function (if any)
  2427. if (!p_state || awaited) {
  2428. if (EngineDebugger::is_active()) {
  2429. GDScriptLanguage::get_singleton()->exit_function();
  2430. }
  2431. #endif
  2432. if (_stack_size) {
  2433. //free stack
  2434. for (int i = 0; i < _stack_size; i++) {
  2435. stack[i].~Variant();
  2436. }
  2437. }
  2438. #ifdef DEBUG_ENABLED
  2439. }
  2440. #endif
  2441. return retvalue;
  2442. }