as_module.cpp 37 KB

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