123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "consoleNamespace.h"
- #include "platform/platform.h"
- #include "console/console.h"
- #include "console/ast.h"
- #include "collection/findIterator.h"
- #include "io/resource/resourceManager.h"
- #include "string/findMatch.h"
- #include "console/consoleInternal.h"
- #include "io/fileStream.h"
- #include "console/compiler.h"
- #include "consoleNamespace_ScriptBinding.h"
- U32 Namespace::mCacheSequence = 0;
- DataChunker Namespace::mCacheAllocator;
- DataChunker Namespace::mAllocator;
- Namespace *Namespace::mNamespaceList = NULL;
- Namespace *Namespace::mGlobalNamespace = NULL;
- Namespace::Entry::Entry()
- {
- mCode = NULL;
- mType = InvalidFunctionType;
- }
- void Namespace::Entry::clear()
- {
- if(mCode)
- {
- mCode->decRefCount();
- mCode = NULL;
- }
- // Clean up usage strings generated for script functions.
- if( ( mType == Namespace::Entry::ScriptFunctionType ) && mUsage )
- {
- delete mUsage;
- mUsage = NULL;
- }
- }
- Namespace::Namespace()
- {
- mPackage = NULL;
- mUsage = NULL;
- mCleanUpUsage = false;
- mName = NULL;
- mParent = NULL;
- mNext = NULL;
- mEntryList = NULL;
- mHashSize = 0;
- mHashTable = 0;
- mHashSequence = 0;
- mRefCountToParent = 0;
- mClassRep = 0;
- }
- Namespace::~Namespace()
- {
- if( mUsage && mCleanUpUsage )
- {
- dFree (const_cast <char *> (mUsage));
- mUsage = NULL;
- mCleanUpUsage = false;
- }
- }
- void Namespace::clearEntries()
- {
- for(Entry *walk = mEntryList; walk; walk = walk->mNext)
- walk->clear();
- }
- Namespace *Namespace::find(StringTableEntry name, StringTableEntry package)
- {
- for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
- if(walk->mName == name && walk->mPackage == package)
- return walk;
- Namespace *ret = (Namespace *) mAllocator.alloc(sizeof(Namespace));
- constructInPlace(ret);
- ret->mPackage = package;
- ret->mName = name;
- ret->mNext = mNamespaceList;
- mNamespaceList = ret;
- return ret;
- }
- bool Namespace::canTabComplete(const char *prevText, const char *bestMatch, const char *newText, S32 baseLen, bool fForward)
- {
- // test if it matches the first baseLen chars:
- if(dStrnicmp(newText, prevText, baseLen))
- return false;
- if (fForward)
- {
- if(!bestMatch)
- return dStricmp(newText, prevText) > 0;
- else
- return (dStricmp(newText, prevText) > 0) &&
- (dStricmp(newText, bestMatch) < 0);
- }
- else
- {
- if (dStrlen(prevText) == (U32) baseLen)
- {
- // look for the 'worst match'
- if(!bestMatch)
- return dStricmp(newText, prevText) > 0;
- else
- return dStricmp(newText, bestMatch) > 0;
- }
- else
- {
- if (!bestMatch)
- return (dStricmp(newText, prevText) < 0);
- else
- return (dStricmp(newText, prevText) < 0) &&
- (dStricmp(newText, bestMatch) > 0);
- }
- }
- }
- bool Namespace::unlinkClass(Namespace* parent)
- {
- Namespace* walk = this;
- while(walk->mParent && walk->mParent->mName == mName)
- walk = walk->mParent;
- // Make sure "parent" is the direct parent namespace.
- if(walk->mParent && walk->mParent != parent)
- {
- Con::errorf(ConsoleLogEntry::General, "Namespace::unlinkClass - cannot unlink namespace parent linkage for %s for %s.",
- walk->mName, walk->mParent->mName);
- return false;
- }
- // Decrease the reference count. Note that we do this on the bottom-most namespace.
- AssertWarn(mRefCountToParent > 0, "Namespace::unlinkClass - reference count to parent is already at 0");
- mRefCountToParent--;
- // Unlink if the count dropped to zero.
- if(mRefCountToParent == 0)
- {
- walk->mParent = NULL;
- trashCache();
- }
- return true;
- }
- bool Namespace::classLinkTo(Namespace* parent)
- {
- Namespace* walk = this;
- while(walk->mParent && walk->mParent->mName == mName)
- walk = walk->mParent;
- // Make sure there is no existing parent namespace.
- if(walk->mParent && walk->mParent != parent)
- {
- Con::errorf(ConsoleLogEntry::General, "Namespace::classLinkTo - cannot change namespace parent linkage of %s from %s to %s.",
- walk->mName, walk->mParent->mName, parent->mName);
- return false;
- }
- // Increase the reference count and add the parent namespace.
- mRefCountToParent++;
- walk->mParent = parent;
- trashCache();
- return true;
- }
- void Namespace::buildHashTable()
- {
- if(mHashSequence == mCacheSequence)
- return;
- if(!mEntryList && mParent)
- {
- mParent->buildHashTable();
- mHashTable = mParent->mHashTable;
- mHashSize = mParent->mHashSize;
- mHashSequence = mCacheSequence;
- return;
- }
- U32 entryCount = 0;
- Namespace * ns;
- for(ns = this; ns; ns = ns->mParent)
- for(Entry *walk = ns->mEntryList; walk; walk = walk->mNext)
- if(lookupRecursive(walk->mFunctionName) == walk)
- entryCount++;
- mHashSize = entryCount + (entryCount >> 1) + 1;
- if(!(mHashSize & 1))
- mHashSize++;
- mHashTable = (Entry **) mCacheAllocator.alloc(sizeof(Entry *) * mHashSize);
- for(U32 i = 0; i < mHashSize; i++)
- mHashTable[i] = NULL;
- for(ns = this; ns; ns = ns->mParent)
- {
- for(Entry *walk = ns->mEntryList; walk; walk = walk->mNext)
- {
- U32 index = HashPointer(walk->mFunctionName) % mHashSize;
- while(mHashTable[index] && mHashTable[index]->mFunctionName != walk->mFunctionName)
- {
- index++;
- if(index >= mHashSize)
- index = 0;
- }
- if(!mHashTable[index])
- mHashTable[index] = walk;
- }
- }
- mHashSequence = mCacheSequence;
- }
- void Namespace::init()
- {
- // create the global namespace
- mGlobalNamespace = find(NULL);
- }
- Namespace *Namespace::global()
- {
- return mGlobalNamespace;
- }
- void Namespace::shutdown()
- {
- for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
- walk->clearEntries();
- }
- void Namespace::trashCache()
- {
- mCacheSequence++;
- mCacheAllocator.freeBlocks();
- }
- const char *Namespace::tabComplete(const char *prevText, S32 baseLen, bool fForward)
- {
- if(mHashSequence != mCacheSequence)
- buildHashTable();
- const char *bestMatch = NULL;
- for(U32 i = 0; i < mHashSize; i++)
- if(mHashTable[i] && canTabComplete(prevText, bestMatch, mHashTable[i]->mFunctionName, baseLen, fForward))
- bestMatch = mHashTable[i]->mFunctionName;
- return bestMatch;
- }
- Namespace::Entry *Namespace::lookupRecursive(StringTableEntry name)
- {
- for(Namespace *ns = this; ns; ns = ns->mParent)
- for(Entry *walk = ns->mEntryList; walk; walk = walk->mNext)
- if(walk->mFunctionName == name)
- return walk;
- return NULL;
- }
- Namespace::Entry *Namespace::lookup(StringTableEntry name)
- {
- if(mHashSequence != mCacheSequence)
- buildHashTable();
- U32 index = HashPointer(name) % mHashSize;
- while(mHashTable[index] && mHashTable[index]->mFunctionName != name)
- {
- index++;
- if(index >= mHashSize)
- index = 0;
- }
- return mHashTable[index];
- }
- static S32 QSORT_CALLBACK compareEntries(const void* a,const void* b)
- {
- const Namespace::Entry* fa = *((Namespace::Entry**)a);
- const Namespace::Entry* fb = *((Namespace::Entry**)b);
- return dStricmp(fa->mFunctionName, fb->mFunctionName);
- }
- void Namespace::getEntryList(Vector<Entry *> *vec)
- {
- if(mHashSequence != mCacheSequence)
- buildHashTable();
- for(U32 i = 0; i < mHashSize; i++)
- if(mHashTable[i])
- vec->push_back(mHashTable[i]);
- dQsort(vec->address(),vec->size(),sizeof(Namespace::Entry *),compareEntries);
- }
- Namespace::Entry *Namespace::createLocalEntry(StringTableEntry name)
- {
- for(Entry *walk = mEntryList; walk; walk = walk->mNext)
- {
- if(walk->mFunctionName == name)
- {
- walk->clear();
- return walk;
- }
- }
- Entry *ent = (Entry *) mAllocator.alloc(sizeof(Entry));
- constructInPlace(ent);
- ent->mNamespace = this;
- ent->mFunctionName = name;
- ent->mNext = mEntryList;
- ent->mPackage = mPackage;
- mEntryList = ent;
- return ent;
- }
- void Namespace::addFunction(StringTableEntry name, CodeBlock *cb, U32 functionOffset, const char* usage)
- {
- Entry *ent = createLocalEntry(name);
- trashCache();
- ent->mUsage = usage;
- ent->mCode = cb;
- ent->mFunctionOffset = functionOffset;
- ent->mCode->incRefCount();
- ent->mType = Entry::ScriptFunctionType;
- }
- void Namespace::addCommand(StringTableEntry name,StringCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
- {
- Entry *ent = createLocalEntry(name);
- trashCache();
- ent->mUsage = usage;
- ent->mMinArgs = minArgs;
- ent->mMaxArgs = maxArgs;
- ent->mType = Entry::StringCallbackType;
- ent->cb.mStringCallbackFunc = cb;
- }
- void Namespace::addCommand(StringTableEntry name,IntCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
- {
- Entry *ent = createLocalEntry(name);
- trashCache();
- ent->mUsage = usage;
- ent->mMinArgs = minArgs;
- ent->mMaxArgs = maxArgs;
- ent->mType = Entry::IntCallbackType;
- ent->cb.mIntCallbackFunc = cb;
- }
- void Namespace::addCommand(StringTableEntry name,VoidCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
- {
- Entry *ent = createLocalEntry(name);
- trashCache();
- ent->mUsage = usage;
- ent->mMinArgs = minArgs;
- ent->mMaxArgs = maxArgs;
- ent->mType = Entry::VoidCallbackType;
- ent->cb.mVoidCallbackFunc = cb;
- }
- void Namespace::addCommand(StringTableEntry name,FloatCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
- {
- Entry *ent = createLocalEntry(name);
- trashCache();
- ent->mUsage = usage;
- ent->mMinArgs = minArgs;
- ent->mMaxArgs = maxArgs;
- ent->mType = Entry::FloatCallbackType;
- ent->cb.mFloatCallbackFunc = cb;
- }
- void Namespace::addCommand(StringTableEntry name,BoolCallback cb, const char *usage, S32 minArgs, S32 maxArgs)
- {
- Entry *ent = createLocalEntry(name);
- trashCache();
- ent->mUsage = usage;
- ent->mMinArgs = minArgs;
- ent->mMaxArgs = maxArgs;
- ent->mType = Entry::BoolCallbackType;
- ent->cb.mBoolCallbackFunc = cb;
- }
- void Namespace::addOverload(const char * name, const char *altUsage)
- {
- static U32 uid=0;
- char buffer[1024];
- char lilBuffer[32];
- dStrcpy(buffer, name);
- dSprintf(lilBuffer, 32, "_%d", uid++);
- dStrcat(buffer, lilBuffer);
- Entry *ent = createLocalEntry(StringTable->insert( buffer ));
- trashCache();
- ent->mUsage = altUsage;
- ent->mMinArgs = -1;
- ent->mMaxArgs = -2;
- ent->mType = Entry::OverloadMarker;
- ent->cb.mGroupName = name;
- }
- void Namespace::markGroup(const char* name, const char* usage)
- {
- static U32 uid=0;
- char buffer[1024];
- char lilBuffer[32];
- dStrcpy(buffer, name);
- dSprintf(lilBuffer, 32, "_%d", uid++);
- dStrcat(buffer, lilBuffer);
- Entry *ent = createLocalEntry(StringTable->insert( buffer ));
- trashCache();
- if(usage != NULL)
- lastUsage = (char*)(ent->mUsage = usage);
- else
- ent->mUsage = lastUsage;
- ent->mMinArgs = -1; // Make sure it explodes if somehow we run this entry.
- ent->mMaxArgs = -2;
- ent->mType = Entry::GroupMarker;
- ent->cb.mGroupName = name;
- }
- extern S32 executeBlock(StmtNode *block, ExprEvalState *state);
- const char *Namespace::Entry::execute(S32 argc, const char **argv, ExprEvalState *state)
- {
- if(mType == ScriptFunctionType)
- {
- if(mFunctionOffset)
- return mCode->exec(mFunctionOffset, argv[0], mNamespace, argc, argv, false, mPackage);
- else
- return "";
- }
- if((mMinArgs && argc < mMinArgs) || (mMaxArgs && argc > mMaxArgs))
- {
- Con::warnf(ConsoleLogEntry::Script, "%s::%s - wrong number of arguments.", mNamespace->mName, mFunctionName);
- Con::warnf(ConsoleLogEntry::Script, "usage: %s", mUsage);
- return "";
- }
- static char returnBuffer[32];
- switch(mType)
- {
- case StringCallbackType:
- return cb.mStringCallbackFunc(state->thisObject, argc, argv);
- case IntCallbackType:
- dSprintf(returnBuffer, sizeof(returnBuffer), "%d",
- cb.mIntCallbackFunc(state->thisObject, argc, argv));
- return returnBuffer;
- case FloatCallbackType:
- dSprintf(returnBuffer, sizeof(returnBuffer), "%.9g",
- cb.mFloatCallbackFunc(state->thisObject, argc, argv));
- return returnBuffer;
- case VoidCallbackType:
- cb.mVoidCallbackFunc(state->thisObject, argc, argv);
- return "";
- case BoolCallbackType:
- dSprintf(returnBuffer, sizeof(returnBuffer), "%d",
- (U32)cb.mBoolCallbackFunc(state->thisObject, argc, argv));
- return returnBuffer;
- }
- return "";
- }
- StringTableEntry Namespace::mActivePackages[Namespace::MaxActivePackages];
- U32 Namespace::mNumActivePackages = 0;
- U32 Namespace::mOldNumActivePackages = 0;
- bool Namespace::isPackage(StringTableEntry name)
- {
- for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
- if(walk->mPackage == name)
- return true;
- return false;
- }
- void Namespace::activatePackage(StringTableEntry name)
- {
- if(mNumActivePackages == MaxActivePackages)
- {
- Con::printf("ActivatePackage(%s) failed - Max package limit reached: %d", name, MaxActivePackages);
- return;
- }
- if(!name)
- return;
- // see if this one's already active
- for(U32 i = 0; i < mNumActivePackages; i++)
- if(mActivePackages[i] == name)
- return;
- // kill the cache
- trashCache();
- // find all the package namespaces...
- for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
- {
- if(walk->mPackage == name)
- {
- Namespace *parent = Namespace::find(walk->mName);
- // hook the parent
- walk->mParent = parent->mParent;
- parent->mParent = walk;
- // now swap the entries:
- Entry *ew;
- for(ew = parent->mEntryList; ew; ew = ew->mNext)
- ew->mNamespace = walk;
- for(ew = walk->mEntryList; ew; ew = ew->mNext)
- ew->mNamespace = parent;
- ew = walk->mEntryList;
- walk->mEntryList = parent->mEntryList;
- parent->mEntryList = ew;
- }
- }
- mActivePackages[mNumActivePackages++] = name;
- }
- void Namespace::deactivatePackage(StringTableEntry name)
- {
- S32 i, j;
- for(i = 0; i < (S32)mNumActivePackages; i++)
- if(mActivePackages[i] == name)
- break;
- if(i == mNumActivePackages)
- return;
- trashCache();
- for(j = mNumActivePackages - 1; j >= i; j--)
- {
- // gotta unlink em in reverse order...
- for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
- {
- if(walk->mPackage == mActivePackages[j])
- {
- Namespace *parent = Namespace::find(walk->mName);
- // hook the parent
- parent->mParent = walk->mParent;
- walk->mParent = NULL;
- // now swap the entries:
- Entry *ew;
- for(ew = parent->mEntryList; ew; ew = ew->mNext)
- ew->mNamespace = walk;
- for(ew = walk->mEntryList; ew; ew = ew->mNext)
- ew->mNamespace = parent;
- ew = walk->mEntryList;
- walk->mEntryList = parent->mEntryList;
- parent->mEntryList = ew;
- }
- }
- }
- mNumActivePackages = i;
- }
- void Namespace::unlinkPackages()
- {
- mOldNumActivePackages = mNumActivePackages;
- if(!mNumActivePackages)
- return;
- deactivatePackage(mActivePackages[0]);
- }
- void Namespace::relinkPackages()
- {
- if(!mOldNumActivePackages)
- return;
- for(U32 i = 0; i < mOldNumActivePackages; i++)
- activatePackage(mActivePackages[i]);
- }
|