gdscript_vm.cpp 111 KB

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