as_scriptfunction.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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_scriptfunction.cpp
  25. //
  26. // A container for a compiled script function
  27. //
  28. #include "as_config.h"
  29. #include "as_scriptfunction.h"
  30. #include "as_tokendef.h"
  31. #include "as_scriptengine.h"
  32. #include "as_callfunc.h"
  33. #include "as_bytecode.h"
  34. #include "as_texts.h"
  35. #include "as_scriptnode.h"
  36. #include "as_builder.h"
  37. #include "as_scriptcode.h"
  38. #include <cstdlib> // qsort
  39. BEGIN_AS_NAMESPACE
  40. #ifdef AS_MAX_PORTABILITY
  41. static void ScriptFunction_AddRef_Generic(asIScriptGeneric *gen)
  42. {
  43. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  44. self->AddRef();
  45. }
  46. static void ScriptFunction_Release_Generic(asIScriptGeneric *gen)
  47. {
  48. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  49. self->Release();
  50. }
  51. static void ScriptFunction_GetRefCount_Generic(asIScriptGeneric *gen)
  52. {
  53. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  54. *(int*)gen->GetAddressOfReturnLocation() = self->GetRefCount();
  55. }
  56. static void ScriptFunction_SetFlag_Generic(asIScriptGeneric *gen)
  57. {
  58. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  59. self->SetFlag();
  60. }
  61. static void ScriptFunction_GetFlag_Generic(asIScriptGeneric *gen)
  62. {
  63. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  64. *(bool*)gen->GetAddressOfReturnLocation() = self->GetFlag();
  65. }
  66. static void ScriptFunction_EnumReferences_Generic(asIScriptGeneric *gen)
  67. {
  68. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  69. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  70. self->EnumReferences(engine);
  71. }
  72. static void ScriptFunction_ReleaseAllHandles_Generic(asIScriptGeneric *gen)
  73. {
  74. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  75. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  76. self->ReleaseAllHandles(engine);
  77. }
  78. static void ScriptFunction_CreateDelegate_Generic(asIScriptGeneric *gen)
  79. {
  80. asCScriptFunction *func = (asCScriptFunction*)gen->GetArgAddress(0);
  81. void *obj = gen->GetArgAddress(1);
  82. gen->SetReturnAddress(CreateDelegate(func, obj));
  83. }
  84. // TODO: 2.29.0: operator==
  85. /*static void ScriptFunction_opEquals_Generic(asIScriptGeneric *gen)
  86. {
  87. asCScriptFunction *funcSelf = (asCScriptFunction*)gen->GetObject();
  88. asCScriptFunction *funcOther = (asCScriptFunction*)gen->GetArgAddress(0);
  89. *(bool*)gen->GetAddressOfReturnLocation() = *funcSelf == *funcOther;
  90. }
  91. */
  92. #endif
  93. void RegisterScriptFunction(asCScriptEngine *engine)
  94. {
  95. // Register the gc behaviours for the script functions
  96. int r = 0;
  97. UNUSED_VAR(r); // It is only used in debug mode
  98. engine->functionBehaviours.engine = engine;
  99. engine->functionBehaviours.flags = asOBJ_REF | asOBJ_GC | asOBJ_SCRIPT_FUNCTION;
  100. engine->functionBehaviours.name = "_builtin_function_";
  101. #ifndef AS_MAX_PORTABILITY
  102. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCScriptFunction,AddRef), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  103. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCScriptFunction,Release), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  104. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCScriptFunction,GetRefCount), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  105. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCScriptFunction,SetFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  106. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCScriptFunction,GetFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  107. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCScriptFunction,EnumReferences), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  108. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCScriptFunction,ReleaseAllHandles), asCALL_THISCALL, 0); asASSERT( r >= 0 );
  109. // TODO: 2.29.0: Need some way to allow the arg type to adapt when the funcdefs are instanciated
  110. // r = engine->RegisterMethodToObjectType(&engine->functionBehaviours, "bool opEquals(const int &in)", asMETHOD(asCScriptFunction,operator==), asCALL_THISCALL); asASSERT( r >= 0 );
  111. #else
  112. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(ScriptFunction_AddRef_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  113. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(ScriptFunction_Release_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  114. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(ScriptFunction_GetRefCount_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  115. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(ScriptFunction_SetFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  116. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(ScriptFunction_GetFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  117. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(ScriptFunction_EnumReferences_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  118. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(ScriptFunction_ReleaseAllHandles_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
  119. // r = engine->RegisterMethodToObjectType(&engine->functionBehaviours, "bool opEquals(const int &in)", asFUNCTION(ScriptFunction_opEquals_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  120. #endif
  121. // Register the builtin function for creating delegates
  122. // This function returns a handle to the delegate, but since the type is not known at this time it is
  123. // registered to return a void then the return type is changed manually to the builtin function type
  124. // The name of the function is an invalid identifier so it cannot be invoked accidentally from the script
  125. #ifndef AS_MAX_PORTABILITY
  126. r = engine->RegisterGlobalFunction("void f(int &in, int &in)", asFUNCTION(CreateDelegate), asCALL_CDECL); asASSERT( r >= 0 );
  127. #else
  128. r = engine->RegisterGlobalFunction("void f(int &in, int &in)", asFUNCTION(ScriptFunction_CreateDelegate_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  129. #endif
  130. // Rename the function so that it cannot be called manually by the script
  131. int idx = engine->registeredGlobalFuncs.GetIndex(engine->scriptFunctions[r]);
  132. engine->registeredGlobalFuncs.Erase(idx);
  133. engine->scriptFunctions[r]->name = DELEGATE_FACTORY;
  134. engine->registeredGlobalFuncs.Put(engine->scriptFunctions[r]);
  135. // Change the return type so the VM will know the function really returns a handle
  136. engine->scriptFunctions[r]->returnType = asCDataType::CreateObject(&engine->functionBehaviours, false);
  137. engine->scriptFunctions[r]->returnType.MakeHandle(true);
  138. }
  139. asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj)
  140. {
  141. if( func == 0 || obj == 0 )
  142. {
  143. // TODO: delegate: Should set script exception
  144. return 0;
  145. }
  146. // Create an instance of a asCScriptFunction with the type asFUNC_DELEGATE
  147. // The delegate shouldn't have a function id and is not added to the engine->scriptFunctions
  148. asCScriptFunction *delegate = asNEW(asCScriptFunction)(static_cast<asCScriptEngine*>(func->GetEngine()), 0, asFUNC_DELEGATE);
  149. if( delegate )
  150. delegate->MakeDelegate(func, obj);
  151. return delegate;
  152. }
  153. // internal
  154. void asCScriptFunction::MakeDelegate(asCScriptFunction *func, void *obj)
  155. {
  156. // Increase the reference of the function and object
  157. func->AddRef();
  158. funcForDelegate = func;
  159. func->GetEngine()->AddRefScriptObject(obj, func->GetObjectType());
  160. objForDelegate = obj;
  161. // The return type and parameters are copied from the delegated method to this object
  162. // TODO: optimize: Do we really need to copy? Whenever requested the delegate can simply return the delegated methods' info directly
  163. parameterTypes = func->parameterTypes;
  164. returnType = func->returnType;
  165. inOutFlags = func->inOutFlags;
  166. // The delegate doesn't own the parameters as it will only forward them to the real method
  167. // so the exception handler must not clean up the parameters for the delegate
  168. dontCleanUpOnException = true;
  169. }
  170. // interface
  171. void *asCScriptFunction::GetDelegateObject() const
  172. {
  173. return objForDelegate;
  174. }
  175. // interface
  176. asIObjectType *asCScriptFunction::GetDelegateObjectType() const
  177. {
  178. if( objForDelegate == 0 || funcForDelegate == 0 )
  179. return 0;
  180. return funcForDelegate->objectType;
  181. }
  182. // interface
  183. asIScriptFunction *asCScriptFunction::GetDelegateFunction() const
  184. {
  185. return funcForDelegate;
  186. }
  187. // TODO: 2.29.0: operator==
  188. /*
  189. // internal
  190. bool asCScriptFunction::operator==(const asCScriptFunction &other) const
  191. {
  192. if( this == &other ) return true;
  193. if( this->funcType == asFUNC_DELEGATE && other.funcType == asFUNC_DELEGATE )
  194. {
  195. if( this->objForDelegate == other.objForDelegate &&
  196. this->funcForDelegate == other.funcForDelegate )
  197. return true;
  198. }
  199. return false;
  200. }
  201. */
  202. // internal
  203. int asCScriptFunction::RegisterListPattern(const char *decl, asCScriptNode *listNodes)
  204. {
  205. if( listNodes == 0 )
  206. return asINVALID_ARG;
  207. // Build the representation of the list pattern from the script nodes
  208. asSListPatternNode *node;
  209. listPattern = asNEW(asSListPatternNode)(asLPT_START);
  210. node = listPattern;
  211. // Recursively parse the child
  212. int r = ParseListPattern(node, decl, listNodes);
  213. node->next = asNEW(asSListPatternNode)(asLPT_END);
  214. return r;
  215. }
  216. // internal
  217. int asCScriptFunction::ParseListPattern(asSListPatternNode *&target, const char *decl, asCScriptNode *listNodes)
  218. {
  219. asSListPatternNode *node = target;
  220. listNodes = listNodes->firstChild;
  221. while( listNodes )
  222. {
  223. if( listNodes->nodeType == snIdentifier )
  224. {
  225. asCString token(&decl[listNodes->tokenPos], listNodes->tokenLength);
  226. if( token == "repeat" )
  227. {
  228. node->next = asNEW(asSListPatternNode)(asLPT_REPEAT);
  229. node = node->next;
  230. }
  231. else if( token == "repeat_same" )
  232. {
  233. // TODO: list: Should make sure this is a sub-list
  234. node->next = asNEW(asSListPatternNode)(asLPT_REPEAT_SAME);
  235. node = node->next;
  236. }
  237. else
  238. {
  239. // Shouldn't happen as the parser already reported the error
  240. asASSERT(false);
  241. }
  242. }
  243. else if( listNodes->nodeType == snDataType )
  244. {
  245. asCDataType dt;
  246. asCBuilder builder(engine, 0);
  247. asCScriptCode code;
  248. code.SetCode("", decl, 0, false);
  249. dt = builder.CreateDataTypeFromNode(listNodes, &code, engine->defaultNamespace, false, returnType.GetObjectType());
  250. node->next = asNEW(asSListPatternDataTypeNode)(dt);
  251. node = node->next;
  252. }
  253. else if( listNodes->nodeType == snListPattern )
  254. {
  255. node->next = asNEW(asSListPatternNode)(asLPT_START);
  256. node = node->next;
  257. // Recursively parse the child
  258. int r = ParseListPattern(node, decl, listNodes);
  259. if( r < 0 )
  260. return r;
  261. node->next = asNEW(asSListPatternNode)(asLPT_END);
  262. node = node->next;
  263. }
  264. else
  265. {
  266. // Unexpected token in the list, the parser shouldn't have allowed
  267. asASSERT( false );
  268. return -1;
  269. }
  270. listNodes = listNodes->next;
  271. }
  272. target = node;
  273. return 0;
  274. }
  275. // internal
  276. asCScriptFunction::asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType _funcType)
  277. {
  278. refCount.set(1);
  279. this->engine = engine;
  280. this->scriptData = 0;
  281. funcType = _funcType;
  282. module = mod;
  283. objectType = 0;
  284. name = "";
  285. isReadOnly = false;
  286. isPrivate = false;
  287. isFinal = false;
  288. isOverride = false;
  289. sysFuncIntf = 0;
  290. signatureId = 0;
  291. dontCleanUpOnException = false;
  292. vfTableIdx = -1;
  293. gcFlag = false;
  294. userData = 0;
  295. id = 0;
  296. accessMask = 0xFFFFFFFF;
  297. isShared = false;
  298. nameSpace = engine->nameSpaces[0];
  299. objForDelegate = 0;
  300. funcForDelegate = 0;
  301. listPattern = 0;
  302. if( funcType == asFUNC_SCRIPT )
  303. AllocateScriptFunctionData();
  304. // Notify the GC of script functions
  305. if( (funcType == asFUNC_SCRIPT && mod == 0) || (funcType == asFUNC_DELEGATE) )
  306. engine->gc.AddScriptObjectToGC(this, &engine->functionBehaviours);
  307. }
  308. void asCScriptFunction::AllocateScriptFunctionData()
  309. {
  310. if( scriptData ) return;
  311. scriptData = asNEW(ScriptFunctionData);
  312. scriptData->stackNeeded = 0;
  313. scriptData->variableSpace = 0;
  314. scriptData->scriptSectionIdx = -1;
  315. scriptData->declaredAt = 0;
  316. scriptData->jitFunction = 0;
  317. }
  318. void asCScriptFunction::DeallocateScriptFunctionData()
  319. {
  320. if( !scriptData ) return;
  321. for( asUINT n = 0; n < scriptData->variables.GetLength(); n++ )
  322. asDELETE(scriptData->variables[n],asSScriptVariable);
  323. scriptData->variables.SetLength(0);
  324. asDELETE(scriptData, ScriptFunctionData);
  325. scriptData = 0;
  326. }
  327. // internal
  328. asCScriptFunction::~asCScriptFunction()
  329. {
  330. // Imported functions are not reference counted, nor are dummy
  331. // functions that are allocated on the stack
  332. asASSERT( funcType == asFUNC_DUMMY ||
  333. funcType == asFUNC_IMPORTED ||
  334. refCount.get() == 0 );
  335. // If the engine pointer is 0, then DestroyInternal has already been called and there is nothing more to do
  336. if( engine == 0 ) return;
  337. DestroyInternal();
  338. // Tell engine to free the function id. This will make it impossible to
  339. // refer to the function by id. Where this is done, it is quite possible
  340. // they will leak.
  341. if( funcType != -1 && funcType != asFUNC_IMPORTED && id )
  342. engine->FreeScriptFunctionId(id);
  343. id = 0;
  344. // Finally set the engine pointer to 0 because it must not be accessed again
  345. engine = 0;
  346. }
  347. // internal
  348. void asCScriptFunction::DestroyInternal()
  349. {
  350. // Clean up user data
  351. for( asUINT n = 0; n < userData.GetLength(); n += 2 )
  352. {
  353. if( userData[n+1] )
  354. {
  355. for( asUINT c = 0; c < engine->cleanFunctionFuncs.GetLength(); c++ )
  356. if( engine->cleanFunctionFuncs[c].type == userData[n] )
  357. engine->cleanFunctionFuncs[c].cleanFunc(this);
  358. }
  359. }
  360. userData.SetLength(0);
  361. // Release all references the function holds to other objects
  362. ReleaseReferences();
  363. parameterTypes.SetLength(0);
  364. returnType = asCDataType::CreatePrimitive(ttVoid, false);
  365. for( asUINT p = 0; p < defaultArgs.GetLength(); p++ )
  366. if( defaultArgs[p] )
  367. asDELETE(defaultArgs[p], asCString);
  368. defaultArgs.SetLength(0);
  369. if( sysFuncIntf )
  370. asDELETE(sysFuncIntf,asSSystemFunctionInterface);
  371. sysFuncIntf = 0;
  372. DeallocateScriptFunctionData();
  373. // Deallocate list pattern data
  374. while( listPattern )
  375. {
  376. asSListPatternNode *n = listPattern->next;
  377. asDELETE(listPattern, asSListPatternNode);
  378. listPattern = n;
  379. }
  380. }
  381. // interface
  382. int asCScriptFunction::GetId() const
  383. {
  384. return id;
  385. }
  386. // interface
  387. int asCScriptFunction::AddRef() const
  388. {
  389. gcFlag = false;
  390. asASSERT( funcType != asFUNC_IMPORTED );
  391. return refCount.atomicInc();
  392. }
  393. // interface
  394. int asCScriptFunction::Release() const
  395. {
  396. gcFlag = false;
  397. asASSERT( funcType != asFUNC_IMPORTED );
  398. int r = refCount.atomicDec();
  399. if( r == 0 &&
  400. funcType != asFUNC_FUNCDEF && // Funcdefs are treated as object types and will be deleted by ClearUnusedTypes()
  401. funcType != asFUNC_DUMMY ) // Dummy functions are allocated on the stack and cannot be deleted
  402. asDELETE(const_cast<asCScriptFunction*>(this),asCScriptFunction);
  403. return r;
  404. }
  405. // internal
  406. void asCScriptFunction::Orphan(asIScriptModule *mod)
  407. {
  408. if( mod && module == mod )
  409. {
  410. module = 0;
  411. if( funcType == asFUNC_SCRIPT && refCount.get() > 1 )
  412. {
  413. // This function is being orphaned, so notify the GC so it can check for circular references
  414. engine->gc.AddScriptObjectToGC(this, &engine->functionBehaviours);
  415. }
  416. }
  417. Release();
  418. }
  419. // interface
  420. int asCScriptFunction::GetTypeId() const
  421. {
  422. // This const cast is ok, the object won't be modified
  423. asCDataType dt = asCDataType::CreateFuncDef(const_cast<asCScriptFunction*>(this));
  424. return engine->GetTypeIdFromDataType(dt);
  425. }
  426. // interface
  427. bool asCScriptFunction::IsCompatibleWithTypeId(int typeId) const
  428. {
  429. asCDataType dt = engine->GetDataTypeFromTypeId(typeId);
  430. // Make sure the type is a function
  431. asCScriptFunction *func = dt.GetFuncDef();
  432. if( func == 0 )
  433. return false;
  434. if( !IsSignatureExceptNameEqual(func) )
  435. return false;
  436. // If this is a class method, then only return true if the object type is the same
  437. if( objectType != func->objectType )
  438. return false;
  439. return true;
  440. }
  441. // interface
  442. const char *asCScriptFunction::GetModuleName() const
  443. {
  444. if( module )
  445. {
  446. return module->name.AddressOf();
  447. }
  448. return 0;
  449. }
  450. // interface
  451. asIScriptModule *asCScriptFunction::GetModule() const
  452. {
  453. return module;
  454. }
  455. // interface
  456. asIObjectType *asCScriptFunction::GetObjectType() const
  457. {
  458. return objectType;
  459. }
  460. // interface
  461. const char *asCScriptFunction::GetObjectName() const
  462. {
  463. if( objectType )
  464. return objectType->GetName();
  465. return 0;
  466. }
  467. // interface
  468. const char *asCScriptFunction::GetName() const
  469. {
  470. return name.AddressOf();
  471. }
  472. // interface
  473. const char *asCScriptFunction::GetNamespace() const
  474. {
  475. return nameSpace->name.AddressOf();
  476. }
  477. // interface
  478. bool asCScriptFunction::IsReadOnly() const
  479. {
  480. return isReadOnly;
  481. }
  482. // interface
  483. bool asCScriptFunction::IsPrivate() const
  484. {
  485. return isPrivate;
  486. }
  487. // internal
  488. int asCScriptFunction::GetSpaceNeededForArguments()
  489. {
  490. // We need to check the size for each type
  491. int s = 0;
  492. for( asUINT n = 0; n < parameterTypes.GetLength(); n++ )
  493. s += parameterTypes[n].GetSizeOnStackDWords();
  494. return s;
  495. }
  496. // internal
  497. int asCScriptFunction::GetSpaceNeededForReturnValue()
  498. {
  499. return returnType.GetSizeOnStackDWords();
  500. }
  501. // internal
  502. bool asCScriptFunction::DoesReturnOnStack() const
  503. {
  504. if( returnType.GetObjectType() &&
  505. (returnType.GetObjectType()->flags & asOBJ_VALUE) &&
  506. !returnType.IsReference() )
  507. return true;
  508. return false;
  509. }
  510. // internal
  511. asCString asCScriptFunction::GetDeclarationStr(bool includeObjectName, bool includeNamespace, bool includeParamNames) const
  512. {
  513. asCString str;
  514. // TODO: default arg: Make the declaration with the default args an option
  515. // Don't add the return type for constructors and destructors
  516. if( !(returnType.GetTokenType() == ttVoid &&
  517. objectType &&
  518. (name == objectType->name || (name.GetLength() > 0 && name[0] == '~') ||
  519. name == "_beh_0_" || name == "_beh_2_")) )
  520. {
  521. str = returnType.Format();
  522. str += " ";
  523. }
  524. if( objectType && includeObjectName )
  525. {
  526. if( includeNamespace )
  527. str += objectType->nameSpace->name + "::";
  528. if( objectType->name != "" )
  529. str += objectType->name + "::";
  530. else
  531. str += "_unnamed_type_::";
  532. }
  533. else if( includeNamespace )
  534. {
  535. str += nameSpace->name + "::";
  536. }
  537. if( name == "" )
  538. str += "_unnamed_function_(";
  539. else if( name.SubString(0,5) == "_beh_" && name.GetLength() == 7 )
  540. {
  541. if( name[5] == '0' + asBEHAVE_CONSTRUCT )
  542. str += objectType->name + "(";
  543. else if( name[5] == '0' + asBEHAVE_FACTORY )
  544. str += returnType.GetObjectType()->name + "(";
  545. else if( name[5] == '0' + asBEHAVE_DESTRUCT )
  546. str += "~" + objectType->name + "(";
  547. else
  548. str += name + "(";
  549. }
  550. else
  551. str += name + "(";
  552. if( parameterTypes.GetLength() > 0 )
  553. {
  554. asUINT n;
  555. for( n = 0; n < parameterTypes.GetLength() - 1; n++ )
  556. {
  557. str += parameterTypes[n].Format();
  558. if( parameterTypes[n].IsReference() && inOutFlags.GetLength() > n )
  559. {
  560. if( inOutFlags[n] == asTM_INREF ) str += "in";
  561. else if( inOutFlags[n] == asTM_OUTREF ) str += "out";
  562. else if( inOutFlags[n] == asTM_INOUTREF ) str += "inout";
  563. }
  564. if( includeParamNames && n < parameterNames.GetLength() && parameterNames[n].GetLength() != 0 )
  565. {
  566. str += " ";
  567. str += parameterNames[n];
  568. }
  569. if( defaultArgs.GetLength() > n && defaultArgs[n] )
  570. {
  571. asCString tmp;
  572. tmp.Format(" = %s", defaultArgs[n]->AddressOf());
  573. str += tmp;
  574. }
  575. str += ", ";
  576. }
  577. // Add the last parameter
  578. str += parameterTypes[n].Format();
  579. if( parameterTypes[n].IsReference() && inOutFlags.GetLength() > n )
  580. {
  581. if( inOutFlags[n] == asTM_INREF ) str += "in";
  582. else if( inOutFlags[n] == asTM_OUTREF ) str += "out";
  583. else if( inOutFlags[n] == asTM_INOUTREF ) str += "inout";
  584. }
  585. if( includeParamNames && n < parameterNames.GetLength() && parameterNames[n].GetLength() != 0 )
  586. {
  587. str += " ";
  588. str += parameterNames[n];
  589. }
  590. if( defaultArgs.GetLength() > n && defaultArgs[n] )
  591. {
  592. asCString tmp;
  593. tmp.Format(" = %s", defaultArgs[n]->AddressOf());
  594. str += tmp;
  595. }
  596. }
  597. str += ")";
  598. if( isReadOnly )
  599. str += " const";
  600. // Add the declaration of the list pattern
  601. if( listPattern )
  602. {
  603. asSListPatternNode *n = listPattern;
  604. bool first = true;
  605. while( n )
  606. {
  607. if( n->type == asLPT_START )
  608. {
  609. str += " {";
  610. first = true;
  611. }
  612. else if( n->type == asLPT_END )
  613. {
  614. str += " }";
  615. first = false;
  616. }
  617. else if( n->type == asLPT_REPEAT )
  618. str += " repeat";
  619. else if( n->type == asLPT_REPEAT_SAME )
  620. str += " repeat_same";
  621. else if( n->type == asLPT_TYPE )
  622. {
  623. if( first )
  624. {
  625. str += " ";
  626. first = false;
  627. }
  628. else
  629. str += ", ";
  630. str += reinterpret_cast<asSListPatternDataTypeNode*>(n)->dataType.Format();
  631. }
  632. n = n->next;
  633. }
  634. }
  635. return str;
  636. }
  637. // interface
  638. int asCScriptFunction::FindNextLineWithCode(int line) const
  639. {
  640. if( scriptData == 0 ) return -1;
  641. if( scriptData->lineNumbers.GetLength() == 0 ) return -1;
  642. // The line numbers for constructors are not in order due to the way
  643. // class members can be initialized directly in the declaration
  644. if( objectType && objectType->name == name )
  645. {
  646. // Sort all line numbers before looking for the next
  647. asCArray<int> lineNbrs;
  648. for( asUINT n = 1; n < scriptData->lineNumbers.GetLength(); n += 2 )
  649. lineNbrs.PushLast(scriptData->lineNumbers[n]&0xFFFFF);
  650. struct C
  651. {
  652. static int cmp(const void *a, const void *b) { return *(int*)a - *(int*)b; }
  653. };
  654. std::qsort(&lineNbrs[0], lineNbrs.GetLength(), sizeof(int), C::cmp);
  655. if( line < lineNbrs[0] && line < (scriptData->declaredAt&0xFFFFF)) return -1;
  656. if( line > lineNbrs[lineNbrs.GetLength()-1] ) return -1;
  657. // Find the line with code on or right after the input line
  658. // TODO: optimize: Do binary search
  659. for( asUINT n = 0; n < lineNbrs.GetLength(); n++ )
  660. if( line <= lineNbrs[n] )
  661. return lineNbrs[n];
  662. }
  663. else
  664. {
  665. // Check if given line is outside function
  666. if( line < (scriptData->declaredAt&0xFFFFF) ) return -1;
  667. if( line > (scriptData->lineNumbers[scriptData->lineNumbers.GetLength()-1]&0xFFFFF) ) return -1;
  668. // Find the line with code on or right after the input line
  669. // TODO: optimize: Do binary search instead
  670. for( asUINT n = 1; n < scriptData->lineNumbers.GetLength(); n += 2 )
  671. {
  672. if( line <= (scriptData->lineNumbers[n]&0xFFFFF) )
  673. return (scriptData->lineNumbers[n]&0xFFFFF);
  674. }
  675. }
  676. return -1;
  677. }
  678. // internal
  679. int asCScriptFunction::GetLineNumber(int programPosition, int *sectionIdx)
  680. {
  681. asASSERT( scriptData );
  682. if( sectionIdx ) *sectionIdx = scriptData->scriptSectionIdx;
  683. if( scriptData->lineNumbers.GetLength() == 0 ) return 0;
  684. if( sectionIdx )
  685. {
  686. // Find the correct section index if the function is compiled from multiple sections
  687. // This array will be empty most of the time so we don't need a sofisticated algorithm to search it
  688. for( asUINT n = 0; n < scriptData->sectionIdxs.GetLength(); n += 2 )
  689. {
  690. if( scriptData->sectionIdxs[n] <= programPosition )
  691. *sectionIdx = scriptData->sectionIdxs[n+1];
  692. }
  693. }
  694. // Do a binary search in the buffer
  695. int max = (int)scriptData->lineNumbers.GetLength()/2 - 1;
  696. int min = 0;
  697. int i = max/2;
  698. for(;;)
  699. {
  700. if( scriptData->lineNumbers[i*2] < programPosition )
  701. {
  702. // Have we found the largest number < programPosition?
  703. if( max == i ) return scriptData->lineNumbers[i*2+1];
  704. if( scriptData->lineNumbers[i*2+2] > programPosition ) return scriptData->lineNumbers[i*2+1];
  705. min = i + 1;
  706. i = (max + min)/2;
  707. }
  708. else if( scriptData->lineNumbers[i*2] > programPosition )
  709. {
  710. // Have we found the smallest number > programPosition?
  711. if( min == i ) return scriptData->lineNumbers[i*2+1];
  712. max = i - 1;
  713. i = (max + min)/2;
  714. }
  715. else
  716. {
  717. // We found the exact position
  718. return scriptData->lineNumbers[i*2+1];
  719. }
  720. }
  721. }
  722. // interface
  723. asEFuncType asCScriptFunction::GetFuncType() const
  724. {
  725. return funcType;
  726. }
  727. // interface
  728. asUINT asCScriptFunction::GetVarCount() const
  729. {
  730. if( scriptData )
  731. return asUINT(scriptData->variables.GetLength());
  732. return 0;
  733. }
  734. // interface
  735. int asCScriptFunction::GetVar(asUINT index, const char **name, int *typeId) const
  736. {
  737. if( scriptData == 0 )
  738. return asNOT_SUPPORTED;
  739. if( index >= scriptData->variables.GetLength() )
  740. return asINVALID_ARG;
  741. if( name )
  742. *name = scriptData->variables[index]->name.AddressOf();
  743. if( typeId )
  744. *typeId = engine->GetTypeIdFromDataType(scriptData->variables[index]->type);
  745. return asSUCCESS;
  746. }
  747. // interface
  748. const char *asCScriptFunction::GetVarDecl(asUINT index, bool includeNamespace) const
  749. {
  750. if( scriptData == 0 || index >= scriptData->variables.GetLength() )
  751. return 0;
  752. asCString *tempString = &asCThreadManager::GetLocalData()->string;
  753. *tempString = scriptData->variables[index]->type.Format(includeNamespace);
  754. *tempString += " " + scriptData->variables[index]->name;
  755. return tempString->AddressOf();
  756. }
  757. // internal
  758. void asCScriptFunction::AddVariable(asCString &name, asCDataType &type, int stackOffset)
  759. {
  760. asASSERT( scriptData );
  761. asSScriptVariable *var = asNEW(asSScriptVariable);
  762. if( var == 0 )
  763. {
  764. // Out of memory
  765. return;
  766. }
  767. var->name = name;
  768. var->type = type;
  769. var->stackOffset = stackOffset;
  770. var->declaredAtProgramPos = 0;
  771. scriptData->variables.PushLast(var);
  772. }
  773. // internal
  774. asCObjectType *asCScriptFunction::GetObjectTypeOfLocalVar(short varOffset)
  775. {
  776. asASSERT( scriptData );
  777. for( asUINT n = 0; n < scriptData->objVariablePos.GetLength(); n++ )
  778. {
  779. if( scriptData->objVariablePos[n] == varOffset )
  780. return scriptData->objVariableTypes[n];
  781. }
  782. return 0;
  783. }
  784. // internal
  785. void asCScriptFunction::ComputeSignatureId()
  786. {
  787. // This function will compute the signatureId based on the
  788. // function name, return type, and parameter types. The object
  789. // type for methods is not used, so that class methods and
  790. // interface methods match each other.
  791. for( asUINT n = 0; n < engine->signatureIds.GetLength(); n++ )
  792. {
  793. if( !IsSignatureEqual(engine->signatureIds[n]) ) continue;
  794. // We don't need to increment the reference counter here, because
  795. // asCScriptEngine::FreeScriptFunctionId will maintain the signature
  796. // id as the function is freed.
  797. signatureId = engine->signatureIds[n]->signatureId;
  798. return;
  799. }
  800. signatureId = id;
  801. engine->signatureIds.PushLast(this);
  802. }
  803. // internal
  804. bool asCScriptFunction::IsSignatureEqual(const asCScriptFunction *func) const
  805. {
  806. if( !IsSignatureExceptNameEqual(func) || name != func->name ) return false;
  807. return true;
  808. }
  809. // internal
  810. bool asCScriptFunction::IsSignatureExceptNameEqual(const asCScriptFunction *func) const
  811. {
  812. return IsSignatureExceptNameEqual(func->returnType, func->parameterTypes, func->inOutFlags, func->objectType, func->isReadOnly);
  813. }
  814. // internal
  815. bool asCScriptFunction::IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &paramInOut, const asCObjectType *objType, bool readOnly) const
  816. {
  817. if( this->returnType != retType ) return false;
  818. return IsSignatureExceptNameAndReturnTypeEqual(paramTypes, paramInOut, objType, readOnly);
  819. }
  820. // internal
  821. bool asCScriptFunction::IsSignatureExceptNameAndObjectTypeEqual(const asCScriptFunction *func) const
  822. {
  823. return IsSignatureExceptNameEqual(func->returnType, func->parameterTypes, func->inOutFlags, objectType, isReadOnly);
  824. }
  825. // internal
  826. bool asCScriptFunction::IsSignatureExceptNameAndReturnTypeEqual(const asCScriptFunction *func) const
  827. {
  828. return IsSignatureExceptNameAndReturnTypeEqual(func->parameterTypes, func->inOutFlags, func->objectType, func->isReadOnly);
  829. }
  830. // internal
  831. bool asCScriptFunction::IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &paramInOut, const asCObjectType *objType, bool readOnly) const
  832. {
  833. if( this->isReadOnly != readOnly ) return false;
  834. if( this->inOutFlags != paramInOut ) return false;
  835. if( this->parameterTypes != paramTypes ) return false;
  836. if( (this->objectType != 0) != (objType != 0) ) return false;
  837. return true;
  838. }
  839. // internal
  840. void asCScriptFunction::AddReferences()
  841. {
  842. // This array will be used to make sure we only add the reference to the same resource once
  843. // This is especially important for global variables, as it expects the initialization function
  844. // to hold only one reference to the variable. However, if the variable is initialized through
  845. // the default constructor followed by the assignment operator we will have two references to
  846. // the variable in the function.
  847. asCArray<void*> ptrs;
  848. // Only count references if there is any bytecode
  849. if( scriptData && scriptData->byteCode.GetLength() )
  850. {
  851. if( returnType.IsObject() )
  852. returnType.GetObjectType()->AddRef();
  853. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  854. if( parameterTypes[p].IsObject() )
  855. parameterTypes[p].GetObjectType()->AddRef();
  856. for( asUINT v = 0; v < scriptData->objVariableTypes.GetLength(); v++ )
  857. if( scriptData->objVariableTypes[v] ) // The null handle is also stored, but it doesn't have an object type
  858. scriptData->objVariableTypes[v]->AddRef();
  859. // Go through the byte code and add references to all resources used by the function
  860. asCArray<asDWORD> &bc = scriptData->byteCode;
  861. for( asUINT n = 0; n < bc.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&bc[n]].type] )
  862. {
  863. switch( *(asBYTE*)&bc[n] )
  864. {
  865. // Object types
  866. case asBC_OBJTYPE:
  867. case asBC_FREE:
  868. case asBC_REFCPY:
  869. case asBC_RefCpyV:
  870. {
  871. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
  872. objType->AddRef();
  873. }
  874. break;
  875. // Object type and function
  876. case asBC_ALLOC:
  877. {
  878. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
  879. objType->AddRef();
  880. int func = asBC_INTARG(&bc[n]+AS_PTR_SIZE);
  881. if( func )
  882. engine->scriptFunctions[func]->AddRef();
  883. }
  884. break;
  885. // Global variables
  886. case asBC_PGA:
  887. case asBC_PshGPtr:
  888. case asBC_LDG:
  889. case asBC_PshG4:
  890. case asBC_LdGRdR4:
  891. case asBC_CpyGtoV4:
  892. case asBC_CpyVtoG4:
  893. case asBC_SetG4:
  894. // Need to increase the reference for each global variable
  895. {
  896. void *gvarPtr = (void*)asBC_PTRARG(&bc[n]);
  897. if( !gvarPtr ) break;
  898. asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);
  899. if( !prop ) break;
  900. // Only addref the properties once
  901. if( !ptrs.Exists(gvarPtr) )
  902. {
  903. prop->AddRef();
  904. ptrs.PushLast(gvarPtr);
  905. }
  906. asCConfigGroup *group = engine->FindConfigGroupForGlobalVar(prop->id);
  907. if( group != 0 ) group->AddRef();
  908. }
  909. break;
  910. // System functions
  911. case asBC_CALLSYS:
  912. {
  913. int funcId = asBC_INTARG(&bc[n]);
  914. asCConfigGroup *group = engine->FindConfigGroupForFunction(funcId);
  915. if( group != 0 ) group->AddRef();
  916. engine->scriptFunctions[funcId]->AddRef();
  917. }
  918. break;
  919. // Functions
  920. case asBC_CALL:
  921. case asBC_CALLINTF:
  922. {
  923. int func = asBC_INTARG(&bc[n]);
  924. engine->scriptFunctions[func]->AddRef();
  925. }
  926. break;
  927. // Function pointers
  928. case asBC_FuncPtr:
  929. {
  930. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&bc[n]);
  931. func->AddRef();
  932. }
  933. break;
  934. }
  935. }
  936. }
  937. }
  938. // internal
  939. void asCScriptFunction::ReleaseReferences()
  940. {
  941. asCArray<void*> ptrs;
  942. // Only count references if there is any bytecode
  943. if( scriptData && scriptData->byteCode.GetLength() )
  944. {
  945. if( returnType.IsObject() )
  946. returnType.GetObjectType()->Release();
  947. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  948. if( parameterTypes[p].IsObject() )
  949. parameterTypes[p].GetObjectType()->Release();
  950. for( asUINT v = 0; v < scriptData->objVariableTypes.GetLength(); v++ )
  951. if( scriptData->objVariableTypes[v] )
  952. scriptData->objVariableTypes[v]->Release();
  953. // Go through the byte code and release references to all resources used by the function
  954. asCArray<asDWORD> &bc = scriptData->byteCode;
  955. for( asUINT n = 0; n < bc.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&bc[n]].type] )
  956. {
  957. switch( *(asBYTE*)&bc[n] )
  958. {
  959. // Object types
  960. case asBC_OBJTYPE:
  961. case asBC_FREE:
  962. case asBC_REFCPY:
  963. case asBC_RefCpyV:
  964. {
  965. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
  966. if( objType )
  967. objType->Release();
  968. }
  969. break;
  970. // Object type and function
  971. case asBC_ALLOC:
  972. {
  973. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
  974. if( objType )
  975. objType->Release();
  976. int func = asBC_INTARG(&bc[n]+AS_PTR_SIZE);
  977. if( func )
  978. {
  979. asCScriptFunction *fptr = engine->scriptFunctions[func];
  980. if( fptr )
  981. fptr->Release();
  982. // The engine may have been forced to destroy the function internals early
  983. // and this may will make it impossible to find the function by id anymore.
  984. // This should only happen if the engine is released while the application
  985. // is still keeping functions alive.
  986. // TODO: Fix this possible memory leak
  987. }
  988. }
  989. break;
  990. // Global variables
  991. case asBC_PGA:
  992. case asBC_PshGPtr:
  993. case asBC_LDG:
  994. case asBC_PshG4:
  995. case asBC_LdGRdR4:
  996. case asBC_CpyGtoV4:
  997. case asBC_CpyVtoG4:
  998. case asBC_SetG4:
  999. // Need to increase the reference for each global variable
  1000. {
  1001. void *gvarPtr = (void*)asBC_PTRARG(&bc[n]);
  1002. if( !gvarPtr ) break;
  1003. asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);
  1004. if( !prop ) break;
  1005. // Only release the properties once
  1006. if( !ptrs.Exists(gvarPtr) )
  1007. {
  1008. prop->Release();
  1009. ptrs.PushLast(gvarPtr);
  1010. }
  1011. asCConfigGroup *group = engine->FindConfigGroupForGlobalVar(prop->id);
  1012. if( group != 0 ) group->Release();
  1013. }
  1014. break;
  1015. // System functions
  1016. case asBC_CALLSYS:
  1017. {
  1018. int funcId = asBC_INTARG(&bc[n]);
  1019. asCConfigGroup *group = engine->FindConfigGroupForFunction(funcId);
  1020. if( group != 0 ) group->Release();
  1021. if( funcId )
  1022. engine->scriptFunctions[funcId]->Release();
  1023. }
  1024. break;
  1025. // Functions
  1026. case asBC_CALL:
  1027. case asBC_CALLINTF:
  1028. {
  1029. int func = asBC_INTARG(&bc[n]);
  1030. if( func )
  1031. {
  1032. asCScriptFunction *fptr = engine->scriptFunctions[func];
  1033. if( fptr )
  1034. fptr->Release();
  1035. // The engine may have been forced to destroy the function internals early
  1036. // and this may will make it impossible to find the function by id anymore.
  1037. // This should only happen if the engine is released while the application
  1038. // is still keeping functions alive.
  1039. // TODO: Fix this possible memory leak
  1040. }
  1041. }
  1042. break;
  1043. // Function pointers
  1044. case asBC_FuncPtr:
  1045. {
  1046. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&bc[n]);
  1047. if( func )
  1048. func->Release();
  1049. }
  1050. break;
  1051. }
  1052. }
  1053. // Release the jit compiled function
  1054. if( scriptData->jitFunction )
  1055. engine->jitCompiler->ReleaseJITFunction(scriptData->jitFunction);
  1056. scriptData->jitFunction = 0;
  1057. }
  1058. // Delegate
  1059. if( objForDelegate )
  1060. engine->ReleaseScriptObject(objForDelegate, funcForDelegate->GetObjectType());
  1061. objForDelegate = 0;
  1062. if( funcForDelegate )
  1063. funcForDelegate->Release();
  1064. funcForDelegate = 0;
  1065. }
  1066. // interface
  1067. int asCScriptFunction::GetReturnTypeId(asDWORD *flags) const
  1068. {
  1069. if( flags )
  1070. {
  1071. if( returnType.IsReference() )
  1072. {
  1073. *flags = asTM_INOUTREF;
  1074. *flags |= returnType.IsReadOnly() ? asTM_CONST : 0;
  1075. }
  1076. else
  1077. *flags = asTM_NONE;
  1078. }
  1079. return engine->GetTypeIdFromDataType(returnType);
  1080. }
  1081. // interface
  1082. asUINT asCScriptFunction::GetParamCount() const
  1083. {
  1084. return (asUINT)parameterTypes.GetLength();
  1085. }
  1086. // interface
  1087. int asCScriptFunction::GetParam(asUINT index, int *typeId, asDWORD *flags, const char **name, const char **defaultArg) const
  1088. {
  1089. if( index >= parameterTypes.GetLength() )
  1090. return asINVALID_ARG;
  1091. if( typeId )
  1092. *typeId = engine->GetTypeIdFromDataType(parameterTypes[index]);
  1093. if( flags )
  1094. {
  1095. *flags = inOutFlags[index];
  1096. *flags |= parameterTypes[index].IsReadOnly() ? asTM_CONST : 0;
  1097. }
  1098. if( name )
  1099. {
  1100. // The parameter names are not stored if loading from bytecode without debug information
  1101. if( index < parameterNames.GetLength() )
  1102. *name = parameterNames[index].AddressOf();
  1103. else
  1104. *name = 0;
  1105. }
  1106. if( defaultArg )
  1107. {
  1108. if( index < defaultArgs.GetLength() && defaultArgs[index] )
  1109. *defaultArg = defaultArgs[index]->AddressOf();
  1110. else
  1111. *defaultArg = 0;
  1112. }
  1113. return asSUCCESS;
  1114. }
  1115. #ifdef AS_DEPRECATED
  1116. // Deprecated since 2014-04-06, 2.29.0
  1117. int asCScriptFunction::GetParamTypeId(asUINT index, asDWORD *flags) const
  1118. {
  1119. if( index >= parameterTypes.GetLength() )
  1120. return asINVALID_ARG;
  1121. if( flags )
  1122. {
  1123. *flags = inOutFlags[index];
  1124. *flags |= parameterTypes[index].IsReadOnly() ? asTM_CONST : 0;
  1125. }
  1126. return engine->GetTypeIdFromDataType(parameterTypes[index]);
  1127. }
  1128. #endif
  1129. // interface
  1130. asIScriptEngine *asCScriptFunction::GetEngine() const
  1131. {
  1132. return engine;
  1133. }
  1134. // interface
  1135. const char *asCScriptFunction::GetDeclaration(bool includeObjectName, bool includeNamespace, bool includeParamNames) const
  1136. {
  1137. asCString *tempString = &asCThreadManager::GetLocalData()->string;
  1138. *tempString = GetDeclarationStr(includeObjectName, includeNamespace, includeParamNames);
  1139. return tempString->AddressOf();
  1140. }
  1141. // interface
  1142. const char *asCScriptFunction::GetScriptSectionName() const
  1143. {
  1144. if( scriptData && scriptData->scriptSectionIdx >= 0 )
  1145. return engine->scriptSectionNames[scriptData->scriptSectionIdx]->AddressOf();
  1146. return 0;
  1147. }
  1148. // interface
  1149. const char *asCScriptFunction::GetConfigGroup() const
  1150. {
  1151. asCConfigGroup *group = 0;
  1152. if( funcType != asFUNC_FUNCDEF )
  1153. group = engine->FindConfigGroupForFunction(id);
  1154. else
  1155. group = engine->FindConfigGroupForFuncDef(this);
  1156. if( group == 0 )
  1157. return 0;
  1158. return group->groupName.AddressOf();
  1159. }
  1160. // interface
  1161. asDWORD asCScriptFunction::GetAccessMask() const
  1162. {
  1163. return accessMask;
  1164. }
  1165. // internal
  1166. void asCScriptFunction::JITCompile()
  1167. {
  1168. if( funcType != asFUNC_SCRIPT )
  1169. return;
  1170. asASSERT( scriptData );
  1171. asIJITCompiler *jit = engine->GetJITCompiler();
  1172. if( !jit )
  1173. return;
  1174. // Make sure the function has been compiled with JitEntry instructions
  1175. // For functions that has JitEntry this will be a quick test
  1176. asUINT length;
  1177. asDWORD *byteCode = GetByteCode(&length);
  1178. asDWORD *end = byteCode + length;
  1179. bool foundJitEntry = false;
  1180. while( byteCode < end )
  1181. {
  1182. // Determine the instruction
  1183. asEBCInstr op = asEBCInstr(*(asBYTE*)byteCode);
  1184. if( op == asBC_JitEntry )
  1185. {
  1186. foundJitEntry = true;
  1187. break;
  1188. }
  1189. // Move to next instruction
  1190. byteCode += asBCTypeSize[asBCInfo[op].type];
  1191. }
  1192. if( !foundJitEntry )
  1193. {
  1194. asCString msg;
  1195. msg.Format(TXT_NO_JIT_IN_FUNC_s, GetDeclaration());
  1196. engine->WriteMessage("", 0, 0, asMSGTYPE_WARNING, msg.AddressOf());
  1197. }
  1198. // Release the previous function, if any
  1199. if( scriptData->jitFunction )
  1200. {
  1201. engine->jitCompiler->ReleaseJITFunction(scriptData->jitFunction);
  1202. scriptData->jitFunction = 0;
  1203. }
  1204. // Compile for native system
  1205. int r = jit->CompileFunction(this, &scriptData->jitFunction);
  1206. if( r < 0 )
  1207. asASSERT( scriptData->jitFunction == 0 );
  1208. }
  1209. // interface
  1210. asDWORD *asCScriptFunction::GetByteCode(asUINT *length)
  1211. {
  1212. if( scriptData == 0 ) return 0;
  1213. if( length )
  1214. *length = (asUINT)scriptData->byteCode.GetLength();
  1215. if( scriptData->byteCode.GetLength() )
  1216. return scriptData->byteCode.AddressOf();
  1217. return 0;
  1218. }
  1219. // interface
  1220. void *asCScriptFunction::SetUserData(void *data, asPWORD type)
  1221. {
  1222. // As a thread might add a new new user data at the same time as another
  1223. // it is necessary to protect both read and write access to the userData member
  1224. ACQUIREEXCLUSIVE(engine->engineRWLock);
  1225. // It is not intended to store a lot of different types of userdata,
  1226. // so a more complex structure like a associative map would just have
  1227. // more overhead than a simple array.
  1228. for( asUINT n = 0; n < userData.GetLength(); n += 2 )
  1229. {
  1230. if( userData[n] == type )
  1231. {
  1232. void *oldData = reinterpret_cast<void*>(userData[n+1]);
  1233. userData[n+1] = reinterpret_cast<asPWORD>(data);
  1234. RELEASEEXCLUSIVE(engine->engineRWLock);
  1235. return oldData;
  1236. }
  1237. }
  1238. userData.PushLast(type);
  1239. userData.PushLast(reinterpret_cast<asPWORD>(data));
  1240. RELEASEEXCLUSIVE(engine->engineRWLock);
  1241. return 0;
  1242. }
  1243. // interface
  1244. void *asCScriptFunction::GetUserData(asPWORD type) const
  1245. {
  1246. // There may be multiple threads reading, but when
  1247. // setting the user data nobody must be reading.
  1248. ACQUIRESHARED(engine->engineRWLock);
  1249. for( asUINT n = 0; n < userData.GetLength(); n += 2 )
  1250. {
  1251. if( userData[n] == type )
  1252. {
  1253. RELEASESHARED(engine->engineRWLock);
  1254. return reinterpret_cast<void*>(userData[n+1]);
  1255. }
  1256. }
  1257. RELEASESHARED(engine->engineRWLock);
  1258. return 0;
  1259. }
  1260. // internal
  1261. // TODO: cleanup: This method should probably be a member of the engine
  1262. asCGlobalProperty *asCScriptFunction::GetPropertyByGlobalVarPtr(void *gvarPtr)
  1263. {
  1264. asSMapNode<void*, asCGlobalProperty*> *node;
  1265. if( engine->varAddressMap.MoveTo(&node, gvarPtr) )
  1266. {
  1267. asASSERT(gvarPtr == node->value->GetAddressOfValue());
  1268. return node->value;
  1269. }
  1270. return 0;
  1271. }
  1272. // internal
  1273. int asCScriptFunction::GetRefCount()
  1274. {
  1275. return refCount.get();
  1276. }
  1277. // internal
  1278. void asCScriptFunction::SetFlag()
  1279. {
  1280. gcFlag = true;
  1281. }
  1282. // internal
  1283. bool asCScriptFunction::GetFlag()
  1284. {
  1285. return gcFlag;
  1286. }
  1287. // internal
  1288. void asCScriptFunction::EnumReferences(asIScriptEngine *)
  1289. {
  1290. // Notify the GC of all object types used
  1291. if( returnType.IsObject() )
  1292. engine->GCEnumCallback(returnType.GetObjectType());
  1293. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  1294. if( parameterTypes[p].IsObject() )
  1295. engine->GCEnumCallback(parameterTypes[p].GetObjectType());
  1296. if( scriptData )
  1297. {
  1298. for( asUINT t = 0; t < scriptData->objVariableTypes.GetLength(); t++ )
  1299. engine->GCEnumCallback(scriptData->objVariableTypes[t]);
  1300. // Notify the GC of all script functions that is accessed
  1301. asCArray<asDWORD> &bc = scriptData->byteCode;
  1302. for( asUINT n = 0; n < bc.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&bc[n]].type] )
  1303. {
  1304. switch( *(asBYTE*)&bc[n] )
  1305. {
  1306. case asBC_OBJTYPE:
  1307. case asBC_FREE:
  1308. case asBC_REFCPY:
  1309. case asBC_RefCpyV:
  1310. {
  1311. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
  1312. engine->GCEnumCallback(objType);
  1313. }
  1314. break;
  1315. case asBC_ALLOC:
  1316. {
  1317. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
  1318. engine->GCEnumCallback(objType);
  1319. int func = asBC_INTARG(&bc[n]+AS_PTR_SIZE);
  1320. if( func )
  1321. engine->GCEnumCallback(engine->scriptFunctions[func]);
  1322. }
  1323. break;
  1324. case asBC_CALL:
  1325. case asBC_CALLINTF:
  1326. {
  1327. int func = asBC_INTARG(&bc[n]);
  1328. if( func )
  1329. engine->GCEnumCallback(engine->scriptFunctions[func]);
  1330. }
  1331. break;
  1332. // Function pointers
  1333. case asBC_FuncPtr:
  1334. {
  1335. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&bc[n]);
  1336. if( func )
  1337. engine->GCEnumCallback(func);
  1338. }
  1339. break;
  1340. // Global variables
  1341. case asBC_PGA:
  1342. case asBC_PshGPtr:
  1343. case asBC_LDG:
  1344. case asBC_PshG4:
  1345. case asBC_LdGRdR4:
  1346. case asBC_CpyGtoV4:
  1347. case asBC_CpyVtoG4:
  1348. case asBC_SetG4:
  1349. // Need to enumerate the reference for each global variable
  1350. {
  1351. // TODO: optimize: Keep an array of accessed global properties
  1352. void *gvarPtr = (void*)asBC_PTRARG(&bc[n]);
  1353. asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);
  1354. engine->GCEnumCallback(prop);
  1355. }
  1356. break;
  1357. }
  1358. }
  1359. }
  1360. // Delegate
  1361. if( objForDelegate )
  1362. engine->GCEnumCallback(objForDelegate);
  1363. if( funcForDelegate )
  1364. engine->GCEnumCallback(funcForDelegate);
  1365. }
  1366. // internal
  1367. void asCScriptFunction::ReleaseAllHandles(asIScriptEngine *)
  1368. {
  1369. // Release paramaters
  1370. if( scriptData && scriptData->byteCode.GetLength() )
  1371. {
  1372. if( returnType.IsObject() )
  1373. {
  1374. returnType.GetObjectType()->Release();
  1375. returnType = asCDataType::CreatePrimitive(ttVoid, false);
  1376. }
  1377. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  1378. if( parameterTypes[p].IsObject() )
  1379. {
  1380. parameterTypes[p].GetObjectType()->Release();
  1381. parameterTypes[p] = asCDataType::CreatePrimitive(ttInt, false);
  1382. }
  1383. for( asUINT n = 0; n < scriptData->objVariableTypes.GetLength(); n++ )
  1384. if( scriptData->objVariableTypes[n] ) // Null handle is also stored, but it doesn't have an object type
  1385. scriptData->objVariableTypes[n]->Release();
  1386. scriptData->objVariableTypes.SetLength(0);
  1387. // Release all script functions
  1388. asCArray<asDWORD> &bc = scriptData->byteCode;
  1389. for( asUINT n = 0; n < bc.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&bc[n]].type] )
  1390. {
  1391. switch( *(asBYTE*)&bc[n] )
  1392. {
  1393. // Object types
  1394. case asBC_OBJTYPE:
  1395. case asBC_FREE:
  1396. case asBC_REFCPY:
  1397. case asBC_RefCpyV:
  1398. {
  1399. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
  1400. if( objType )
  1401. {
  1402. objType->Release();
  1403. *(asPWORD*)&bc[n+1] = 0;
  1404. }
  1405. }
  1406. break;
  1407. case asBC_ALLOC:
  1408. {
  1409. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
  1410. if( objType )
  1411. {
  1412. objType->Release();
  1413. *(asPWORD*)&bc[n+1] = 0;
  1414. }
  1415. int func = asBC_INTARG(&bc[n]+AS_PTR_SIZE);
  1416. if( func )
  1417. {
  1418. engine->scriptFunctions[func]->Release();
  1419. bc[n+AS_PTR_SIZE+1] = 0;
  1420. }
  1421. }
  1422. break;
  1423. case asBC_CALL:
  1424. case asBC_CALLINTF:
  1425. {
  1426. int func = asBC_INTARG(&bc[n]);
  1427. if( func )
  1428. {
  1429. engine->scriptFunctions[func]->Release();
  1430. bc[n+1] = 0;
  1431. }
  1432. }
  1433. break;
  1434. // Function pointers
  1435. case asBC_FuncPtr:
  1436. {
  1437. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&bc[n]);
  1438. if( func )
  1439. {
  1440. func->Release();
  1441. *(asPWORD*)&bc[n+1] = 0;
  1442. }
  1443. }
  1444. break;
  1445. // The global variables are not released here. It is enough that the global
  1446. // variable itself release the function to break the circle
  1447. }
  1448. }
  1449. }
  1450. // Delegate
  1451. if( objForDelegate )
  1452. engine->ReleaseScriptObject(objForDelegate, funcForDelegate->GetObjectType());
  1453. objForDelegate = 0;
  1454. if( funcForDelegate )
  1455. funcForDelegate->Release();
  1456. funcForDelegate = 0;
  1457. }
  1458. // internal
  1459. bool asCScriptFunction::IsShared() const
  1460. {
  1461. // All system functions are shared
  1462. if( funcType == asFUNC_SYSTEM ) return true;
  1463. // All class methods for shared classes are also shared
  1464. if( objectType && (objectType->flags & asOBJ_SHARED) ) return true;
  1465. // Functions that have been specifically marked as shared are shared
  1466. return isShared;
  1467. }
  1468. // internal
  1469. bool asCScriptFunction::IsFinal() const
  1470. {
  1471. return isFinal;
  1472. }
  1473. // internal
  1474. bool asCScriptFunction::IsOverride() const
  1475. {
  1476. return isOverride;
  1477. }
  1478. END_AS_NAMESPACE