ScriptEngine.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Exception.h"
  25. #include "Log.h"
  26. #include "Profiler.h"
  27. #include "RegisterArray.h"
  28. #include "RegisterStdString.h"
  29. #include "ScriptEngine.h"
  30. #include "ScriptFile.h"
  31. #include "StringUtils.h"
  32. #include <angelscript.h>
  33. #include "DebugNew.h"
  34. void messageCallback(const asSMessageInfo* msg, void* param)
  35. {
  36. ScriptEngine* engine = static_cast<ScriptEngine*>(param);
  37. engine->logMessage(msg);
  38. }
  39. ScriptEngine::ScriptEngine() :
  40. mAngelScriptEngine(0),
  41. mImmediateContext(0)
  42. {
  43. mAngelScriptEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
  44. if (!mAngelScriptEngine)
  45. EXCEPTION("Could not create AngelScript engine");
  46. LOGINFO("Script engine created");
  47. mAngelScriptEngine->SetUserData(this);
  48. mAngelScriptEngine->SetEngineProperty(asEP_USE_CHARACTER_LITERALS, true);
  49. mAngelScriptEngine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, true);
  50. mAngelScriptEngine->SetMessageCallback(asFUNCTION(messageCallback), this, asCALL_CDECL);
  51. // Register the array and string types, but leave it for the script engine instantiator to install the rest of the API
  52. {
  53. PROFILE(Script_RegisterInbuiltTypes);
  54. LOGDEBUG("Registering array and string types");
  55. registerArray(mAngelScriptEngine);
  56. registerStdString(mAngelScriptEngine);
  57. }
  58. // Create the context for immediate execution
  59. mImmediateContext = mAngelScriptEngine->CreateContext();
  60. // Create the function/method contexts
  61. for (unsigned i = 0 ; i < MAX_SCRIPT_NESTING_LEVEL; ++i)
  62. mScriptFileContexts.push_back(mAngelScriptEngine->CreateContext());
  63. }
  64. ScriptEngine::~ScriptEngine()
  65. {
  66. LOGINFO("Script engine shut down");
  67. if (mImmediateContext)
  68. {
  69. mImmediateContext->Release();
  70. mImmediateContext = 0;
  71. }
  72. for (unsigned i = 0 ; i < MAX_SCRIPT_NESTING_LEVEL; ++i)
  73. {
  74. if (mScriptFileContexts[i])
  75. mScriptFileContexts[i]->Release();
  76. }
  77. mScriptFileContexts.clear();
  78. if (mAngelScriptEngine)
  79. {
  80. mAngelScriptEngine->Release();
  81. mAngelScriptEngine = 0;
  82. }
  83. }
  84. bool ScriptEngine::execute(const std::string& line)
  85. {
  86. // Note: compiling code each time is slow. Not to be used for performance-critical or repeating activity
  87. PROFILE(Script_ExecuteImmediate);
  88. std::string wrappedLine = "void f(){\n" + line + ";\n}";
  89. // If necessary, create a dummy module for compiling the line
  90. asIScriptModule* module = 0;
  91. if (mImmediateScriptFile)
  92. module = mImmediateScriptFile->getScriptModule();
  93. if (!module)
  94. module = mAngelScriptEngine->GetModule("ExecuteImmediate", asGM_CREATE_IF_NOT_EXISTS);
  95. if (!module)
  96. return false;
  97. // Use the line as the function name to get an easy to understand error message in case of failure
  98. asIScriptFunction *function = 0;
  99. if (module->CompileFunction(line.c_str(), wrappedLine.c_str(), -1, 0, &function) < 0)
  100. return false;
  101. if (mImmediateContext->Prepare(function->GetId()) < 0)
  102. {
  103. function->Release();
  104. return false;
  105. }
  106. bool success = false;
  107. success = mImmediateContext->Execute() >= 0;
  108. mImmediateContext->Unprepare();
  109. function->Release();
  110. return success;
  111. }
  112. void ScriptEngine::garbageCollect(bool fullCycle)
  113. {
  114. PROFILE(Script_GarbageCollect);
  115. if (fullCycle)
  116. mAngelScriptEngine->GarbageCollect(asGC_FULL_CYCLE);
  117. else
  118. {
  119. // If not doing a full cycle, first detect garbage using one cycle, then do a full destruction
  120. // This is faster than doing an actual full cycle
  121. mAngelScriptEngine->GarbageCollect(asGC_ONE_STEP | asGC_DETECT_GARBAGE);
  122. mAngelScriptEngine->GarbageCollect(asGC_FULL_CYCLE | asGC_DESTROY_GARBAGE);
  123. }
  124. }
  125. void ScriptEngine::setImmediateScriptFile(ScriptFile* file)
  126. {
  127. mImmediateScriptFile = file;
  128. }
  129. void ScriptEngine::setLogMode(ScriptLogMode mode)
  130. {
  131. mLogMode = mode;
  132. }
  133. void ScriptEngine::clearLogMessages()
  134. {
  135. mLogMessages.clear();
  136. }
  137. void ScriptEngine::logMessage(const asSMessageInfo* msg)
  138. {
  139. std::string message = std::string(msg->section) + " (" + toString(msg->row) + "," + toString(msg->col) + ") " +
  140. std::string(msg->message);
  141. if (mLogMode == LOGMODE_IMMEDIATE)
  142. {
  143. switch (msg->type)
  144. {
  145. case asMSGTYPE_ERROR:
  146. LOGERROR(message);
  147. break;
  148. case asMSGTYPE_WARNING:
  149. LOGWARNING(message);
  150. break;
  151. default:
  152. LOGINFO(message);
  153. break;
  154. }
  155. }
  156. else
  157. {
  158. // In retained mode, ignore info messages
  159. if ((msg->type == asMSGTYPE_ERROR) || (msg->type == asMSGTYPE_WARNING))
  160. mLogMessages += message + "\n";
  161. }
  162. }
  163. asIScriptContext* ScriptEngine::getScriptFileContext(unsigned nestingLevel) const
  164. {
  165. if (nestingLevel >= mScriptFileContexts.size())
  166. return 0;
  167. return mScriptFileContexts[nestingLevel];
  168. }