as_scriptfunction.cpp 27 KB

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