2
0

consoleNamespace.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "consoleNamespace.h"
  23. #include "platform/platform.h"
  24. #include "console/console.h"
  25. #include "console/ast.h"
  26. #include "collection/findIterator.h"
  27. #include "io/resource/resourceManager.h"
  28. #include "string/findMatch.h"
  29. #include "console/consoleInternal.h"
  30. #include "io/fileStream.h"
  31. #include "console/compiler.h"
  32. U32 Namespace::mCacheSequence = 0;
  33. DataChunker Namespace::mCacheAllocator;
  34. DataChunker Namespace::mAllocator;
  35. Namespace *Namespace::mNamespaceList = NULL;
  36. Namespace *Namespace::mGlobalNamespace = NULL;
  37. Namespace::Entry::Entry()
  38. {
  39. mCode = NULL;
  40. mType = InvalidFunctionType;
  41. }
  42. void Namespace::Entry::clear()
  43. {
  44. if(mCode)
  45. {
  46. mCode->decRefCount();
  47. mCode = NULL;
  48. }
  49. // Clean up usage strings generated for script functions.
  50. if( ( mType == Namespace::Entry::ScriptFunctionType ) && mUsage )
  51. {
  52. delete mUsage;
  53. mUsage = NULL;
  54. }
  55. }
  56. Namespace::Namespace()
  57. {
  58. mPackage = NULL;
  59. mUsage = NULL;
  60. mCleanUpUsage = false;
  61. mName = NULL;
  62. mParent = NULL;
  63. mNext = NULL;
  64. mEntryList = NULL;
  65. mHashSize = 0;
  66. mHashTable = 0;
  67. mHashSequence = 0;
  68. mRefCountToParent = 0;
  69. mClassRep = 0;
  70. }
  71. Namespace::~Namespace()
  72. {
  73. if( mUsage && mCleanUpUsage )
  74. {
  75. dFree (const_cast <char *> (mUsage));
  76. mUsage = NULL;
  77. mCleanUpUsage = false;
  78. }
  79. }
  80. void Namespace::clearEntries()
  81. {
  82. for(Entry *walk = mEntryList; walk; walk = walk->mNext)
  83. walk->clear();
  84. }
  85. Namespace *Namespace::find(StringTableEntry name, StringTableEntry package)
  86. {
  87. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  88. if(walk->mName == name && walk->mPackage == package)
  89. return walk;
  90. Namespace *ret = (Namespace *) mAllocator.alloc(sizeof(Namespace));
  91. constructInPlace(ret);
  92. ret->mPackage = package;
  93. ret->mName = name;
  94. ret->mNext = mNamespaceList;
  95. mNamespaceList = ret;
  96. return ret;
  97. }
  98. bool Namespace::canTabComplete(const char *prevText, const char *bestMatch, const char *newText, S32 baseLen, bool fForward)
  99. {
  100. // test if it matches the first baseLen chars:
  101. if(dStrnicmp(newText, prevText, baseLen))
  102. return false;
  103. if (fForward)
  104. {
  105. if(!bestMatch)
  106. return dStricmp(newText, prevText) > 0;
  107. else
  108. return (dStricmp(newText, prevText) > 0) &&
  109. (dStricmp(newText, bestMatch) < 0);
  110. }
  111. else
  112. {
  113. if (dStrlen(prevText) == (U32) baseLen)
  114. {
  115. // look for the 'worst match'
  116. if(!bestMatch)
  117. return dStricmp(newText, prevText) > 0;
  118. else
  119. return dStricmp(newText, bestMatch) > 0;
  120. }
  121. else
  122. {
  123. if (!bestMatch)
  124. return (dStricmp(newText, prevText) < 0);
  125. else
  126. return (dStricmp(newText, prevText) < 0) &&
  127. (dStricmp(newText, bestMatch) > 0);
  128. }
  129. }
  130. }
  131. bool Namespace::unlinkClass(Namespace *parent)
  132. {
  133. Namespace *walk = this;
  134. while(walk->mParent && walk->mParent->mName == mName)
  135. walk = walk->mParent;
  136. if(walk->mParent && walk->mParent != parent)
  137. {
  138. Con::errorf(ConsoleLogEntry::General, "Namespace::unlinkClass - cannot unlink namespace parent linkage for %s for %s.",
  139. walk->mName, walk->mParent->mName);
  140. return false;
  141. }
  142. mRefCountToParent--;
  143. AssertFatal(mRefCountToParent >= 0, "Namespace::unlinkClass - reference count to parent is less than 0");
  144. if(mRefCountToParent == 0)
  145. walk->mParent = NULL;
  146. trashCache();
  147. return true;
  148. }
  149. bool Namespace::classLinkTo(Namespace *parent)
  150. {
  151. Namespace *walk = this;
  152. while(walk->mParent && walk->mParent->mName == mName)
  153. walk = walk->mParent;
  154. if(walk->mParent && walk->mParent != parent)
  155. {
  156. Con::errorf(ConsoleLogEntry::General, "Error: cannot change namespace parent linkage of %s from %s to %s.",
  157. walk->mName, walk->mParent->mName, parent->mName);
  158. return false;
  159. }
  160. mRefCountToParent++;
  161. walk->mParent = parent;
  162. trashCache();
  163. return true;
  164. }
  165. void Namespace::buildHashTable()
  166. {
  167. if(mHashSequence == mCacheSequence)
  168. return;
  169. if(!mEntryList && mParent)
  170. {
  171. mParent->buildHashTable();
  172. mHashTable = mParent->mHashTable;
  173. mHashSize = mParent->mHashSize;
  174. mHashSequence = mCacheSequence;
  175. return;
  176. }
  177. U32 entryCount = 0;
  178. Namespace * ns;
  179. for(ns = this; ns; ns = ns->mParent)
  180. for(Entry *walk = ns->mEntryList; walk; walk = walk->mNext)
  181. if(lookupRecursive(walk->mFunctionName) == walk)
  182. entryCount++;
  183. mHashSize = entryCount + (entryCount >> 1) + 1;
  184. if(!(mHashSize & 1))
  185. mHashSize++;
  186. mHashTable = (Entry **) mCacheAllocator.alloc(sizeof(Entry *) * mHashSize);
  187. for(U32 i = 0; i < mHashSize; i++)
  188. mHashTable[i] = NULL;
  189. for(ns = this; ns; ns = ns->mParent)
  190. {
  191. for(Entry *walk = ns->mEntryList; walk; walk = walk->mNext)
  192. {
  193. U32 index = HashPointer(walk->mFunctionName) % mHashSize;
  194. while(mHashTable[index] && mHashTable[index]->mFunctionName != walk->mFunctionName)
  195. {
  196. index++;
  197. if(index >= mHashSize)
  198. index = 0;
  199. }
  200. if(!mHashTable[index])
  201. mHashTable[index] = walk;
  202. }
  203. }
  204. mHashSequence = mCacheSequence;
  205. }
  206. void Namespace::init()
  207. {
  208. // create the global namespace
  209. mGlobalNamespace = find(NULL);
  210. }
  211. Namespace *Namespace::global()
  212. {
  213. return mGlobalNamespace;
  214. }
  215. void Namespace::shutdown()
  216. {
  217. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  218. walk->clearEntries();
  219. }
  220. void Namespace::trashCache()
  221. {
  222. mCacheSequence++;
  223. mCacheAllocator.freeBlocks();
  224. }
  225. const char *Namespace::tabComplete(const char *prevText, S32 baseLen, bool fForward)
  226. {
  227. if(mHashSequence != mCacheSequence)
  228. buildHashTable();
  229. const char *bestMatch = NULL;
  230. for(U32 i = 0; i < mHashSize; i++)
  231. if(mHashTable[i] && canTabComplete(prevText, bestMatch, mHashTable[i]->mFunctionName, baseLen, fForward))
  232. bestMatch = mHashTable[i]->mFunctionName;
  233. return bestMatch;
  234. }
  235. Namespace::Entry *Namespace::lookupRecursive(StringTableEntry name)
  236. {
  237. for(Namespace *ns = this; ns; ns = ns->mParent)
  238. for(Entry *walk = ns->mEntryList; walk; walk = walk->mNext)
  239. if(walk->mFunctionName == name)
  240. return walk;
  241. return NULL;
  242. }
  243. Namespace::Entry *Namespace::lookup(StringTableEntry name)
  244. {
  245. if(mHashSequence != mCacheSequence)
  246. buildHashTable();
  247. U32 index = HashPointer(name) % mHashSize;
  248. while(mHashTable[index] && mHashTable[index]->mFunctionName != name)
  249. {
  250. index++;
  251. if(index >= mHashSize)
  252. index = 0;
  253. }
  254. return mHashTable[index];
  255. }
  256. static S32 QSORT_CALLBACK compareEntries(const void* a,const void* b)
  257. {
  258. const Namespace::Entry* fa = *((Namespace::Entry**)a);
  259. const Namespace::Entry* fb = *((Namespace::Entry**)b);
  260. return dStricmp(fa->mFunctionName, fb->mFunctionName);
  261. }
  262. void Namespace::getEntryList(Vector<Entry *> *vec)
  263. {
  264. if(mHashSequence != mCacheSequence)
  265. buildHashTable();
  266. for(U32 i = 0; i < mHashSize; i++)
  267. if(mHashTable[i])
  268. vec->push_back(mHashTable[i]);
  269. dQsort(vec->address(),vec->size(),sizeof(Namespace::Entry *),compareEntries);
  270. }
  271. Namespace::Entry *Namespace::createLocalEntry(StringTableEntry name)
  272. {
  273. for(Entry *walk = mEntryList; walk; walk = walk->mNext)
  274. {
  275. if(walk->mFunctionName == name)
  276. {
  277. walk->clear();
  278. return walk;
  279. }
  280. }
  281. Entry *ent = (Entry *) mAllocator.alloc(sizeof(Entry));
  282. constructInPlace(ent);
  283. ent->mNamespace = this;
  284. ent->mFunctionName = name;
  285. ent->mNext = mEntryList;
  286. ent->mPackage = mPackage;
  287. mEntryList = ent;
  288. return ent;
  289. }
  290. void Namespace::addFunction(StringTableEntry name, CodeBlock *cb, U32 functionOffset, const char* usage)
  291. {
  292. Entry *ent = createLocalEntry(name);
  293. trashCache();
  294. ent->mUsage = usage;
  295. ent->mCode = cb;
  296. ent->mFunctionOffset = functionOffset;
  297. ent->mCode->incRefCount();
  298. ent->mType = Entry::ScriptFunctionType;
  299. }
  300. void Namespace::addCommand(StringTableEntry name,StringCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
  301. {
  302. Entry *ent = createLocalEntry(name);
  303. trashCache();
  304. ent->mUsage = usage;
  305. ent->mMinArgs = minArgs;
  306. ent->mMaxArgs = maxArgs;
  307. ent->mType = Entry::StringCallbackType;
  308. ent->cb.mStringCallbackFunc = cb;
  309. }
  310. void Namespace::addCommand(StringTableEntry name,IntCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
  311. {
  312. Entry *ent = createLocalEntry(name);
  313. trashCache();
  314. ent->mUsage = usage;
  315. ent->mMinArgs = minArgs;
  316. ent->mMaxArgs = maxArgs;
  317. ent->mType = Entry::IntCallbackType;
  318. ent->cb.mIntCallbackFunc = cb;
  319. }
  320. void Namespace::addCommand(StringTableEntry name,VoidCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
  321. {
  322. Entry *ent = createLocalEntry(name);
  323. trashCache();
  324. ent->mUsage = usage;
  325. ent->mMinArgs = minArgs;
  326. ent->mMaxArgs = maxArgs;
  327. ent->mType = Entry::VoidCallbackType;
  328. ent->cb.mVoidCallbackFunc = cb;
  329. }
  330. void Namespace::addCommand(StringTableEntry name,FloatCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
  331. {
  332. Entry *ent = createLocalEntry(name);
  333. trashCache();
  334. ent->mUsage = usage;
  335. ent->mMinArgs = minArgs;
  336. ent->mMaxArgs = maxArgs;
  337. ent->mType = Entry::FloatCallbackType;
  338. ent->cb.mFloatCallbackFunc = cb;
  339. }
  340. void Namespace::addCommand(StringTableEntry name,BoolCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
  341. {
  342. Entry *ent = createLocalEntry(name);
  343. trashCache();
  344. ent->mUsage = usage;
  345. ent->mMinArgs = minArgs;
  346. ent->mMaxArgs = maxArgs;
  347. ent->mType = Entry::BoolCallbackType;
  348. ent->cb.mBoolCallbackFunc = cb;
  349. }
  350. void Namespace::addOverload(const char * name, const char *altUsage)
  351. {
  352. static U32 uid=0;
  353. char buffer[1024];
  354. char lilBuffer[32];
  355. dStrcpy(buffer, name);
  356. dSprintf(lilBuffer, 32, "_%d", uid++);
  357. dStrcat(buffer, lilBuffer);
  358. Entry *ent = createLocalEntry(StringTable->insert( buffer ));
  359. trashCache();
  360. ent->mUsage = altUsage;
  361. ent->mMinArgs = -1;
  362. ent->mMaxArgs = -2;
  363. ent->mType = Entry::OverloadMarker;
  364. ent->cb.mGroupName = name;
  365. }
  366. void Namespace::markGroup(const char* name, const char* usage)
  367. {
  368. static U32 uid=0;
  369. char buffer[1024];
  370. char lilBuffer[32];
  371. dStrcpy(buffer, name);
  372. dSprintf(lilBuffer, 32, "_%d", uid++);
  373. dStrcat(buffer, lilBuffer);
  374. Entry *ent = createLocalEntry(StringTable->insert( buffer ));
  375. trashCache();
  376. if(usage != NULL)
  377. lastUsage = (char*)(ent->mUsage = usage);
  378. else
  379. ent->mUsage = lastUsage;
  380. ent->mMinArgs = -1; // Make sure it explodes if somehow we run this entry.
  381. ent->mMaxArgs = -2;
  382. ent->mType = Entry::GroupMarker;
  383. ent->cb.mGroupName = name;
  384. }
  385. extern S32 executeBlock(StmtNode *block, ExprEvalState *state);
  386. const char *Namespace::Entry::execute(S32 argc, const char **argv, ExprEvalState *state)
  387. {
  388. if(mType == ScriptFunctionType)
  389. {
  390. if(mFunctionOffset)
  391. return mCode->exec(mFunctionOffset, argv[0], mNamespace, argc, argv, false, mPackage);
  392. else
  393. return "";
  394. }
  395. if((mMinArgs && argc < mMinArgs) || (mMaxArgs && argc > mMaxArgs))
  396. {
  397. Con::warnf(ConsoleLogEntry::Script, "%s::%s - wrong number of arguments.", mNamespace->mName, mFunctionName);
  398. Con::warnf(ConsoleLogEntry::Script, "usage: %s", mUsage);
  399. return "";
  400. }
  401. static char returnBuffer[32];
  402. switch(mType)
  403. {
  404. case StringCallbackType:
  405. return cb.mStringCallbackFunc(state->thisObject, argc, argv);
  406. case IntCallbackType:
  407. dSprintf(returnBuffer, sizeof(returnBuffer), "%d",
  408. cb.mIntCallbackFunc(state->thisObject, argc, argv));
  409. return returnBuffer;
  410. case FloatCallbackType:
  411. dSprintf(returnBuffer, sizeof(returnBuffer), "%.9g",
  412. cb.mFloatCallbackFunc(state->thisObject, argc, argv));
  413. return returnBuffer;
  414. case VoidCallbackType:
  415. cb.mVoidCallbackFunc(state->thisObject, argc, argv);
  416. return "";
  417. case BoolCallbackType:
  418. dSprintf(returnBuffer, sizeof(returnBuffer), "%d",
  419. (U32)cb.mBoolCallbackFunc(state->thisObject, argc, argv));
  420. return returnBuffer;
  421. }
  422. return "";
  423. }
  424. StringTableEntry Namespace::mActivePackages[Namespace::MaxActivePackages];
  425. U32 Namespace::mNumActivePackages = 0;
  426. U32 Namespace::mOldNumActivePackages = 0;
  427. bool Namespace::isPackage(StringTableEntry name)
  428. {
  429. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  430. if(walk->mPackage == name)
  431. return true;
  432. return false;
  433. }
  434. void Namespace::activatePackage(StringTableEntry name)
  435. {
  436. if(mNumActivePackages == MaxActivePackages)
  437. {
  438. Con::printf("ActivatePackage(%s) failed - Max package limit reached: %d", name, MaxActivePackages);
  439. return;
  440. }
  441. if(!name)
  442. return;
  443. // see if this one's already active
  444. for(U32 i = 0; i < mNumActivePackages; i++)
  445. if(mActivePackages[i] == name)
  446. return;
  447. // kill the cache
  448. trashCache();
  449. // find all the package namespaces...
  450. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  451. {
  452. if(walk->mPackage == name)
  453. {
  454. Namespace *parent = Namespace::find(walk->mName);
  455. // hook the parent
  456. walk->mParent = parent->mParent;
  457. parent->mParent = walk;
  458. // now swap the entries:
  459. Entry *ew;
  460. for(ew = parent->mEntryList; ew; ew = ew->mNext)
  461. ew->mNamespace = walk;
  462. for(ew = walk->mEntryList; ew; ew = ew->mNext)
  463. ew->mNamespace = parent;
  464. ew = walk->mEntryList;
  465. walk->mEntryList = parent->mEntryList;
  466. parent->mEntryList = ew;
  467. }
  468. }
  469. mActivePackages[mNumActivePackages++] = name;
  470. }
  471. void Namespace::deactivatePackage(StringTableEntry name)
  472. {
  473. S32 i, j;
  474. for(i = 0; i < (S32)mNumActivePackages; i++)
  475. if(mActivePackages[i] == name)
  476. break;
  477. if(i == mNumActivePackages)
  478. return;
  479. trashCache();
  480. for(j = mNumActivePackages - 1; j >= i; j--)
  481. {
  482. // gotta unlink em in reverse order...
  483. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  484. {
  485. if(walk->mPackage == mActivePackages[j])
  486. {
  487. Namespace *parent = Namespace::find(walk->mName);
  488. // hook the parent
  489. parent->mParent = walk->mParent;
  490. walk->mParent = NULL;
  491. // now swap the entries:
  492. Entry *ew;
  493. for(ew = parent->mEntryList; ew; ew = ew->mNext)
  494. ew->mNamespace = walk;
  495. for(ew = walk->mEntryList; ew; ew = ew->mNext)
  496. ew->mNamespace = parent;
  497. ew = walk->mEntryList;
  498. walk->mEntryList = parent->mEntryList;
  499. parent->mEntryList = ew;
  500. }
  501. }
  502. }
  503. mNumActivePackages = i;
  504. }
  505. void Namespace::unlinkPackages()
  506. {
  507. mOldNumActivePackages = mNumActivePackages;
  508. if(!mNumActivePackages)
  509. return;
  510. deactivatePackage(mActivePackages[0]);
  511. }
  512. void Namespace::relinkPackages()
  513. {
  514. if(!mOldNumActivePackages)
  515. return;
  516. for(U32 i = 0; i < mOldNumActivePackages; i++)
  517. activatePackage(mActivePackages[i]);
  518. }
  519. ConsoleFunctionGroupBegin( Packages, "Functions relating to the control of packages.");
  520. ConsoleFunction(isPackage,bool,2,2,"( packageName ) Use the isPackage function to check if the name or ID specified in packageName is a valid package.\n"
  521. "@param packagename The name or ID of an existing package.\n"
  522. "@return Returns true if packageName is a valid package, false otherwise.\n"
  523. "@sa activatePackage, deactivatePackage")
  524. {
  525. StringTableEntry packageName = StringTable->insert(argv[1]);
  526. return Namespace::isPackage(packageName);
  527. }
  528. ConsoleFunction(activatePackage, void,2,2,"( packageName ) Use the activatePackage function to activate a package definition and to re-define all functions named within this package with the definitions provided in the package body.\n"
  529. "This pushes the newly activated package onto the top of the package stack.\n"
  530. "@param packagename The name or ID of an existing package.\n"
  531. "@return No return value.\n"
  532. "@sa deactivatePackage, isPackage")
  533. {
  534. StringTableEntry packageName = StringTable->insert(argv[1]);
  535. Namespace::activatePackage(packageName);
  536. }
  537. ConsoleFunction(deactivatePackage, void,2,2,"( packageName ) Use the deactivatePackage function to deactivate a package definition and to pop any definitions from this package off the package stack.\n"
  538. "This also causes any subsequently stacked packages to be popped. i.e. If any packages were activated after the one specified in packageName, they too will be deactivated and popped.\n"
  539. "@param packagename The name or ID of an existing package.\n"
  540. "@return No return value.\n"
  541. "@sa activatePackage, isPackage")
  542. {
  543. StringTableEntry packageName = StringTable->insert(argv[1]);
  544. Namespace::deactivatePackage(packageName);
  545. }
  546. ConsoleFunctionGroupEnd( Packages );