as_callfunc.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2014 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_callfunc.cpp
  25. //
  26. // These functions handle the actual calling of system functions
  27. //
  28. #include "as_config.h"
  29. #include "as_callfunc.h"
  30. #include "as_scriptengine.h"
  31. #include "as_texts.h"
  32. #include "as_context.h"
  33. BEGIN_AS_NAMESPACE
  34. int DetectCallingConvention(bool isMethod, const asSFuncPtr &ptr, int callConv, void *objForThiscall, asSSystemFunctionInterface *internal)
  35. {
  36. memset(internal, 0, sizeof(asSSystemFunctionInterface));
  37. internal->func = ptr.ptr.f.func;
  38. internal->objForThiscall = 0;
  39. // Was a compatible calling convention specified?
  40. if( internal->func )
  41. {
  42. if( ptr.flag == 1 && callConv != asCALL_GENERIC )
  43. return asWRONG_CALLING_CONV;
  44. else if( ptr.flag == 2 && (callConv == asCALL_GENERIC || callConv == asCALL_THISCALL || callConv == asCALL_THISCALL_ASGLOBAL) )
  45. return asWRONG_CALLING_CONV;
  46. else if( ptr.flag == 3 && !(callConv == asCALL_THISCALL || callConv == asCALL_THISCALL_ASGLOBAL) )
  47. return asWRONG_CALLING_CONV;
  48. }
  49. asDWORD base = callConv;
  50. if( !isMethod )
  51. {
  52. if( base == asCALL_CDECL )
  53. internal->callConv = ICC_CDECL;
  54. else if( base == asCALL_STDCALL )
  55. internal->callConv = ICC_STDCALL;
  56. else if( base == asCALL_THISCALL_ASGLOBAL )
  57. {
  58. if( objForThiscall == 0 )
  59. return asINVALID_ARG;
  60. internal->objForThiscall = objForThiscall;
  61. internal->callConv = ICC_THISCALL;
  62. // This is really a thiscall, so it is necessary to check for virtual method pointers
  63. base = asCALL_THISCALL;
  64. isMethod = true;
  65. }
  66. else if( base == asCALL_GENERIC )
  67. internal->callConv = ICC_GENERIC_FUNC;
  68. else
  69. return asNOT_SUPPORTED;
  70. }
  71. if( isMethod )
  72. {
  73. #ifndef AS_NO_CLASS_METHODS
  74. if( base == asCALL_THISCALL )
  75. {
  76. internal->callConv = ICC_THISCALL;
  77. #ifdef GNU_STYLE_VIRTUAL_METHOD
  78. if( (size_t(ptr.ptr.f.func) & 1) )
  79. internal->callConv = ICC_VIRTUAL_THISCALL;
  80. #endif
  81. internal->baseOffset = ( int )MULTI_BASE_OFFSET(ptr);
  82. #if defined(AS_ARM) && defined(__GNUC__)
  83. // As the least significant bit in func is used to switch to THUMB mode
  84. // on ARM processors, the LSB in the __delta variable is used instead of
  85. // the one in __pfn on ARM processors.
  86. if( (size_t(internal->baseOffset) & 1) )
  87. internal->callConv = ICC_VIRTUAL_THISCALL;
  88. #endif
  89. #ifdef HAVE_VIRTUAL_BASE_OFFSET
  90. // We don't support virtual inheritance
  91. if( VIRTUAL_BASE_OFFSET(ptr) != 0 )
  92. return asNOT_SUPPORTED;
  93. #endif
  94. }
  95. else
  96. #endif
  97. if( base == asCALL_CDECL_OBJLAST )
  98. internal->callConv = ICC_CDECL_OBJLAST;
  99. else if( base == asCALL_CDECL_OBJFIRST )
  100. internal->callConv = ICC_CDECL_OBJFIRST;
  101. else if( base == asCALL_GENERIC )
  102. internal->callConv = ICC_GENERIC_METHOD;
  103. else
  104. return asNOT_SUPPORTED;
  105. }
  106. return 0;
  107. }
  108. // This function should prepare system functions so that it will be faster to call them
  109. int PrepareSystemFunctionGeneric(asCScriptFunction *func, asSSystemFunctionInterface *internal, asCScriptEngine * /*engine*/)
  110. {
  111. asASSERT(internal->callConv == ICC_GENERIC_METHOD || internal->callConv == ICC_GENERIC_FUNC);
  112. // Calculate the size needed for the parameters
  113. internal->paramSize = func->GetSpaceNeededForArguments();
  114. return 0;
  115. }
  116. // This function should prepare system functions so that it will be faster to call them
  117. int PrepareSystemFunction(asCScriptFunction *func, asSSystemFunctionInterface *internal, asCScriptEngine *engine)
  118. {
  119. #ifdef AS_MAX_PORTABILITY
  120. // This should never happen, as when AS_MAX_PORTABILITY is on, all functions
  121. // are asCALL_GENERIC, which are prepared by PrepareSystemFunctionGeneric
  122. asASSERT(false);
  123. #endif
  124. // References are always returned as primitive data
  125. if( func->returnType.IsReference() || func->returnType.IsObjectHandle() )
  126. {
  127. internal->hostReturnInMemory = false;
  128. internal->hostReturnSize = sizeof(void*)/4;
  129. internal->hostReturnFloat = false;
  130. }
  131. // Registered types have special flags that determine how they are returned
  132. else if( func->returnType.IsObject() )
  133. {
  134. asDWORD objType = func->returnType.GetObjectType()->flags;
  135. // Only value types can be returned by value
  136. asASSERT( objType & asOBJ_VALUE );
  137. if( !(objType & (asOBJ_APP_CLASS | asOBJ_APP_PRIMITIVE | asOBJ_APP_FLOAT)) )
  138. {
  139. // If the return is by value then we need to know the true type
  140. engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, func->GetDeclarationStr().AddressOf());
  141. asCString str;
  142. str.Format(TXT_CANNOT_RET_TYPE_s_BY_VAL, func->returnType.GetObjectType()->name.AddressOf());
  143. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  144. engine->ConfigError(asINVALID_CONFIGURATION, 0, 0, 0);
  145. }
  146. else if( objType & asOBJ_APP_CLASS )
  147. {
  148. internal->hostReturnFloat = false;
  149. if( objType & COMPLEX_RETURN_MASK )
  150. {
  151. internal->hostReturnInMemory = true;
  152. internal->hostReturnSize = sizeof(void*)/4;
  153. }
  154. else
  155. {
  156. #ifdef HAS_128_BIT_PRIMITIVES
  157. if( func->returnType.GetSizeInMemoryDWords() > 4 )
  158. #else
  159. if( func->returnType.GetSizeInMemoryDWords() > 2 )
  160. #endif
  161. {
  162. internal->hostReturnInMemory = true;
  163. internal->hostReturnSize = sizeof(void*)/4;
  164. }
  165. else
  166. {
  167. internal->hostReturnInMemory = false;
  168. internal->hostReturnSize = func->returnType.GetSizeInMemoryDWords();
  169. #ifdef SPLIT_OBJS_BY_MEMBER_TYPES
  170. if( func->returnType.GetObjectType()->flags & asOBJ_APP_CLASS_ALLFLOATS )
  171. internal->hostReturnFloat = true;
  172. #endif
  173. }
  174. #ifdef THISCALL_RETURN_SIMPLE_IN_MEMORY
  175. if((internal->callConv == ICC_THISCALL ||
  176. internal->callConv == ICC_VIRTUAL_THISCALL) &&
  177. func->returnType.GetSizeInMemoryDWords() >= THISCALL_RETURN_SIMPLE_IN_MEMORY_MIN_SIZE)
  178. {
  179. internal->hostReturnInMemory = true;
  180. internal->hostReturnSize = sizeof(void*)/4;
  181. }
  182. #endif
  183. #ifdef CDECL_RETURN_SIMPLE_IN_MEMORY
  184. if((internal->callConv == ICC_CDECL ||
  185. internal->callConv == ICC_CDECL_OBJLAST ||
  186. internal->callConv == ICC_CDECL_OBJFIRST) &&
  187. func->returnType.GetSizeInMemoryDWords() >= CDECL_RETURN_SIMPLE_IN_MEMORY_MIN_SIZE)
  188. {
  189. internal->hostReturnInMemory = true;
  190. internal->hostReturnSize = sizeof(void*)/4;
  191. }
  192. #endif
  193. #ifdef STDCALL_RETURN_SIMPLE_IN_MEMORY
  194. if( internal->callConv == ICC_STDCALL &&
  195. func->returnType.GetSizeInMemoryDWords() >= STDCALL_RETURN_SIMPLE_IN_MEMORY_MIN_SIZE)
  196. {
  197. internal->hostReturnInMemory = true;
  198. internal->hostReturnSize = sizeof(void*)/4;
  199. }
  200. #endif
  201. }
  202. #ifdef SPLIT_OBJS_BY_MEMBER_TYPES
  203. // It's not safe to return objects by value because different registers
  204. // will be used depending on the memory layout of the object.
  205. // Ref: http://www.x86-64.org/documentation/abi.pdf
  206. // Ref: http://www.agner.org/optimize/calling_conventions.pdf
  207. // If the application informs that the class should be treated as all integers, then we allow it
  208. if( !internal->hostReturnInMemory &&
  209. !(func->returnType.GetObjectType()->flags & (asOBJ_APP_CLASS_ALLINTS | asOBJ_APP_CLASS_ALLFLOATS)) )
  210. {
  211. engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, func->GetDeclarationStr().AddressOf());
  212. asCString str;
  213. str.Format(TXT_DONT_SUPPORT_RET_TYPE_s_BY_VAL, func->returnType.Format().AddressOf());
  214. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  215. engine->ConfigError(asINVALID_CONFIGURATION, 0, 0, 0);
  216. }
  217. #endif
  218. }
  219. else if( objType & asOBJ_APP_PRIMITIVE )
  220. {
  221. internal->hostReturnInMemory = false;
  222. internal->hostReturnSize = func->returnType.GetSizeInMemoryDWords();
  223. internal->hostReturnFloat = false;
  224. }
  225. else if( objType & asOBJ_APP_FLOAT )
  226. {
  227. internal->hostReturnInMemory = false;
  228. internal->hostReturnSize = func->returnType.GetSizeInMemoryDWords();
  229. internal->hostReturnFloat = true;
  230. }
  231. }
  232. // Primitive types can easily be determined
  233. #ifdef HAS_128_BIT_PRIMITIVES
  234. else if( func->returnType.GetSizeInMemoryDWords() > 4 )
  235. {
  236. // Shouldn't be possible to get here
  237. asASSERT(false);
  238. }
  239. else if( func->returnType.GetSizeInMemoryDWords() == 4 )
  240. {
  241. internal->hostReturnInMemory = false;
  242. internal->hostReturnSize = 4;
  243. internal->hostReturnFloat = false;
  244. }
  245. #else
  246. else if( func->returnType.GetSizeInMemoryDWords() > 2 )
  247. {
  248. // Shouldn't be possible to get here
  249. asASSERT(false);
  250. }
  251. #endif
  252. else if( func->returnType.GetSizeInMemoryDWords() == 2 )
  253. {
  254. internal->hostReturnInMemory = false;
  255. internal->hostReturnSize = 2;
  256. internal->hostReturnFloat = func->returnType.IsEqualExceptConst(asCDataType::CreatePrimitive(ttDouble, true));
  257. }
  258. else if( func->returnType.GetSizeInMemoryDWords() == 1 )
  259. {
  260. internal->hostReturnInMemory = false;
  261. internal->hostReturnSize = 1;
  262. internal->hostReturnFloat = func->returnType.IsEqualExceptConst(asCDataType::CreatePrimitive(ttFloat, true));
  263. }
  264. else
  265. {
  266. internal->hostReturnInMemory = false;
  267. internal->hostReturnSize = 0;
  268. internal->hostReturnFloat = false;
  269. }
  270. // Calculate the size needed for the parameters
  271. internal->paramSize = func->GetSpaceNeededForArguments();
  272. // Verify if the function takes any objects by value
  273. asUINT n;
  274. internal->takesObjByVal = false;
  275. for( n = 0; n < func->parameterTypes.GetLength(); n++ )
  276. {
  277. if( func->parameterTypes[n].IsObject() && !func->parameterTypes[n].IsObjectHandle() && !func->parameterTypes[n].IsReference() )
  278. {
  279. internal->takesObjByVal = true;
  280. // Can't pass objects by value unless the application type is informed
  281. if( !(func->parameterTypes[n].GetObjectType()->flags & (asOBJ_APP_CLASS | asOBJ_APP_PRIMITIVE | asOBJ_APP_FLOAT)) )
  282. {
  283. engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, func->GetDeclarationStr().AddressOf());
  284. asCString str;
  285. str.Format(TXT_CANNOT_PASS_TYPE_s_BY_VAL, func->parameterTypes[n].GetObjectType()->name.AddressOf());
  286. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  287. engine->ConfigError(asINVALID_CONFIGURATION, 0, 0, 0);
  288. }
  289. #ifdef SPLIT_OBJS_BY_MEMBER_TYPES
  290. // It's not safe to pass objects by value because different registers
  291. // will be used depending on the memory layout of the object
  292. // Ref: http://www.x86-64.org/documentation/abi.pdf
  293. // Ref: http://www.agner.org/optimize/calling_conventions.pdf
  294. if(
  295. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  296. !(func->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK) &&
  297. #endif
  298. #ifdef LARGE_OBJS_PASS_BY_REF
  299. func->parameterTypes[n].GetSizeInMemoryDWords() < AS_LARGE_OBJ_MIN_SIZE &&
  300. #endif
  301. !(func->parameterTypes[n].GetObjectType()->flags & (asOBJ_APP_PRIMITIVE | asOBJ_APP_FLOAT | asOBJ_APP_CLASS_ALLINTS | asOBJ_APP_CLASS_ALLFLOATS)) )
  302. {
  303. engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, func->GetDeclarationStr().AddressOf());
  304. asCString str;
  305. str.Format(TXT_DONT_SUPPORT_TYPE_s_BY_VAL, func->parameterTypes[n].GetObjectType()->name.AddressOf());
  306. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  307. engine->ConfigError(asINVALID_CONFIGURATION, 0, 0, 0);
  308. }
  309. #endif
  310. break;
  311. }
  312. }
  313. // Verify if the function has any registered autohandles
  314. internal->hasAutoHandles = false;
  315. for( n = 0; n < internal->paramAutoHandles.GetLength(); n++ )
  316. {
  317. if( internal->paramAutoHandles[n] )
  318. {
  319. internal->hasAutoHandles = true;
  320. break;
  321. }
  322. }
  323. return 0;
  324. }
  325. #ifdef AS_MAX_PORTABILITY
  326. int CallSystemFunction(int id, asCContext *context, void *objectPointer)
  327. {
  328. asCScriptEngine *engine = context->m_engine;
  329. asSSystemFunctionInterface *sysFunc = engine->scriptFunctions[id]->sysFuncIntf;
  330. int callConv = sysFunc->callConv;
  331. if( callConv == ICC_GENERIC_FUNC || callConv == ICC_GENERIC_METHOD )
  332. return context->CallGeneric(id, objectPointer);
  333. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  334. return 0;
  335. }
  336. #else
  337. //
  338. // CallSystemFunctionNative
  339. //
  340. // This function is implemented for each platform where the native calling conventions is supported.
  341. // See the various as_callfunc_xxx.cpp files for their implementation. It is responsible for preparing
  342. // the arguments for the function call, calling the function, and then retrieving the return value.
  343. //
  344. // Parameters:
  345. //
  346. // context - This is the context that can be used to retrieve specific information from the engine
  347. // descr - This is the script function object that holds the information on how to call the function
  348. // obj - This is the object pointer, if the call is for a class method, otherwise it is null
  349. // args - This is the function arguments, which are packed as in AngelScript
  350. // retPointer - This points to a the memory buffer where the return object is to be placed, if the function returns the value in memory rather than in registers
  351. // retQW2 - This output parameter should be used if the function returns a value larger than 64bits in registers
  352. //
  353. // Return value:
  354. //
  355. // The function should return the value that is returned in registers.
  356. //
  357. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &retQW2);
  358. int CallSystemFunction(int id, asCContext *context, void *objectPointer)
  359. {
  360. asCScriptEngine *engine = context->m_engine;
  361. asCScriptFunction *descr = engine->scriptFunctions[id];
  362. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  363. int callConv = sysFunc->callConv;
  364. if( callConv == ICC_GENERIC_FUNC || callConv == ICC_GENERIC_METHOD )
  365. return context->CallGeneric(id, objectPointer);
  366. asQWORD retQW = 0;
  367. asQWORD retQW2 = 0;
  368. asDWORD *args = context->m_regs.stackPointer;
  369. void *retPointer = 0;
  370. void *obj = 0;
  371. int popSize = sysFunc->paramSize;
  372. if( callConv >= ICC_THISCALL )
  373. {
  374. if( sysFunc->objForThiscall )
  375. {
  376. // This class method is being called as if it is a global function
  377. obj = sysFunc->objForThiscall;
  378. asASSERT( objectPointer == 0 );
  379. }
  380. else if( objectPointer )
  381. {
  382. obj = objectPointer;
  383. }
  384. else
  385. {
  386. // The object pointer should be popped from the context stack
  387. popSize += AS_PTR_SIZE;
  388. // Check for null pointer
  389. obj = (void*)*(asPWORD*)(args);
  390. if( obj == 0 )
  391. {
  392. context->SetInternalException(TXT_NULL_POINTER_ACCESS);
  393. return 0;
  394. }
  395. // Add the base offset for multiple inheritance
  396. #if defined(__GNUC__) && defined(AS_ARM)
  397. // On GNUC + ARM the lsb of the offset is used to indicate a virtual function
  398. // and the whole offset is thus shifted one bit left to keep the original
  399. // offset resolution
  400. obj = (void*)(asPWORD(obj) + (sysFunc->baseOffset>>1));
  401. #else
  402. obj = (void*)(asPWORD(obj) + sysFunc->baseOffset);
  403. #endif
  404. // Skip the object pointer
  405. args += AS_PTR_SIZE;
  406. }
  407. }
  408. if( descr->DoesReturnOnStack() )
  409. {
  410. // Get the address of the location for the return value from the stack
  411. retPointer = (void*)*(asPWORD*)(args);
  412. popSize += AS_PTR_SIZE;
  413. args += AS_PTR_SIZE;
  414. // When returning the value on the location allocated by the called
  415. // we shouldn't set the object type in the register
  416. context->m_regs.objectType = 0;
  417. }
  418. else
  419. {
  420. // Set the object type of the reference held in the register
  421. context->m_regs.objectType = descr->returnType.GetObjectType();
  422. }
  423. context->m_callingSystemFunction = descr;
  424. #ifdef AS_NO_EXCEPTIONS
  425. retQW = CallSystemFunctionNative(context, descr, obj, args, sysFunc->hostReturnInMemory ? retPointer : 0, retQW2);
  426. #else
  427. // This try/catch block is to catch potential exception that may
  428. // be thrown by the registered function. The implementation of the
  429. // CallSystemFunctionNative() must make sure not to have any manual
  430. // clean-up after the call to the real function, or that won't be
  431. // executed in case of an exception.
  432. try
  433. {
  434. retQW = CallSystemFunctionNative(context, descr, obj, args, sysFunc->hostReturnInMemory ? retPointer : 0, retQW2);
  435. }
  436. catch(...)
  437. {
  438. // Convert the exception to a script exception so the VM can
  439. // properly report the error to the application and then clean up
  440. context->SetException(TXT_EXCEPTION_CAUGHT);
  441. }
  442. #endif
  443. context->m_callingSystemFunction = 0;
  444. #if defined(COMPLEX_OBJS_PASSED_BY_REF) || defined(AS_LARGE_OBJS_PASSED_BY_REF)
  445. if( sysFunc->takesObjByVal )
  446. {
  447. // Need to free the complex objects passed by value, but that the
  448. // calling convention implicitly passes by reference behind the scene as the
  449. // calling function is the owner of that memory.
  450. // args is pointing to the first real argument as used in CallSystemFunctionNative,
  451. // i.e. hidden arguments such as the object pointer and return address have already
  452. // been skipped.
  453. int spos = 0;
  454. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  455. {
  456. bool needFree = false;
  457. asCDataType &dt = descr->parameterTypes[n];
  458. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  459. if( dt.GetObjectType() && dt.GetObjectType()->flags & COMPLEX_MASK ) needFree = true;
  460. #endif
  461. #ifdef AS_LARGE_OBJS_PASSED_BY_REF
  462. if( dt.GetSizeInMemoryDWords() >= AS_LARGE_OBJ_MIN_SIZE ) needFree = true;
  463. #endif
  464. if( needFree &&
  465. dt.IsObject() &&
  466. !dt.IsObjectHandle() &&
  467. !dt.IsReference() )
  468. {
  469. void *obj = (void*)*(asPWORD*)&args[spos];
  470. spos += AS_PTR_SIZE;
  471. #ifndef AS_CALLEE_DESTROY_OBJ_BY_VAL
  472. // If the called function doesn't destroy objects passed by value we must do so here
  473. asSTypeBehaviour *beh = &dt.GetObjectType()->beh;
  474. if( beh->destruct )
  475. engine->CallObjectMethod(obj, beh->destruct);
  476. #endif
  477. engine->CallFree(obj);
  478. }
  479. else
  480. spos += dt.GetSizeOnStackDWords();
  481. }
  482. }
  483. #endif
  484. // Store the returned value in our stack
  485. if( descr->returnType.IsObject() && !descr->returnType.IsReference() )
  486. {
  487. if( descr->returnType.IsObjectHandle() )
  488. {
  489. #if defined(AS_BIG_ENDIAN) && AS_PTR_SIZE == 1
  490. // Since we're treating the system function as if it is returning a QWORD we are
  491. // actually receiving the value in the high DWORD of retQW.
  492. retQW >>= 32;
  493. #endif
  494. context->m_regs.objectRegister = (void*)(asPWORD)retQW;
  495. if( sysFunc->returnAutoHandle && context->m_regs.objectRegister )
  496. {
  497. asASSERT( !(descr->returnType.GetObjectType()->flags & asOBJ_NOCOUNT) );
  498. engine->CallObjectMethod(context->m_regs.objectRegister, descr->returnType.GetObjectType()->beh.addref);
  499. }
  500. }
  501. else
  502. {
  503. asASSERT( retPointer );
  504. if( !sysFunc->hostReturnInMemory )
  505. {
  506. // Copy the returned value to the pointer sent by the script engine
  507. if( sysFunc->hostReturnSize == 1 )
  508. {
  509. #if defined(AS_BIG_ENDIAN) && AS_PTR_SIZE == 1
  510. // Since we're treating the system function as if it is returning a QWORD we are
  511. // actually receiving the value in the high DWORD of retQW.
  512. retQW >>= 32;
  513. #endif
  514. *(asDWORD*)retPointer = (asDWORD)retQW;
  515. }
  516. else if( sysFunc->hostReturnSize == 2 )
  517. *(asQWORD*)retPointer = retQW;
  518. else if( sysFunc->hostReturnSize == 3 )
  519. {
  520. *(asQWORD*)retPointer = retQW;
  521. *(((asDWORD*)retPointer) + 2) = (asDWORD)retQW2;
  522. }
  523. else // if( sysFunc->hostReturnSize == 4 )
  524. {
  525. *(asQWORD*)retPointer = retQW;
  526. *(((asQWORD*)retPointer) + 1) = retQW2;
  527. }
  528. }
  529. if( context->m_status == asEXECUTION_EXCEPTION )
  530. {
  531. // If the function raised a script exception it really shouldn't have
  532. // initialized the object. However, as it is a soft exception there is
  533. // no way for the application to not return a value, so instead we simply
  534. // destroy it here, to pretend it was never created.
  535. if( descr->returnType.GetObjectType()->beh.destruct )
  536. engine->CallObjectMethod(retPointer, descr->returnType.GetObjectType()->beh.destruct);
  537. }
  538. }
  539. }
  540. else
  541. {
  542. // Store value in value register
  543. if( sysFunc->hostReturnSize == 1 )
  544. {
  545. #if defined(AS_BIG_ENDIAN)
  546. // Since we're treating the system function as if it is returning a QWORD we are
  547. // actually receiving the value in the high DWORD of retQW.
  548. retQW >>= 32;
  549. // Due to endian issues we need to handle return values that are
  550. // less than a DWORD (32 bits) in size specially
  551. int numBytes = descr->returnType.GetSizeInMemoryBytes();
  552. if( descr->returnType.IsReference() ) numBytes = 4;
  553. switch( numBytes )
  554. {
  555. case 1:
  556. {
  557. // 8 bits
  558. asBYTE *val = (asBYTE*)&context->m_regs.valueRegister;
  559. val[0] = (asBYTE)retQW;
  560. val[1] = 0;
  561. val[2] = 0;
  562. val[3] = 0;
  563. val[4] = 0;
  564. val[5] = 0;
  565. val[6] = 0;
  566. val[7] = 0;
  567. }
  568. break;
  569. case 2:
  570. {
  571. // 16 bits
  572. asWORD *val = (asWORD*)&context->m_regs.valueRegister;
  573. val[0] = (asWORD)retQW;
  574. val[1] = 0;
  575. val[2] = 0;
  576. val[3] = 0;
  577. }
  578. break;
  579. default:
  580. {
  581. // 32 bits
  582. asDWORD *val = (asDWORD*)&context->m_regs.valueRegister;
  583. val[0] = (asDWORD)retQW;
  584. val[1] = 0;
  585. }
  586. break;
  587. }
  588. #else
  589. *(asDWORD*)&context->m_regs.valueRegister = (asDWORD)retQW;
  590. #endif
  591. }
  592. else
  593. context->m_regs.valueRegister = retQW;
  594. }
  595. // Release autohandles in the arguments
  596. if( sysFunc->hasAutoHandles )
  597. {
  598. args = context->m_regs.stackPointer;
  599. if( callConv >= ICC_THISCALL && !objectPointer )
  600. args += AS_PTR_SIZE;
  601. int spos = 0;
  602. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  603. {
  604. if( sysFunc->paramAutoHandles[n] && *(asPWORD*)&args[spos] != 0 )
  605. {
  606. // Call the release method on the type
  607. engine->CallObjectMethod((void*)*(asPWORD*)&args[spos], descr->parameterTypes[n].GetObjectType()->beh.release);
  608. *(asPWORD*)&args[spos] = 0;
  609. }
  610. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  611. spos += AS_PTR_SIZE;
  612. else
  613. spos += descr->parameterTypes[n].GetSizeOnStackDWords();
  614. }
  615. }
  616. return popSize;
  617. }
  618. #endif // AS_MAX_PORTABILITY
  619. END_AS_NAMESPACE