gdscript_vm.cpp 99 KB

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