gdscript_function.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. /*************************************************************************/
  2. /* gdscript_function.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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/os/os.h"
  32. #include "gdscript.h"
  33. #include "gdscript_functions.h"
  34. Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_instance, GDScript *p_script, Variant &self, Variant *p_stack, String &r_error) const {
  35. int address = p_address & ADDR_MASK;
  36. //sequential table (jump table generated by compiler)
  37. switch ((p_address & ADDR_TYPE_MASK) >> ADDR_BITS) {
  38. case ADDR_TYPE_SELF: {
  39. #ifdef DEBUG_ENABLED
  40. if (unlikely(!p_instance)) {
  41. r_error = "Cannot access self without instance.";
  42. return NULL;
  43. }
  44. #endif
  45. return &self;
  46. } break;
  47. case ADDR_TYPE_CLASS: {
  48. return &p_script->_static_ref;
  49. } break;
  50. case ADDR_TYPE_MEMBER: {
  51. #ifdef DEBUG_ENABLED
  52. if (unlikely(!p_instance)) {
  53. r_error = "Cannot access member without instance.";
  54. return NULL;
  55. }
  56. #endif
  57. //member indexing is O(1)
  58. return &p_instance->members.write[address];
  59. } break;
  60. case ADDR_TYPE_CLASS_CONSTANT: {
  61. //todo change to index!
  62. GDScript *o = p_script;
  63. #ifdef DEBUG_ENABLED
  64. ERR_FAIL_INDEX_V(address, _global_names_count, NULL);
  65. #endif
  66. const StringName *sn = &_global_names_ptr[address];
  67. while (o) {
  68. GDScript *s = o;
  69. while (s) {
  70. Map<StringName, Variant>::Element *E = s->constants.find(*sn);
  71. if (E) {
  72. return &E->get();
  73. }
  74. s = s->_base;
  75. }
  76. o = o->_owner;
  77. }
  78. ERR_EXPLAIN("GDScriptCompiler bug...");
  79. ERR_FAIL_V(NULL);
  80. } break;
  81. case ADDR_TYPE_LOCAL_CONSTANT: {
  82. #ifdef DEBUG_ENABLED
  83. ERR_FAIL_INDEX_V(address, _constant_count, NULL);
  84. #endif
  85. return &_constants_ptr[address];
  86. } break;
  87. case ADDR_TYPE_STACK:
  88. case ADDR_TYPE_STACK_VARIABLE: {
  89. #ifdef DEBUG_ENABLED
  90. ERR_FAIL_INDEX_V(address, _stack_size, NULL);
  91. #endif
  92. return &p_stack[address];
  93. } break;
  94. case ADDR_TYPE_GLOBAL: {
  95. #ifdef DEBUG_ENABLED
  96. ERR_FAIL_INDEX_V(address, GDScriptLanguage::get_singleton()->get_global_array_size(), NULL);
  97. #endif
  98. return &GDScriptLanguage::get_singleton()->get_global_array()[address];
  99. } break;
  100. #ifdef TOOLS_ENABLED
  101. case ADDR_TYPE_NAMED_GLOBAL: {
  102. #ifdef DEBUG_ENABLED
  103. ERR_FAIL_INDEX_V(address, _named_globals_count, NULL);
  104. #endif
  105. StringName id = _named_globals_ptr[address];
  106. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(id)) {
  107. return (Variant *)&GDScriptLanguage::get_singleton()->get_named_globals_map()[id];
  108. } else {
  109. r_error = "Autoload singleton '" + String(id) + "' has been removed.";
  110. return NULL;
  111. }
  112. } break;
  113. #endif
  114. case ADDR_TYPE_NIL: {
  115. return &nil;
  116. } break;
  117. }
  118. ERR_EXPLAIN("Bad Code! (Addressing Mode)");
  119. ERR_FAIL_V(NULL);
  120. return NULL;
  121. }
  122. String GDScriptFunction::_get_call_error(const Variant::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  123. String err_text;
  124. if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  125. int errorarg = p_err.argument;
  126. 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(p_err.expected) + ".";
  127. } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  128. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  129. } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  130. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  131. } else if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  132. err_text = "Invalid call. Nonexistent " + p_where + ".";
  133. } else if (p_err.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  134. err_text = "Attempt to call " + p_where + " on a null instance.";
  135. } else {
  136. err_text = "Bug, call error: #" + itos(p_err.error);
  137. }
  138. return err_text;
  139. }
  140. #ifdef DEBUG_ENABLED
  141. static String _get_var_type(const Variant *p_type) {
  142. String basestr;
  143. if (p_type->get_type() == Variant::OBJECT) {
  144. Object *bobj = *p_type;
  145. if (!bobj) {
  146. basestr = "null instance";
  147. } else {
  148. if (ObjectDB::instance_validate(bobj)) {
  149. if (bobj->get_script_instance())
  150. basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  151. else
  152. basestr = bobj->get_class();
  153. } else {
  154. basestr = "previously freed instance";
  155. }
  156. }
  157. } else {
  158. basestr = Variant::get_type_name(p_type->get_type());
  159. }
  160. return basestr;
  161. }
  162. #endif
  163. #if defined(__GNUC__)
  164. #define OPCODES_TABLE \
  165. static const void *switch_table_ops[] = { \
  166. &&OPCODE_OPERATOR, \
  167. &&OPCODE_EXTENDS_TEST, \
  168. &&OPCODE_IS_BUILTIN, \
  169. &&OPCODE_SET, \
  170. &&OPCODE_GET, \
  171. &&OPCODE_SET_NAMED, \
  172. &&OPCODE_GET_NAMED, \
  173. &&OPCODE_SET_MEMBER, \
  174. &&OPCODE_GET_MEMBER, \
  175. &&OPCODE_ASSIGN, \
  176. &&OPCODE_ASSIGN_TRUE, \
  177. &&OPCODE_ASSIGN_FALSE, \
  178. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  179. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  180. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  181. &&OPCODE_CAST_TO_BUILTIN, \
  182. &&OPCODE_CAST_TO_NATIVE, \
  183. &&OPCODE_CAST_TO_SCRIPT, \
  184. &&OPCODE_CONSTRUCT, \
  185. &&OPCODE_CONSTRUCT_ARRAY, \
  186. &&OPCODE_CONSTRUCT_DICTIONARY, \
  187. &&OPCODE_CALL, \
  188. &&OPCODE_CALL_RETURN, \
  189. &&OPCODE_CALL_BUILT_IN, \
  190. &&OPCODE_CALL_SELF, \
  191. &&OPCODE_CALL_SELF_BASE, \
  192. &&OPCODE_YIELD, \
  193. &&OPCODE_YIELD_SIGNAL, \
  194. &&OPCODE_YIELD_RESUME, \
  195. &&OPCODE_JUMP, \
  196. &&OPCODE_JUMP_IF, \
  197. &&OPCODE_JUMP_IF_NOT, \
  198. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  199. &&OPCODE_RETURN, \
  200. &&OPCODE_ITERATE_BEGIN, \
  201. &&OPCODE_ITERATE, \
  202. &&OPCODE_ASSERT, \
  203. &&OPCODE_BREAKPOINT, \
  204. &&OPCODE_LINE, \
  205. &&OPCODE_END \
  206. };
  207. #define OPCODE(m_op) \
  208. m_op:
  209. #define OPCODE_WHILE(m_test)
  210. #define OPCODES_END \
  211. OPSEXIT:
  212. #define OPCODES_OUT \
  213. OPSOUT:
  214. #define DISPATCH_OPCODE goto *switch_table_ops[_code_ptr[ip]]
  215. #define OPCODE_SWITCH(m_test) DISPATCH_OPCODE;
  216. #define OPCODE_BREAK goto OPSEXIT
  217. #define OPCODE_OUT goto OPSOUT
  218. #else
  219. #define OPCODES_TABLE
  220. #define OPCODE(m_op) case m_op:
  221. #define OPCODE_WHILE(m_test) while (m_test)
  222. #define OPCODES_END
  223. #define OPCODES_OUT
  224. #define DISPATCH_OPCODE continue
  225. #define OPCODE_SWITCH(m_test) switch (m_test)
  226. #define OPCODE_BREAK break
  227. #define OPCODE_OUT break
  228. #endif
  229. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state) {
  230. OPCODES_TABLE;
  231. if (!_code_ptr) {
  232. return Variant();
  233. }
  234. r_err.error = Variant::CallError::CALL_OK;
  235. Variant self;
  236. Variant retvalue;
  237. Variant *stack = NULL;
  238. Variant **call_args;
  239. int defarg = 0;
  240. #ifdef DEBUG_ENABLED
  241. //GDScriptLanguage::get_singleton()->calls++;
  242. #endif
  243. uint32_t alloca_size = 0;
  244. GDScript *script;
  245. int ip = 0;
  246. int line = _initial_line;
  247. if (p_state) {
  248. //use existing (supplied) state (yielded)
  249. stack = (Variant *)p_state->stack.ptr();
  250. call_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  251. line = p_state->line;
  252. ip = p_state->ip;
  253. alloca_size = p_state->stack.size();
  254. script = p_state->script.ptr();
  255. p_instance = p_state->instance;
  256. defarg = p_state->defarg;
  257. self = p_state->self;
  258. //stack[p_state->result_pos]=p_state->result; //assign stack with result
  259. } else {
  260. if (p_argcount != _argument_count) {
  261. if (p_argcount > _argument_count) {
  262. r_err.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  263. r_err.argument = _argument_count;
  264. return Variant();
  265. } else if (p_argcount < _argument_count - _default_arg_count) {
  266. r_err.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  267. r_err.argument = _argument_count - _default_arg_count;
  268. return Variant();
  269. } else {
  270. defarg = _argument_count - p_argcount;
  271. }
  272. }
  273. alloca_size = sizeof(Variant *) * _call_size + sizeof(Variant) * _stack_size;
  274. if (alloca_size) {
  275. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  276. if (_stack_size) {
  277. stack = (Variant *)aptr;
  278. for (int i = 0; i < p_argcount; i++) {
  279. if (!argument_types[i].has_type) {
  280. memnew_placement(&stack[i], Variant(*p_args[i]));
  281. continue;
  282. }
  283. if (!argument_types[i].is_type(*p_args[i], true)) {
  284. r_err.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  285. r_err.argument = i;
  286. r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT;
  287. return Variant();
  288. }
  289. if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
  290. Variant arg = Variant::construct(argument_types[i].builtin_type, &p_args[i], 1, r_err);
  291. memnew_placement(&stack[i], Variant(arg));
  292. } else {
  293. memnew_placement(&stack[i], Variant(*p_args[i]));
  294. }
  295. }
  296. for (int i = p_argcount; i < _stack_size; i++) {
  297. memnew_placement(&stack[i], Variant);
  298. }
  299. } else {
  300. stack = NULL;
  301. }
  302. if (_call_size) {
  303. call_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  304. } else {
  305. call_args = NULL;
  306. }
  307. } else {
  308. stack = NULL;
  309. call_args = NULL;
  310. }
  311. if (p_instance) {
  312. if (p_instance->base_ref && static_cast<Reference *>(p_instance->owner)->is_referenced()) {
  313. self = REF(static_cast<Reference *>(p_instance->owner));
  314. } else {
  315. self = p_instance->owner;
  316. }
  317. script = p_instance->script.ptr();
  318. } else {
  319. script = _script;
  320. }
  321. }
  322. String err_text;
  323. #ifdef DEBUG_ENABLED
  324. if (ScriptDebugger::get_singleton())
  325. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  326. #define GD_ERR_BREAK(m_cond) \
  327. { \
  328. if (unlikely(m_cond)) { \
  329. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  330. OPCODE_BREAK; \
  331. } else \
  332. _err_error_exists = false; \
  333. }
  334. #define CHECK_SPACE(m_space) \
  335. GD_ERR_BREAK((ip + m_space) > _code_size)
  336. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  337. Variant *m_v; \
  338. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, stack, err_text); \
  339. if (unlikely(!m_v)) \
  340. OPCODE_BREAK;
  341. #else
  342. #define GD_ERR_BREAK(m_cond)
  343. #define CHECK_SPACE(m_space)
  344. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  345. Variant *m_v; \
  346. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, stack, err_text);
  347. #endif
  348. #ifdef DEBUG_ENABLED
  349. uint64_t function_start_time = 0;
  350. uint64_t function_call_time = 0;
  351. if (GDScriptLanguage::get_singleton()->profiling) {
  352. function_start_time = OS::get_singleton()->get_ticks_usec();
  353. function_call_time = 0;
  354. profile.call_count++;
  355. profile.frame_call_count++;
  356. }
  357. bool exit_ok = false;
  358. #endif
  359. #ifdef DEBUG_ENABLED
  360. OPCODE_WHILE(ip < _code_size) {
  361. int last_opcode = _code_ptr[ip];
  362. #else
  363. OPCODE_WHILE(true) {
  364. #endif
  365. OPCODE_SWITCH(_code_ptr[ip]) {
  366. OPCODE(OPCODE_OPERATOR) {
  367. CHECK_SPACE(5);
  368. bool valid;
  369. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 1];
  370. GD_ERR_BREAK(op >= Variant::OP_MAX);
  371. GET_VARIANT_PTR(a, 2);
  372. GET_VARIANT_PTR(b, 3);
  373. GET_VARIANT_PTR(dst, 4);
  374. #ifdef DEBUG_ENABLED
  375. Variant ret;
  376. Variant::evaluate(op, *a, *b, ret, valid);
  377. #else
  378. Variant::evaluate(op, *a, *b, *dst, valid);
  379. #endif
  380. #ifdef DEBUG_ENABLED
  381. if (!valid) {
  382. if (ret.get_type() == Variant::STRING) {
  383. //return a string when invalid with the error
  384. err_text = ret;
  385. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  386. } else {
  387. 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) + "'.";
  388. }
  389. OPCODE_BREAK;
  390. }
  391. *dst = ret;
  392. #endif
  393. ip += 5;
  394. }
  395. DISPATCH_OPCODE;
  396. OPCODE(OPCODE_EXTENDS_TEST) {
  397. CHECK_SPACE(4);
  398. GET_VARIANT_PTR(a, 1);
  399. GET_VARIANT_PTR(b, 2);
  400. GET_VARIANT_PTR(dst, 3);
  401. #ifdef DEBUG_ENABLED
  402. if (a->get_type() != Variant::OBJECT || a->operator Object *() == NULL) {
  403. err_text = "Left operand of 'is' is not an instance of anything.";
  404. OPCODE_BREAK;
  405. }
  406. if (b->get_type() != Variant::OBJECT || b->operator Object *() == NULL) {
  407. err_text = "Right operand of 'is' is not a class.";
  408. OPCODE_BREAK;
  409. }
  410. #endif
  411. Object *obj_A = *a;
  412. Object *obj_B = *b;
  413. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  414. bool extends_ok = false;
  415. if (scr_B) {
  416. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  417. //in other situation, this shoul return false.
  418. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  419. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  420. //bool found=false;
  421. while (cmp) {
  422. if (cmp == scr_B) {
  423. //inherits from script, all ok
  424. extends_ok = true;
  425. break;
  426. }
  427. cmp = cmp->_base;
  428. }
  429. }
  430. } else {
  431. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  432. #ifdef DEBUG_ENABLED
  433. if (!nc) {
  434. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  435. OPCODE_BREAK;
  436. }
  437. #endif
  438. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  439. }
  440. *dst = extends_ok;
  441. ip += 4;
  442. }
  443. DISPATCH_OPCODE;
  444. OPCODE(OPCODE_IS_BUILTIN) {
  445. CHECK_SPACE(4);
  446. GET_VARIANT_PTR(value, 1);
  447. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 2];
  448. GET_VARIANT_PTR(dst, 3);
  449. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  450. *dst = value->get_type() == var_type;
  451. ip += 4;
  452. }
  453. DISPATCH_OPCODE;
  454. OPCODE(OPCODE_SET) {
  455. CHECK_SPACE(3);
  456. GET_VARIANT_PTR(dst, 1);
  457. GET_VARIANT_PTR(index, 2);
  458. GET_VARIANT_PTR(value, 3);
  459. bool valid;
  460. dst->set(*index, *value, &valid);
  461. #ifdef DEBUG_ENABLED
  462. if (!valid) {
  463. String v = index->operator String();
  464. if (v != "") {
  465. v = "'" + v + "'";
  466. } else {
  467. v = "of type '" + _get_var_type(index) + "'";
  468. }
  469. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  470. OPCODE_BREAK;
  471. }
  472. #endif
  473. ip += 4;
  474. }
  475. DISPATCH_OPCODE;
  476. OPCODE(OPCODE_GET) {
  477. CHECK_SPACE(3);
  478. GET_VARIANT_PTR(src, 1);
  479. GET_VARIANT_PTR(index, 2);
  480. GET_VARIANT_PTR(dst, 3);
  481. bool valid;
  482. #ifdef DEBUG_ENABLED
  483. //allow better error message in cases where src and dst are the same stack position
  484. Variant ret = src->get(*index, &valid);
  485. #else
  486. *dst = src->get(*index, &valid);
  487. #endif
  488. #ifdef DEBUG_ENABLED
  489. if (!valid) {
  490. String v = index->operator String();
  491. if (v != "") {
  492. v = "'" + v + "'";
  493. } else {
  494. v = "of type '" + _get_var_type(index) + "'";
  495. }
  496. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  497. OPCODE_BREAK;
  498. }
  499. *dst = ret;
  500. #endif
  501. ip += 4;
  502. }
  503. DISPATCH_OPCODE;
  504. OPCODE(OPCODE_SET_NAMED) {
  505. CHECK_SPACE(3);
  506. GET_VARIANT_PTR(dst, 1);
  507. GET_VARIANT_PTR(value, 3);
  508. int indexname = _code_ptr[ip + 2];
  509. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  510. const StringName *index = &_global_names_ptr[indexname];
  511. bool valid;
  512. dst->set_named(*index, *value, &valid);
  513. #ifdef DEBUG_ENABLED
  514. if (!valid) {
  515. String err_type;
  516. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  517. OPCODE_BREAK;
  518. }
  519. #endif
  520. ip += 4;
  521. }
  522. DISPATCH_OPCODE;
  523. OPCODE(OPCODE_GET_NAMED) {
  524. CHECK_SPACE(4);
  525. GET_VARIANT_PTR(src, 1);
  526. GET_VARIANT_PTR(dst, 3);
  527. int indexname = _code_ptr[ip + 2];
  528. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  529. const StringName *index = &_global_names_ptr[indexname];
  530. bool valid;
  531. #ifdef DEBUG_ENABLED
  532. //allow better error message in cases where src and dst are the same stack position
  533. Variant ret = src->get_named(*index, &valid);
  534. #else
  535. *dst = src->get_named(*index, &valid);
  536. #endif
  537. #ifdef DEBUG_ENABLED
  538. if (!valid) {
  539. if (src->has_method(*index)) {
  540. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "'). Did you mean '." + index->operator String() + "()' or funcref(obj, \"" + index->operator String() + "\") ?";
  541. } else {
  542. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  543. }
  544. OPCODE_BREAK;
  545. }
  546. *dst = ret;
  547. #endif
  548. ip += 4;
  549. }
  550. DISPATCH_OPCODE;
  551. OPCODE(OPCODE_SET_MEMBER) {
  552. CHECK_SPACE(3);
  553. int indexname = _code_ptr[ip + 1];
  554. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  555. const StringName *index = &_global_names_ptr[indexname];
  556. GET_VARIANT_PTR(src, 2);
  557. bool valid;
  558. #ifndef DEBUG_ENABLED
  559. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  560. #else
  561. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  562. if (!ok) {
  563. err_text = "Internal error setting property: " + String(*index);
  564. OPCODE_BREAK;
  565. } else if (!valid) {
  566. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  567. OPCODE_BREAK;
  568. }
  569. #endif
  570. ip += 3;
  571. }
  572. DISPATCH_OPCODE;
  573. OPCODE(OPCODE_GET_MEMBER) {
  574. CHECK_SPACE(3);
  575. int indexname = _code_ptr[ip + 1];
  576. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  577. const StringName *index = &_global_names_ptr[indexname];
  578. GET_VARIANT_PTR(dst, 2);
  579. #ifndef DEBUG_ENABLED
  580. ClassDB::get_property(p_instance->owner, *index, *dst);
  581. #else
  582. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  583. if (!ok) {
  584. err_text = "Internal error getting property: " + String(*index);
  585. OPCODE_BREAK;
  586. }
  587. #endif
  588. ip += 3;
  589. }
  590. DISPATCH_OPCODE;
  591. OPCODE(OPCODE_ASSIGN) {
  592. CHECK_SPACE(3);
  593. GET_VARIANT_PTR(dst, 1);
  594. GET_VARIANT_PTR(src, 2);
  595. *dst = *src;
  596. ip += 3;
  597. }
  598. DISPATCH_OPCODE;
  599. OPCODE(OPCODE_ASSIGN_TRUE) {
  600. CHECK_SPACE(2);
  601. GET_VARIANT_PTR(dst, 1);
  602. *dst = true;
  603. ip += 2;
  604. }
  605. DISPATCH_OPCODE;
  606. OPCODE(OPCODE_ASSIGN_FALSE) {
  607. CHECK_SPACE(2);
  608. GET_VARIANT_PTR(dst, 1);
  609. *dst = false;
  610. ip += 2;
  611. }
  612. DISPATCH_OPCODE;
  613. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  614. CHECK_SPACE(4);
  615. GET_VARIANT_PTR(dst, 2);
  616. GET_VARIANT_PTR(src, 3);
  617. #ifdef DEBUG_ENABLED
  618. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 1];
  619. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  620. if (src->get_type() != var_type) {
  621. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  622. Variant::CallError ce;
  623. *dst = Variant::construct(var_type, const_cast<const Variant **>(&src), 1, ce);
  624. } else {
  625. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  626. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  627. OPCODE_BREAK;
  628. }
  629. } else {
  630. #endif // DEBUG_ENABLED
  631. *dst = *src;
  632. #ifdef DEBUG_ENABLED
  633. }
  634. #endif // DEBUG_ENABLED
  635. ip += 4;
  636. }
  637. DISPATCH_OPCODE;
  638. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  639. CHECK_SPACE(4);
  640. GET_VARIANT_PTR(dst, 2);
  641. GET_VARIANT_PTR(src, 3);
  642. #ifdef DEBUG_ENABLED
  643. GET_VARIANT_PTR(type, 1);
  644. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  645. GD_ERR_BREAK(!nc);
  646. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  647. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  648. "' to a variable of type '" + nc->get_name() + "'.";
  649. OPCODE_BREAK;
  650. }
  651. Object *src_obj = src->operator Object *();
  652. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  653. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  654. "' to a variable of type '" + nc->get_name() + "'.";
  655. OPCODE_BREAK;
  656. }
  657. #endif // DEBUG_ENABLED
  658. *dst = *src;
  659. ip += 4;
  660. }
  661. DISPATCH_OPCODE;
  662. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  663. CHECK_SPACE(4);
  664. GET_VARIANT_PTR(dst, 2);
  665. GET_VARIANT_PTR(src, 3);
  666. #ifdef DEBUG_ENABLED
  667. GET_VARIANT_PTR(type, 1);
  668. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  669. GD_ERR_BREAK(!base_type);
  670. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  671. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  672. OPCODE_BREAK;
  673. }
  674. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  675. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  676. if (!scr_inst) {
  677. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  678. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  679. OPCODE_BREAK;
  680. }
  681. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  682. bool valid = false;
  683. while (src_type) {
  684. if (src_type == base_type) {
  685. valid = true;
  686. break;
  687. }
  688. src_type = src_type->get_base_script().ptr();
  689. }
  690. if (!valid) {
  691. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  692. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  693. OPCODE_BREAK;
  694. }
  695. }
  696. #endif // DEBUG_ENABLED
  697. *dst = *src;
  698. ip += 4;
  699. }
  700. DISPATCH_OPCODE;
  701. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  702. CHECK_SPACE(4);
  703. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 1];
  704. GET_VARIANT_PTR(src, 2);
  705. GET_VARIANT_PTR(dst, 3);
  706. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  707. Variant::CallError err;
  708. *dst = Variant::construct(to_type, (const Variant **)&src, 1, err);
  709. #ifdef DEBUG_ENABLED
  710. if (err.error != Variant::CallError::CALL_OK) {
  711. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  712. OPCODE_BREAK;
  713. }
  714. #endif
  715. ip += 4;
  716. }
  717. DISPATCH_OPCODE;
  718. OPCODE(OPCODE_CAST_TO_NATIVE) {
  719. CHECK_SPACE(4);
  720. GET_VARIANT_PTR(to_type, 1);
  721. GET_VARIANT_PTR(src, 2);
  722. GET_VARIANT_PTR(dst, 3);
  723. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  724. GD_ERR_BREAK(!nc);
  725. #ifdef DEBUG_ENABLED
  726. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  727. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  728. OPCODE_BREAK;
  729. }
  730. #endif
  731. Object *src_obj = src->operator Object *();
  732. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  733. *dst = Variant(); // invalid cast, assign NULL
  734. } else {
  735. *dst = *src;
  736. }
  737. ip += 4;
  738. }
  739. DISPATCH_OPCODE;
  740. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  741. CHECK_SPACE(4);
  742. GET_VARIANT_PTR(to_type, 1);
  743. GET_VARIANT_PTR(src, 2);
  744. GET_VARIANT_PTR(dst, 3);
  745. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  746. GD_ERR_BREAK(!base_type);
  747. #ifdef DEBUG_ENABLED
  748. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  749. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  750. OPCODE_BREAK;
  751. }
  752. #endif
  753. bool valid = false;
  754. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  755. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  756. if (scr_inst) {
  757. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  758. while (src_type) {
  759. if (src_type == base_type) {
  760. valid = true;
  761. break;
  762. }
  763. src_type = src_type->get_base_script().ptr();
  764. }
  765. }
  766. }
  767. if (valid) {
  768. *dst = *src; // Valid cast, copy the source object
  769. } else {
  770. *dst = Variant(); // invalid cast, assign NULL
  771. }
  772. ip += 4;
  773. }
  774. DISPATCH_OPCODE;
  775. OPCODE(OPCODE_CONSTRUCT) {
  776. CHECK_SPACE(2);
  777. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  778. int argc = _code_ptr[ip + 2];
  779. CHECK_SPACE(argc + 2);
  780. Variant **argptrs = call_args;
  781. for (int i = 0; i < argc; i++) {
  782. GET_VARIANT_PTR(v, 3 + i);
  783. argptrs[i] = v;
  784. }
  785. GET_VARIANT_PTR(dst, 3 + argc);
  786. Variant::CallError err;
  787. *dst = Variant::construct(t, (const Variant **)argptrs, argc, err);
  788. #ifdef DEBUG_ENABLED
  789. if (err.error != Variant::CallError::CALL_OK) {
  790. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  791. OPCODE_BREAK;
  792. }
  793. #endif
  794. ip += 4 + argc;
  795. //construct a basic type
  796. }
  797. DISPATCH_OPCODE;
  798. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  799. CHECK_SPACE(1);
  800. int argc = _code_ptr[ip + 1];
  801. Array array; //arrays are always shared
  802. array.resize(argc);
  803. CHECK_SPACE(argc + 2);
  804. for (int i = 0; i < argc; i++) {
  805. GET_VARIANT_PTR(v, 2 + i);
  806. array[i] = *v;
  807. }
  808. GET_VARIANT_PTR(dst, 2 + argc);
  809. *dst = array;
  810. ip += 3 + argc;
  811. }
  812. DISPATCH_OPCODE;
  813. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  814. CHECK_SPACE(1);
  815. int argc = _code_ptr[ip + 1];
  816. Dictionary dict; //arrays are always shared
  817. CHECK_SPACE(argc * 2 + 2);
  818. for (int i = 0; i < argc; i++) {
  819. GET_VARIANT_PTR(k, 2 + i * 2 + 0);
  820. GET_VARIANT_PTR(v, 2 + i * 2 + 1);
  821. dict[*k] = *v;
  822. }
  823. GET_VARIANT_PTR(dst, 2 + argc * 2);
  824. *dst = dict;
  825. ip += 3 + argc * 2;
  826. }
  827. DISPATCH_OPCODE;
  828. OPCODE(OPCODE_CALL_RETURN)
  829. OPCODE(OPCODE_CALL) {
  830. CHECK_SPACE(4);
  831. bool call_ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  832. int argc = _code_ptr[ip + 1];
  833. GET_VARIANT_PTR(base, 2);
  834. int nameg = _code_ptr[ip + 3];
  835. GD_ERR_BREAK(nameg < 0 || nameg >= _global_names_count);
  836. const StringName *methodname = &_global_names_ptr[nameg];
  837. GD_ERR_BREAK(argc < 0);
  838. ip += 4;
  839. CHECK_SPACE(argc + 1);
  840. Variant **argptrs = call_args;
  841. for (int i = 0; i < argc; i++) {
  842. GET_VARIANT_PTR(v, i);
  843. argptrs[i] = v;
  844. }
  845. #ifdef DEBUG_ENABLED
  846. uint64_t call_time = 0;
  847. if (GDScriptLanguage::get_singleton()->profiling) {
  848. call_time = OS::get_singleton()->get_ticks_usec();
  849. }
  850. #endif
  851. Variant::CallError err;
  852. if (call_ret) {
  853. GET_VARIANT_PTR(ret, argc);
  854. base->call_ptr(*methodname, (const Variant **)argptrs, argc, ret, err);
  855. } else {
  856. base->call_ptr(*methodname, (const Variant **)argptrs, argc, NULL, err);
  857. }
  858. #ifdef DEBUG_ENABLED
  859. if (GDScriptLanguage::get_singleton()->profiling) {
  860. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  861. }
  862. if (err.error != Variant::CallError::CALL_OK) {
  863. String methodstr = *methodname;
  864. String basestr = _get_var_type(base);
  865. if (methodstr == "call") {
  866. if (argc >= 1) {
  867. methodstr = String(*argptrs[0]) + " (via call)";
  868. if (err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  869. err.argument -= 1;
  870. }
  871. }
  872. } else if (methodstr == "free") {
  873. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  874. if (base->is_ref()) {
  875. err_text = "Attempted to free a reference.";
  876. OPCODE_BREAK;
  877. } else if (base->get_type() == Variant::OBJECT) {
  878. err_text = "Attempted to free a locked object (calling or emitting).";
  879. OPCODE_BREAK;
  880. }
  881. }
  882. }
  883. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  884. OPCODE_BREAK;
  885. }
  886. #endif
  887. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  888. ip += argc + 1;
  889. }
  890. DISPATCH_OPCODE;
  891. OPCODE(OPCODE_CALL_BUILT_IN) {
  892. CHECK_SPACE(4);
  893. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 1]);
  894. int argc = _code_ptr[ip + 2];
  895. GD_ERR_BREAK(argc < 0);
  896. ip += 3;
  897. CHECK_SPACE(argc + 1);
  898. Variant **argptrs = call_args;
  899. for (int i = 0; i < argc; i++) {
  900. GET_VARIANT_PTR(v, i);
  901. argptrs[i] = v;
  902. }
  903. GET_VARIANT_PTR(dst, argc);
  904. Variant::CallError err;
  905. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  906. #ifdef DEBUG_ENABLED
  907. if (err.error != Variant::CallError::CALL_OK) {
  908. String methodstr = GDScriptFunctions::get_func_name(func);
  909. if (dst->get_type() == Variant::STRING) {
  910. //call provided error string
  911. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  912. } else {
  913. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  914. }
  915. OPCODE_BREAK;
  916. }
  917. #endif
  918. ip += argc + 1;
  919. }
  920. DISPATCH_OPCODE;
  921. OPCODE(OPCODE_CALL_SELF) {
  922. OPCODE_BREAK;
  923. }
  924. OPCODE(OPCODE_CALL_SELF_BASE) {
  925. CHECK_SPACE(2);
  926. int self_fun = _code_ptr[ip + 1];
  927. #ifdef DEBUG_ENABLED
  928. if (self_fun < 0 || self_fun >= _global_names_count) {
  929. err_text = "compiler bug, function name not found";
  930. OPCODE_BREAK;
  931. }
  932. #endif
  933. const StringName *methodname = &_global_names_ptr[self_fun];
  934. int argc = _code_ptr[ip + 2];
  935. CHECK_SPACE(2 + argc + 1);
  936. Variant **argptrs = call_args;
  937. for (int i = 0; i < argc; i++) {
  938. GET_VARIANT_PTR(v, i + 3);
  939. argptrs[i] = v;
  940. }
  941. GET_VARIANT_PTR(dst, argc + 3);
  942. const GDScript *gds = _script;
  943. const Map<StringName, GDScriptFunction *>::Element *E = NULL;
  944. while (gds->base.ptr()) {
  945. gds = gds->base.ptr();
  946. E = gds->member_functions.find(*methodname);
  947. if (E)
  948. break;
  949. }
  950. Variant::CallError err;
  951. if (E) {
  952. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  953. } else if (gds->native.ptr()) {
  954. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  955. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  956. if (!mb) {
  957. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  958. } else {
  959. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  960. }
  961. } else {
  962. err.error = Variant::CallError::CALL_OK;
  963. }
  964. } else {
  965. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  966. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  967. } else {
  968. err.error = Variant::CallError::CALL_OK;
  969. }
  970. }
  971. if (err.error != Variant::CallError::CALL_OK) {
  972. String methodstr = *methodname;
  973. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  974. OPCODE_BREAK;
  975. }
  976. ip += 4 + argc;
  977. }
  978. DISPATCH_OPCODE;
  979. OPCODE(OPCODE_YIELD)
  980. OPCODE(OPCODE_YIELD_SIGNAL) {
  981. int ipofs = 1;
  982. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  983. CHECK_SPACE(4);
  984. ipofs += 2;
  985. } else {
  986. CHECK_SPACE(2);
  987. }
  988. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  989. gdfs->function = this;
  990. gdfs->state.stack.resize(alloca_size);
  991. //copy variant stack
  992. for (int i = 0; i < _stack_size; i++) {
  993. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  994. }
  995. gdfs->state.stack_size = _stack_size;
  996. gdfs->state.self = self;
  997. gdfs->state.alloca_size = alloca_size;
  998. gdfs->state.script = Ref<GDScript>(_script);
  999. gdfs->state.ip = ip + ipofs;
  1000. gdfs->state.line = line;
  1001. gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_id() : 0;
  1002. //gdfs->state.result_pos=ip+ipofs-1;
  1003. gdfs->state.defarg = defarg;
  1004. gdfs->state.instance = p_instance;
  1005. gdfs->function = this;
  1006. retvalue = gdfs;
  1007. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  1008. //do the oneshot connect
  1009. GET_VARIANT_PTR(argobj, 1);
  1010. GET_VARIANT_PTR(argname, 2);
  1011. #ifdef DEBUG_ENABLED
  1012. if (argobj->get_type() != Variant::OBJECT) {
  1013. err_text = "First argument of yield() not of type object.";
  1014. OPCODE_BREAK;
  1015. }
  1016. if (argname->get_type() != Variant::STRING) {
  1017. err_text = "Second argument of yield() not a string (for signal name).";
  1018. OPCODE_BREAK;
  1019. }
  1020. #endif
  1021. Object *obj = argobj->operator Object *();
  1022. String signal = argname->operator String();
  1023. #ifdef DEBUG_ENABLED
  1024. if (!obj) {
  1025. err_text = "First argument of yield() is null.";
  1026. OPCODE_BREAK;
  1027. }
  1028. if (ScriptDebugger::get_singleton()) {
  1029. if (!ObjectDB::instance_validate(obj)) {
  1030. err_text = "First argument of yield() is a previously freed instance.";
  1031. OPCODE_BREAK;
  1032. }
  1033. }
  1034. if (signal.length() == 0) {
  1035. err_text = "Second argument of yield() is an empty string (for signal name).";
  1036. OPCODE_BREAK;
  1037. }
  1038. Error err = obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1039. if (err != OK) {
  1040. err_text = "Error connecting to signal: " + signal + " during yield().";
  1041. OPCODE_BREAK;
  1042. }
  1043. #else
  1044. obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1045. #endif
  1046. }
  1047. #ifdef DEBUG_ENABLED
  1048. exit_ok = true;
  1049. #endif
  1050. OPCODE_BREAK;
  1051. }
  1052. OPCODE(OPCODE_YIELD_RESUME) {
  1053. CHECK_SPACE(2);
  1054. #ifdef DEBUG_ENABLED
  1055. if (!p_state) {
  1056. err_text = ("Invalid Resume (bug?)");
  1057. OPCODE_BREAK;
  1058. }
  1059. #endif
  1060. GET_VARIANT_PTR(result, 1);
  1061. *result = p_state->result;
  1062. ip += 2;
  1063. }
  1064. DISPATCH_OPCODE;
  1065. OPCODE(OPCODE_JUMP) {
  1066. CHECK_SPACE(2);
  1067. int to = _code_ptr[ip + 1];
  1068. GD_ERR_BREAK(to < 0 || to > _code_size);
  1069. ip = to;
  1070. }
  1071. DISPATCH_OPCODE;
  1072. OPCODE(OPCODE_JUMP_IF) {
  1073. CHECK_SPACE(3);
  1074. GET_VARIANT_PTR(test, 1);
  1075. bool result = test->booleanize();
  1076. if (result) {
  1077. int to = _code_ptr[ip + 2];
  1078. GD_ERR_BREAK(to < 0 || to > _code_size);
  1079. ip = to;
  1080. } else {
  1081. ip += 3;
  1082. }
  1083. }
  1084. DISPATCH_OPCODE;
  1085. OPCODE(OPCODE_JUMP_IF_NOT) {
  1086. CHECK_SPACE(3);
  1087. GET_VARIANT_PTR(test, 1);
  1088. bool result = test->booleanize();
  1089. if (!result) {
  1090. int to = _code_ptr[ip + 2];
  1091. GD_ERR_BREAK(to < 0 || to > _code_size);
  1092. ip = to;
  1093. } else {
  1094. ip += 3;
  1095. }
  1096. }
  1097. DISPATCH_OPCODE;
  1098. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1099. CHECK_SPACE(2);
  1100. ip = _default_arg_ptr[defarg];
  1101. }
  1102. DISPATCH_OPCODE;
  1103. OPCODE(OPCODE_RETURN) {
  1104. CHECK_SPACE(2);
  1105. GET_VARIANT_PTR(r, 1);
  1106. retvalue = *r;
  1107. #ifdef DEBUG_ENABLED
  1108. exit_ok = true;
  1109. #endif
  1110. OPCODE_BREAK;
  1111. }
  1112. OPCODE(OPCODE_ITERATE_BEGIN) {
  1113. CHECK_SPACE(8); //space for this a regular iterate
  1114. GET_VARIANT_PTR(counter, 1);
  1115. GET_VARIANT_PTR(container, 2);
  1116. bool valid;
  1117. if (!container->iter_init(*counter, valid)) {
  1118. #ifdef DEBUG_ENABLED
  1119. if (!valid) {
  1120. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "'.";
  1121. OPCODE_BREAK;
  1122. }
  1123. #endif
  1124. int jumpto = _code_ptr[ip + 3];
  1125. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1126. ip = jumpto;
  1127. } else {
  1128. GET_VARIANT_PTR(iterator, 4);
  1129. *iterator = container->iter_get(*counter, valid);
  1130. #ifdef DEBUG_ENABLED
  1131. if (!valid) {
  1132. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "'.";
  1133. OPCODE_BREAK;
  1134. }
  1135. #endif
  1136. ip += 5; //skip regular iterate which is always next
  1137. }
  1138. }
  1139. DISPATCH_OPCODE;
  1140. OPCODE(OPCODE_ITERATE) {
  1141. CHECK_SPACE(4);
  1142. GET_VARIANT_PTR(counter, 1);
  1143. GET_VARIANT_PTR(container, 2);
  1144. bool valid;
  1145. if (!container->iter_next(*counter, valid)) {
  1146. #ifdef DEBUG_ENABLED
  1147. if (!valid) {
  1148. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1149. OPCODE_BREAK;
  1150. }
  1151. #endif
  1152. int jumpto = _code_ptr[ip + 3];
  1153. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1154. ip = jumpto;
  1155. } else {
  1156. GET_VARIANT_PTR(iterator, 4);
  1157. *iterator = container->iter_get(*counter, valid);
  1158. #ifdef DEBUG_ENABLED
  1159. if (!valid) {
  1160. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1161. OPCODE_BREAK;
  1162. }
  1163. #endif
  1164. ip += 5; //loop again
  1165. }
  1166. }
  1167. DISPATCH_OPCODE;
  1168. OPCODE(OPCODE_ASSERT) {
  1169. CHECK_SPACE(2);
  1170. #ifdef DEBUG_ENABLED
  1171. GET_VARIANT_PTR(test, 1);
  1172. bool result = test->booleanize();
  1173. if (!result) {
  1174. err_text = "Assertion failed.";
  1175. OPCODE_BREAK;
  1176. }
  1177. #endif
  1178. ip += 2;
  1179. }
  1180. DISPATCH_OPCODE;
  1181. OPCODE(OPCODE_BREAKPOINT) {
  1182. #ifdef DEBUG_ENABLED
  1183. if (ScriptDebugger::get_singleton()) {
  1184. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1185. }
  1186. #endif
  1187. ip += 1;
  1188. }
  1189. DISPATCH_OPCODE;
  1190. OPCODE(OPCODE_LINE) {
  1191. CHECK_SPACE(2);
  1192. line = _code_ptr[ip + 1];
  1193. ip += 2;
  1194. if (ScriptDebugger::get_singleton()) {
  1195. // line
  1196. bool do_break = false;
  1197. if (ScriptDebugger::get_singleton()->get_lines_left() > 0) {
  1198. if (ScriptDebugger::get_singleton()->get_depth() <= 0)
  1199. ScriptDebugger::get_singleton()->set_lines_left(ScriptDebugger::get_singleton()->get_lines_left() - 1);
  1200. if (ScriptDebugger::get_singleton()->get_lines_left() <= 0)
  1201. do_break = true;
  1202. }
  1203. if (ScriptDebugger::get_singleton()->is_breakpoint(line, source))
  1204. do_break = true;
  1205. if (do_break) {
  1206. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1207. }
  1208. ScriptDebugger::get_singleton()->line_poll();
  1209. }
  1210. }
  1211. DISPATCH_OPCODE;
  1212. OPCODE(OPCODE_END) {
  1213. #ifdef DEBUG_ENABLED
  1214. exit_ok = true;
  1215. #endif
  1216. OPCODE_BREAK;
  1217. }
  1218. // Enable for debugging
  1219. #if 0
  1220. default: {
  1221. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1222. OPCODE_BREAK;
  1223. }
  1224. #endif
  1225. }
  1226. OPCODES_END
  1227. #ifdef DEBUG_ENABLED
  1228. if (exit_ok)
  1229. OPCODE_OUT;
  1230. //error
  1231. // function, file, line, error, explanation
  1232. String err_file;
  1233. if (p_instance)
  1234. err_file = p_instance->script->path;
  1235. else if (script)
  1236. err_file = script->path;
  1237. if (err_file == "")
  1238. err_file = "<built-in>";
  1239. String err_func = name;
  1240. if (p_instance && p_instance->script->name != "")
  1241. err_func = p_instance->script->name + "." + err_func;
  1242. int err_line = line;
  1243. if (err_text == "") {
  1244. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1245. }
  1246. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1247. // debugger break did not happen
  1248. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1249. }
  1250. #endif
  1251. OPCODE_OUT;
  1252. }
  1253. OPCODES_OUT
  1254. #ifdef DEBUG_ENABLED
  1255. if (GDScriptLanguage::get_singleton()->profiling) {
  1256. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1257. profile.total_time += time_taken;
  1258. profile.self_time += time_taken - function_call_time;
  1259. profile.frame_total_time += time_taken;
  1260. profile.frame_self_time += time_taken - function_call_time;
  1261. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1262. }
  1263. if (ScriptDebugger::get_singleton())
  1264. GDScriptLanguage::get_singleton()->exit_function();
  1265. #endif
  1266. if (_stack_size) {
  1267. //free stack
  1268. for (int i = 0; i < _stack_size; i++)
  1269. stack[i].~Variant();
  1270. }
  1271. return retvalue;
  1272. }
  1273. const int *GDScriptFunction::get_code() const {
  1274. return _code_ptr;
  1275. }
  1276. int GDScriptFunction::get_code_size() const {
  1277. return _code_size;
  1278. }
  1279. Variant GDScriptFunction::get_constant(int p_idx) const {
  1280. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  1281. return constants[p_idx];
  1282. }
  1283. StringName GDScriptFunction::get_global_name(int p_idx) const {
  1284. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  1285. return global_names[p_idx];
  1286. }
  1287. int GDScriptFunction::get_default_argument_count() const {
  1288. return _default_arg_count;
  1289. }
  1290. int GDScriptFunction::get_default_argument_addr(int p_idx) const {
  1291. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), -1);
  1292. return default_arguments[p_idx];
  1293. }
  1294. GDScriptDataType GDScriptFunction::get_return_type() const {
  1295. return return_type;
  1296. }
  1297. GDScriptDataType GDScriptFunction::get_argument_type(int p_idx) const {
  1298. ERR_FAIL_INDEX_V(p_idx, argument_types.size(), GDScriptDataType());
  1299. return argument_types[p_idx];
  1300. }
  1301. StringName GDScriptFunction::get_name() const {
  1302. return name;
  1303. }
  1304. int GDScriptFunction::get_max_stack_size() const {
  1305. return _stack_size;
  1306. }
  1307. struct _GDFKC {
  1308. int order;
  1309. List<int> pos;
  1310. };
  1311. struct _GDFKCS {
  1312. int order;
  1313. StringName id;
  1314. int pos;
  1315. bool operator<(const _GDFKCS &p_r) const {
  1316. return order < p_r.order;
  1317. }
  1318. };
  1319. void GDScriptFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const {
  1320. int oc = 0;
  1321. Map<StringName, _GDFKC> sdmap;
  1322. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  1323. const StackDebug &sd = E->get();
  1324. if (sd.line > p_line)
  1325. break;
  1326. if (sd.added) {
  1327. if (!sdmap.has(sd.identifier)) {
  1328. _GDFKC d;
  1329. d.order = oc++;
  1330. d.pos.push_back(sd.pos);
  1331. sdmap[sd.identifier] = d;
  1332. } else {
  1333. sdmap[sd.identifier].pos.push_back(sd.pos);
  1334. }
  1335. } else {
  1336. ERR_CONTINUE(!sdmap.has(sd.identifier));
  1337. sdmap[sd.identifier].pos.pop_back();
  1338. if (sdmap[sd.identifier].pos.empty())
  1339. sdmap.erase(sd.identifier);
  1340. }
  1341. }
  1342. List<_GDFKCS> stackpositions;
  1343. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  1344. _GDFKCS spp;
  1345. spp.id = E->key();
  1346. spp.order = E->get().order;
  1347. spp.pos = E->get().pos.back()->get();
  1348. stackpositions.push_back(spp);
  1349. }
  1350. stackpositions.sort();
  1351. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  1352. Pair<StringName, int> p;
  1353. p.first = E->get().id;
  1354. p.second = E->get().pos;
  1355. r_stackvars->push_back(p);
  1356. }
  1357. }
  1358. GDScriptFunction::GDScriptFunction() :
  1359. function_list(this) {
  1360. _stack_size = 0;
  1361. _call_size = 0;
  1362. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1363. name = "<anonymous>";
  1364. #ifdef DEBUG_ENABLED
  1365. _func_cname = NULL;
  1366. if (GDScriptLanguage::get_singleton()->lock) {
  1367. GDScriptLanguage::get_singleton()->lock->lock();
  1368. }
  1369. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1370. if (GDScriptLanguage::get_singleton()->lock) {
  1371. GDScriptLanguage::get_singleton()->lock->unlock();
  1372. }
  1373. profile.call_count = 0;
  1374. profile.self_time = 0;
  1375. profile.total_time = 0;
  1376. profile.frame_call_count = 0;
  1377. profile.frame_self_time = 0;
  1378. profile.frame_total_time = 0;
  1379. profile.last_frame_call_count = 0;
  1380. profile.last_frame_self_time = 0;
  1381. profile.last_frame_total_time = 0;
  1382. #endif
  1383. }
  1384. GDScriptFunction::~GDScriptFunction() {
  1385. #ifdef DEBUG_ENABLED
  1386. if (GDScriptLanguage::get_singleton()->lock) {
  1387. GDScriptLanguage::get_singleton()->lock->lock();
  1388. }
  1389. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1390. if (GDScriptLanguage::get_singleton()->lock) {
  1391. GDScriptLanguage::get_singleton()->lock->unlock();
  1392. }
  1393. #endif
  1394. }
  1395. /////////////////////
  1396. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1397. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1398. #ifdef DEBUG_ENABLED
  1399. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1400. ERR_FAIL_V(Variant());
  1401. #else
  1402. return Variant();
  1403. #endif
  1404. }
  1405. Variant arg;
  1406. r_error.error = Variant::CallError::CALL_OK;
  1407. ERR_FAIL_COND_V(!function, Variant());
  1408. if (p_argcount == 0) {
  1409. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1410. r_error.argument = 1;
  1411. return Variant();
  1412. } else if (p_argcount == 1) {
  1413. //noooneee
  1414. } else if (p_argcount == 2) {
  1415. arg = *p_args[0];
  1416. } else {
  1417. Array extra_args;
  1418. for (int i = 0; i < p_argcount - 1; i++) {
  1419. extra_args.push_back(*p_args[i]);
  1420. }
  1421. arg = extra_args;
  1422. }
  1423. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  1424. if (self.is_null()) {
  1425. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1426. r_error.argument = p_argcount - 1;
  1427. r_error.expected = Variant::OBJECT;
  1428. return Variant();
  1429. }
  1430. state.result = arg;
  1431. Variant ret = function->call(NULL, NULL, 0, r_error, &state);
  1432. bool completed = true;
  1433. // If the return value is a GDScriptFunctionState reference,
  1434. // then the function did yield again after resuming.
  1435. if (ret.is_ref()) {
  1436. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1437. if (gdfs && gdfs->function == function) {
  1438. completed = false;
  1439. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1440. }
  1441. }
  1442. function = NULL; //cleaned up;
  1443. state.result = Variant();
  1444. if (completed) {
  1445. if (first_state.is_valid()) {
  1446. first_state->emit_signal("completed", ret);
  1447. } else {
  1448. emit_signal("completed", ret);
  1449. }
  1450. }
  1451. return ret;
  1452. }
  1453. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  1454. if (function == NULL)
  1455. return false;
  1456. if (p_extended_check) {
  1457. //class instance gone?
  1458. if (state.instance_id && !ObjectDB::get_instance(state.instance_id))
  1459. return false;
  1460. }
  1461. return true;
  1462. }
  1463. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  1464. ERR_FAIL_COND_V(!function, Variant());
  1465. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1466. #ifdef DEBUG_ENABLED
  1467. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1468. ERR_FAIL_V(Variant());
  1469. #else
  1470. return Variant();
  1471. #endif
  1472. }
  1473. state.result = p_arg;
  1474. Variant::CallError err;
  1475. Variant ret = function->call(NULL, NULL, 0, err, &state);
  1476. bool completed = true;
  1477. // If the return value is a GDScriptFunctionState reference,
  1478. // then the function did yield again after resuming.
  1479. if (ret.is_ref()) {
  1480. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1481. if (gdfs && gdfs->function == function) {
  1482. completed = false;
  1483. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1484. }
  1485. }
  1486. function = NULL; //cleaned up;
  1487. state.result = Variant();
  1488. if (completed) {
  1489. if (first_state.is_valid()) {
  1490. first_state->emit_signal("completed", ret);
  1491. } else {
  1492. emit_signal("completed", ret);
  1493. }
  1494. }
  1495. return ret;
  1496. }
  1497. void GDScriptFunctionState::_bind_methods() {
  1498. ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
  1499. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));
  1500. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1501. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1502. }
  1503. GDScriptFunctionState::GDScriptFunctionState() {
  1504. function = NULL;
  1505. }
  1506. GDScriptFunctionState::~GDScriptFunctionState() {
  1507. if (function != NULL) {
  1508. //never called, deinitialize stack
  1509. for (int i = 0; i < state.stack_size; i++) {
  1510. Variant *v = (Variant *)&state.stack[sizeof(Variant) * i];
  1511. v->~Variant();
  1512. }
  1513. }
  1514. }