gdscript_function.cpp 50 KB

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