as_callfunc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2021 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. andreas@angelcode.com
  22. */
  23. //
  24. // as_callfunc.h
  25. //
  26. // These functions handle the actual calling of system functions
  27. //
  28. #ifndef AS_CALLFUNC_H
  29. #define AS_CALLFUNC_H
  30. #include "as_array.h"
  31. BEGIN_AS_NAMESPACE
  32. class asCContext;
  33. class asCScriptEngine;
  34. class asCScriptFunction;
  35. class asCObjectType;
  36. struct asSSystemFunctionInterface;
  37. int DetectCallingConvention(bool isMethod, const asSFuncPtr &ptr, int callConv, void *auxiliary, asSSystemFunctionInterface *internal);
  38. int PrepareSystemFunctionGeneric(asCScriptFunction *func, asSSystemFunctionInterface *internal, asCScriptEngine *engine);
  39. int PrepareSystemFunction(asCScriptFunction *func, asSSystemFunctionInterface *internal, asCScriptEngine *engine);
  40. int CallSystemFunction(int id, asCContext *context);
  41. inline asPWORD FuncPtrToUInt(asFUNCTION_t func)
  42. {
  43. // A little trickery as the C++ standard doesn't allow direct
  44. // conversion between function pointer and data pointer
  45. union { asFUNCTION_t func; asPWORD idx; } u;
  46. u.func = func;
  47. return u.idx;
  48. }
  49. enum internalCallConv
  50. {
  51. ICC_GENERIC_FUNC,
  52. ICC_GENERIC_FUNC_RETURNINMEM, // never used
  53. ICC_CDECL,
  54. ICC_CDECL_RETURNINMEM,
  55. ICC_STDCALL,
  56. ICC_STDCALL_RETURNINMEM,
  57. ICC_THISCALL,
  58. ICC_THISCALL_RETURNINMEM,
  59. ICC_VIRTUAL_THISCALL,
  60. ICC_VIRTUAL_THISCALL_RETURNINMEM,
  61. ICC_CDECL_OBJLAST,
  62. ICC_CDECL_OBJLAST_RETURNINMEM,
  63. ICC_CDECL_OBJFIRST,
  64. ICC_CDECL_OBJFIRST_RETURNINMEM,
  65. ICC_GENERIC_METHOD,
  66. ICC_GENERIC_METHOD_RETURNINMEM, // never used
  67. ICC_THISCALL_OBJLAST,
  68. ICC_THISCALL_OBJLAST_RETURNINMEM,
  69. ICC_VIRTUAL_THISCALL_OBJLAST,
  70. ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM,
  71. ICC_THISCALL_OBJFIRST,
  72. ICC_THISCALL_OBJFIRST_RETURNINMEM,
  73. ICC_VIRTUAL_THISCALL_OBJFIRST,
  74. ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM
  75. };
  76. struct asSSystemFunctionInterface
  77. {
  78. asFUNCTION_t func;
  79. int baseOffset;
  80. internalCallConv callConv;
  81. bool hostReturnInMemory;
  82. bool hostReturnFloat;
  83. int hostReturnSize;
  84. int paramSize;
  85. bool takesObjByVal;
  86. asCArray<bool> paramAutoHandles; // TODO: Should be able to remove this array. Perhaps the flags can be stored together with the inOutFlags in asCScriptFunction?
  87. bool returnAutoHandle;
  88. int compositeOffset;
  89. bool isCompositeIndirect;
  90. void *auxiliary; // can be used for functors, e.g. by asCALL_THISCALL_ASGLOBAL or asCALL_THISCALL_OBJFIRST
  91. struct SClean
  92. {
  93. asCObjectType *ot; // argument type for clean up
  94. short op; // clean up operation: 0 = release, 1 = free, 2 = destruct then free
  95. short off; // argument offset on the stack
  96. };
  97. asCArray<SClean> cleanArgs;
  98. asSSystemFunctionInterface()
  99. {
  100. Clear();
  101. }
  102. asSSystemFunctionInterface(const asSSystemFunctionInterface &in)
  103. {
  104. *this = in;
  105. }
  106. void Clear()
  107. {
  108. func = 0;
  109. baseOffset = 0;
  110. callConv = ICC_GENERIC_FUNC;
  111. hostReturnInMemory = false;
  112. hostReturnFloat = false;
  113. hostReturnSize = 0;
  114. paramSize = 0;
  115. takesObjByVal = false;
  116. returnAutoHandle = false;
  117. compositeOffset = 0;
  118. isCompositeIndirect = false;
  119. auxiliary = 0;
  120. paramAutoHandles.SetLength(0);
  121. cleanArgs.SetLength(0);
  122. }
  123. asSSystemFunctionInterface &operator=(const asSSystemFunctionInterface &in)
  124. {
  125. func = in.func;
  126. baseOffset = in.baseOffset;
  127. callConv = in.callConv;
  128. hostReturnInMemory = in.hostReturnInMemory;
  129. hostReturnFloat = in.hostReturnFloat;
  130. hostReturnSize = in.hostReturnSize;
  131. paramSize = in.paramSize;
  132. takesObjByVal = in.takesObjByVal;
  133. returnAutoHandle = in.returnAutoHandle;
  134. compositeOffset = in.compositeOffset;
  135. isCompositeIndirect = in.isCompositeIndirect;
  136. auxiliary = in.auxiliary;
  137. cleanArgs = in.cleanArgs;
  138. paramAutoHandles = in.paramAutoHandles;
  139. return *this;
  140. }
  141. };
  142. END_AS_NAMESPACE
  143. #endif