as_scriptfunction.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2012 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. BEGIN_AS_NAMESPACE
  36. #ifdef AS_MAX_PORTABILITY
  37. static void ScriptFunction_AddRef_Generic(asIScriptGeneric *gen)
  38. {
  39. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  40. self->AddRef();
  41. }
  42. static void ScriptFunction_Release_Generic(asIScriptGeneric *gen)
  43. {
  44. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  45. self->Release();
  46. }
  47. static void ScriptFunction_GetRefCount_Generic(asIScriptGeneric *gen)
  48. {
  49. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  50. *(int*)gen->GetAddressOfReturnLocation() = self->GetRefCount();
  51. }
  52. static void ScriptFunction_SetFlag_Generic(asIScriptGeneric *gen)
  53. {
  54. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  55. self->SetFlag();
  56. }
  57. static void ScriptFunction_GetFlag_Generic(asIScriptGeneric *gen)
  58. {
  59. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  60. *(bool*)gen->GetAddressOfReturnLocation() = self->GetFlag();
  61. }
  62. static void ScriptFunction_EnumReferences_Generic(asIScriptGeneric *gen)
  63. {
  64. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  65. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  66. self->EnumReferences(engine);
  67. }
  68. static void ScriptFunction_ReleaseAllHandles_Generic(asIScriptGeneric *gen)
  69. {
  70. asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
  71. asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
  72. self->ReleaseAllHandles(engine);
  73. }
  74. #endif
  75. void RegisterScriptFunction(asCScriptEngine *engine)
  76. {
  77. // Register the gc behaviours for the script functions
  78. int r = 0;
  79. UNUSED_VAR(r); // It is only used in debug mode
  80. engine->functionBehaviours.engine = engine;
  81. engine->functionBehaviours.flags = asOBJ_REF | asOBJ_GC;
  82. engine->functionBehaviours.name = "_builtin_function_";
  83. #ifndef AS_MAX_PORTABILITY
  84. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCScriptFunction,AddRef), asCALL_THISCALL); asASSERT( r >= 0 );
  85. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCScriptFunction,Release), asCALL_THISCALL); asASSERT( r >= 0 );
  86. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCScriptFunction,GetRefCount), asCALL_THISCALL); asASSERT( r >= 0 );
  87. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCScriptFunction,SetFlag), asCALL_THISCALL); asASSERT( r >= 0 );
  88. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCScriptFunction,GetFlag), asCALL_THISCALL); asASSERT( r >= 0 );
  89. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCScriptFunction,EnumReferences), asCALL_THISCALL); asASSERT( r >= 0 );
  90. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCScriptFunction,ReleaseAllHandles), asCALL_THISCALL); asASSERT( r >= 0 );
  91. #else
  92. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(ScriptFunction_AddRef_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  93. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(ScriptFunction_Release_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  94. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(ScriptFunction_GetRefCount_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  95. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(ScriptFunction_SetFlag_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  96. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(ScriptFunction_GetFlag_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  97. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(ScriptFunction_EnumReferences_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  98. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(ScriptFunction_ReleaseAllHandles_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  99. #endif
  100. }
  101. // internal
  102. asCScriptFunction::asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType _funcType)
  103. {
  104. refCount.set(1);
  105. this->engine = engine;
  106. funcType = _funcType;
  107. module = mod;
  108. objectType = 0;
  109. name = "";
  110. isReadOnly = false;
  111. isPrivate = false;
  112. isFinal = false;
  113. isOverride = false;
  114. stackNeeded = 0;
  115. sysFuncIntf = 0;
  116. signatureId = 0;
  117. scriptSectionIdx = -1;
  118. dontCleanUpOnException = false;
  119. vfTableIdx = -1;
  120. jitFunction = 0;
  121. gcFlag = false;
  122. userData = 0;
  123. id = 0;
  124. accessMask = 0xFFFFFFFF;
  125. isShared = false;
  126. variableSpace = 0;
  127. // TODO: runtime optimize: The engine could notify the GC just before it wants to
  128. // discard the function. That way the GC won't waste time
  129. // trying to determine if the functions are garbage before
  130. // they can actually be considered garbage.
  131. // Notify the GC of script functions
  132. if( funcType == asFUNC_SCRIPT )
  133. engine->gc.AddScriptObjectToGC(this, &engine->functionBehaviours);
  134. }
  135. // internal
  136. asCScriptFunction::~asCScriptFunction()
  137. {
  138. // Imported functions are not reference counted, nor are dummy
  139. // functions that are allocated on the stack
  140. asASSERT( funcType == asFUNC_DUMMY ||
  141. funcType == asFUNC_IMPORTED ||
  142. refCount.get() == 0 );
  143. // If the engine pointer is 0, then DestroyInternal has already been called and there is nothing more to do
  144. if( engine == 0 ) return;
  145. DestroyInternal();
  146. // Tell engine to free the function id. This will make it impossible to
  147. // refer to the function by id. Where this is done, it is quite possible
  148. // they will leak.
  149. if( funcType != -1 && funcType != asFUNC_IMPORTED && id )
  150. engine->FreeScriptFunctionId(id);
  151. id = 0;
  152. // Finally set the engine pointer to 0 because it must not be accessed again
  153. engine = 0;
  154. }
  155. // internal
  156. void asCScriptFunction::DestroyInternal()
  157. {
  158. // Clean up user data
  159. if( userData && engine->cleanFunctionFunc )
  160. engine->cleanFunctionFunc(this);
  161. userData = 0;
  162. // Release all references the function holds to other objects
  163. ReleaseReferences();
  164. parameterTypes.SetLength(0);
  165. returnType = asCDataType::CreatePrimitive(ttVoid, false);
  166. byteCode.SetLength(0);
  167. for( asUINT n = 0; n < variables.GetLength(); n++ )
  168. asDELETE(variables[n],asSScriptVariable);
  169. variables.SetLength(0);
  170. for( asUINT p = 0; p < defaultArgs.GetLength(); p++ )
  171. if( defaultArgs[p] )
  172. asDELETE(defaultArgs[p], asCString);
  173. defaultArgs.SetLength(0);
  174. if( sysFuncIntf )
  175. asDELETE(sysFuncIntf,asSSystemFunctionInterface);
  176. sysFuncIntf = 0;
  177. }
  178. // interface
  179. int asCScriptFunction::GetId() const
  180. {
  181. return id;
  182. }
  183. // interface
  184. int asCScriptFunction::AddRef() const
  185. {
  186. gcFlag = false;
  187. asASSERT( funcType != asFUNC_IMPORTED );
  188. return refCount.atomicInc();
  189. }
  190. // interface
  191. int asCScriptFunction::Release() const
  192. {
  193. gcFlag = false;
  194. asASSERT( funcType != asFUNC_IMPORTED );
  195. int r = refCount.atomicDec();
  196. if( r == 0 && funcType != asFUNC_DUMMY ) // Dummy functions are allocated on the stack and cannot be deleted
  197. asDELETE(const_cast<asCScriptFunction*>(this),asCScriptFunction);
  198. return r;
  199. }
  200. // interface
  201. const char *asCScriptFunction::GetModuleName() const
  202. {
  203. if( module )
  204. {
  205. return module->name.AddressOf();
  206. }
  207. return 0;
  208. }
  209. // interface
  210. asIObjectType *asCScriptFunction::GetObjectType() const
  211. {
  212. return objectType;
  213. }
  214. // interface
  215. const char *asCScriptFunction::GetObjectName() const
  216. {
  217. if( objectType )
  218. return objectType->GetName();
  219. return 0;
  220. }
  221. // interface
  222. const char *asCScriptFunction::GetName() const
  223. {
  224. return name.AddressOf();
  225. }
  226. // interface
  227. const char *asCScriptFunction::GetNamespace() const
  228. {
  229. return nameSpace.AddressOf();
  230. }
  231. // interface
  232. bool asCScriptFunction::IsReadOnly() const
  233. {
  234. return isReadOnly;
  235. }
  236. // interface
  237. bool asCScriptFunction::IsPrivate() const
  238. {
  239. return isPrivate;
  240. }
  241. // internal
  242. int asCScriptFunction::GetSpaceNeededForArguments()
  243. {
  244. // We need to check the size for each type
  245. int s = 0;
  246. for( asUINT n = 0; n < parameterTypes.GetLength(); n++ )
  247. s += parameterTypes[n].GetSizeOnStackDWords();
  248. return s;
  249. }
  250. // internal
  251. int asCScriptFunction::GetSpaceNeededForReturnValue()
  252. {
  253. return returnType.GetSizeOnStackDWords();
  254. }
  255. // internal
  256. bool asCScriptFunction::DoesReturnOnStack() const
  257. {
  258. if( returnType.GetObjectType() &&
  259. (returnType.GetObjectType()->flags & asOBJ_VALUE) &&
  260. !returnType.IsReference() )
  261. return true;
  262. return false;
  263. }
  264. // internal
  265. asCString asCScriptFunction::GetDeclarationStr(bool includeObjectName, bool includeNamespace) const
  266. {
  267. asCString str;
  268. // TODO: default arg: Make the declaration with the default args an option
  269. // Don't add the return type for constructors and destructors
  270. if( !(returnType.GetTokenType() == ttVoid &&
  271. objectType &&
  272. (name == objectType->name || (name.GetLength() > 0 && name[0] == '~'))) )
  273. {
  274. str = returnType.Format();
  275. str += " ";
  276. }
  277. if( objectType && includeObjectName )
  278. {
  279. if( includeNamespace )
  280. str += objectType->nameSpace + "::";
  281. if( objectType->name != "" )
  282. str += objectType->name + "::";
  283. else
  284. str += "_unnamed_type_::";
  285. }
  286. else if( includeNamespace )
  287. {
  288. str += nameSpace + "::";
  289. }
  290. if( name == "" )
  291. str += "_unnamed_function_(";
  292. else
  293. str += name + "(";
  294. if( parameterTypes.GetLength() > 0 )
  295. {
  296. asUINT n;
  297. for( n = 0; n < parameterTypes.GetLength() - 1; n++ )
  298. {
  299. str += parameterTypes[n].Format();
  300. if( parameterTypes[n].IsReference() && inOutFlags.GetLength() > n )
  301. {
  302. if( inOutFlags[n] == asTM_INREF ) str += "in";
  303. else if( inOutFlags[n] == asTM_OUTREF ) str += "out";
  304. else if( inOutFlags[n] == asTM_INOUTREF ) str += "inout";
  305. }
  306. if( defaultArgs.GetLength() > n && defaultArgs[n] )
  307. {
  308. asCString tmp;
  309. tmp.Format(" arg%d = %s", n, defaultArgs[n]->AddressOf());
  310. str += tmp;
  311. }
  312. str += ", ";
  313. }
  314. // Add the last parameter
  315. str += parameterTypes[n].Format();
  316. if( parameterTypes[n].IsReference() && inOutFlags.GetLength() > n )
  317. {
  318. if( inOutFlags[n] == asTM_INREF ) str += "in";
  319. else if( inOutFlags[n] == asTM_OUTREF ) str += "out";
  320. else if( inOutFlags[n] == asTM_INOUTREF ) str += "inout";
  321. }
  322. if( defaultArgs.GetLength() > n && defaultArgs[n] )
  323. {
  324. asCString tmp;
  325. tmp.Format(" arg%d = %s", n, defaultArgs[n]->AddressOf());
  326. str += tmp;
  327. }
  328. }
  329. str += ")";
  330. if( isReadOnly )
  331. str += " const";
  332. return str;
  333. }
  334. // interface
  335. int asCScriptFunction::FindNextLineWithCode(int line) const
  336. {
  337. if( lineNumbers.GetLength() == 0 ) return -1;
  338. // Check if given line is outside function
  339. // TODO: should start at declaration instead of first line of code
  340. if( line < (lineNumbers[1]&0xFFFFF) ) return -1;
  341. if( line > (lineNumbers[lineNumbers.GetLength()-1]&0xFFFFF) ) return -1;
  342. // Find the line with code on or right after the input line
  343. // TODO: optimize: Do binary search instead
  344. if( line == (lineNumbers[1]&0xFFFFF) ) return line;
  345. for( asUINT n = 3; n < lineNumbers.GetLength(); n += 2 )
  346. {
  347. if( line <= (lineNumbers[n]&0xFFFFF) )
  348. return (lineNumbers[n]&0xFFFFF);
  349. }
  350. return -1;
  351. }
  352. // internal
  353. int asCScriptFunction::GetLineNumber(int programPosition)
  354. {
  355. if( lineNumbers.GetLength() == 0 ) return 0;
  356. // Do a binary search in the buffer
  357. int max = (int)lineNumbers.GetLength()/2 - 1;
  358. int min = 0;
  359. int i = max/2;
  360. for(;;)
  361. {
  362. if( lineNumbers[i*2] < programPosition )
  363. {
  364. // Have we found the largest number < programPosition?
  365. if( max == i ) return lineNumbers[i*2+1];
  366. if( lineNumbers[i*2+2] > programPosition ) return lineNumbers[i*2+1];
  367. min = i + 1;
  368. i = (max + min)/2;
  369. }
  370. else if( lineNumbers[i*2] > programPosition )
  371. {
  372. // Have we found the smallest number > programPosition?
  373. if( min == i ) return lineNumbers[i*2+1];
  374. max = i - 1;
  375. i = (max + min)/2;
  376. }
  377. else
  378. {
  379. // We found the exact position
  380. return lineNumbers[i*2+1];
  381. }
  382. }
  383. }
  384. // interface
  385. asEFuncType asCScriptFunction::GetFuncType() const
  386. {
  387. return funcType;
  388. }
  389. // interface
  390. asUINT asCScriptFunction::GetVarCount() const
  391. {
  392. return int(variables.GetLength());
  393. }
  394. // interface
  395. int asCScriptFunction::GetVar(asUINT index, const char **name, int *typeId) const
  396. {
  397. if( index >= variables.GetLength() )
  398. return asINVALID_ARG;
  399. if( name )
  400. *name = variables[index]->name.AddressOf();
  401. if( typeId )
  402. *typeId = engine->GetTypeIdFromDataType(variables[index]->type);
  403. return asSUCCESS;
  404. }
  405. // interface
  406. const char *asCScriptFunction::GetVarDecl(asUINT index) const
  407. {
  408. if( index >= variables.GetLength() )
  409. return 0;
  410. asCString *tempString = &asCThreadManager::GetLocalData()->string;
  411. *tempString = variables[index]->type.Format();
  412. *tempString += " " + variables[index]->name;
  413. return tempString->AddressOf();
  414. }
  415. // internal
  416. void asCScriptFunction::AddVariable(asCString &name, asCDataType &type, int stackOffset)
  417. {
  418. asSScriptVariable *var = asNEW(asSScriptVariable);
  419. var->name = name;
  420. var->type = type;
  421. var->stackOffset = stackOffset;
  422. var->declaredAtProgramPos = 0;
  423. variables.PushLast(var);
  424. }
  425. // internal
  426. void asCScriptFunction::ComputeSignatureId()
  427. {
  428. // This function will compute the signatureId based on the
  429. // function name, return type, and parameter types. The object
  430. // type for methods is not used, so that class methods and
  431. // interface methods match each other.
  432. for( asUINT n = 0; n < engine->signatureIds.GetLength(); n++ )
  433. {
  434. if( !IsSignatureEqual(engine->signatureIds[n]) ) continue;
  435. // We don't need to increment the reference counter here, because
  436. // asCScriptEngine::FreeScriptFunctionId will maintain the signature
  437. // id as the function is freed.
  438. signatureId = engine->signatureIds[n]->signatureId;
  439. return;
  440. }
  441. signatureId = id;
  442. engine->signatureIds.PushLast(this);
  443. }
  444. // internal
  445. bool asCScriptFunction::IsSignatureEqual(const asCScriptFunction *func) const
  446. {
  447. if( !IsSignatureExceptNameEqual(func) || name != func->name ) return false;
  448. return true;
  449. }
  450. // internal
  451. bool asCScriptFunction::IsSignatureExceptNameEqual(const asCScriptFunction *func) const
  452. {
  453. return IsSignatureExceptNameEqual(func->returnType, func->parameterTypes, func->inOutFlags, func->objectType, func->isReadOnly);
  454. }
  455. // internal
  456. bool asCScriptFunction::IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &paramInOut, const asCObjectType *objType, bool readOnly) const
  457. {
  458. if( this->returnType != retType ) return false;
  459. return IsSignatureExceptNameAndReturnTypeEqual(paramTypes, paramInOut, objType, readOnly);
  460. }
  461. // internal
  462. bool asCScriptFunction::IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &paramInOut, const asCObjectType *objType, bool readOnly) const
  463. {
  464. if( this->isReadOnly != readOnly ) return false;
  465. if( this->inOutFlags != paramInOut ) return false;
  466. if( this->parameterTypes != paramTypes ) return false;
  467. if( (this->objectType != 0) != (objType != 0) ) return false;
  468. return true;
  469. }
  470. // internal
  471. void asCScriptFunction::AddReferences()
  472. {
  473. asUINT n;
  474. // This array will be used to make sure we only add the reference to the same resource once
  475. // This is especially important for global variables, as it expects the initialization function
  476. // to hold only one reference to the variable. However, if the variable is initialized through
  477. // the default constructor followed by the assignment operator we will have two references to
  478. // the variable in the function.
  479. asCArray<void*> ptrs;
  480. // Only count references if there is any bytecode
  481. if( byteCode.GetLength() )
  482. {
  483. if( returnType.IsObject() )
  484. returnType.GetObjectType()->AddRef();
  485. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  486. if( parameterTypes[p].IsObject() )
  487. parameterTypes[p].GetObjectType()->AddRef();
  488. for( asUINT n = 0; n < objVariableTypes.GetLength(); n++ )
  489. objVariableTypes[n]->AddRef();
  490. }
  491. // Go through the byte code and add references to all resources used by the function
  492. for( n = 0; n < byteCode.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&byteCode[n]].type] )
  493. {
  494. switch( *(asBYTE*)&byteCode[n] )
  495. {
  496. // Object types
  497. case asBC_OBJTYPE:
  498. case asBC_FREE:
  499. case asBC_REFCPY:
  500. case asBC_RefCpyV:
  501. {
  502. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  503. objType->AddRef();
  504. }
  505. break;
  506. // Object type and function
  507. case asBC_ALLOC:
  508. {
  509. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  510. objType->AddRef();
  511. int func = asBC_INTARG(&byteCode[n]+AS_PTR_SIZE);
  512. if( func )
  513. engine->scriptFunctions[func]->AddRef();
  514. }
  515. break;
  516. // Global variables
  517. case asBC_PGA:
  518. case asBC_PshGPtr:
  519. case asBC_LDG:
  520. case asBC_PshG4:
  521. case asBC_LdGRdR4:
  522. case asBC_CpyGtoV4:
  523. case asBC_CpyVtoG4:
  524. case asBC_SetG4:
  525. // Need to increase the reference for each global variable
  526. {
  527. void *gvarPtr = (void*)asBC_PTRARG(&byteCode[n]);
  528. if( !gvarPtr ) break;
  529. asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);
  530. if( !prop ) break;
  531. // Only addref the properties once
  532. if( !ptrs.Exists(gvarPtr) )
  533. {
  534. prop->AddRef();
  535. ptrs.PushLast(gvarPtr);
  536. }
  537. asCConfigGroup *group = engine->FindConfigGroupForGlobalVar(prop->id);
  538. if( group != 0 ) group->AddRef();
  539. }
  540. break;
  541. // System functions
  542. case asBC_CALLSYS:
  543. {
  544. int funcId = asBC_INTARG(&byteCode[n]);
  545. asCConfigGroup *group = engine->FindConfigGroupForFunction(funcId);
  546. if( group != 0 ) group->AddRef();
  547. engine->scriptFunctions[funcId]->AddRef();
  548. }
  549. break;
  550. // Functions
  551. case asBC_CALL:
  552. case asBC_CALLINTF:
  553. {
  554. int func = asBC_INTARG(&byteCode[n]);
  555. engine->scriptFunctions[func]->AddRef();
  556. }
  557. break;
  558. // Function pointers
  559. case asBC_FuncPtr:
  560. {
  561. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&byteCode[n]);
  562. func->AddRef();
  563. }
  564. break;
  565. }
  566. }
  567. }
  568. // internal
  569. void asCScriptFunction::ReleaseReferences()
  570. {
  571. asUINT n;
  572. asCArray<void*> ptrs;
  573. // Only count references if there is any bytecode
  574. if( byteCode.GetLength() )
  575. {
  576. if( returnType.IsObject() )
  577. returnType.GetObjectType()->Release();
  578. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  579. if( parameterTypes[p].IsObject() )
  580. parameterTypes[p].GetObjectType()->Release();
  581. for( asUINT n = 0; n < objVariableTypes.GetLength(); n++ )
  582. if( objVariableTypes[n] )
  583. objVariableTypes[n]->Release();
  584. }
  585. // Go through the byte code and release references to all resources used by the function
  586. for( n = 0; n < byteCode.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&byteCode[n]].type] )
  587. {
  588. switch( *(asBYTE*)&byteCode[n] )
  589. {
  590. // Object types
  591. case asBC_OBJTYPE:
  592. case asBC_FREE:
  593. case asBC_REFCPY:
  594. case asBC_RefCpyV:
  595. {
  596. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  597. if( objType )
  598. objType->Release();
  599. }
  600. break;
  601. // Object type and function
  602. case asBC_ALLOC:
  603. {
  604. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  605. if( objType )
  606. objType->Release();
  607. int func = asBC_INTARG(&byteCode[n]+AS_PTR_SIZE);
  608. if( func )
  609. {
  610. asCScriptFunction *fptr = engine->scriptFunctions[func];
  611. if( fptr )
  612. fptr->Release();
  613. // The engine may have been forced to destroy the function internals early
  614. // and this may will make it impossible to find the function by id anymore.
  615. // This should only happen if the engine is released while the application
  616. // is still keeping functions alive.
  617. // TODO: Fix this possible memory leak
  618. }
  619. }
  620. break;
  621. // Global variables
  622. case asBC_PGA:
  623. case asBC_PshGPtr:
  624. case asBC_LDG:
  625. case asBC_PshG4:
  626. case asBC_LdGRdR4:
  627. case asBC_CpyGtoV4:
  628. case asBC_CpyVtoG4:
  629. case asBC_SetG4:
  630. // Need to increase the reference for each global variable
  631. {
  632. void *gvarPtr = (void*)asBC_PTRARG(&byteCode[n]);
  633. if( !gvarPtr ) break;
  634. asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);
  635. if( !prop ) break;
  636. // Only release the properties once
  637. if( !ptrs.Exists(gvarPtr) )
  638. {
  639. prop->Release();
  640. ptrs.PushLast(gvarPtr);
  641. }
  642. asCConfigGroup *group = engine->FindConfigGroupForGlobalVar(prop->id);
  643. if( group != 0 ) group->Release();
  644. }
  645. break;
  646. // System functions
  647. case asBC_CALLSYS:
  648. {
  649. int funcId = asBC_INTARG(&byteCode[n]);
  650. asCConfigGroup *group = engine->FindConfigGroupForFunction(funcId);
  651. if( group != 0 ) group->Release();
  652. if( funcId )
  653. engine->scriptFunctions[funcId]->Release();
  654. }
  655. break;
  656. // Functions
  657. case asBC_CALL:
  658. case asBC_CALLINTF:
  659. {
  660. int func = asBC_INTARG(&byteCode[n]);
  661. if( func )
  662. {
  663. asCScriptFunction *fptr = engine->scriptFunctions[func];
  664. if( fptr )
  665. fptr->Release();
  666. // The engine may have been forced to destroy the function internals early
  667. // and this may will make it impossible to find the function by id anymore.
  668. // This should only happen if the engine is released while the application
  669. // is still keeping functions alive.
  670. // TODO: Fix this possible memory leak
  671. }
  672. }
  673. break;
  674. // Function pointers
  675. case asBC_FuncPtr:
  676. {
  677. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&byteCode[n]);
  678. if( func )
  679. func->Release();
  680. }
  681. break;
  682. }
  683. }
  684. // Release the jit compiled function
  685. if( jitFunction )
  686. engine->jitCompiler->ReleaseJITFunction(jitFunction);
  687. jitFunction = 0;
  688. }
  689. // interface
  690. int asCScriptFunction::GetReturnTypeId() const
  691. {
  692. return engine->GetTypeIdFromDataType(returnType);
  693. }
  694. // interface
  695. asUINT asCScriptFunction::GetParamCount() const
  696. {
  697. return (asUINT)parameterTypes.GetLength();
  698. }
  699. // interface
  700. int asCScriptFunction::GetParamTypeId(asUINT index, asDWORD *flags) const
  701. {
  702. if( index >= parameterTypes.GetLength() )
  703. return asINVALID_ARG;
  704. if( flags )
  705. *flags = inOutFlags[index];
  706. return engine->GetTypeIdFromDataType(parameterTypes[index]);
  707. }
  708. // interface
  709. asIScriptEngine *asCScriptFunction::GetEngine() const
  710. {
  711. return engine;
  712. }
  713. // interface
  714. const char *asCScriptFunction::GetDeclaration(bool includeObjectName, bool includeNamespace) const
  715. {
  716. asCString *tempString = &asCThreadManager::GetLocalData()->string;
  717. *tempString = GetDeclarationStr(includeObjectName, includeNamespace);
  718. return tempString->AddressOf();
  719. }
  720. // interface
  721. const char *asCScriptFunction::GetScriptSectionName() const
  722. {
  723. if( scriptSectionIdx >= 0 )
  724. return engine->scriptSectionNames[scriptSectionIdx]->AddressOf();
  725. return 0;
  726. }
  727. // interface
  728. const char *asCScriptFunction::GetConfigGroup() const
  729. {
  730. asCConfigGroup *group = 0;
  731. if( funcType != asFUNC_FUNCDEF )
  732. group = engine->FindConfigGroupForFunction(id);
  733. else
  734. group = engine->FindConfigGroupForFuncDef(this);
  735. if( group == 0 )
  736. return 0;
  737. return group->groupName.AddressOf();
  738. }
  739. // interface
  740. asDWORD asCScriptFunction::GetAccessMask() const
  741. {
  742. return accessMask;
  743. }
  744. // internal
  745. void asCScriptFunction::JITCompile()
  746. {
  747. asIJITCompiler *jit = engine->GetJITCompiler();
  748. if( !jit )
  749. return;
  750. // Release the previous function, if any
  751. if( jitFunction )
  752. {
  753. engine->jitCompiler->ReleaseJITFunction(jitFunction);
  754. jitFunction = 0;
  755. }
  756. // Compile for native system
  757. int r = jit->CompileFunction(this, &jitFunction);
  758. if( r < 0 )
  759. {
  760. asASSERT( jitFunction == 0 );
  761. }
  762. }
  763. // interface
  764. asDWORD *asCScriptFunction::GetByteCode(asUINT *length)
  765. {
  766. if( length )
  767. *length = (asUINT)byteCode.GetLength();
  768. if( byteCode.GetLength() )
  769. {
  770. return byteCode.AddressOf();
  771. }
  772. return 0;
  773. }
  774. // interface
  775. void *asCScriptFunction::SetUserData(void *data)
  776. {
  777. void *oldData = userData;
  778. userData = data;
  779. return oldData;
  780. }
  781. // interface
  782. void *asCScriptFunction::GetUserData() const
  783. {
  784. return userData;
  785. }
  786. // internal
  787. asCGlobalProperty *asCScriptFunction::GetPropertyByGlobalVarPtr(void *gvarPtr)
  788. {
  789. for( asUINT g = 0; g < engine->globalProperties.GetLength(); g++ )
  790. if( engine->globalProperties[g] && engine->globalProperties[g]->GetAddressOfValue() == gvarPtr )
  791. return engine->globalProperties[g];
  792. return 0;
  793. }
  794. // internal
  795. int asCScriptFunction::GetRefCount()
  796. {
  797. return refCount.get();
  798. }
  799. // internal
  800. void asCScriptFunction::SetFlag()
  801. {
  802. gcFlag = true;
  803. }
  804. // internal
  805. bool asCScriptFunction::GetFlag()
  806. {
  807. return gcFlag;
  808. }
  809. // internal
  810. void asCScriptFunction::EnumReferences(asIScriptEngine *)
  811. {
  812. // Notify the GC of all object types used
  813. if( returnType.IsObject() )
  814. engine->GCEnumCallback(returnType.GetObjectType());
  815. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  816. if( parameterTypes[p].IsObject() )
  817. engine->GCEnumCallback(parameterTypes[p].GetObjectType());
  818. for( asUINT t = 0; t < objVariableTypes.GetLength(); t++ )
  819. engine->GCEnumCallback(objVariableTypes[t]);
  820. // Notify the GC of all script functions that is accessed
  821. for( asUINT n = 0; n < byteCode.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&byteCode[n]].type] )
  822. {
  823. switch( *(asBYTE*)&byteCode[n] )
  824. {
  825. case asBC_OBJTYPE:
  826. case asBC_FREE:
  827. case asBC_REFCPY:
  828. case asBC_RefCpyV:
  829. {
  830. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  831. engine->GCEnumCallback(objType);
  832. }
  833. break;
  834. case asBC_ALLOC:
  835. {
  836. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  837. engine->GCEnumCallback(objType);
  838. int func = asBC_INTARG(&byteCode[n]+AS_PTR_SIZE);
  839. if( func )
  840. engine->GCEnumCallback(engine->scriptFunctions[func]);
  841. }
  842. break;
  843. case asBC_CALL:
  844. case asBC_CALLINTF:
  845. {
  846. int func = asBC_INTARG(&byteCode[n]);
  847. if( func )
  848. engine->GCEnumCallback(engine->scriptFunctions[func]);
  849. }
  850. break;
  851. // Function pointers
  852. case asBC_FuncPtr:
  853. {
  854. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&byteCode[n]);
  855. if( func )
  856. engine->GCEnumCallback(func);
  857. }
  858. break;
  859. // Global variables
  860. case asBC_PGA:
  861. case asBC_PshGPtr:
  862. case asBC_LDG:
  863. case asBC_PshG4:
  864. case asBC_LdGRdR4:
  865. case asBC_CpyGtoV4:
  866. case asBC_CpyVtoG4:
  867. case asBC_SetG4:
  868. // Need to enumerate the reference for each global variable
  869. {
  870. // TODO: optimize: Keep an array of accessed global properties
  871. void *gvarPtr = (void*)asBC_PTRARG(&byteCode[n]);
  872. asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);
  873. engine->GCEnumCallback(prop);
  874. }
  875. break;
  876. }
  877. }
  878. }
  879. // internal
  880. void asCScriptFunction::ReleaseAllHandles(asIScriptEngine *)
  881. {
  882. // Release paramaters
  883. if( byteCode.GetLength() )
  884. {
  885. if( returnType.IsObject() )
  886. {
  887. returnType.GetObjectType()->Release();
  888. returnType = asCDataType::CreatePrimitive(ttVoid, false);
  889. }
  890. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  891. if( parameterTypes[p].IsObject() )
  892. {
  893. parameterTypes[p].GetObjectType()->Release();
  894. parameterTypes[p] = asCDataType::CreatePrimitive(ttInt, false);
  895. }
  896. for( asUINT n = 0; n < objVariableTypes.GetLength(); n++ )
  897. objVariableTypes[n]->Release();
  898. objVariableTypes.SetLength(0);
  899. }
  900. // Release all script functions
  901. for( asUINT n = 0; n < byteCode.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&byteCode[n]].type] )
  902. {
  903. switch( *(asBYTE*)&byteCode[n] )
  904. {
  905. // Object types
  906. case asBC_OBJTYPE:
  907. case asBC_FREE:
  908. case asBC_REFCPY:
  909. case asBC_RefCpyV:
  910. {
  911. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  912. if( objType )
  913. {
  914. objType->Release();
  915. *(void**)&byteCode[n+1] = 0;
  916. }
  917. }
  918. break;
  919. case asBC_ALLOC:
  920. {
  921. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  922. if( objType )
  923. {
  924. objType->Release();
  925. * (void**)&byteCode[n+1] = 0;
  926. }
  927. int func = asBC_INTARG(&byteCode[n]+AS_PTR_SIZE);
  928. if( func )
  929. {
  930. engine->scriptFunctions[func]->Release();
  931. byteCode[n+AS_PTR_SIZE+1] = 0;
  932. }
  933. }
  934. break;
  935. case asBC_CALL:
  936. case asBC_CALLINTF:
  937. {
  938. int func = asBC_INTARG(&byteCode[n]);
  939. if( func )
  940. {
  941. engine->scriptFunctions[func]->Release();
  942. byteCode[n+1] = 0;
  943. }
  944. }
  945. break;
  946. // Function pointers
  947. case asBC_FuncPtr:
  948. {
  949. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&byteCode[n]);
  950. if( func )
  951. {
  952. func->Release();
  953. *(asPWORD*)&byteCode[n+1] = 0;
  954. }
  955. }
  956. break;
  957. // The global variables are not released here. It is enough that the global
  958. // variable itself release the function to break the circle
  959. }
  960. }
  961. }
  962. // internal
  963. bool asCScriptFunction::IsShared() const
  964. {
  965. // All system functions are shared
  966. if( funcType == asFUNC_SYSTEM ) return true;
  967. // All class methods for shared classes are also shared
  968. if( objectType && (objectType->flags & asOBJ_SHARED) ) return true;
  969. // Functions that have been specifically marked as shared are shared
  970. return isShared;
  971. }
  972. // internal
  973. bool asCScriptFunction::IsFinal() const
  974. {
  975. return isFinal;
  976. }
  977. // internal
  978. bool asCScriptFunction::IsOverride() const
  979. {
  980. return isOverride;
  981. }
  982. END_AS_NAMESPACE