as_module.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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. asASSERT(threadManager);
  557. asCString *tempString = &threadManager->GetLocalData()->string;
  558. *tempString = prop->type.Format();
  559. *tempString += " ";
  560. if( includeNamespace )
  561. *tempString += prop->nameSpace + "::";
  562. *tempString += prop->name;
  563. return tempString->AddressOf();
  564. }
  565. // interface
  566. int asCModule::GetGlobalVar(asUINT index, const char **name, const char **nameSpace, int *typeId, bool *isConst) const
  567. {
  568. if( index >= scriptGlobals.GetLength() )
  569. return asINVALID_ARG;
  570. asCGlobalProperty *prop = scriptGlobals[index];
  571. if( name )
  572. *name = prop->name.AddressOf();
  573. if( nameSpace )
  574. *nameSpace = prop->nameSpace.AddressOf();
  575. if( typeId )
  576. *typeId = engine->GetTypeIdFromDataType(prop->type);
  577. if( isConst )
  578. *isConst = prop->type.IsReadOnly();
  579. return asSUCCESS;
  580. }
  581. // interface
  582. asUINT asCModule::GetObjectTypeCount() const
  583. {
  584. return (asUINT)classTypes.GetLength();
  585. }
  586. // interface
  587. asIObjectType *asCModule::GetObjectTypeByIndex(asUINT index) const
  588. {
  589. if( index >= classTypes.GetLength() )
  590. return 0;
  591. return classTypes[index];
  592. }
  593. // interface
  594. asIObjectType *asCModule::GetObjectTypeByName(const char *name) const
  595. {
  596. for( asUINT n = 0; n < classTypes.GetLength(); n++ )
  597. {
  598. if( classTypes[n] &&
  599. classTypes[n]->name == name &&
  600. classTypes[n]->nameSpace == defaultNamespace )
  601. return classTypes[n];
  602. }
  603. return 0;
  604. }
  605. // interface
  606. int asCModule::GetTypeIdByDecl(const char *decl) const
  607. {
  608. asCDataType dt;
  609. asCBuilder bld(engine, const_cast<asCModule*>(this));
  610. int r = bld.ParseDataType(decl, &dt, defaultNamespace);
  611. if( r < 0 )
  612. return asINVALID_TYPE;
  613. return engine->GetTypeIdFromDataType(dt);
  614. }
  615. // interface
  616. asUINT asCModule::GetEnumCount() const
  617. {
  618. return (asUINT)enumTypes.GetLength();
  619. }
  620. // interface
  621. const char *asCModule::GetEnumByIndex(asUINT index, int *enumTypeId, const char **nameSpace) const
  622. {
  623. if( index >= enumTypes.GetLength() )
  624. return 0;
  625. if( enumTypeId )
  626. *enumTypeId = GetTypeIdByDecl(enumTypes[index]->name.AddressOf());
  627. if( nameSpace )
  628. *nameSpace = enumTypes[index]->name.AddressOf();
  629. return enumTypes[index]->name.AddressOf();
  630. }
  631. // interface
  632. int asCModule::GetEnumValueCount(int enumTypeId) const
  633. {
  634. asCDataType dt = engine->GetDataTypeFromTypeId(enumTypeId);
  635. asCObjectType *t = dt.GetObjectType();
  636. if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
  637. return asINVALID_TYPE;
  638. return (int)t->enumValues.GetLength();
  639. }
  640. // interface
  641. const char *asCModule::GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const
  642. {
  643. asCDataType dt = engine->GetDataTypeFromTypeId(enumTypeId);
  644. asCObjectType *t = dt.GetObjectType();
  645. if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
  646. return 0;
  647. if( index >= t->enumValues.GetLength() )
  648. return 0;
  649. if( outValue )
  650. *outValue = t->enumValues[index]->value;
  651. return t->enumValues[index]->name.AddressOf();
  652. }
  653. // interface
  654. asUINT asCModule::GetTypedefCount() const
  655. {
  656. return (asUINT)typeDefs.GetLength();
  657. }
  658. // interface
  659. const char *asCModule::GetTypedefByIndex(asUINT index, int *typeId, const char **nameSpace) const
  660. {
  661. if( index >= typeDefs.GetLength() )
  662. return 0;
  663. if( typeId )
  664. *typeId = GetTypeIdByDecl(typeDefs[index]->name.AddressOf());
  665. if( nameSpace )
  666. *nameSpace = typeDefs[index]->nameSpace.AddressOf();
  667. return typeDefs[index]->name.AddressOf();
  668. }
  669. // internal
  670. int asCModule::GetNextImportedFunctionId()
  671. {
  672. // TODO: multithread: This will break if one thread if freeing a module, while another is being compiled
  673. if( engine->freeImportedFunctionIdxs.GetLength() )
  674. return FUNC_IMPORTED | (asUINT)engine->freeImportedFunctionIdxs[engine->freeImportedFunctionIdxs.GetLength()-1];
  675. return FUNC_IMPORTED | (asUINT)engine->importedFunctions.GetLength();
  676. }
  677. // internal
  678. 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)
  679. {
  680. asASSERT(id >= 0);
  681. // Store the function information
  682. asCScriptFunction *func = asNEW(asCScriptFunction)(engine, this, isInterface ? asFUNC_INTERFACE : asFUNC_SCRIPT);
  683. func->name = name;
  684. func->nameSpace = ns;
  685. func->id = id;
  686. func->returnType = returnType;
  687. func->scriptSectionIdx = sectionIdx;
  688. for( int n = 0; n < paramCount; n++ )
  689. {
  690. func->parameterTypes.PushLast(params[n]);
  691. func->inOutFlags.PushLast(inOutFlags[n]);
  692. func->defaultArgs.PushLast(defaultArgs[n]);
  693. }
  694. func->objectType = objType;
  695. func->isReadOnly = isConstMethod;
  696. func->isPrivate = isPrivate;
  697. func->isFinal = isFinal;
  698. func->isOverride = isOverride;
  699. // All methods of shared objects are also shared
  700. if( objType && objType->IsShared() )
  701. isShared = true;
  702. func->isShared = isShared;
  703. // Verify that we are not assigning either the final or override specifier(s) if we are registering a non-member function
  704. asASSERT( !(!objType && isFinal) );
  705. asASSERT( !(!objType && isOverride) );
  706. // The script function's refCount was initialized to 1
  707. scriptFunctions.PushLast(func);
  708. engine->SetScriptFunction(func);
  709. // Compute the signature id
  710. if( objType )
  711. func->ComputeSignatureId();
  712. // Add reference
  713. if( isGlobalFunction )
  714. {
  715. globalFunctions.PushLast(func);
  716. func->AddRef();
  717. }
  718. return 0;
  719. }
  720. // internal
  721. int asCModule::AddScriptFunction(asCScriptFunction *func)
  722. {
  723. scriptFunctions.PushLast(func);
  724. func->AddRef();
  725. engine->SetScriptFunction(func);
  726. return 0;
  727. }
  728. // internal
  729. int asCModule::AddImportedFunction(int id, const char *name, const asCDataType &returnType, asCDataType *params, asETypeModifiers *inOutFlags, int paramCount, const asCString &moduleName)
  730. {
  731. asASSERT(id >= 0);
  732. // Store the function information
  733. asCScriptFunction *func = asNEW(asCScriptFunction)(engine, this, asFUNC_IMPORTED);
  734. func->name = name;
  735. func->id = id;
  736. func->returnType = returnType;
  737. for( int n = 0; n < paramCount; n++ )
  738. {
  739. func->parameterTypes.PushLast(params[n]);
  740. func->inOutFlags.PushLast(inOutFlags[n]);
  741. }
  742. func->objectType = 0;
  743. sBindInfo *info = asNEW(sBindInfo);
  744. info->importedFunctionSignature = func;
  745. info->boundFunctionId = -1;
  746. info->importFromModule = moduleName;
  747. bindInformations.PushLast(info);
  748. // Add the info to the array in the engine
  749. if( engine->freeImportedFunctionIdxs.GetLength() )
  750. engine->importedFunctions[engine->freeImportedFunctionIdxs.PopLast()] = info;
  751. else
  752. engine->importedFunctions.PushLast(info);
  753. return 0;
  754. }
  755. // internal
  756. asCScriptFunction *asCModule::GetImportedFunction(int index) const
  757. {
  758. return bindInformations[index]->importedFunctionSignature;
  759. }
  760. // interface
  761. int asCModule::BindImportedFunction(asUINT index, int sourceId)
  762. {
  763. // First unbind the old function
  764. int r = UnbindImportedFunction(index);
  765. if( r < 0 ) return r;
  766. // Must verify that the interfaces are equal
  767. asCScriptFunction *dst = GetImportedFunction(index);
  768. if( dst == 0 ) return asNO_FUNCTION;
  769. asCScriptFunction *src = engine->GetScriptFunction(sourceId);
  770. if( src == 0 )
  771. return asNO_FUNCTION;
  772. // Verify return type
  773. if( dst->returnType != src->returnType )
  774. return asINVALID_INTERFACE;
  775. if( dst->parameterTypes.GetLength() != src->parameterTypes.GetLength() )
  776. return asINVALID_INTERFACE;
  777. for( size_t n = 0; n < dst->parameterTypes.GetLength(); ++n )
  778. {
  779. if( dst->parameterTypes[n] != src->parameterTypes[n] )
  780. return asINVALID_INTERFACE;
  781. }
  782. bindInformations[index]->boundFunctionId = sourceId;
  783. engine->scriptFunctions[sourceId]->AddRef();
  784. return asSUCCESS;
  785. }
  786. // interface
  787. int asCModule::UnbindImportedFunction(asUINT index)
  788. {
  789. if( index >= bindInformations.GetLength() )
  790. return asINVALID_ARG;
  791. // Remove reference to old module
  792. int oldFuncID = bindInformations[index]->boundFunctionId;
  793. if( oldFuncID != -1 )
  794. {
  795. bindInformations[index]->boundFunctionId = -1;
  796. engine->scriptFunctions[oldFuncID]->Release();
  797. }
  798. return asSUCCESS;
  799. }
  800. // interface
  801. const char *asCModule::GetImportedFunctionDeclaration(asUINT index) const
  802. {
  803. asCScriptFunction *func = GetImportedFunction(index);
  804. if( func == 0 ) return 0;
  805. asASSERT(threadManager);
  806. asCString *tempString = &threadManager->GetLocalData()->string;
  807. *tempString = func->GetDeclarationStr();
  808. return tempString->AddressOf();
  809. }
  810. // interface
  811. const char *asCModule::GetImportedFunctionSourceModule(asUINT index) const
  812. {
  813. if( index >= bindInformations.GetLength() )
  814. return 0;
  815. return bindInformations[index]->importFromModule.AddressOf();
  816. }
  817. // inteface
  818. int asCModule::BindAllImportedFunctions()
  819. {
  820. bool notAllFunctionsWereBound = false;
  821. // Bind imported functions
  822. int c = GetImportedFunctionCount();
  823. for( int n = 0; n < c; ++n )
  824. {
  825. asCScriptFunction *func = GetImportedFunction(n);
  826. if( func == 0 ) return asERROR;
  827. asCString str = func->GetDeclarationStr();
  828. // Get module name from where the function should be imported
  829. const char *moduleName = GetImportedFunctionSourceModule(n);
  830. if( moduleName == 0 ) return asERROR;
  831. asCModule *srcMod = engine->GetModule(moduleName, false);
  832. int funcId = -1;
  833. if( srcMod )
  834. funcId = srcMod->GetFunctionIdByDecl(str.AddressOf());
  835. if( funcId < 0 )
  836. notAllFunctionsWereBound = true;
  837. else
  838. {
  839. if( BindImportedFunction(n, funcId) < 0 )
  840. notAllFunctionsWereBound = true;
  841. }
  842. }
  843. if( notAllFunctionsWereBound )
  844. return asCANT_BIND_ALL_FUNCTIONS;
  845. return asSUCCESS;
  846. }
  847. // interface
  848. int asCModule::UnbindAllImportedFunctions()
  849. {
  850. asUINT c = GetImportedFunctionCount();
  851. for( asUINT n = 0; n < c; ++n )
  852. UnbindImportedFunction(n);
  853. return asSUCCESS;
  854. }
  855. // internal
  856. asCObjectType *asCModule::GetObjectType(const char *type, const asCString &ns)
  857. {
  858. size_t n;
  859. // TODO: optimize: Improve linear search
  860. for( n = 0; n < classTypes.GetLength(); n++ )
  861. if( classTypes[n]->name == type &&
  862. classTypes[n]->nameSpace == ns )
  863. return classTypes[n];
  864. for( n = 0; n < enumTypes.GetLength(); n++ )
  865. if( enumTypes[n]->name == type &&
  866. enumTypes[n]->nameSpace == ns )
  867. return enumTypes[n];
  868. for( n = 0; n < typeDefs.GetLength(); n++ )
  869. if( typeDefs[n]->name == type &&
  870. typeDefs[n]->nameSpace == ns )
  871. return typeDefs[n];
  872. return 0;
  873. }
  874. // internal
  875. asCGlobalProperty *asCModule::AllocateGlobalProperty(const char *name, const asCDataType &dt, const asCString &ns)
  876. {
  877. asCGlobalProperty *prop = engine->AllocateGlobalProperty();
  878. prop->name = name;
  879. prop->nameSpace = ns;
  880. // Allocate the memory for this property based on its type
  881. prop->type = dt;
  882. prop->AllocateMemory();
  883. // Store the variable in the module scope (the reference count is already set to 1)
  884. scriptGlobals.PushLast(prop);
  885. return prop;
  886. }
  887. #ifdef AS_DEPRECATED
  888. // Deprecated since 2.23.0 - 2012-01-30
  889. // internal
  890. void asCModule::ResolveInterfaceIds(asCArray<void*> *substitutions)
  891. {
  892. // For each of the interfaces declared in the script find identical interface in the engine.
  893. // If an identical interface was found then substitute the current id for the identical interface's id,
  894. // then remove this interface declaration. If an interface was modified by the declaration, then
  895. // retry the detection of identical interface for it since it may now match another.
  896. // For an interface to be equal to another the name and methods must match. If the interface
  897. // references another interface, then that must be checked as well, which can lead to circular references.
  898. // Example:
  899. //
  900. // interface A { void f(B@); }
  901. // interface B { void f(A@); void f(C@); }
  902. // interface C { void f(A@); }
  903. //
  904. // A1 equals A2 if and only if B1 equals B2
  905. // B1 equals B2 if and only if A1 equals A2 and C1 equals C2
  906. // C1 equals C2 if and only if A1 equals A2
  907. unsigned int i;
  908. // The interface can only be equal to interfaces declared in other modules.
  909. // Interfaces registered by the application will conflict with this one if it has the same name.
  910. // This means that we only need to look for the interfaces in the engine->classTypes, but not in engine->objectTypes.
  911. asCArray<sObjectTypePair> equals;
  912. for( i = 0; i < classTypes.GetLength(); i++ )
  913. {
  914. asCObjectType *intf1 = classTypes[i];
  915. if( !intf1->IsInterface() )
  916. continue;
  917. // The interface may have been determined to be equal to another already
  918. bool found = false;
  919. for( unsigned int e = 0; e < equals.GetLength(); e++ )
  920. {
  921. if( equals[e].a == intf1 )
  922. {
  923. found = true;
  924. break;
  925. }
  926. }
  927. if( found )
  928. continue;
  929. for( unsigned int n = 0; n < engine->classTypes.GetLength(); n++ )
  930. {
  931. // Don't compare against self
  932. if( engine->classTypes[n] == intf1 )
  933. continue;
  934. asCObjectType *intf2 = engine->classTypes[n];
  935. // Assume the interface are equal, then validate this
  936. sObjectTypePair pair = {intf1,intf2};
  937. equals.PushLast(pair);
  938. if( AreInterfacesEqual(intf1, intf2, equals) )
  939. break;
  940. // Since they are not equal, remove them from the list again
  941. equals.PopLast();
  942. }
  943. }
  944. // For each of the interfaces that have been found to be equal we need to
  945. // remove the new declaration and instead have the module use the existing one.
  946. for( i = 0; i < equals.GetLength(); i++ )
  947. {
  948. // Substitute the old object type from the module's class types
  949. unsigned int c;
  950. for( c = 0; c < classTypes.GetLength(); c++ )
  951. {
  952. if( classTypes[c] == equals[i].a )
  953. {
  954. if( substitutions )
  955. {
  956. substitutions->PushLast(equals[i].a);
  957. substitutions->PushLast(equals[i].b);
  958. }
  959. classTypes[c] = equals[i].b;
  960. equals[i].b->AddRef();
  961. break;
  962. }
  963. }
  964. // Remove the old object type from the engine's class types
  965. engine->classTypes.RemoveValue(equals[i].a);
  966. // Only interfaces in the module are using the type so far
  967. for( c = 0; c < classTypes.GetLength(); c++ )
  968. {
  969. if( classTypes[c]->IsInterface() )
  970. {
  971. asCObjectType *intf = classTypes[c];
  972. for( asUINT m = 0; m < intf->GetMethodCount(); m++ )
  973. {
  974. asCScriptFunction *func = engine->GetScriptFunction(intf->methods[m]);
  975. if( func )
  976. {
  977. // Check if it is the same type
  978. if( func->returnType.GetObjectType() == equals[i].a )
  979. func->returnType.SetObjectType(equals[i].b);
  980. // Look for template instances that uses the interface as subtype
  981. else if( func->returnType.IsTemplate() &&
  982. func->returnType.GetSubType().GetObjectType() == equals[i].a )
  983. {
  984. // Find the originating template type
  985. asCObjectType *templ = 0;
  986. for( asUINT t = 0; t < engine->registeredObjTypes.GetLength(); t++ )
  987. if( (engine->registeredObjTypes[t]->flags & asOBJ_TEMPLATE) &&
  988. engine->registeredObjTypes[t]->name == func->returnType.GetObjectType()->name &&
  989. !engine->templateInstanceTypes.Exists(engine->registeredObjTypes[t]) )
  990. {
  991. templ = engine->registeredObjTypes[t];
  992. break;
  993. }
  994. asASSERT( templ );
  995. asCDataType subType = func->returnType.GetSubType();
  996. subType.SetObjectType(equals[i].b);
  997. func->returnType.SetObjectType(engine->GetTemplateInstanceType(templ, subType));
  998. }
  999. for( asUINT p = 0; p < func->GetParamCount(); p++ )
  1000. {
  1001. // Check if it is the same type
  1002. if( func->parameterTypes[p].GetObjectType() == equals[i].a )
  1003. func->parameterTypes[p].SetObjectType(equals[i].b);
  1004. // Look for template instances that uses the interface as subtype
  1005. else if( func->parameterTypes[p].IsTemplate() &&
  1006. func->parameterTypes[p].GetSubType().GetObjectType() == equals[i].a )
  1007. {
  1008. // Find the originating template type
  1009. asCObjectType *templ = 0;
  1010. for( asUINT t = 0; t < engine->registeredObjTypes.GetLength(); t++ )
  1011. if( (engine->registeredObjTypes[t]->flags & asOBJ_TEMPLATE) &&
  1012. engine->registeredObjTypes[t]->name == func->parameterTypes[p].GetObjectType()->name &&
  1013. !engine->templateInstanceTypes.Exists(engine->registeredObjTypes[t]) )
  1014. {
  1015. templ = engine->registeredObjTypes[t];
  1016. break;
  1017. }
  1018. asASSERT( templ );
  1019. asCDataType subType = func->parameterTypes[p].GetSubType();
  1020. subType.SetObjectType(equals[i].b);
  1021. func->parameterTypes[p].SetObjectType(engine->GetTemplateInstanceType(templ, subType));
  1022. }
  1023. }
  1024. }
  1025. }
  1026. }
  1027. }
  1028. // Substitute all interface methods in the module. Delete all methods for the old interface
  1029. for( unsigned int m = 0; m < equals[i].a->methods.GetLength(); m++ )
  1030. {
  1031. for( c = 0; c < scriptFunctions.GetLength(); c++ )
  1032. {
  1033. if( scriptFunctions[c]->id == equals[i].a->methods[m] )
  1034. {
  1035. if( substitutions )
  1036. substitutions->PushLast(scriptFunctions[c]);
  1037. scriptFunctions[c]->Release();
  1038. scriptFunctions[c] = engine->GetScriptFunction(equals[i].b->methods[m]);
  1039. scriptFunctions[c]->AddRef();
  1040. if( substitutions )
  1041. substitutions->PushLast(scriptFunctions[c]);
  1042. }
  1043. }
  1044. }
  1045. // If there was a template instance, it needs to be removed first
  1046. for( asUINT n = 0; n < engine->templateTypes.GetLength(); n++ )
  1047. {
  1048. if( engine->templateTypes[n]->templateSubType.GetObjectType() == equals[i].a )
  1049. {
  1050. // The use of the template has already been exchanged above
  1051. engine->RemoveTemplateInstanceType(engine->templateTypes[n]);
  1052. n--;
  1053. }
  1054. }
  1055. // Deallocate the object type
  1056. asDELETE(equals[i].a, asCObjectType);
  1057. }
  1058. }
  1059. // internal
  1060. bool asCModule::AreInterfacesEqual(asCObjectType *a, asCObjectType *b, asCArray<sObjectTypePair> &equals)
  1061. {
  1062. // An interface is considered equal to another if the following criterias apply:
  1063. //
  1064. // - The interface names are equal
  1065. // - The number of methods is equal
  1066. // - All the methods are equal
  1067. // - The order of the methods is equal
  1068. // - If a method returns or takes an interface by handle or reference, both interfaces must be equal
  1069. // ------------
  1070. // TODO: Study the possiblity of allowing interfaces where methods are declared in different orders to
  1071. // be considered equal. The compiler and VM can handle this, but it complicates the comparison of interfaces
  1072. // where multiple methods take different interfaces as parameters (or return values). Example:
  1073. //
  1074. // interface A
  1075. // {
  1076. // void f(B, C);
  1077. // void f(B);
  1078. // void f(C);
  1079. // }
  1080. //
  1081. // If 'void f(B)' in module A is compared against 'void f(C)' in module B, then the code will assume
  1082. // interface B in module A equals interface C in module B. Thus 'void f(B, C)' in module A won't match
  1083. // 'void f(C, B)' in module B.
  1084. // ------------
  1085. // Are both interfaces?
  1086. if( !a->IsInterface() || !b->IsInterface() )
  1087. return false;
  1088. // Are the names equal?
  1089. if( a->name != b->name )
  1090. return false;
  1091. // Are the namespaces equal?
  1092. if( a->nameSpace != b->nameSpace )
  1093. return false;
  1094. // Are the number of methods equal?
  1095. if( a->methods.GetLength() != b->methods.GetLength() )
  1096. return false;
  1097. // Keep the number of equals in the list so we can restore it later if necessary
  1098. int prevEquals = (int)equals.GetLength();
  1099. // Are the methods equal to each other?
  1100. bool match = true;
  1101. for( unsigned int n = 0; n < a->methods.GetLength(); n++ )
  1102. {
  1103. match = false;
  1104. asCScriptFunction *funcA = (asCScriptFunction*)engine->GetFunctionById(a->methods[n]);
  1105. asCScriptFunction *funcB = (asCScriptFunction*)engine->GetFunctionById(b->methods[n]);
  1106. // funcB can be null if the module that created the interface has been
  1107. // discarded but the type has not yet been released by the engine.
  1108. if( funcB == 0 )
  1109. break;
  1110. // The methods must have the same name and the same number of parameters
  1111. if( funcA->name != funcB->name ||
  1112. funcA->parameterTypes.GetLength() != funcB->parameterTypes.GetLength() )
  1113. break;
  1114. // The return types must be equal. If the return type is an interface the interfaces must match.
  1115. if( !AreTypesEqual(funcA->returnType, funcB->returnType, equals) )
  1116. break;
  1117. match = true;
  1118. for( unsigned int p = 0; p < funcA->parameterTypes.GetLength(); p++ )
  1119. {
  1120. if( !AreTypesEqual(funcA->parameterTypes[p], funcB->parameterTypes[p], equals) ||
  1121. funcA->inOutFlags[p] != funcB->inOutFlags[p] )
  1122. {
  1123. match = false;
  1124. break;
  1125. }
  1126. }
  1127. if( !match )
  1128. break;
  1129. }
  1130. // For each of the new interfaces that we're assuming to be equal, we need to validate this
  1131. if( match )
  1132. {
  1133. for( unsigned int n = prevEquals; n < equals.GetLength(); n++ )
  1134. {
  1135. if( !AreInterfacesEqual(equals[n].a, equals[n].b, equals) )
  1136. {
  1137. match = false;
  1138. break;
  1139. }
  1140. }
  1141. }
  1142. if( !match )
  1143. {
  1144. // The interfaces doesn't match.
  1145. // Restore the list of previous equals before we go on, so
  1146. // the caller can continue comparing with another interface
  1147. equals.SetLength(prevEquals);
  1148. }
  1149. return match;
  1150. }
  1151. // internal
  1152. bool asCModule::AreTypesEqual(const asCDataType &a, const asCDataType &b, asCArray<sObjectTypePair> &equals)
  1153. {
  1154. if( !a.IsEqualExceptInterfaceType(b) )
  1155. return false;
  1156. asCObjectType *ai = a.GetObjectType();
  1157. asCObjectType *bi = b.GetObjectType();
  1158. if( ai && (ai->flags & asOBJ_TEMPLATE) )
  1159. {
  1160. ai = ai->templateSubType.GetObjectType();
  1161. bi = bi->templateSubType.GetObjectType();
  1162. }
  1163. if( ai && ai->IsInterface() )
  1164. {
  1165. // If the interface is in the equals list, then the pair must match the pair in the list
  1166. bool found = false;
  1167. unsigned int e;
  1168. for( e = 0; e < equals.GetLength(); e++ )
  1169. {
  1170. if( equals[e].a == ai )
  1171. {
  1172. found = true;
  1173. break;
  1174. }
  1175. }
  1176. if( found )
  1177. {
  1178. // Do the pairs match?
  1179. if( equals[e].b != bi )
  1180. return false;
  1181. }
  1182. else
  1183. {
  1184. // Assume they are equal from now on
  1185. sObjectTypePair pair = {ai, bi};
  1186. equals.PushLast(pair);
  1187. }
  1188. }
  1189. return true;
  1190. }
  1191. #endif // AS_DEPRECATED
  1192. // interface
  1193. int asCModule::SaveByteCode(asIBinaryStream *out) const
  1194. {
  1195. #ifdef AS_NO_COMPILER
  1196. UNUSED_VAR(out);
  1197. return asNOT_SUPPORTED;
  1198. #else
  1199. if( out == 0 ) return asINVALID_ARG;
  1200. asCWriter write(const_cast<asCModule*>(this), out, engine);
  1201. return write.Write();
  1202. #endif
  1203. }
  1204. // interface
  1205. int asCModule::LoadByteCode(asIBinaryStream *in)
  1206. {
  1207. if( in == 0 ) return asINVALID_ARG;
  1208. // Only permit loading bytecode if no other thread is currently compiling
  1209. // TODO: It should be possible to have multiple threads perform compilations
  1210. int r = engine->RequestBuild();
  1211. if( r < 0 )
  1212. return r;
  1213. asCReader read(this, in, engine);
  1214. r = read.Read();
  1215. JITCompile();
  1216. engine->BuildCompleted();
  1217. return r;
  1218. }
  1219. // interface
  1220. int asCModule::CompileGlobalVar(const char *sectionName, const char *code, int lineOffset)
  1221. {
  1222. #ifdef AS_NO_COMPILER
  1223. UNUSED_VAR(sectionName);
  1224. UNUSED_VAR(code);
  1225. UNUSED_VAR(lineOffset);
  1226. return asNOT_SUPPORTED;
  1227. #else
  1228. // Validate arguments
  1229. if( code == 0 )
  1230. return asINVALID_ARG;
  1231. // Only one thread may build at one time
  1232. // TODO: It should be possible to have multiple threads perform compilations
  1233. int r = engine->RequestBuild();
  1234. if( r < 0 )
  1235. return r;
  1236. // Prepare the engine
  1237. engine->PrepareEngine();
  1238. if( engine->configFailed )
  1239. {
  1240. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INVALID_CONFIGURATION);
  1241. engine->BuildCompleted();
  1242. return asINVALID_CONFIGURATION;
  1243. }
  1244. // Compile the global variable and add it to the module scope
  1245. asCBuilder builder(engine, this);
  1246. asCString str = code;
  1247. // TODO: namespace: Add the global var to the default namespace
  1248. r = builder.CompileGlobalVar(sectionName, str.AddressOf(), lineOffset);
  1249. engine->BuildCompleted();
  1250. // Initialize the variable
  1251. if( r >= 0 && engine->ep.initGlobalVarsAfterBuild )
  1252. {
  1253. // Clear the memory
  1254. asCGlobalProperty *prop = scriptGlobals[scriptGlobals.GetLength()-1];
  1255. memset(prop->GetAddressOfValue(), 0, sizeof(asDWORD)*prop->type.GetSizeOnStackDWords());
  1256. if( prop->GetInitFunc() )
  1257. {
  1258. // Call the init function for the global variable
  1259. asIScriptContext *ctx = 0;
  1260. int r = engine->CreateContext(&ctx, true);
  1261. if( r < 0 )
  1262. return r;
  1263. r = ctx->Prepare(prop->GetInitFunc()->id);
  1264. if( r >= 0 )
  1265. r = ctx->Execute();
  1266. ctx->Release();
  1267. }
  1268. }
  1269. return r;
  1270. #endif
  1271. }
  1272. // interface
  1273. int asCModule::CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD compileFlags, asIScriptFunction **outFunc)
  1274. {
  1275. #ifdef AS_NO_COMPILER
  1276. UNUSED_VAR(sectionName);
  1277. UNUSED_VAR(code);
  1278. UNUSED_VAR(lineOffset);
  1279. UNUSED_VAR(compileFlags);
  1280. UNUSED_VAR(outFunc);
  1281. return asNOT_SUPPORTED;
  1282. #else
  1283. asASSERT(outFunc == 0 || *outFunc == 0);
  1284. // Validate arguments
  1285. if( code == 0 ||
  1286. (compileFlags != 0 && compileFlags != asCOMP_ADD_TO_MODULE) )
  1287. return asINVALID_ARG;
  1288. // Only one thread may build at one time
  1289. // TODO: It should be possible to have multiple threads perform compilations
  1290. int r = engine->RequestBuild();
  1291. if( r < 0 )
  1292. return r;
  1293. // Prepare the engine
  1294. engine->PrepareEngine();
  1295. if( engine->configFailed )
  1296. {
  1297. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INVALID_CONFIGURATION);
  1298. engine->BuildCompleted();
  1299. return asINVALID_CONFIGURATION;
  1300. }
  1301. // Compile the single function
  1302. asCBuilder builder(engine, this);
  1303. asCString str = code;
  1304. asCScriptFunction *func = 0;
  1305. // TODO: namespace: Add the function to the default namespace
  1306. r = builder.CompileFunction(sectionName, str.AddressOf(), lineOffset, compileFlags, &func);
  1307. engine->BuildCompleted();
  1308. if( r >= 0 && outFunc )
  1309. {
  1310. // Return the function to the caller
  1311. *outFunc = func;
  1312. func->AddRef();
  1313. }
  1314. // Release our reference to the function
  1315. if( func )
  1316. func->Release();
  1317. return r;
  1318. #endif
  1319. }
  1320. // interface
  1321. int asCModule::RemoveFunction(int funcId)
  1322. {
  1323. if( funcId >= 0 && funcId < (int)engine->scriptFunctions.GetLength() )
  1324. return RemoveFunction(engine->scriptFunctions[funcId]);
  1325. return asNO_FUNCTION;
  1326. }
  1327. // interface
  1328. int asCModule::RemoveFunction(asIScriptFunction *func)
  1329. {
  1330. // Find the global function
  1331. asCScriptFunction *f = static_cast<asCScriptFunction*>(func);
  1332. int idx = globalFunctions.IndexOf(f);
  1333. if( idx >= 0 )
  1334. {
  1335. globalFunctions.RemoveIndex(idx);
  1336. f->Release();
  1337. scriptFunctions.RemoveValue(f);
  1338. f->Release();
  1339. return 0;
  1340. }
  1341. return asNO_FUNCTION;
  1342. }
  1343. // internal
  1344. int asCModule::AddFuncDef(const char *name, const asCString &ns)
  1345. {
  1346. asCScriptFunction *func = asNEW(asCScriptFunction)(engine, 0, asFUNC_FUNCDEF);
  1347. func->name = name;
  1348. func->nameSpace = ns;
  1349. funcDefs.PushLast(func);
  1350. engine->funcDefs.PushLast(func);
  1351. func->id = engine->GetNextScriptFunctionId();
  1352. engine->SetScriptFunction(func);
  1353. return (int)funcDefs.GetLength()-1;
  1354. }
  1355. // interface
  1356. asDWORD asCModule::SetAccessMask(asDWORD mask)
  1357. {
  1358. asDWORD old = accessMask;
  1359. accessMask = mask;
  1360. return old;
  1361. }
  1362. END_AS_NAMESPACE