gdscript_vm.cpp 104 KB

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