gdscript_vm.cpp 110 KB

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