as_module.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2012 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_module.cpp
  25. //
  26. // A class that holds a script module
  27. //
  28. #include "as_config.h"
  29. #include "as_module.h"
  30. #include "as_builder.h"
  31. #include "as_context.h"
  32. #include "as_texts.h"
  33. BEGIN_AS_NAMESPACE
  34. // internal
  35. asCModule::asCModule(const char *name, asCScriptEngine *engine)
  36. {
  37. this->name = name;
  38. this->engine = engine;
  39. userData = 0;
  40. builder = 0;
  41. isGlobalVarInitialized = false;
  42. accessMask = 1;
  43. }
  44. // internal
  45. asCModule::~asCModule()
  46. {
  47. InternalReset();
  48. if( builder )
  49. {
  50. asDELETE(builder,asCBuilder);
  51. builder = 0;
  52. }
  53. // Clean the user data
  54. if( userData && engine->cleanModuleFunc )
  55. engine->cleanModuleFunc(this);
  56. // Remove the module from the engine
  57. if( engine )
  58. {
  59. if( engine->lastModule == this )
  60. engine->lastModule = 0;
  61. engine->scriptModules.RemoveValue(this);
  62. }
  63. }
  64. // interface
  65. void *asCModule::SetUserData(void *data)
  66. {
  67. void *oldData = userData;
  68. userData = data;
  69. return oldData;
  70. }
  71. // interface
  72. void *asCModule::GetUserData() const
  73. {
  74. return userData;
  75. }
  76. // interface
  77. asIScriptEngine *asCModule::GetEngine() const
  78. {
  79. return engine;
  80. }
  81. // interface
  82. void asCModule::SetName(const char *name)
  83. {
  84. this->name = name;
  85. }
  86. // interface
  87. const char *asCModule::GetName() const
  88. {
  89. return name.AddressOf();
  90. }
  91. // interface
  92. int asCModule::SetDefaultNamespace(const char *nameSpace)
  93. {
  94. // TODO: cleanup: This function is similar to asCScriptEngine::SetDefaultNamespace. Can we reuse the code?
  95. if( nameSpace == 0 )
  96. return asINVALID_ARG;
  97. defaultNamespace = nameSpace;
  98. if( defaultNamespace != "" )
  99. {
  100. // Make sure the namespace is composed of alternating identifier and ::
  101. size_t pos = 0;
  102. bool expectIdentifier = true;
  103. size_t len;
  104. eTokenType t = ttIdentifier;
  105. for( ; pos < defaultNamespace.GetLength(); pos += len )
  106. {
  107. t = engine->tok.GetToken(defaultNamespace.AddressOf() + pos, defaultNamespace.GetLength() - pos, &len);
  108. if( (expectIdentifier && t != ttIdentifier) || (!expectIdentifier && t != ttScope) )
  109. return asINVALID_DECLARATION;
  110. expectIdentifier = !expectIdentifier;
  111. }
  112. // If the namespace ends with :: then strip it off
  113. if( t == ttScope )
  114. defaultNamespace.SetLength(defaultNamespace.GetLength()-2);
  115. }
  116. return 0;
  117. }
  118. // interface
  119. int asCModule::AddScriptSection(const char *name, const char *code, size_t codeLength, int lineOffset)
  120. {
  121. #ifdef AS_NO_COMPILER
  122. UNUSED_VAR(name);
  123. UNUSED_VAR(code);
  124. UNUSED_VAR(codeLength);
  125. UNUSED_VAR(lineOffset);
  126. return asNOT_SUPPORTED;
  127. #else
  128. if( !builder )
  129. builder = asNEW(asCBuilder)(engine, this);
  130. return builder->AddCode(name, code, (int)codeLength, lineOffset, (int)engine->GetScriptSectionNameIndex(name ? name : ""), engine->ep.copyScriptSections);
  131. #endif
  132. }
  133. // internal
  134. void asCModule::JITCompile()
  135. {
  136. for (unsigned int i = 0; i < scriptFunctions.GetLength(); i++)
  137. {
  138. scriptFunctions[i]->JITCompile();
  139. }
  140. }
  141. // interface
  142. int asCModule::Build()
  143. {
  144. #ifdef AS_NO_COMPILER
  145. return asNOT_SUPPORTED;
  146. #else
  147. // Only one thread may build at one time
  148. // TODO: It should be possible to have multiple threads perform compilations
  149. int r = engine->RequestBuild();
  150. if( r < 0 )
  151. return r;
  152. engine->PrepareEngine();
  153. if( engine->configFailed )
  154. {
  155. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INVALID_CONFIGURATION);
  156. engine->BuildCompleted();
  157. return asINVALID_CONFIGURATION;
  158. }
  159. InternalReset();
  160. if( !builder )
  161. {
  162. engine->BuildCompleted();
  163. return asSUCCESS;
  164. }
  165. // Compile the script
  166. r = builder->Build();
  167. asDELETE(builder,asCBuilder);
  168. builder = 0;
  169. if( r < 0 )
  170. {
  171. // Reset module again
  172. InternalReset();
  173. engine->BuildCompleted();
  174. return r;
  175. }
  176. JITCompile();
  177. engine->PrepareEngine();
  178. engine->BuildCompleted();
  179. // Initialize global variables
  180. if( r >= 0 && engine->ep.initGlobalVarsAfterBuild )
  181. r = ResetGlobalVars(0);
  182. return r;
  183. #endif
  184. }
  185. // interface
  186. int asCModule::ResetGlobalVars(asIScriptContext *ctx)
  187. {
  188. if( isGlobalVarInitialized )
  189. CallExit();
  190. return CallInit(ctx);
  191. }
  192. // interface
  193. int asCModule::GetFunctionIdByIndex(asUINT index) const
  194. {
  195. if( index >= globalFunctions.GetLength() )
  196. return asNO_FUNCTION;
  197. return globalFunctions[index]->id;
  198. }
  199. // interface
  200. asIScriptFunction *asCModule::GetFunctionByIndex(asUINT index) const
  201. {
  202. if( index >= globalFunctions.GetLength() )
  203. return 0;
  204. return globalFunctions[index];
  205. }
  206. // internal
  207. int asCModule::CallInit(asIScriptContext *myCtx)
  208. {
  209. if( isGlobalVarInitialized )
  210. return asERROR;
  211. // Each global variable needs to be cleared individually
  212. asUINT n;
  213. for( n = 0; n < scriptGlobals.GetLength(); n++ )
  214. {
  215. if( scriptGlobals[n] )
  216. {
  217. memset(scriptGlobals[n]->GetAddressOfValue(), 0, sizeof(asDWORD)*scriptGlobals[n]->type.GetSizeOnStackDWords());
  218. }
  219. }
  220. // Call the init function for each of the global variables
  221. asIScriptContext *ctx = myCtx;
  222. int r = asEXECUTION_FINISHED;
  223. for( n = 0; n < scriptGlobals.GetLength() && r == asEXECUTION_FINISHED; n++ )
  224. {
  225. if( scriptGlobals[n]->GetInitFunc() )
  226. {
  227. if( ctx == 0 )
  228. {
  229. r = engine->CreateContext(&ctx, true);
  230. if( r < 0 )
  231. break;
  232. }
  233. r = ctx->Prepare(scriptGlobals[n]->GetInitFunc()->id);
  234. if( r >= 0 )
  235. {
  236. r = ctx->Execute();
  237. if( r != asEXECUTION_FINISHED )
  238. {
  239. asCString msg;
  240. msg.Format(TXT_FAILED_TO_INITIALIZE_s, scriptGlobals[n]->name.AddressOf());
  241. asCScriptFunction *func = scriptGlobals[n]->GetInitFunc();
  242. engine->WriteMessage(func->scriptSectionIdx >= 0 ? engine->scriptSectionNames[func->scriptSectionIdx]->AddressOf() : "",
  243. func->GetLineNumber(0) & 0xFFFFF,
  244. func->GetLineNumber(0) >> 20,
  245. asMSGTYPE_ERROR,
  246. msg.AddressOf());
  247. if( r == asEXECUTION_EXCEPTION )
  248. {
  249. const asIScriptFunction *function = ctx->GetExceptionFunction();
  250. msg.Format(TXT_EXCEPTION_s_IN_s, ctx->GetExceptionString(), function->GetDeclaration());
  251. engine->WriteMessage(function->GetScriptSectionName(),
  252. ctx->GetExceptionLineNumber(),
  253. 0,
  254. asMSGTYPE_INFORMATION,
  255. msg.AddressOf());
  256. }
  257. }
  258. }
  259. }
  260. }
  261. if( ctx && !myCtx )
  262. {
  263. ctx->Release();
  264. ctx = 0;
  265. }
  266. // Even if the initialization failed we need to set the
  267. // flag that the variables have been initialized, otherwise
  268. // the module won't free those variables that really were
  269. // initialized.
  270. isGlobalVarInitialized = true;
  271. if( r != asEXECUTION_FINISHED )
  272. return asINIT_GLOBAL_VARS_FAILED;
  273. return asSUCCESS;
  274. }
  275. // internal
  276. void asCModule::CallExit()
  277. {
  278. if( !isGlobalVarInitialized ) return;
  279. for( size_t n = 0; n < scriptGlobals.GetLength(); n++ )
  280. {
  281. if( scriptGlobals[n]->type.IsObject() )
  282. {
  283. void **obj = (void**)scriptGlobals[n]->GetAddressOfValue();
  284. if( *obj )
  285. {
  286. asCObjectType *ot = scriptGlobals[n]->type.GetObjectType();
  287. if( ot->flags & asOBJ_REF )
  288. {
  289. asASSERT( (ot->flags & asOBJ_NOCOUNT) || ot->beh.release );
  290. if( ot->beh.release )
  291. engine->CallObjectMethod(*obj, ot->beh.release);
  292. }
  293. else
  294. {
  295. if( ot->beh.destruct )
  296. engine->CallObjectMethod(*obj, ot->beh.destruct);
  297. engine->CallFree(*obj);
  298. }
  299. // Set the address to 0 as someone might try to access the variable afterwards
  300. *obj = 0;
  301. }
  302. }
  303. }
  304. isGlobalVarInitialized = false;
  305. }
  306. // internal
  307. void asCModule::InternalReset()
  308. {
  309. CallExit();
  310. size_t n;
  311. // Release all global functions
  312. for( n = 0; n < globalFunctions.GetLength(); n++ )
  313. {
  314. if( globalFunctions[n] )
  315. globalFunctions[n]->Release();
  316. }
  317. globalFunctions.SetLength(0);
  318. // First release all compiled functions
  319. for( n = 0; n < scriptFunctions.GetLength(); n++ )
  320. {
  321. if( scriptFunctions[n] )
  322. {
  323. // Remove the module reference in the functions
  324. scriptFunctions[n]->module = 0;
  325. scriptFunctions[n]->Release();
  326. }
  327. }
  328. scriptFunctions.SetLength(0);
  329. // Release the global properties declared in the module
  330. for( n = 0; n < scriptGlobals.GetLength(); n++ )
  331. scriptGlobals[n]->Release();
  332. scriptGlobals.SetLength(0);
  333. UnbindAllImportedFunctions();
  334. // Free bind information
  335. for( n = 0; n < bindInformations.GetLength(); n++ )
  336. {
  337. asUINT id = bindInformations[n]->importedFunctionSignature->id & 0xFFFF;
  338. engine->importedFunctions[id] = 0;
  339. engine->freeImportedFunctionIdxs.PushLast(id);
  340. asDELETE(bindInformations[n]->importedFunctionSignature, asCScriptFunction);
  341. asDELETE(bindInformations[n], sBindInfo);
  342. }
  343. bindInformations.SetLength(0);
  344. // Free declared types, including classes, typedefs, and enums
  345. for( n = 0; n < classTypes.GetLength(); n++ )
  346. classTypes[n]->Release();
  347. classTypes.SetLength(0);
  348. for( n = 0; n < enumTypes.GetLength(); n++ )
  349. enumTypes[n]->Release();
  350. enumTypes.SetLength(0);
  351. for( n = 0; n < typeDefs.GetLength(); n++ )
  352. typeDefs[n]->Release();
  353. typeDefs.SetLength(0);
  354. // Free funcdefs
  355. for( n = 0; n < funcDefs.GetLength(); n++ )
  356. {
  357. // TODO: funcdefs: These may be shared between modules, so we can't just remove them
  358. engine->funcDefs.RemoveValue(funcDefs[n]);
  359. funcDefs[n]->Release();
  360. }
  361. funcDefs.SetLength(0);
  362. }
  363. // interface
  364. int asCModule::GetFunctionIdByName(const char *name) const
  365. {
  366. // TODO: optimize: Improve linear search
  367. // Find the function id
  368. int id = -1;
  369. for( size_t n = 0; n < globalFunctions.GetLength(); n++ )
  370. {
  371. if( globalFunctions[n]->name == name &&
  372. globalFunctions[n]->nameSpace == defaultNamespace )
  373. {
  374. if( id == -1 )
  375. id = globalFunctions[n]->id;
  376. else
  377. return asMULTIPLE_FUNCTIONS;
  378. }
  379. }
  380. if( id == -1 ) return asNO_FUNCTION;
  381. return id;
  382. }
  383. // interface
  384. asIScriptFunction *asCModule::GetFunctionByName(const char *name) const
  385. {
  386. int id = GetFunctionIdByName(name);
  387. if( id < 0 )
  388. return 0;
  389. return engine->GetFunctionById(id);
  390. }
  391. // interface
  392. asUINT asCModule::GetImportedFunctionCount() const
  393. {
  394. return (asUINT)bindInformations.GetLength();
  395. }
  396. // interface
  397. int asCModule::GetImportedFunctionIndexByDecl(const char *decl) const
  398. {
  399. asCBuilder bld(engine, const_cast<asCModule*>(this));
  400. asCScriptFunction func(engine, const_cast<asCModule*>(this), asFUNC_DUMMY);
  401. bld.ParseFunctionDeclaration(0, decl, &func, false);
  402. // TODO: optimize: Improve linear search
  403. // Search script functions for matching interface
  404. int id = -1;
  405. for( asUINT n = 0; n < bindInformations.GetLength(); ++n )
  406. {
  407. if( func.name == bindInformations[n]->importedFunctionSignature->name &&
  408. func.returnType == bindInformations[n]->importedFunctionSignature->returnType &&
  409. func.parameterTypes.GetLength() == bindInformations[n]->importedFunctionSignature->parameterTypes.GetLength() )
  410. {
  411. bool match = true;
  412. for( asUINT p = 0; p < func.parameterTypes.GetLength(); ++p )
  413. {
  414. if( func.parameterTypes[p] != bindInformations[n]->importedFunctionSignature->parameterTypes[p] )
  415. {
  416. match = false;
  417. break;
  418. }
  419. }
  420. if( match )
  421. {
  422. if( id == -1 )
  423. id = n;
  424. else
  425. return asMULTIPLE_FUNCTIONS;
  426. }
  427. }
  428. }
  429. if( id == -1 ) return asNO_FUNCTION;
  430. return id;
  431. }
  432. // interface
  433. asUINT asCModule::GetFunctionCount() const
  434. {
  435. return (asUINT)globalFunctions.GetLength();
  436. }
  437. // interface
  438. int asCModule::GetFunctionIdByDecl(const char *decl) const
  439. {
  440. asCBuilder bld(engine, const_cast<asCModule*>(this));
  441. asCScriptFunction func(engine, const_cast<asCModule*>(this), asFUNC_DUMMY);
  442. int r = bld.ParseFunctionDeclaration(0, decl, &func, false);
  443. if( r < 0 )
  444. return asINVALID_DECLARATION;
  445. // Use the defaultNamespace implicitly unless an explicit namespace has been provided
  446. asCString ns = func.nameSpace == "" ? defaultNamespace : func.nameSpace;
  447. // TODO: optimize: Improve linear search
  448. // Search script functions for matching interface
  449. int id = -1;
  450. for( size_t n = 0; n < globalFunctions.GetLength(); ++n )
  451. {
  452. if( globalFunctions[n]->objectType == 0 &&
  453. func.name == globalFunctions[n]->name &&
  454. func.returnType == globalFunctions[n]->returnType &&
  455. func.parameterTypes.GetLength() == globalFunctions[n]->parameterTypes.GetLength() &&
  456. ns == globalFunctions[n]->nameSpace )
  457. {
  458. bool match = true;
  459. for( size_t p = 0; p < func.parameterTypes.GetLength(); ++p )
  460. {
  461. if( func.parameterTypes[p] != globalFunctions[n]->parameterTypes[p] )
  462. {
  463. match = false;
  464. break;
  465. }
  466. }
  467. if( match )
  468. {
  469. if( id == -1 )
  470. id = globalFunctions[n]->id;
  471. else
  472. return asMULTIPLE_FUNCTIONS;
  473. }
  474. }
  475. }
  476. if( id == -1 ) return asNO_FUNCTION;
  477. return id;
  478. }
  479. // interface
  480. asIScriptFunction *asCModule::GetFunctionByDecl(const char *decl) const
  481. {
  482. int id = GetFunctionIdByDecl(decl);
  483. return engine->GetFunctionById(id);
  484. }
  485. // interface
  486. asUINT asCModule::GetGlobalVarCount() const
  487. {
  488. return (asUINT)scriptGlobals.GetLength();
  489. }
  490. // interface
  491. int asCModule::GetGlobalVarIndexByName(const char *name) const
  492. {
  493. // Find the global var id
  494. int id = -1;
  495. for( size_t n = 0; n < scriptGlobals.GetLength(); n++ )
  496. {
  497. if( scriptGlobals[n]->name == name &&
  498. scriptGlobals[n]->nameSpace == defaultNamespace )
  499. {
  500. id = (int)n;
  501. break;
  502. }
  503. }
  504. if( id == -1 ) return asNO_GLOBAL_VAR;
  505. return id;
  506. }
  507. // interface
  508. int asCModule::RemoveGlobalVar(asUINT index)
  509. {
  510. if( index >= scriptGlobals.GetLength() )
  511. return asINVALID_ARG;
  512. scriptGlobals[index]->Release();
  513. scriptGlobals.RemoveIndex(index);
  514. return 0;
  515. }
  516. // interface
  517. int asCModule::GetGlobalVarIndexByDecl(const char *decl) const
  518. {
  519. asCBuilder bld(engine, const_cast<asCModule*>(this));
  520. asCString name, nameSpace;
  521. asCDataType dt;
  522. bld.ParseVariableDeclaration(decl, defaultNamespace, name, nameSpace, dt);
  523. // TODO: optimize: Improve linear search
  524. // Search global variables for a match
  525. int id = -1;
  526. for( size_t n = 0; n < scriptGlobals.GetLength(); ++n )
  527. {
  528. if( name == scriptGlobals[n]->name &&
  529. nameSpace == scriptGlobals[n]->nameSpace &&
  530. dt == scriptGlobals[n]->type )
  531. {
  532. id = (int)n;
  533. break;
  534. }
  535. }
  536. if( id == -1 ) return asNO_GLOBAL_VAR;
  537. return id;
  538. }
  539. // interface
  540. void *asCModule::GetAddressOfGlobalVar(asUINT index)
  541. {
  542. if( index >= scriptGlobals.GetLength() )
  543. return 0;
  544. // For object variables it's necessary to dereference the pointer to get the address of the value
  545. if( scriptGlobals[index]->type.IsObject() &&
  546. !scriptGlobals[index]->type.IsObjectHandle() )
  547. return *(void**)(scriptGlobals[index]->GetAddressOfValue());
  548. return (void*)(scriptGlobals[index]->GetAddressOfValue());
  549. }
  550. // interface
  551. const char *asCModule::GetGlobalVarDeclaration(asUINT index, bool includeNamespace) const
  552. {
  553. if( index >= scriptGlobals.GetLength() )
  554. return 0;
  555. asCGlobalProperty *prop = scriptGlobals[index];
  556. asCString *tempString = &asCThreadManager::GetLocalData()->string;
  557. *tempString = prop->type.Format();
  558. *tempString += " ";
  559. if( includeNamespace )
  560. *tempString += prop->nameSpace + "::";
  561. *tempString += prop->name;
  562. return tempString->AddressOf();
  563. }
  564. // interface
  565. int asCModule::GetGlobalVar(asUINT index, const char **name, const char **nameSpace, int *typeId, bool *isConst) const
  566. {
  567. if( index >= scriptGlobals.GetLength() )
  568. return asINVALID_ARG;
  569. asCGlobalProperty *prop = scriptGlobals[index];
  570. if( name )
  571. *name = prop->name.AddressOf();
  572. if( nameSpace )
  573. *nameSpace = prop->nameSpace.AddressOf();
  574. if( typeId )
  575. *typeId = engine->GetTypeIdFromDataType(prop->type);
  576. if( isConst )
  577. *isConst = prop->type.IsReadOnly();
  578. return asSUCCESS;
  579. }
  580. // interface
  581. asUINT asCModule::GetObjectTypeCount() const
  582. {
  583. return (asUINT)classTypes.GetLength();
  584. }
  585. // interface
  586. asIObjectType *asCModule::GetObjectTypeByIndex(asUINT index) const
  587. {
  588. if( index >= classTypes.GetLength() )
  589. return 0;
  590. return classTypes[index];
  591. }
  592. // interface
  593. asIObjectType *asCModule::GetObjectTypeByName(const char *name) const
  594. {
  595. for( asUINT n = 0; n < classTypes.GetLength(); n++ )
  596. {
  597. if( classTypes[n] &&
  598. classTypes[n]->name == name &&
  599. classTypes[n]->nameSpace == defaultNamespace )
  600. return classTypes[n];
  601. }
  602. return 0;
  603. }
  604. // interface
  605. int asCModule::GetTypeIdByDecl(const char *decl) const
  606. {
  607. asCDataType dt;
  608. asCBuilder bld(engine, const_cast<asCModule*>(this));
  609. int r = bld.ParseDataType(decl, &dt, defaultNamespace);
  610. if( r < 0 )
  611. return asINVALID_TYPE;
  612. return engine->GetTypeIdFromDataType(dt);
  613. }
  614. // interface
  615. asUINT asCModule::GetEnumCount() const
  616. {
  617. return (asUINT)enumTypes.GetLength();
  618. }
  619. // interface
  620. const char *asCModule::GetEnumByIndex(asUINT index, int *enumTypeId, const char **nameSpace) const
  621. {
  622. if( index >= enumTypes.GetLength() )
  623. return 0;
  624. if( enumTypeId )
  625. *enumTypeId = GetTypeIdByDecl(enumTypes[index]->name.AddressOf());
  626. if( nameSpace )
  627. *nameSpace = enumTypes[index]->name.AddressOf();
  628. return enumTypes[index]->name.AddressOf();
  629. }
  630. // interface
  631. int asCModule::GetEnumValueCount(int enumTypeId) const
  632. {
  633. asCDataType dt = engine->GetDataTypeFromTypeId(enumTypeId);
  634. asCObjectType *t = dt.GetObjectType();
  635. if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
  636. return asINVALID_TYPE;
  637. return (int)t->enumValues.GetLength();
  638. }
  639. // interface
  640. const char *asCModule::GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const
  641. {
  642. asCDataType dt = engine->GetDataTypeFromTypeId(enumTypeId);
  643. asCObjectType *t = dt.GetObjectType();
  644. if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
  645. return 0;
  646. if( index >= t->enumValues.GetLength() )
  647. return 0;
  648. if( outValue )
  649. *outValue = t->enumValues[index]->value;
  650. return t->enumValues[index]->name.AddressOf();
  651. }
  652. // interface
  653. asUINT asCModule::GetTypedefCount() const
  654. {
  655. return (asUINT)typeDefs.GetLength();
  656. }
  657. // interface
  658. const char *asCModule::GetTypedefByIndex(asUINT index, int *typeId, const char **nameSpace) const
  659. {
  660. if( index >= typeDefs.GetLength() )
  661. return 0;
  662. if( typeId )
  663. *typeId = GetTypeIdByDecl(typeDefs[index]->name.AddressOf());
  664. if( nameSpace )
  665. *nameSpace = typeDefs[index]->nameSpace.AddressOf();
  666. return typeDefs[index]->name.AddressOf();
  667. }
  668. // internal
  669. int asCModule::GetNextImportedFunctionId()
  670. {
  671. // TODO: multithread: This will break if one thread if freeing a module, while another is being compiled
  672. if( engine->freeImportedFunctionIdxs.GetLength() )
  673. return FUNC_IMPORTED | (asUINT)engine->freeImportedFunctionIdxs[engine->freeImportedFunctionIdxs.GetLength()-1];
  674. return FUNC_IMPORTED | (asUINT)engine->importedFunctions.GetLength();
  675. }
  676. // internal
  677. int asCModule::AddScriptFunction(int sectionIdx, int id, const char *name, const asCDataType &returnType, asCDataType *params, asETypeModifiers *inOutFlags, asCString **defaultArgs, int paramCount, bool isInterface, asCObjectType *objType, bool isConstMethod, bool isGlobalFunction, bool isPrivate, bool isFinal, bool isOverride, bool isShared, const asCString &ns)
  678. {
  679. asASSERT(id >= 0);
  680. // Store the function information
  681. asCScriptFunction *func = asNEW(asCScriptFunction)(engine, this, isInterface ? asFUNC_INTERFACE : asFUNC_SCRIPT);
  682. func->name = name;
  683. func->nameSpace = ns;
  684. func->id = id;
  685. func->returnType = returnType;
  686. func->scriptSectionIdx = sectionIdx;
  687. for( int n = 0; n < paramCount; n++ )
  688. {
  689. func->parameterTypes.PushLast(params[n]);
  690. func->inOutFlags.PushLast(inOutFlags[n]);
  691. func->defaultArgs.PushLast(defaultArgs[n]);
  692. }
  693. func->objectType = objType;
  694. func->isReadOnly = isConstMethod;
  695. func->isPrivate = isPrivate;
  696. func->isFinal = isFinal;
  697. func->isOverride = isOverride;
  698. // All methods of shared objects are also shared
  699. if( objType && objType->IsShared() )
  700. isShared = true;
  701. func->isShared = isShared;
  702. // Verify that we are not assigning either the final or override specifier(s) if we are registering a non-member function
  703. asASSERT( !(!objType && isFinal) );
  704. asASSERT( !(!objType && isOverride) );
  705. // The script function's refCount was initialized to 1
  706. scriptFunctions.PushLast(func);
  707. engine->SetScriptFunction(func);
  708. // Compute the signature id
  709. if( objType )
  710. func->ComputeSignatureId();
  711. // Add reference
  712. if( isGlobalFunction )
  713. {
  714. globalFunctions.PushLast(func);
  715. func->AddRef();
  716. }
  717. return 0;
  718. }
  719. // internal
  720. int asCModule::AddScriptFunction(asCScriptFunction *func)
  721. {
  722. scriptFunctions.PushLast(func);
  723. func->AddRef();
  724. engine->SetScriptFunction(func);
  725. return 0;
  726. }
  727. // internal
  728. int asCModule::AddImportedFunction(int id, const char *name, const asCDataType &returnType, asCDataType *params, asETypeModifiers *inOutFlags, int paramCount, const asCString &moduleName)
  729. {
  730. asASSERT(id >= 0);
  731. // Store the function information
  732. asCScriptFunction *func = asNEW(asCScriptFunction)(engine, this, asFUNC_IMPORTED);
  733. func->name = name;
  734. func->id = id;
  735. func->returnType = returnType;
  736. for( int n = 0; n < paramCount; n++ )
  737. {
  738. func->parameterTypes.PushLast(params[n]);
  739. func->inOutFlags.PushLast(inOutFlags[n]);
  740. }
  741. func->objectType = 0;
  742. sBindInfo *info = asNEW(sBindInfo);
  743. info->importedFunctionSignature = func;
  744. info->boundFunctionId = -1;
  745. info->importFromModule = moduleName;
  746. bindInformations.PushLast(info);
  747. // Add the info to the array in the engine
  748. if( engine->freeImportedFunctionIdxs.GetLength() )
  749. engine->importedFunctions[engine->freeImportedFunctionIdxs.PopLast()] = info;
  750. else
  751. engine->importedFunctions.PushLast(info);
  752. return 0;
  753. }
  754. // internal
  755. asCScriptFunction *asCModule::GetImportedFunction(int index) const
  756. {
  757. return bindInformations[index]->importedFunctionSignature;
  758. }
  759. // interface
  760. int asCModule::BindImportedFunction(asUINT index, int sourceId)
  761. {
  762. // First unbind the old function
  763. int r = UnbindImportedFunction(index);
  764. if( r < 0 ) return r;
  765. // Must verify that the interfaces are equal
  766. asCScriptFunction *dst = GetImportedFunction(index);
  767. if( dst == 0 ) return asNO_FUNCTION;
  768. asCScriptFunction *src = engine->GetScriptFunction(sourceId);
  769. if( src == 0 )
  770. return asNO_FUNCTION;
  771. // Verify return type
  772. if( dst->returnType != src->returnType )
  773. return asINVALID_INTERFACE;
  774. if( dst->parameterTypes.GetLength() != src->parameterTypes.GetLength() )
  775. return asINVALID_INTERFACE;
  776. for( size_t n = 0; n < dst->parameterTypes.GetLength(); ++n )
  777. {
  778. if( dst->parameterTypes[n] != src->parameterTypes[n] )
  779. return asINVALID_INTERFACE;
  780. }
  781. bindInformations[index]->boundFunctionId = sourceId;
  782. engine->scriptFunctions[sourceId]->AddRef();
  783. return asSUCCESS;
  784. }
  785. // interface
  786. int asCModule::UnbindImportedFunction(asUINT index)
  787. {
  788. if( index >= bindInformations.GetLength() )
  789. return asINVALID_ARG;
  790. // Remove reference to old module
  791. int oldFuncID = bindInformations[index]->boundFunctionId;
  792. if( oldFuncID != -1 )
  793. {
  794. bindInformations[index]->boundFunctionId = -1;
  795. engine->scriptFunctions[oldFuncID]->Release();
  796. }
  797. return asSUCCESS;
  798. }
  799. // interface
  800. const char *asCModule::GetImportedFunctionDeclaration(asUINT index) const
  801. {
  802. asCScriptFunction *func = GetImportedFunction(index);
  803. if( func == 0 ) return 0;
  804. asCString *tempString = &asCThreadManager::GetLocalData()->string;
  805. *tempString = func->GetDeclarationStr();
  806. return tempString->AddressOf();
  807. }
  808. // interface
  809. const char *asCModule::GetImportedFunctionSourceModule(asUINT index) const
  810. {
  811. if( index >= bindInformations.GetLength() )
  812. return 0;
  813. return bindInformations[index]->importFromModule.AddressOf();
  814. }
  815. // inteface
  816. int asCModule::BindAllImportedFunctions()
  817. {
  818. bool notAllFunctionsWereBound = false;
  819. // Bind imported functions
  820. int c = GetImportedFunctionCount();
  821. for( int n = 0; n < c; ++n )
  822. {
  823. asCScriptFunction *func = GetImportedFunction(n);
  824. if( func == 0 ) return asERROR;
  825. asCString str = func->GetDeclarationStr();
  826. // Get module name from where the function should be imported
  827. const char *moduleName = GetImportedFunctionSourceModule(n);
  828. if( moduleName == 0 ) return asERROR;
  829. asCModule *srcMod = engine->GetModule(moduleName, false);
  830. int funcId = -1;
  831. if( srcMod )
  832. funcId = srcMod->GetFunctionIdByDecl(str.AddressOf());
  833. if( funcId < 0 )
  834. notAllFunctionsWereBound = true;
  835. else
  836. {
  837. if( BindImportedFunction(n, funcId) < 0 )
  838. notAllFunctionsWereBound = true;
  839. }
  840. }
  841. if( notAllFunctionsWereBound )
  842. return asCANT_BIND_ALL_FUNCTIONS;
  843. return asSUCCESS;
  844. }
  845. // interface
  846. int asCModule::UnbindAllImportedFunctions()
  847. {
  848. asUINT c = GetImportedFunctionCount();
  849. for( asUINT n = 0; n < c; ++n )
  850. UnbindImportedFunction(n);
  851. return asSUCCESS;
  852. }
  853. // internal
  854. asCObjectType *asCModule::GetObjectType(const char *type, const asCString &ns)
  855. {
  856. size_t n;
  857. // TODO: optimize: Improve linear search
  858. for( n = 0; n < classTypes.GetLength(); n++ )
  859. if( classTypes[n]->name == type &&
  860. classTypes[n]->nameSpace == ns )
  861. return classTypes[n];
  862. for( n = 0; n < enumTypes.GetLength(); n++ )
  863. if( enumTypes[n]->name == type &&
  864. enumTypes[n]->nameSpace == ns )
  865. return enumTypes[n];
  866. for( n = 0; n < typeDefs.GetLength(); n++ )
  867. if( typeDefs[n]->name == type &&
  868. typeDefs[n]->nameSpace == ns )
  869. return typeDefs[n];
  870. return 0;
  871. }
  872. // internal
  873. asCGlobalProperty *asCModule::AllocateGlobalProperty(const char *name, const asCDataType &dt, const asCString &ns)
  874. {
  875. asCGlobalProperty *prop = engine->AllocateGlobalProperty();
  876. prop->name = name;
  877. prop->nameSpace = ns;
  878. // Allocate the memory for this property based on its type
  879. prop->type = dt;
  880. prop->AllocateMemory();
  881. // Store the variable in the module scope (the reference count is already set to 1)
  882. scriptGlobals.PushLast(prop);
  883. return prop;
  884. }
  885. #ifdef AS_DEPRECATED
  886. // Deprecated since 2.23.0 - 2012-01-30
  887. // internal
  888. void asCModule::ResolveInterfaceIds(asCArray<void*> *substitutions)
  889. {
  890. // For each of the interfaces declared in the script find identical interface in the engine.
  891. // If an identical interface was found then substitute the current id for the identical interface's id,
  892. // then remove this interface declaration. If an interface was modified by the declaration, then
  893. // retry the detection of identical interface for it since it may now match another.
  894. // For an interface to be equal to another the name and methods must match. If the interface
  895. // references another interface, then that must be checked as well, which can lead to circular references.
  896. // Example:
  897. //
  898. // interface A { void f(B@); }
  899. // interface B { void f(A@); void f(C@); }
  900. // interface C { void f(A@); }
  901. //
  902. // A1 equals A2 if and only if B1 equals B2
  903. // B1 equals B2 if and only if A1 equals A2 and C1 equals C2
  904. // C1 equals C2 if and only if A1 equals A2
  905. unsigned int i;
  906. // The interface can only be equal to interfaces declared in other modules.
  907. // Interfaces registered by the application will conflict with this one if it has the same name.
  908. // This means that we only need to look for the interfaces in the engine->classTypes, but not in engine->objectTypes.
  909. asCArray<sObjectTypePair> equals;
  910. for( i = 0; i < classTypes.GetLength(); i++ )
  911. {
  912. asCObjectType *intf1 = classTypes[i];
  913. if( !intf1->IsInterface() )
  914. continue;
  915. // The interface may have been determined to be equal to another already
  916. bool found = false;
  917. for( unsigned int e = 0; e < equals.GetLength(); e++ )
  918. {
  919. if( equals[e].a == intf1 )
  920. {
  921. found = true;
  922. break;
  923. }
  924. }
  925. if( found )
  926. continue;
  927. for( unsigned int n = 0; n < engine->classTypes.GetLength(); n++ )
  928. {
  929. // Don't compare against self
  930. if( engine->classTypes[n] == intf1 )
  931. continue;
  932. asCObjectType *intf2 = engine->classTypes[n];
  933. // Assume the interface are equal, then validate this
  934. sObjectTypePair pair = {intf1,intf2};
  935. equals.PushLast(pair);
  936. if( AreInterfacesEqual(intf1, intf2, equals) )
  937. break;
  938. // Since they are not equal, remove them from the list again
  939. equals.PopLast();
  940. }
  941. }
  942. // For each of the interfaces that have been found to be equal we need to
  943. // remove the new declaration and instead have the module use the existing one.
  944. for( i = 0; i < equals.GetLength(); i++ )
  945. {
  946. // Substitute the old object type from the module's class types
  947. unsigned int c;
  948. for( c = 0; c < classTypes.GetLength(); c++ )
  949. {
  950. if( classTypes[c] == equals[i].a )
  951. {
  952. if( substitutions )
  953. {
  954. substitutions->PushLast(equals[i].a);
  955. substitutions->PushLast(equals[i].b);
  956. }
  957. classTypes[c] = equals[i].b;
  958. equals[i].b->AddRef();
  959. break;
  960. }
  961. }
  962. // Remove the old object type from the engine's class types
  963. engine->classTypes.RemoveValue(equals[i].a);
  964. // Only interfaces in the module are using the type so far
  965. for( c = 0; c < classTypes.GetLength(); c++ )
  966. {
  967. if( classTypes[c]->IsInterface() )
  968. {
  969. asCObjectType *intf = classTypes[c];
  970. for( asUINT m = 0; m < intf->GetMethodCount(); m++ )
  971. {
  972. asCScriptFunction *func = engine->GetScriptFunction(intf->methods[m]);
  973. if( func )
  974. {
  975. // Check if it is the same type
  976. if( func->returnType.GetObjectType() == equals[i].a )
  977. func->returnType.SetObjectType(equals[i].b);
  978. // Look for template instances that uses the interface as subtype
  979. else if( func->returnType.IsTemplate() &&
  980. func->returnType.GetSubType().GetObjectType() == equals[i].a )
  981. {
  982. // Find the originating template type
  983. asCObjectType *templ = 0;
  984. for( asUINT t = 0; t < engine->registeredObjTypes.GetLength(); t++ )
  985. if( (engine->registeredObjTypes[t]->flags & asOBJ_TEMPLATE) &&
  986. engine->registeredObjTypes[t]->name == func->returnType.GetObjectType()->name &&
  987. !engine->templateInstanceTypes.Exists(engine->registeredObjTypes[t]) )
  988. {
  989. templ = engine->registeredObjTypes[t];
  990. break;
  991. }
  992. asASSERT( templ );
  993. asCDataType subType = func->returnType.GetSubType();
  994. subType.SetObjectType(equals[i].b);
  995. func->returnType.SetObjectType(engine->GetTemplateInstanceType(templ, subType));
  996. }
  997. for( asUINT p = 0; p < func->GetParamCount(); p++ )
  998. {
  999. // Check if it is the same type
  1000. if( func->parameterTypes[p].GetObjectType() == equals[i].a )
  1001. func->parameterTypes[p].SetObjectType(equals[i].b);
  1002. // Look for template instances that uses the interface as subtype
  1003. else if( func->parameterTypes[p].IsTemplate() &&
  1004. func->parameterTypes[p].GetSubType().GetObjectType() == equals[i].a )
  1005. {
  1006. // Find the originating template type
  1007. asCObjectType *templ = 0;
  1008. for( asUINT t = 0; t < engine->registeredObjTypes.GetLength(); t++ )
  1009. if( (engine->registeredObjTypes[t]->flags & asOBJ_TEMPLATE) &&
  1010. engine->registeredObjTypes[t]->name == func->parameterTypes[p].GetObjectType()->name &&
  1011. !engine->templateInstanceTypes.Exists(engine->registeredObjTypes[t]) )
  1012. {
  1013. templ = engine->registeredObjTypes[t];
  1014. break;
  1015. }
  1016. asASSERT( templ );
  1017. asCDataType subType = func->parameterTypes[p].GetSubType();
  1018. subType.SetObjectType(equals[i].b);
  1019. func->parameterTypes[p].SetObjectType(engine->GetTemplateInstanceType(templ, subType));
  1020. }
  1021. }
  1022. }
  1023. }
  1024. }
  1025. }
  1026. // Substitute all interface methods in the module. Delete all methods for the old interface
  1027. for( unsigned int m = 0; m < equals[i].a->methods.GetLength(); m++ )
  1028. {
  1029. for( c = 0; c < scriptFunctions.GetLength(); c++ )
  1030. {
  1031. if( scriptFunctions[c]->id == equals[i].a->methods[m] )
  1032. {
  1033. if( substitutions )
  1034. substitutions->PushLast(scriptFunctions[c]);
  1035. scriptFunctions[c]->Release();
  1036. scriptFunctions[c] = engine->GetScriptFunction(equals[i].b->methods[m]);
  1037. scriptFunctions[c]->AddRef();
  1038. if( substitutions )
  1039. substitutions->PushLast(scriptFunctions[c]);
  1040. }
  1041. }
  1042. }
  1043. // If there was a template instance, it needs to be removed first
  1044. for( asUINT n = 0; n < engine->templateTypes.GetLength(); n++ )
  1045. {
  1046. if( engine->templateTypes[n]->templateSubType.GetObjectType() == equals[i].a )
  1047. {
  1048. // The use of the template has already been exchanged above
  1049. engine->RemoveTemplateInstanceType(engine->templateTypes[n]);
  1050. n--;
  1051. }
  1052. }
  1053. // Deallocate the object type
  1054. asDELETE(equals[i].a, asCObjectType);
  1055. }
  1056. }
  1057. // internal
  1058. bool asCModule::AreInterfacesEqual(asCObjectType *a, asCObjectType *b, asCArray<sObjectTypePair> &equals)
  1059. {
  1060. // An interface is considered equal to another if the following criterias apply:
  1061. //
  1062. // - The interface names are equal
  1063. // - The number of methods is equal
  1064. // - All the methods are equal
  1065. // - The order of the methods is equal
  1066. // - If a method returns or takes an interface by handle or reference, both interfaces must be equal
  1067. // ------------
  1068. // TODO: Study the possiblity of allowing interfaces where methods are declared in different orders to
  1069. // be considered equal. The compiler and VM can handle this, but it complicates the comparison of interfaces
  1070. // where multiple methods take different interfaces as parameters (or return values). Example:
  1071. //
  1072. // interface A
  1073. // {
  1074. // void f(B, C);
  1075. // void f(B);
  1076. // void f(C);
  1077. // }
  1078. //
  1079. // If 'void f(B)' in module A is compared against 'void f(C)' in module B, then the code will assume
  1080. // interface B in module A equals interface C in module B. Thus 'void f(B, C)' in module A won't match
  1081. // 'void f(C, B)' in module B.
  1082. // ------------
  1083. // Are both interfaces?
  1084. if( !a->IsInterface() || !b->IsInterface() )
  1085. return false;
  1086. // Are the names equal?
  1087. if( a->name != b->name )
  1088. return false;
  1089. // Are the namespaces equal?
  1090. if( a->nameSpace != b->nameSpace )
  1091. return false;
  1092. // Are the number of methods equal?
  1093. if( a->methods.GetLength() != b->methods.GetLength() )
  1094. return false;
  1095. // Keep the number of equals in the list so we can restore it later if necessary
  1096. int prevEquals = (int)equals.GetLength();
  1097. // Are the methods equal to each other?
  1098. bool match = true;
  1099. for( unsigned int n = 0; n < a->methods.GetLength(); n++ )
  1100. {
  1101. match = false;
  1102. asCScriptFunction *funcA = (asCScriptFunction*)engine->GetFunctionById(a->methods[n]);
  1103. asCScriptFunction *funcB = (asCScriptFunction*)engine->GetFunctionById(b->methods[n]);
  1104. // funcB can be null if the module that created the interface has been
  1105. // discarded but the type has not yet been released by the engine.
  1106. if( funcB == 0 )
  1107. break;
  1108. // The methods must have the same name and the same number of parameters
  1109. if( funcA->name != funcB->name ||
  1110. funcA->parameterTypes.GetLength() != funcB->parameterTypes.GetLength() )
  1111. break;
  1112. // The return types must be equal. If the return type is an interface the interfaces must match.
  1113. if( !AreTypesEqual(funcA->returnType, funcB->returnType, equals) )
  1114. break;
  1115. match = true;
  1116. for( unsigned int p = 0; p < funcA->parameterTypes.GetLength(); p++ )
  1117. {
  1118. if( !AreTypesEqual(funcA->parameterTypes[p], funcB->parameterTypes[p], equals) ||
  1119. funcA->inOutFlags[p] != funcB->inOutFlags[p] )
  1120. {
  1121. match = false;
  1122. break;
  1123. }
  1124. }
  1125. if( !match )
  1126. break;
  1127. }
  1128. // For each of the new interfaces that we're assuming to be equal, we need to validate this
  1129. if( match )
  1130. {
  1131. for( unsigned int n = prevEquals; n < equals.GetLength(); n++ )
  1132. {
  1133. if( !AreInterfacesEqual(equals[n].a, equals[n].b, equals) )
  1134. {
  1135. match = false;
  1136. break;
  1137. }
  1138. }
  1139. }
  1140. if( !match )
  1141. {
  1142. // The interfaces doesn't match.
  1143. // Restore the list of previous equals before we go on, so
  1144. // the caller can continue comparing with another interface
  1145. equals.SetLength(prevEquals);
  1146. }
  1147. return match;
  1148. }
  1149. // internal
  1150. bool asCModule::AreTypesEqual(const asCDataType &a, const asCDataType &b, asCArray<sObjectTypePair> &equals)
  1151. {
  1152. if( !a.IsEqualExceptInterfaceType(b) )
  1153. return false;
  1154. asCObjectType *ai = a.GetObjectType();
  1155. asCObjectType *bi = b.GetObjectType();
  1156. if( ai && (ai->flags & asOBJ_TEMPLATE) )
  1157. {
  1158. ai = ai->templateSubType.GetObjectType();
  1159. bi = bi->templateSubType.GetObjectType();
  1160. }
  1161. if( ai && ai->IsInterface() )
  1162. {
  1163. // If the interface is in the equals list, then the pair must match the pair in the list
  1164. bool found = false;
  1165. unsigned int e;
  1166. for( e = 0; e < equals.GetLength(); e++ )
  1167. {
  1168. if( equals[e].a == ai )
  1169. {
  1170. found = true;
  1171. break;
  1172. }
  1173. }
  1174. if( found )
  1175. {
  1176. // Do the pairs match?
  1177. if( equals[e].b != bi )
  1178. return false;
  1179. }
  1180. else
  1181. {
  1182. // Assume they are equal from now on
  1183. sObjectTypePair pair = {ai, bi};
  1184. equals.PushLast(pair);
  1185. }
  1186. }
  1187. return true;
  1188. }
  1189. #endif // AS_DEPRECATED
  1190. // interface
  1191. int asCModule::SaveByteCode(asIBinaryStream *out) const
  1192. {
  1193. #ifdef AS_NO_COMPILER
  1194. UNUSED_VAR(out);
  1195. return asNOT_SUPPORTED;
  1196. #else
  1197. if( out == 0 ) return asINVALID_ARG;
  1198. asCWriter write(const_cast<asCModule*>(this), out, engine);
  1199. return write.Write();
  1200. #endif
  1201. }
  1202. // interface
  1203. int asCModule::LoadByteCode(asIBinaryStream *in)
  1204. {
  1205. if( in == 0 ) return asINVALID_ARG;
  1206. // Only permit loading bytecode if no other thread is currently compiling
  1207. // TODO: It should be possible to have multiple threads perform compilations
  1208. int r = engine->RequestBuild();
  1209. if( r < 0 )
  1210. return r;
  1211. asCReader read(this, in, engine);
  1212. r = read.Read();
  1213. JITCompile();
  1214. engine->BuildCompleted();
  1215. return r;
  1216. }
  1217. // interface
  1218. int asCModule::CompileGlobalVar(const char *sectionName, const char *code, int lineOffset)
  1219. {
  1220. #ifdef AS_NO_COMPILER
  1221. UNUSED_VAR(sectionName);
  1222. UNUSED_VAR(code);
  1223. UNUSED_VAR(lineOffset);
  1224. return asNOT_SUPPORTED;
  1225. #else
  1226. // Validate arguments
  1227. if( code == 0 )
  1228. return asINVALID_ARG;
  1229. // Only one thread may build at one time
  1230. // TODO: It should be possible to have multiple threads perform compilations
  1231. int r = engine->RequestBuild();
  1232. if( r < 0 )
  1233. return r;
  1234. // Prepare the engine
  1235. engine->PrepareEngine();
  1236. if( engine->configFailed )
  1237. {
  1238. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INVALID_CONFIGURATION);
  1239. engine->BuildCompleted();
  1240. return asINVALID_CONFIGURATION;
  1241. }
  1242. // Compile the global variable and add it to the module scope
  1243. asCBuilder builder(engine, this);
  1244. asCString str = code;
  1245. // TODO: namespace: Add the global var to the default namespace
  1246. r = builder.CompileGlobalVar(sectionName, str.AddressOf(), lineOffset);
  1247. engine->BuildCompleted();
  1248. // Initialize the variable
  1249. if( r >= 0 && engine->ep.initGlobalVarsAfterBuild )
  1250. {
  1251. // Clear the memory
  1252. asCGlobalProperty *prop = scriptGlobals[scriptGlobals.GetLength()-1];
  1253. memset(prop->GetAddressOfValue(), 0, sizeof(asDWORD)*prop->type.GetSizeOnStackDWords());
  1254. if( prop->GetInitFunc() )
  1255. {
  1256. // Call the init function for the global variable
  1257. asIScriptContext *ctx = 0;
  1258. int r = engine->CreateContext(&ctx, true);
  1259. if( r < 0 )
  1260. return r;
  1261. r = ctx->Prepare(prop->GetInitFunc()->id);
  1262. if( r >= 0 )
  1263. r = ctx->Execute();
  1264. ctx->Release();
  1265. }
  1266. }
  1267. return r;
  1268. #endif
  1269. }
  1270. // interface
  1271. int asCModule::CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD compileFlags, asIScriptFunction **outFunc)
  1272. {
  1273. #ifdef AS_NO_COMPILER
  1274. UNUSED_VAR(sectionName);
  1275. UNUSED_VAR(code);
  1276. UNUSED_VAR(lineOffset);
  1277. UNUSED_VAR(compileFlags);
  1278. UNUSED_VAR(outFunc);
  1279. return asNOT_SUPPORTED;
  1280. #else
  1281. asASSERT(outFunc == 0 || *outFunc == 0);
  1282. // Validate arguments
  1283. if( code == 0 ||
  1284. (compileFlags != 0 && compileFlags != asCOMP_ADD_TO_MODULE) )
  1285. return asINVALID_ARG;
  1286. // Only one thread may build at one time
  1287. // TODO: It should be possible to have multiple threads perform compilations
  1288. int r = engine->RequestBuild();
  1289. if( r < 0 )
  1290. return r;
  1291. // Prepare the engine
  1292. engine->PrepareEngine();
  1293. if( engine->configFailed )
  1294. {
  1295. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INVALID_CONFIGURATION);
  1296. engine->BuildCompleted();
  1297. return asINVALID_CONFIGURATION;
  1298. }
  1299. // Compile the single function
  1300. asCBuilder builder(engine, this);
  1301. asCString str = code;
  1302. asCScriptFunction *func = 0;
  1303. // TODO: namespace: Add the function to the default namespace
  1304. r = builder.CompileFunction(sectionName, str.AddressOf(), lineOffset, compileFlags, &func);
  1305. engine->BuildCompleted();
  1306. if( r >= 0 && outFunc )
  1307. {
  1308. // Return the function to the caller
  1309. *outFunc = func;
  1310. func->AddRef();
  1311. }
  1312. // Release our reference to the function
  1313. if( func )
  1314. func->Release();
  1315. return r;
  1316. #endif
  1317. }
  1318. // interface
  1319. int asCModule::RemoveFunction(int funcId)
  1320. {
  1321. if( funcId >= 0 && funcId < (int)engine->scriptFunctions.GetLength() )
  1322. return RemoveFunction(engine->scriptFunctions[funcId]);
  1323. return asNO_FUNCTION;
  1324. }
  1325. // interface
  1326. int asCModule::RemoveFunction(asIScriptFunction *func)
  1327. {
  1328. // Find the global function
  1329. asCScriptFunction *f = static_cast<asCScriptFunction*>(func);
  1330. int idx = globalFunctions.IndexOf(f);
  1331. if( idx >= 0 )
  1332. {
  1333. globalFunctions.RemoveIndex(idx);
  1334. f->Release();
  1335. scriptFunctions.RemoveValue(f);
  1336. f->Release();
  1337. return 0;
  1338. }
  1339. return asNO_FUNCTION;
  1340. }
  1341. // internal
  1342. int asCModule::AddFuncDef(const char *name, const asCString &ns)
  1343. {
  1344. asCScriptFunction *func = asNEW(asCScriptFunction)(engine, 0, asFUNC_FUNCDEF);
  1345. func->name = name;
  1346. func->nameSpace = ns;
  1347. funcDefs.PushLast(func);
  1348. engine->funcDefs.PushLast(func);
  1349. func->id = engine->GetNextScriptFunctionId();
  1350. engine->SetScriptFunction(func);
  1351. return (int)funcDefs.GetLength()-1;
  1352. }
  1353. // interface
  1354. asDWORD asCModule::SetAccessMask(asDWORD mask)
  1355. {
  1356. asDWORD old = accessMask;
  1357. accessMask = mask;
  1358. return old;
  1359. }
  1360. END_AS_NAMESPACE