consoleNamespace.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. #include "consoleNamespace_ScriptBinding.h"
  33. U32 Namespace::mCacheSequence = 0;
  34. DataChunker Namespace::mCacheAllocator;
  35. DataChunker Namespace::mAllocator;
  36. Namespace *Namespace::mNamespaceList = NULL;
  37. Namespace *Namespace::mGlobalNamespace = NULL;
  38. Namespace::Entry::Entry()
  39. {
  40. mCode = NULL;
  41. mType = InvalidFunctionType;
  42. }
  43. void Namespace::Entry::clear()
  44. {
  45. if(mCode)
  46. {
  47. mCode->decRefCount();
  48. mCode = NULL;
  49. }
  50. // Clean up usage strings generated for script functions.
  51. if( ( mType == Namespace::Entry::ScriptFunctionType ) && mUsage )
  52. {
  53. delete mUsage;
  54. mUsage = NULL;
  55. }
  56. }
  57. Namespace::Namespace()
  58. {
  59. mPackage = NULL;
  60. mUsage = NULL;
  61. mCleanUpUsage = false;
  62. mName = NULL;
  63. mParent = NULL;
  64. mNext = NULL;
  65. mEntryList = NULL;
  66. mHashSize = 0;
  67. mHashTable = 0;
  68. mHashSequence = 0;
  69. mRefCountToParent = 0;
  70. mClassRep = 0;
  71. }
  72. Namespace::~Namespace()
  73. {
  74. if( mUsage && mCleanUpUsage )
  75. {
  76. dFree (const_cast <char *> (mUsage));
  77. mUsage = NULL;
  78. mCleanUpUsage = false;
  79. }
  80. }
  81. void Namespace::clearEntries()
  82. {
  83. for(Entry *walk = mEntryList; walk; walk = walk->mNext)
  84. walk->clear();
  85. }
  86. Namespace *Namespace::find(StringTableEntry name, StringTableEntry package)
  87. {
  88. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  89. if(walk->mName == name && walk->mPackage == package)
  90. return walk;
  91. Namespace *ret = (Namespace *) mAllocator.alloc(sizeof(Namespace));
  92. constructInPlace(ret);
  93. ret->mPackage = package;
  94. ret->mName = name;
  95. ret->mNext = mNamespaceList;
  96. mNamespaceList = ret;
  97. return ret;
  98. }
  99. bool Namespace::canTabComplete(const char *prevText, const char *bestMatch, const char *newText, S32 baseLen, bool fForward)
  100. {
  101. // test if it matches the first baseLen chars:
  102. if(dStrnicmp(newText, prevText, baseLen))
  103. return false;
  104. if (fForward)
  105. {
  106. if(!bestMatch)
  107. return dStricmp(newText, prevText) > 0;
  108. else
  109. return (dStricmp(newText, prevText) > 0) &&
  110. (dStricmp(newText, bestMatch) < 0);
  111. }
  112. else
  113. {
  114. if (dStrlen(prevText) == (U32) baseLen)
  115. {
  116. // look for the 'worst match'
  117. if(!bestMatch)
  118. return dStricmp(newText, prevText) > 0;
  119. else
  120. return dStricmp(newText, bestMatch) > 0;
  121. }
  122. else
  123. {
  124. if (!bestMatch)
  125. return (dStricmp(newText, prevText) < 0);
  126. else
  127. return (dStricmp(newText, prevText) < 0) &&
  128. (dStricmp(newText, bestMatch) > 0);
  129. }
  130. }
  131. }
  132. bool Namespace::unlinkClass(Namespace *parent)
  133. {
  134. Namespace *walk = this;
  135. while(walk->mParent && walk->mParent->mName == mName)
  136. walk = walk->mParent;
  137. if(walk->mParent && walk->mParent != parent)
  138. {
  139. Con::errorf(ConsoleLogEntry::General, "Namespace::unlinkClass - cannot unlink namespace parent linkage for %s for %s.",
  140. walk->mName, walk->mParent->mName);
  141. return false;
  142. }
  143. AssertFatal(mRefCountToParent > 0, "Namespace::unlinkClass - reference count to parent is less than 0");
  144. mRefCountToParent--;
  145. if(mRefCountToParent == 0)
  146. walk->mParent = NULL;
  147. trashCache();
  148. return true;
  149. }
  150. bool Namespace::classLinkTo(Namespace *parent)
  151. {
  152. Namespace *walk = this;
  153. while(walk->mParent && walk->mParent->mName == mName)
  154. walk = walk->mParent;
  155. if(walk->mParent && walk->mParent != parent)
  156. {
  157. Con::errorf(ConsoleLogEntry::General, "Error: cannot change namespace parent linkage of %s from %s to %s.",
  158. walk->mName, walk->mParent->mName, parent->mName);
  159. return false;
  160. }
  161. mRefCountToParent++;
  162. walk->mParent = parent;
  163. trashCache();
  164. return true;
  165. }
  166. void Namespace::buildHashTable()
  167. {
  168. if(mHashSequence == mCacheSequence)
  169. return;
  170. if(!mEntryList && mParent)
  171. {
  172. mParent->buildHashTable();
  173. mHashTable = mParent->mHashTable;
  174. mHashSize = mParent->mHashSize;
  175. mHashSequence = mCacheSequence;
  176. return;
  177. }
  178. U32 entryCount = 0;
  179. Namespace * ns;
  180. for(ns = this; ns; ns = ns->mParent)
  181. for(Entry *walk = ns->mEntryList; walk; walk = walk->mNext)
  182. if(lookupRecursive(walk->mFunctionName) == walk)
  183. entryCount++;
  184. mHashSize = entryCount + (entryCount >> 1) + 1;
  185. if(!(mHashSize & 1))
  186. mHashSize++;
  187. mHashTable = (Entry **) mCacheAllocator.alloc(sizeof(Entry *) * mHashSize);
  188. for(U32 i = 0; i < mHashSize; i++)
  189. mHashTable[i] = NULL;
  190. for(ns = this; ns; ns = ns->mParent)
  191. {
  192. for(Entry *walk = ns->mEntryList; walk; walk = walk->mNext)
  193. {
  194. U32 index = HashPointer(walk->mFunctionName) % mHashSize;
  195. while(mHashTable[index] && mHashTable[index]->mFunctionName != walk->mFunctionName)
  196. {
  197. index++;
  198. if(index >= mHashSize)
  199. index = 0;
  200. }
  201. if(!mHashTable[index])
  202. mHashTable[index] = walk;
  203. }
  204. }
  205. mHashSequence = mCacheSequence;
  206. }
  207. void Namespace::init()
  208. {
  209. // create the global namespace
  210. mGlobalNamespace = find(NULL);
  211. }
  212. Namespace *Namespace::global()
  213. {
  214. return mGlobalNamespace;
  215. }
  216. void Namespace::shutdown()
  217. {
  218. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  219. walk->clearEntries();
  220. }
  221. void Namespace::trashCache()
  222. {
  223. mCacheSequence++;
  224. mCacheAllocator.freeBlocks();
  225. }
  226. const char *Namespace::tabComplete(const char *prevText, S32 baseLen, bool fForward)
  227. {
  228. if(mHashSequence != mCacheSequence)
  229. buildHashTable();
  230. const char *bestMatch = NULL;
  231. for(U32 i = 0; i < mHashSize; i++)
  232. if(mHashTable[i] && canTabComplete(prevText, bestMatch, mHashTable[i]->mFunctionName, baseLen, fForward))
  233. bestMatch = mHashTable[i]->mFunctionName;
  234. return bestMatch;
  235. }
  236. Namespace::Entry *Namespace::lookupRecursive(StringTableEntry name)
  237. {
  238. for(Namespace *ns = this; ns; ns = ns->mParent)
  239. for(Entry *walk = ns->mEntryList; walk; walk = walk->mNext)
  240. if(walk->mFunctionName == name)
  241. return walk;
  242. return NULL;
  243. }
  244. Namespace::Entry *Namespace::lookup(StringTableEntry name)
  245. {
  246. if(mHashSequence != mCacheSequence)
  247. buildHashTable();
  248. U32 index = HashPointer(name) % mHashSize;
  249. while(mHashTable[index] && mHashTable[index]->mFunctionName != name)
  250. {
  251. index++;
  252. if(index >= mHashSize)
  253. index = 0;
  254. }
  255. return mHashTable[index];
  256. }
  257. static S32 QSORT_CALLBACK compareEntries(const void* a,const void* b)
  258. {
  259. const Namespace::Entry* fa = *((Namespace::Entry**)a);
  260. const Namespace::Entry* fb = *((Namespace::Entry**)b);
  261. return dStricmp(fa->mFunctionName, fb->mFunctionName);
  262. }
  263. void Namespace::getEntryList(Vector<Entry *> *vec)
  264. {
  265. if(mHashSequence != mCacheSequence)
  266. buildHashTable();
  267. for(U32 i = 0; i < mHashSize; i++)
  268. if(mHashTable[i])
  269. vec->push_back(mHashTable[i]);
  270. dQsort(vec->address(),vec->size(),sizeof(Namespace::Entry *),compareEntries);
  271. }
  272. Namespace::Entry *Namespace::createLocalEntry(StringTableEntry name)
  273. {
  274. for(Entry *walk = mEntryList; walk; walk = walk->mNext)
  275. {
  276. if(walk->mFunctionName == name)
  277. {
  278. walk->clear();
  279. return walk;
  280. }
  281. }
  282. Entry *ent = (Entry *) mAllocator.alloc(sizeof(Entry));
  283. constructInPlace(ent);
  284. ent->mNamespace = this;
  285. ent->mFunctionName = name;
  286. ent->mNext = mEntryList;
  287. ent->mPackage = mPackage;
  288. mEntryList = ent;
  289. return ent;
  290. }
  291. void Namespace::addFunction(StringTableEntry name, CodeBlock *cb, U32 functionOffset, const char* usage)
  292. {
  293. Entry *ent = createLocalEntry(name);
  294. trashCache();
  295. ent->mUsage = usage;
  296. ent->mCode = cb;
  297. ent->mFunctionOffset = functionOffset;
  298. ent->mCode->incRefCount();
  299. ent->mType = Entry::ScriptFunctionType;
  300. }
  301. void Namespace::addCommand(StringTableEntry name,StringCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
  302. {
  303. Entry *ent = createLocalEntry(name);
  304. trashCache();
  305. ent->mUsage = usage;
  306. ent->mMinArgs = minArgs;
  307. ent->mMaxArgs = maxArgs;
  308. ent->mType = Entry::StringCallbackType;
  309. ent->cb.mStringCallbackFunc = cb;
  310. }
  311. void Namespace::addCommand(StringTableEntry name,IntCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
  312. {
  313. Entry *ent = createLocalEntry(name);
  314. trashCache();
  315. ent->mUsage = usage;
  316. ent->mMinArgs = minArgs;
  317. ent->mMaxArgs = maxArgs;
  318. ent->mType = Entry::IntCallbackType;
  319. ent->cb.mIntCallbackFunc = cb;
  320. }
  321. void Namespace::addCommand(StringTableEntry name,VoidCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
  322. {
  323. Entry *ent = createLocalEntry(name);
  324. trashCache();
  325. ent->mUsage = usage;
  326. ent->mMinArgs = minArgs;
  327. ent->mMaxArgs = maxArgs;
  328. ent->mType = Entry::VoidCallbackType;
  329. ent->cb.mVoidCallbackFunc = cb;
  330. }
  331. void Namespace::addCommand(StringTableEntry name,FloatCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
  332. {
  333. Entry *ent = createLocalEntry(name);
  334. trashCache();
  335. ent->mUsage = usage;
  336. ent->mMinArgs = minArgs;
  337. ent->mMaxArgs = maxArgs;
  338. ent->mType = Entry::FloatCallbackType;
  339. ent->cb.mFloatCallbackFunc = cb;
  340. }
  341. void Namespace::addCommand(StringTableEntry name,BoolCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
  342. {
  343. Entry *ent = createLocalEntry(name);
  344. trashCache();
  345. ent->mUsage = usage;
  346. ent->mMinArgs = minArgs;
  347. ent->mMaxArgs = maxArgs;
  348. ent->mType = Entry::BoolCallbackType;
  349. ent->cb.mBoolCallbackFunc = cb;
  350. }
  351. void Namespace::addOverload(const char * name, const char *altUsage)
  352. {
  353. static U32 uid=0;
  354. char buffer[1024];
  355. char lilBuffer[32];
  356. dStrcpy(buffer, name);
  357. dSprintf(lilBuffer, 32, "_%d", uid++);
  358. dStrcat(buffer, lilBuffer);
  359. Entry *ent = createLocalEntry(StringTable->insert( buffer ));
  360. trashCache();
  361. ent->mUsage = altUsage;
  362. ent->mMinArgs = -1;
  363. ent->mMaxArgs = -2;
  364. ent->mType = Entry::OverloadMarker;
  365. ent->cb.mGroupName = name;
  366. }
  367. void Namespace::markGroup(const char* name, const char* usage)
  368. {
  369. static U32 uid=0;
  370. char buffer[1024];
  371. char lilBuffer[32];
  372. dStrcpy(buffer, name);
  373. dSprintf(lilBuffer, 32, "_%d", uid++);
  374. dStrcat(buffer, lilBuffer);
  375. Entry *ent = createLocalEntry(StringTable->insert( buffer ));
  376. trashCache();
  377. if(usage != NULL)
  378. lastUsage = (char*)(ent->mUsage = usage);
  379. else
  380. ent->mUsage = lastUsage;
  381. ent->mMinArgs = -1; // Make sure it explodes if somehow we run this entry.
  382. ent->mMaxArgs = -2;
  383. ent->mType = Entry::GroupMarker;
  384. ent->cb.mGroupName = name;
  385. }
  386. extern S32 executeBlock(StmtNode *block, ExprEvalState *state);
  387. const char *Namespace::Entry::execute(S32 argc, const char **argv, ExprEvalState *state)
  388. {
  389. if(mType == ScriptFunctionType)
  390. {
  391. if(mFunctionOffset)
  392. return mCode->exec(mFunctionOffset, argv[0], mNamespace, argc, argv, false, mPackage);
  393. else
  394. return "";
  395. }
  396. if((mMinArgs && argc < mMinArgs) || (mMaxArgs && argc > mMaxArgs))
  397. {
  398. Con::warnf(ConsoleLogEntry::Script, "%s::%s - wrong number of arguments.", mNamespace->mName, mFunctionName);
  399. Con::warnf(ConsoleLogEntry::Script, "usage: %s", mUsage);
  400. return "";
  401. }
  402. static char returnBuffer[32];
  403. switch(mType)
  404. {
  405. case StringCallbackType:
  406. return cb.mStringCallbackFunc(state->thisObject, argc, argv);
  407. case IntCallbackType:
  408. dSprintf(returnBuffer, sizeof(returnBuffer), "%d",
  409. cb.mIntCallbackFunc(state->thisObject, argc, argv));
  410. return returnBuffer;
  411. case FloatCallbackType:
  412. dSprintf(returnBuffer, sizeof(returnBuffer), "%.9g",
  413. cb.mFloatCallbackFunc(state->thisObject, argc, argv));
  414. return returnBuffer;
  415. case VoidCallbackType:
  416. cb.mVoidCallbackFunc(state->thisObject, argc, argv);
  417. return "";
  418. case BoolCallbackType:
  419. dSprintf(returnBuffer, sizeof(returnBuffer), "%d",
  420. (U32)cb.mBoolCallbackFunc(state->thisObject, argc, argv));
  421. return returnBuffer;
  422. }
  423. return "";
  424. }
  425. StringTableEntry Namespace::mActivePackages[Namespace::MaxActivePackages];
  426. U32 Namespace::mNumActivePackages = 0;
  427. U32 Namespace::mOldNumActivePackages = 0;
  428. bool Namespace::isPackage(StringTableEntry name)
  429. {
  430. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  431. if(walk->mPackage == name)
  432. return true;
  433. return false;
  434. }
  435. void Namespace::activatePackage(StringTableEntry name)
  436. {
  437. if(mNumActivePackages == MaxActivePackages)
  438. {
  439. Con::printf("ActivatePackage(%s) failed - Max package limit reached: %d", name, MaxActivePackages);
  440. return;
  441. }
  442. if(!name)
  443. return;
  444. // see if this one's already active
  445. for(U32 i = 0; i < mNumActivePackages; i++)
  446. if(mActivePackages[i] == name)
  447. return;
  448. // kill the cache
  449. trashCache();
  450. // find all the package namespaces...
  451. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  452. {
  453. if(walk->mPackage == name)
  454. {
  455. Namespace *parent = Namespace::find(walk->mName);
  456. // hook the parent
  457. walk->mParent = parent->mParent;
  458. parent->mParent = walk;
  459. // now swap the entries:
  460. Entry *ew;
  461. for(ew = parent->mEntryList; ew; ew = ew->mNext)
  462. ew->mNamespace = walk;
  463. for(ew = walk->mEntryList; ew; ew = ew->mNext)
  464. ew->mNamespace = parent;
  465. ew = walk->mEntryList;
  466. walk->mEntryList = parent->mEntryList;
  467. parent->mEntryList = ew;
  468. }
  469. }
  470. mActivePackages[mNumActivePackages++] = name;
  471. }
  472. void Namespace::deactivatePackage(StringTableEntry name)
  473. {
  474. S32 i, j;
  475. for(i = 0; i < (S32)mNumActivePackages; i++)
  476. if(mActivePackages[i] == name)
  477. break;
  478. if(i == mNumActivePackages)
  479. return;
  480. trashCache();
  481. for(j = mNumActivePackages - 1; j >= i; j--)
  482. {
  483. // gotta unlink em in reverse order...
  484. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  485. {
  486. if(walk->mPackage == mActivePackages[j])
  487. {
  488. Namespace *parent = Namespace::find(walk->mName);
  489. // hook the parent
  490. parent->mParent = walk->mParent;
  491. walk->mParent = NULL;
  492. // now swap the entries:
  493. Entry *ew;
  494. for(ew = parent->mEntryList; ew; ew = ew->mNext)
  495. ew->mNamespace = walk;
  496. for(ew = walk->mEntryList; ew; ew = ew->mNext)
  497. ew->mNamespace = parent;
  498. ew = walk->mEntryList;
  499. walk->mEntryList = parent->mEntryList;
  500. parent->mEntryList = ew;
  501. }
  502. }
  503. }
  504. mNumActivePackages = i;
  505. }
  506. void Namespace::unlinkPackages()
  507. {
  508. mOldNumActivePackages = mNumActivePackages;
  509. if(!mNumActivePackages)
  510. return;
  511. deactivatePackage(mActivePackages[0]);
  512. }
  513. void Namespace::relinkPackages()
  514. {
  515. if(!mOldNumActivePackages)
  516. return;
  517. for(U32 i = 0; i < mOldNumActivePackages; i++)
  518. activatePackage(mActivePackages[i]);
  519. }