as_callfunc.cpp 23 KB

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