gdscript_vm.cpp 111 KB

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