as_scriptfunction.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2013 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_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. static void ScriptFunction_CreateDelegate_Generic(asIScriptGeneric *gen)
  75. {
  76. asCScriptFunction *func = (asCScriptFunction*)gen->GetArgAddress(0);
  77. void *obj = gen->GetArgAddress(1);
  78. gen->SetReturnAddress(CreateDelegate(func, obj));
  79. }
  80. #endif
  81. void RegisterScriptFunction(asCScriptEngine *engine)
  82. {
  83. // Register the gc behaviours for the script functions
  84. int r = 0;
  85. UNUSED_VAR(r); // It is only used in debug mode
  86. engine->functionBehaviours.engine = engine;
  87. engine->functionBehaviours.flags = asOBJ_REF | asOBJ_GC | asOBJ_SCRIPT_FUNCTION;
  88. engine->functionBehaviours.name = "_builtin_function_";
  89. #ifndef AS_MAX_PORTABILITY
  90. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCScriptFunction,AddRef), asCALL_THISCALL); asASSERT( r >= 0 );
  91. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCScriptFunction,Release), asCALL_THISCALL); asASSERT( r >= 0 );
  92. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCScriptFunction,GetRefCount), asCALL_THISCALL); asASSERT( r >= 0 );
  93. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCScriptFunction,SetFlag), asCALL_THISCALL); asASSERT( r >= 0 );
  94. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCScriptFunction,GetFlag), asCALL_THISCALL); asASSERT( r >= 0 );
  95. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCScriptFunction,EnumReferences), asCALL_THISCALL); asASSERT( r >= 0 );
  96. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCScriptFunction,ReleaseAllHandles), asCALL_THISCALL); asASSERT( r >= 0 );
  97. #else
  98. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(ScriptFunction_AddRef_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  99. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(ScriptFunction_Release_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  100. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(ScriptFunction_GetRefCount_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  101. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(ScriptFunction_SetFlag_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  102. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(ScriptFunction_GetFlag_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  103. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(ScriptFunction_EnumReferences_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  104. r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(ScriptFunction_ReleaseAllHandles_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  105. #endif
  106. // Register the builtin function for creating delegates
  107. // This function returns a handle to the delegate, but since the type is not known at this time it is
  108. // registered to return a void then the return type is changed manually to the builtin function type
  109. // The name of the function is an invalid identifier so it cannot be invoked accidentally from the script
  110. #ifndef AS_MAX_PORTABILITY
  111. r = engine->RegisterGlobalFunction("void f(int &in, int &in)", asFUNCTION(CreateDelegate), asCALL_CDECL); asASSERT( r >= 0 );
  112. #else
  113. r = engine->RegisterGlobalFunction("void f(int &int, int &in)", asFUNCTION(ScriptFunction_CreateDelegate_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
  114. #endif
  115. // Change the return type so the VM will know the function really returns a handle
  116. engine->scriptFunctions[r]->name = DELEGATE_FACTORY;
  117. engine->scriptFunctions[r]->returnType = asCDataType::CreateObject(&engine->functionBehaviours, false);
  118. engine->scriptFunctions[r]->returnType.MakeHandle(true);
  119. }
  120. asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj)
  121. {
  122. if( func == 0 || obj == 0 )
  123. {
  124. // TODO: delegate: Should set script exception
  125. return 0;
  126. }
  127. // Create an instance of a asCScriptFunction with the type asFUNC_DELEGATE
  128. // The delegate shouldn't have a function id and is not added to the engine->scriptFunctions
  129. asCScriptFunction *delegate = asNEW(asCScriptFunction)(static_cast<asCScriptEngine*>(func->GetEngine()), 0, asFUNC_DELEGATE);
  130. if( delegate )
  131. delegate->MakeDelegate(func, obj);
  132. return delegate;
  133. }
  134. // internal
  135. void asCScriptFunction::MakeDelegate(asCScriptFunction *func, void *obj)
  136. {
  137. // Increase the reference of the function and object
  138. func->AddRef();
  139. funcForDelegate = func;
  140. func->GetEngine()->AddRefScriptObject(obj, func->GetObjectType());
  141. objForDelegate = obj;
  142. // The return type and parameters are copied from the delegated method to this object
  143. // TODO: optimize: Do we really need to copy? Whenever requested the delegate can simply return the delegated methods' info directly
  144. parameterTypes = func->parameterTypes;
  145. returnType = func->returnType;
  146. inOutFlags = func->inOutFlags;
  147. // The delegate doesn't own the parameters as it will only forward them to the real method
  148. // so the exception handler must not clean up the parameters for the delegate
  149. dontCleanUpOnException = true;
  150. }
  151. // internal
  152. asCScriptFunction::asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType _funcType)
  153. {
  154. refCount.set(1);
  155. this->engine = engine;
  156. funcType = _funcType;
  157. module = mod;
  158. objectType = 0;
  159. name = "";
  160. isReadOnly = false;
  161. isPrivate = false;
  162. isFinal = false;
  163. isOverride = false;
  164. stackNeeded = 0;
  165. sysFuncIntf = 0;
  166. signatureId = 0;
  167. scriptSectionIdx = -1;
  168. dontCleanUpOnException = false;
  169. vfTableIdx = -1;
  170. jitFunction = 0;
  171. gcFlag = false;
  172. userData = 0;
  173. id = 0;
  174. accessMask = 0xFFFFFFFF;
  175. isShared = false;
  176. variableSpace = 0;
  177. nameSpace = engine->nameSpaces[0];
  178. objForDelegate = 0;
  179. funcForDelegate = 0;
  180. // Notify the GC of script functions
  181. if( (funcType == asFUNC_SCRIPT && mod == 0) || (funcType == asFUNC_DELEGATE) )
  182. engine->gc.AddScriptObjectToGC(this, &engine->functionBehaviours);
  183. }
  184. // internal
  185. asCScriptFunction::~asCScriptFunction()
  186. {
  187. // Imported functions are not reference counted, nor are dummy
  188. // functions that are allocated on the stack
  189. asASSERT( funcType == asFUNC_DUMMY ||
  190. funcType == asFUNC_IMPORTED ||
  191. refCount.get() == 0 );
  192. // If the engine pointer is 0, then DestroyInternal has already been called and there is nothing more to do
  193. if( engine == 0 ) return;
  194. DestroyInternal();
  195. // Tell engine to free the function id. This will make it impossible to
  196. // refer to the function by id. Where this is done, it is quite possible
  197. // they will leak.
  198. if( funcType != -1 && funcType != asFUNC_IMPORTED && id )
  199. engine->FreeScriptFunctionId(id);
  200. id = 0;
  201. // Finally set the engine pointer to 0 because it must not be accessed again
  202. engine = 0;
  203. }
  204. // internal
  205. void asCScriptFunction::DestroyInternal()
  206. {
  207. // Clean up user data
  208. if( userData && engine->cleanFunctionFunc )
  209. engine->cleanFunctionFunc(this);
  210. userData = 0;
  211. // Release all references the function holds to other objects
  212. ReleaseReferences();
  213. parameterTypes.SetLength(0);
  214. returnType = asCDataType::CreatePrimitive(ttVoid, false);
  215. byteCode.SetLength(0);
  216. for( asUINT n = 0; n < variables.GetLength(); n++ )
  217. asDELETE(variables[n],asSScriptVariable);
  218. variables.SetLength(0);
  219. for( asUINT p = 0; p < defaultArgs.GetLength(); p++ )
  220. if( defaultArgs[p] )
  221. asDELETE(defaultArgs[p], asCString);
  222. defaultArgs.SetLength(0);
  223. if( sysFuncIntf )
  224. asDELETE(sysFuncIntf,asSSystemFunctionInterface);
  225. sysFuncIntf = 0;
  226. }
  227. // interface
  228. int asCScriptFunction::GetId() const
  229. {
  230. return id;
  231. }
  232. // interface
  233. int asCScriptFunction::AddRef() const
  234. {
  235. gcFlag = false;
  236. asASSERT( funcType != asFUNC_IMPORTED );
  237. return refCount.atomicInc();
  238. }
  239. // interface
  240. int asCScriptFunction::Release() const
  241. {
  242. gcFlag = false;
  243. asASSERT( funcType != asFUNC_IMPORTED );
  244. int r = refCount.atomicDec();
  245. if( r == 0 &&
  246. funcType != asFUNC_FUNCDEF && // Funcdefs are treated as object types and will be deleted by ClearUnusedTypes()
  247. funcType != asFUNC_DUMMY ) // Dummy functions are allocated on the stack and cannot be deleted
  248. asDELETE(const_cast<asCScriptFunction*>(this),asCScriptFunction);
  249. return r;
  250. }
  251. // internal
  252. void asCScriptFunction::Orphan(asIScriptModule *mod)
  253. {
  254. if( mod && module == mod )
  255. {
  256. module = 0;
  257. if( funcType == asFUNC_SCRIPT && refCount.get() > 1 )
  258. {
  259. // This function is being orphaned, so notify the GC so it can check for circular references
  260. engine->gc.AddScriptObjectToGC(this, &engine->functionBehaviours);
  261. }
  262. }
  263. Release();
  264. }
  265. // interface
  266. int asCScriptFunction::GetTypeId() const
  267. {
  268. // This const cast is ok, the object won't be modified
  269. asCDataType dt = asCDataType::CreateFuncDef(const_cast<asCScriptFunction*>(this));
  270. return engine->GetTypeIdFromDataType(dt);
  271. }
  272. // interface
  273. bool asCScriptFunction::IsCompatibleWithTypeId(int typeId) const
  274. {
  275. asCDataType dt = engine->GetDataTypeFromTypeId(typeId);
  276. // Make sure the type is a function
  277. asCScriptFunction *func = dt.GetFuncDef();
  278. if( func == 0 )
  279. return false;
  280. if( !IsSignatureExceptNameEqual(func) )
  281. return false;
  282. // If this is a class method, then only return true if the object type is the same
  283. if( objectType != func->objectType )
  284. return false;
  285. return true;
  286. }
  287. // interface
  288. const char *asCScriptFunction::GetModuleName() const
  289. {
  290. if( module )
  291. {
  292. return module->name.AddressOf();
  293. }
  294. return 0;
  295. }
  296. // interface
  297. asIObjectType *asCScriptFunction::GetObjectType() const
  298. {
  299. return objectType;
  300. }
  301. // interface
  302. const char *asCScriptFunction::GetObjectName() const
  303. {
  304. if( objectType )
  305. return objectType->GetName();
  306. return 0;
  307. }
  308. // interface
  309. const char *asCScriptFunction::GetName() const
  310. {
  311. return name.AddressOf();
  312. }
  313. // interface
  314. const char *asCScriptFunction::GetNamespace() const
  315. {
  316. return nameSpace->name.AddressOf();
  317. }
  318. // interface
  319. bool asCScriptFunction::IsReadOnly() const
  320. {
  321. return isReadOnly;
  322. }
  323. // interface
  324. bool asCScriptFunction::IsPrivate() const
  325. {
  326. return isPrivate;
  327. }
  328. // internal
  329. int asCScriptFunction::GetSpaceNeededForArguments()
  330. {
  331. // We need to check the size for each type
  332. int s = 0;
  333. for( asUINT n = 0; n < parameterTypes.GetLength(); n++ )
  334. s += parameterTypes[n].GetSizeOnStackDWords();
  335. return s;
  336. }
  337. // internal
  338. int asCScriptFunction::GetSpaceNeededForReturnValue()
  339. {
  340. return returnType.GetSizeOnStackDWords();
  341. }
  342. // internal
  343. bool asCScriptFunction::DoesReturnOnStack() const
  344. {
  345. if( returnType.GetObjectType() &&
  346. (returnType.GetObjectType()->flags & asOBJ_VALUE) &&
  347. !returnType.IsReference() )
  348. return true;
  349. return false;
  350. }
  351. // internal
  352. asCString asCScriptFunction::GetDeclarationStr(bool includeObjectName, bool includeNamespace) const
  353. {
  354. asCString str;
  355. // TODO: default arg: Make the declaration with the default args an option
  356. // Don't add the return type for constructors and destructors
  357. if( !(returnType.GetTokenType() == ttVoid &&
  358. objectType &&
  359. (name == objectType->name || (name.GetLength() > 0 && name[0] == '~'))) )
  360. {
  361. str = returnType.Format();
  362. str += " ";
  363. }
  364. if( objectType && includeObjectName )
  365. {
  366. if( includeNamespace )
  367. str += objectType->nameSpace->name + "::";
  368. if( objectType->name != "" )
  369. str += objectType->name + "::";
  370. else
  371. str += "_unnamed_type_::";
  372. }
  373. else if( includeNamespace )
  374. {
  375. str += nameSpace->name + "::";
  376. }
  377. if( name == "" )
  378. str += "_unnamed_function_(";
  379. else
  380. str += name + "(";
  381. if( parameterTypes.GetLength() > 0 )
  382. {
  383. asUINT n;
  384. for( n = 0; n < parameterTypes.GetLength() - 1; n++ )
  385. {
  386. str += parameterTypes[n].Format();
  387. if( parameterTypes[n].IsReference() && inOutFlags.GetLength() > n )
  388. {
  389. if( inOutFlags[n] == asTM_INREF ) str += "in";
  390. else if( inOutFlags[n] == asTM_OUTREF ) str += "out";
  391. else if( inOutFlags[n] == asTM_INOUTREF ) str += "inout";
  392. }
  393. if( defaultArgs.GetLength() > n && defaultArgs[n] )
  394. {
  395. asCString tmp;
  396. tmp.Format(" arg%d = %s", n, defaultArgs[n]->AddressOf());
  397. str += tmp;
  398. }
  399. str += ", ";
  400. }
  401. // Add the last parameter
  402. str += parameterTypes[n].Format();
  403. if( parameterTypes[n].IsReference() && inOutFlags.GetLength() > n )
  404. {
  405. if( inOutFlags[n] == asTM_INREF ) str += "in";
  406. else if( inOutFlags[n] == asTM_OUTREF ) str += "out";
  407. else if( inOutFlags[n] == asTM_INOUTREF ) str += "inout";
  408. }
  409. if( defaultArgs.GetLength() > n && defaultArgs[n] )
  410. {
  411. asCString tmp;
  412. tmp.Format(" arg%d = %s", n, defaultArgs[n]->AddressOf());
  413. str += tmp;
  414. }
  415. }
  416. str += ")";
  417. if( isReadOnly )
  418. str += " const";
  419. return str;
  420. }
  421. // interface
  422. int asCScriptFunction::FindNextLineWithCode(int line) const
  423. {
  424. if( lineNumbers.GetLength() == 0 ) return -1;
  425. // Check if given line is outside function
  426. // TODO: should start at declaration instead of first line of code
  427. if( line < (lineNumbers[1]&0xFFFFF) ) return -1;
  428. if( line > (lineNumbers[lineNumbers.GetLength()-1]&0xFFFFF) ) return -1;
  429. // Find the line with code on or right after the input line
  430. // TODO: optimize: Do binary search instead
  431. if( line == (lineNumbers[1]&0xFFFFF) ) return line;
  432. for( asUINT n = 3; n < lineNumbers.GetLength(); n += 2 )
  433. {
  434. if( line <= (lineNumbers[n]&0xFFFFF) )
  435. return (lineNumbers[n]&0xFFFFF);
  436. }
  437. return -1;
  438. }
  439. // internal
  440. int asCScriptFunction::GetLineNumber(int programPosition, int *sectionIdx)
  441. {
  442. if( sectionIdx ) *sectionIdx = scriptSectionIdx;
  443. if( lineNumbers.GetLength() == 0 ) return 0;
  444. if( sectionIdx )
  445. {
  446. // Find the correct section index if the function is compiled from multiple sections
  447. // This array will be empty most of the time so we don't need a sofisticated algorithm to search it
  448. for( asUINT n = 0; n < sectionIdxs.GetLength(); n += 2 )
  449. {
  450. if( sectionIdxs[n] <= programPosition )
  451. *sectionIdx = sectionIdxs[n+1];
  452. }
  453. }
  454. // Do a binary search in the buffer
  455. int max = (int)lineNumbers.GetLength()/2 - 1;
  456. int min = 0;
  457. int i = max/2;
  458. for(;;)
  459. {
  460. if( lineNumbers[i*2] < programPosition )
  461. {
  462. // Have we found the largest number < programPosition?
  463. if( max == i ) return lineNumbers[i*2+1];
  464. if( lineNumbers[i*2+2] > programPosition ) return lineNumbers[i*2+1];
  465. min = i + 1;
  466. i = (max + min)/2;
  467. }
  468. else if( lineNumbers[i*2] > programPosition )
  469. {
  470. // Have we found the smallest number > programPosition?
  471. if( min == i ) return lineNumbers[i*2+1];
  472. max = i - 1;
  473. i = (max + min)/2;
  474. }
  475. else
  476. {
  477. // We found the exact position
  478. return lineNumbers[i*2+1];
  479. }
  480. }
  481. }
  482. // interface
  483. asEFuncType asCScriptFunction::GetFuncType() const
  484. {
  485. return funcType;
  486. }
  487. // interface
  488. asUINT asCScriptFunction::GetVarCount() const
  489. {
  490. return int(variables.GetLength());
  491. }
  492. // interface
  493. int asCScriptFunction::GetVar(asUINT index, const char **name, int *typeId) const
  494. {
  495. if( index >= variables.GetLength() )
  496. return asINVALID_ARG;
  497. if( name )
  498. *name = variables[index]->name.AddressOf();
  499. if( typeId )
  500. *typeId = engine->GetTypeIdFromDataType(variables[index]->type);
  501. return asSUCCESS;
  502. }
  503. // interface
  504. const char *asCScriptFunction::GetVarDecl(asUINT index) const
  505. {
  506. if( index >= variables.GetLength() )
  507. return 0;
  508. asCString *tempString = &asCThreadManager::GetLocalData()->string;
  509. *tempString = variables[index]->type.Format();
  510. *tempString += " " + variables[index]->name;
  511. return tempString->AddressOf();
  512. }
  513. // internal
  514. void asCScriptFunction::AddVariable(asCString &name, asCDataType &type, int stackOffset)
  515. {
  516. asSScriptVariable *var = asNEW(asSScriptVariable);
  517. if( var == 0 )
  518. {
  519. // Out of memory
  520. return;
  521. }
  522. var->name = name;
  523. var->type = type;
  524. var->stackOffset = stackOffset;
  525. var->declaredAtProgramPos = 0;
  526. variables.PushLast(var);
  527. }
  528. // internal
  529. void asCScriptFunction::ComputeSignatureId()
  530. {
  531. // This function will compute the signatureId based on the
  532. // function name, return type, and parameter types. The object
  533. // type for methods is not used, so that class methods and
  534. // interface methods match each other.
  535. for( asUINT n = 0; n < engine->signatureIds.GetLength(); n++ )
  536. {
  537. if( !IsSignatureEqual(engine->signatureIds[n]) ) continue;
  538. // We don't need to increment the reference counter here, because
  539. // asCScriptEngine::FreeScriptFunctionId will maintain the signature
  540. // id as the function is freed.
  541. signatureId = engine->signatureIds[n]->signatureId;
  542. return;
  543. }
  544. signatureId = id;
  545. engine->signatureIds.PushLast(this);
  546. }
  547. // internal
  548. bool asCScriptFunction::IsSignatureEqual(const asCScriptFunction *func) const
  549. {
  550. if( !IsSignatureExceptNameEqual(func) || name != func->name ) return false;
  551. return true;
  552. }
  553. // internal
  554. bool asCScriptFunction::IsSignatureExceptNameEqual(const asCScriptFunction *func) const
  555. {
  556. return IsSignatureExceptNameEqual(func->returnType, func->parameterTypes, func->inOutFlags, func->objectType, func->isReadOnly);
  557. }
  558. // internal
  559. bool asCScriptFunction::IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &paramInOut, const asCObjectType *objType, bool readOnly) const
  560. {
  561. if( this->returnType != retType ) return false;
  562. return IsSignatureExceptNameAndReturnTypeEqual(paramTypes, paramInOut, objType, readOnly);
  563. }
  564. // internal
  565. bool asCScriptFunction::IsSignatureExceptNameAndObjectTypeEqual(const asCScriptFunction *func) const
  566. {
  567. return IsSignatureExceptNameEqual(func->returnType, func->parameterTypes, func->inOutFlags, objectType, isReadOnly);
  568. }
  569. // internal
  570. bool asCScriptFunction::IsSignatureExceptNameAndReturnTypeEqual(const asCScriptFunction *func) const
  571. {
  572. return IsSignatureExceptNameAndReturnTypeEqual(func->parameterTypes, func->inOutFlags, func->objectType, func->isReadOnly);
  573. }
  574. // internal
  575. bool asCScriptFunction::IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &paramInOut, const asCObjectType *objType, bool readOnly) const
  576. {
  577. if( this->isReadOnly != readOnly ) return false;
  578. if( this->inOutFlags != paramInOut ) return false;
  579. if( this->parameterTypes != paramTypes ) return false;
  580. if( (this->objectType != 0) != (objType != 0) ) return false;
  581. return true;
  582. }
  583. // internal
  584. void asCScriptFunction::AddReferences()
  585. {
  586. asUINT n;
  587. // This array will be used to make sure we only add the reference to the same resource once
  588. // This is especially important for global variables, as it expects the initialization function
  589. // to hold only one reference to the variable. However, if the variable is initialized through
  590. // the default constructor followed by the assignment operator we will have two references to
  591. // the variable in the function.
  592. asCArray<void*> ptrs;
  593. // Only count references if there is any bytecode
  594. if( byteCode.GetLength() )
  595. {
  596. if( returnType.IsObject() )
  597. returnType.GetObjectType()->AddRef();
  598. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  599. if( parameterTypes[p].IsObject() )
  600. parameterTypes[p].GetObjectType()->AddRef();
  601. for( asUINT n = 0; n < objVariableTypes.GetLength(); n++ )
  602. objVariableTypes[n]->AddRef();
  603. }
  604. // Go through the byte code and add references to all resources used by the function
  605. for( n = 0; n < byteCode.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&byteCode[n]].type] )
  606. {
  607. switch( *(asBYTE*)&byteCode[n] )
  608. {
  609. // Object types
  610. case asBC_OBJTYPE:
  611. case asBC_FREE:
  612. case asBC_REFCPY:
  613. case asBC_RefCpyV:
  614. {
  615. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  616. objType->AddRef();
  617. }
  618. break;
  619. // Object type and function
  620. case asBC_ALLOC:
  621. {
  622. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  623. objType->AddRef();
  624. int func = asBC_INTARG(&byteCode[n]+AS_PTR_SIZE);
  625. if( func )
  626. engine->scriptFunctions[func]->AddRef();
  627. }
  628. break;
  629. // Global variables
  630. case asBC_PGA:
  631. case asBC_PshGPtr:
  632. case asBC_LDG:
  633. case asBC_PshG4:
  634. case asBC_LdGRdR4:
  635. case asBC_CpyGtoV4:
  636. case asBC_CpyVtoG4:
  637. case asBC_SetG4:
  638. // Need to increase the reference for each global variable
  639. {
  640. void *gvarPtr = (void*)asBC_PTRARG(&byteCode[n]);
  641. if( !gvarPtr ) break;
  642. asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);
  643. if( !prop ) break;
  644. // Only addref the properties once
  645. if( !ptrs.Exists(gvarPtr) )
  646. {
  647. prop->AddRef();
  648. ptrs.PushLast(gvarPtr);
  649. }
  650. asCConfigGroup *group = engine->FindConfigGroupForGlobalVar(prop->id);
  651. if( group != 0 ) group->AddRef();
  652. }
  653. break;
  654. // System functions
  655. case asBC_CALLSYS:
  656. {
  657. int funcId = asBC_INTARG(&byteCode[n]);
  658. asCConfigGroup *group = engine->FindConfigGroupForFunction(funcId);
  659. if( group != 0 ) group->AddRef();
  660. engine->scriptFunctions[funcId]->AddRef();
  661. }
  662. break;
  663. // Functions
  664. case asBC_CALL:
  665. case asBC_CALLINTF:
  666. {
  667. int func = asBC_INTARG(&byteCode[n]);
  668. engine->scriptFunctions[func]->AddRef();
  669. }
  670. break;
  671. // Function pointers
  672. case asBC_FuncPtr:
  673. {
  674. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&byteCode[n]);
  675. func->AddRef();
  676. }
  677. break;
  678. }
  679. }
  680. }
  681. // internal
  682. void asCScriptFunction::ReleaseReferences()
  683. {
  684. asUINT n;
  685. asCArray<void*> ptrs;
  686. // Only count references if there is any bytecode
  687. if( byteCode.GetLength() )
  688. {
  689. if( returnType.IsObject() )
  690. returnType.GetObjectType()->Release();
  691. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  692. if( parameterTypes[p].IsObject() )
  693. parameterTypes[p].GetObjectType()->Release();
  694. for( asUINT n = 0; n < objVariableTypes.GetLength(); n++ )
  695. if( objVariableTypes[n] )
  696. objVariableTypes[n]->Release();
  697. }
  698. // Go through the byte code and release references to all resources used by the function
  699. for( n = 0; n < byteCode.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&byteCode[n]].type] )
  700. {
  701. switch( *(asBYTE*)&byteCode[n] )
  702. {
  703. // Object types
  704. case asBC_OBJTYPE:
  705. case asBC_FREE:
  706. case asBC_REFCPY:
  707. case asBC_RefCpyV:
  708. {
  709. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  710. if( objType )
  711. objType->Release();
  712. }
  713. break;
  714. // Object type and function
  715. case asBC_ALLOC:
  716. {
  717. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  718. if( objType )
  719. objType->Release();
  720. int func = asBC_INTARG(&byteCode[n]+AS_PTR_SIZE);
  721. if( func )
  722. {
  723. asCScriptFunction *fptr = engine->scriptFunctions[func];
  724. if( fptr )
  725. fptr->Release();
  726. // The engine may have been forced to destroy the function internals early
  727. // and this may will make it impossible to find the function by id anymore.
  728. // This should only happen if the engine is released while the application
  729. // is still keeping functions alive.
  730. // TODO: Fix this possible memory leak
  731. }
  732. }
  733. break;
  734. // Global variables
  735. case asBC_PGA:
  736. case asBC_PshGPtr:
  737. case asBC_LDG:
  738. case asBC_PshG4:
  739. case asBC_LdGRdR4:
  740. case asBC_CpyGtoV4:
  741. case asBC_CpyVtoG4:
  742. case asBC_SetG4:
  743. // Need to increase the reference for each global variable
  744. {
  745. void *gvarPtr = (void*)asBC_PTRARG(&byteCode[n]);
  746. if( !gvarPtr ) break;
  747. asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);
  748. if( !prop ) break;
  749. // Only release the properties once
  750. if( !ptrs.Exists(gvarPtr) )
  751. {
  752. prop->Release();
  753. ptrs.PushLast(gvarPtr);
  754. }
  755. asCConfigGroup *group = engine->FindConfigGroupForGlobalVar(prop->id);
  756. if( group != 0 ) group->Release();
  757. }
  758. break;
  759. // System functions
  760. case asBC_CALLSYS:
  761. {
  762. int funcId = asBC_INTARG(&byteCode[n]);
  763. asCConfigGroup *group = engine->FindConfigGroupForFunction(funcId);
  764. if( group != 0 ) group->Release();
  765. if( funcId )
  766. engine->scriptFunctions[funcId]->Release();
  767. }
  768. break;
  769. // Functions
  770. case asBC_CALL:
  771. case asBC_CALLINTF:
  772. {
  773. int func = asBC_INTARG(&byteCode[n]);
  774. if( func )
  775. {
  776. asCScriptFunction *fptr = engine->scriptFunctions[func];
  777. if( fptr )
  778. fptr->Release();
  779. // The engine may have been forced to destroy the function internals early
  780. // and this may will make it impossible to find the function by id anymore.
  781. // This should only happen if the engine is released while the application
  782. // is still keeping functions alive.
  783. // TODO: Fix this possible memory leak
  784. }
  785. }
  786. break;
  787. // Function pointers
  788. case asBC_FuncPtr:
  789. {
  790. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&byteCode[n]);
  791. if( func )
  792. func->Release();
  793. }
  794. break;
  795. }
  796. }
  797. // Release the jit compiled function
  798. if( jitFunction )
  799. engine->jitCompiler->ReleaseJITFunction(jitFunction);
  800. jitFunction = 0;
  801. // Delegate
  802. if( objForDelegate )
  803. engine->ReleaseScriptObject(objForDelegate, funcForDelegate->GetObjectType());
  804. objForDelegate = 0;
  805. if( funcForDelegate )
  806. funcForDelegate->Release();
  807. funcForDelegate = 0;
  808. }
  809. // interface
  810. int asCScriptFunction::GetReturnTypeId() const
  811. {
  812. return engine->GetTypeIdFromDataType(returnType);
  813. }
  814. // interface
  815. asUINT asCScriptFunction::GetParamCount() const
  816. {
  817. return (asUINT)parameterTypes.GetLength();
  818. }
  819. // interface
  820. int asCScriptFunction::GetParamTypeId(asUINT index, asDWORD *flags) const
  821. {
  822. if( index >= parameterTypes.GetLength() )
  823. return asINVALID_ARG;
  824. if( flags )
  825. *flags = inOutFlags[index];
  826. return engine->GetTypeIdFromDataType(parameterTypes[index]);
  827. }
  828. // interface
  829. asIScriptEngine *asCScriptFunction::GetEngine() const
  830. {
  831. return engine;
  832. }
  833. // interface
  834. const char *asCScriptFunction::GetDeclaration(bool includeObjectName, bool includeNamespace) const
  835. {
  836. asCString *tempString = &asCThreadManager::GetLocalData()->string;
  837. *tempString = GetDeclarationStr(includeObjectName, includeNamespace);
  838. return tempString->AddressOf();
  839. }
  840. // interface
  841. const char *asCScriptFunction::GetScriptSectionName() const
  842. {
  843. if( scriptSectionIdx >= 0 )
  844. return engine->scriptSectionNames[scriptSectionIdx]->AddressOf();
  845. return 0;
  846. }
  847. // interface
  848. const char *asCScriptFunction::GetConfigGroup() const
  849. {
  850. asCConfigGroup *group = 0;
  851. if( funcType != asFUNC_FUNCDEF )
  852. group = engine->FindConfigGroupForFunction(id);
  853. else
  854. group = engine->FindConfigGroupForFuncDef(this);
  855. if( group == 0 )
  856. return 0;
  857. return group->groupName.AddressOf();
  858. }
  859. // interface
  860. asDWORD asCScriptFunction::GetAccessMask() const
  861. {
  862. return accessMask;
  863. }
  864. // internal
  865. void asCScriptFunction::JITCompile()
  866. {
  867. asIJITCompiler *jit = engine->GetJITCompiler();
  868. if( !jit )
  869. return;
  870. // Release the previous function, if any
  871. if( jitFunction )
  872. {
  873. engine->jitCompiler->ReleaseJITFunction(jitFunction);
  874. jitFunction = 0;
  875. }
  876. // Compile for native system
  877. int r = jit->CompileFunction(this, &jitFunction);
  878. if( r < 0 )
  879. {
  880. asASSERT( jitFunction == 0 );
  881. }
  882. }
  883. // interface
  884. asDWORD *asCScriptFunction::GetByteCode(asUINT *length)
  885. {
  886. if( length )
  887. *length = (asUINT)byteCode.GetLength();
  888. if( byteCode.GetLength() )
  889. {
  890. return byteCode.AddressOf();
  891. }
  892. return 0;
  893. }
  894. // interface
  895. void *asCScriptFunction::SetUserData(void *data)
  896. {
  897. void *oldData = userData;
  898. userData = data;
  899. return oldData;
  900. }
  901. // interface
  902. void *asCScriptFunction::GetUserData() const
  903. {
  904. return userData;
  905. }
  906. // internal
  907. // TODO: cleanup: This method should probably be a member of the engine
  908. asCGlobalProperty *asCScriptFunction::GetPropertyByGlobalVarPtr(void *gvarPtr)
  909. {
  910. asSMapNode<void*, asCGlobalProperty*> *node;
  911. if( engine->varAddressMap.MoveTo(&node, gvarPtr) )
  912. {
  913. asASSERT(gvarPtr == node->value->GetAddressOfValue());
  914. return node->value;
  915. }
  916. return 0;
  917. }
  918. // internal
  919. int asCScriptFunction::GetRefCount()
  920. {
  921. return refCount.get();
  922. }
  923. // internal
  924. void asCScriptFunction::SetFlag()
  925. {
  926. gcFlag = true;
  927. }
  928. // internal
  929. bool asCScriptFunction::GetFlag()
  930. {
  931. return gcFlag;
  932. }
  933. // internal
  934. void asCScriptFunction::EnumReferences(asIScriptEngine *)
  935. {
  936. // Notify the GC of all object types used
  937. if( returnType.IsObject() )
  938. engine->GCEnumCallback(returnType.GetObjectType());
  939. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  940. if( parameterTypes[p].IsObject() )
  941. engine->GCEnumCallback(parameterTypes[p].GetObjectType());
  942. for( asUINT t = 0; t < objVariableTypes.GetLength(); t++ )
  943. engine->GCEnumCallback(objVariableTypes[t]);
  944. // Notify the GC of all script functions that is accessed
  945. for( asUINT n = 0; n < byteCode.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&byteCode[n]].type] )
  946. {
  947. switch( *(asBYTE*)&byteCode[n] )
  948. {
  949. case asBC_OBJTYPE:
  950. case asBC_FREE:
  951. case asBC_REFCPY:
  952. case asBC_RefCpyV:
  953. {
  954. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  955. engine->GCEnumCallback(objType);
  956. }
  957. break;
  958. case asBC_ALLOC:
  959. {
  960. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  961. engine->GCEnumCallback(objType);
  962. int func = asBC_INTARG(&byteCode[n]+AS_PTR_SIZE);
  963. if( func )
  964. engine->GCEnumCallback(engine->scriptFunctions[func]);
  965. }
  966. break;
  967. case asBC_CALL:
  968. case asBC_CALLINTF:
  969. {
  970. int func = asBC_INTARG(&byteCode[n]);
  971. if( func )
  972. engine->GCEnumCallback(engine->scriptFunctions[func]);
  973. }
  974. break;
  975. // Function pointers
  976. case asBC_FuncPtr:
  977. {
  978. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&byteCode[n]);
  979. if( func )
  980. engine->GCEnumCallback(func);
  981. }
  982. break;
  983. // Global variables
  984. case asBC_PGA:
  985. case asBC_PshGPtr:
  986. case asBC_LDG:
  987. case asBC_PshG4:
  988. case asBC_LdGRdR4:
  989. case asBC_CpyGtoV4:
  990. case asBC_CpyVtoG4:
  991. case asBC_SetG4:
  992. // Need to enumerate the reference for each global variable
  993. {
  994. // TODO: optimize: Keep an array of accessed global properties
  995. void *gvarPtr = (void*)asBC_PTRARG(&byteCode[n]);
  996. asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);
  997. engine->GCEnumCallback(prop);
  998. }
  999. break;
  1000. }
  1001. }
  1002. // Delegate
  1003. if( objForDelegate )
  1004. engine->GCEnumCallback(objForDelegate);
  1005. if( funcForDelegate )
  1006. engine->GCEnumCallback(funcForDelegate);
  1007. }
  1008. // internal
  1009. void asCScriptFunction::ReleaseAllHandles(asIScriptEngine *)
  1010. {
  1011. // Release paramaters
  1012. if( byteCode.GetLength() )
  1013. {
  1014. if( returnType.IsObject() )
  1015. {
  1016. returnType.GetObjectType()->Release();
  1017. returnType = asCDataType::CreatePrimitive(ttVoid, false);
  1018. }
  1019. for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
  1020. if( parameterTypes[p].IsObject() )
  1021. {
  1022. parameterTypes[p].GetObjectType()->Release();
  1023. parameterTypes[p] = asCDataType::CreatePrimitive(ttInt, false);
  1024. }
  1025. for( asUINT n = 0; n < objVariableTypes.GetLength(); n++ )
  1026. objVariableTypes[n]->Release();
  1027. objVariableTypes.SetLength(0);
  1028. }
  1029. // Release all script functions
  1030. for( asUINT n = 0; n < byteCode.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&byteCode[n]].type] )
  1031. {
  1032. switch( *(asBYTE*)&byteCode[n] )
  1033. {
  1034. // Object types
  1035. case asBC_OBJTYPE:
  1036. case asBC_FREE:
  1037. case asBC_REFCPY:
  1038. case asBC_RefCpyV:
  1039. {
  1040. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  1041. if( objType )
  1042. {
  1043. objType->Release();
  1044. *(void**)&byteCode[n+1] = 0;
  1045. }
  1046. }
  1047. break;
  1048. case asBC_ALLOC:
  1049. {
  1050. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&byteCode[n]);
  1051. if( objType )
  1052. {
  1053. objType->Release();
  1054. * (void**)&byteCode[n+1] = 0;
  1055. }
  1056. int func = asBC_INTARG(&byteCode[n]+AS_PTR_SIZE);
  1057. if( func )
  1058. {
  1059. engine->scriptFunctions[func]->Release();
  1060. byteCode[n+AS_PTR_SIZE+1] = 0;
  1061. }
  1062. }
  1063. break;
  1064. case asBC_CALL:
  1065. case asBC_CALLINTF:
  1066. {
  1067. int func = asBC_INTARG(&byteCode[n]);
  1068. if( func )
  1069. {
  1070. engine->scriptFunctions[func]->Release();
  1071. byteCode[n+1] = 0;
  1072. }
  1073. }
  1074. break;
  1075. // Function pointers
  1076. case asBC_FuncPtr:
  1077. {
  1078. asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&byteCode[n]);
  1079. if( func )
  1080. {
  1081. func->Release();
  1082. *(asPWORD*)&byteCode[n+1] = 0;
  1083. }
  1084. }
  1085. break;
  1086. // The global variables are not released here. It is enough that the global
  1087. // variable itself release the function to break the circle
  1088. }
  1089. }
  1090. // Delegate
  1091. if( objForDelegate )
  1092. engine->ReleaseScriptObject(objForDelegate, funcForDelegate->GetObjectType());
  1093. objForDelegate = 0;
  1094. if( funcForDelegate )
  1095. funcForDelegate->Release();
  1096. funcForDelegate = 0;
  1097. }
  1098. // internal
  1099. bool asCScriptFunction::IsShared() const
  1100. {
  1101. // All system functions are shared
  1102. if( funcType == asFUNC_SYSTEM ) return true;
  1103. // All class methods for shared classes are also shared
  1104. if( objectType && (objectType->flags & asOBJ_SHARED) ) return true;
  1105. // Functions that have been specifically marked as shared are shared
  1106. return isShared;
  1107. }
  1108. // internal
  1109. bool asCScriptFunction::IsFinal() const
  1110. {
  1111. return isFinal;
  1112. }
  1113. // internal
  1114. bool asCScriptFunction::IsOverride() const
  1115. {
  1116. return isOverride;
  1117. }
  1118. END_AS_NAMESPACE