as_scriptfunction.cpp 28 KB

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