ScriptEngine.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // Create a dummy module for compiling the line
  90. asIScriptModule* module = mAngelScriptEngine->GetModule("ExecuteImmediate", asGM_ALWAYS_CREATE);
  91. if (!module)
  92. return false;
  93. // Use the line as the function name to get an easy to understand error message in case of failure
  94. asIScriptFunction *function = 0;
  95. if (module->CompileFunction(line.c_str(), wrappedLine.c_str(), -1, 0, &function) < 0)
  96. return false;
  97. if (mImmediateContext->Prepare(function->GetId()) < 0)
  98. {
  99. function->Release();
  100. return false;
  101. }
  102. bool success = false;
  103. success = mImmediateContext->Execute() >= 0;
  104. function->Release();
  105. return success;
  106. }
  107. void ScriptEngine::garbageCollect(bool fullCycle)
  108. {
  109. PROFILE(Script_GarbageCollect);
  110. // Unprepare contexts up to the highest used
  111. mImmediateContext->Unprepare();
  112. unsigned highest = getHighestScriptNestingLevel();
  113. for (unsigned i = 0; i < highest; ++i)
  114. mScriptFileContexts[i]->Unprepare();
  115. // Then actually garbage collect
  116. mAngelScriptEngine->GarbageCollect(fullCycle ? asGC_FULL_CYCLE : asGC_ONE_STEP);
  117. }
  118. void ScriptEngine::setLogMode(ScriptLogMode mode)
  119. {
  120. mLogMode = mode;
  121. }
  122. void ScriptEngine::clearLogMessages()
  123. {
  124. mLogMessages.clear();
  125. }
  126. void ScriptEngine::logMessage(const asSMessageInfo* msg)
  127. {
  128. std::string message = std::string(msg->section) + " (" + toString(msg->row) + "," + toString(msg->col) + ") " +
  129. std::string(msg->message);
  130. if (mLogMode == LOGMODE_IMMEDIATE)
  131. {
  132. switch (msg->type)
  133. {
  134. case asMSGTYPE_ERROR:
  135. LOGERROR(message);
  136. break;
  137. case asMSGTYPE_WARNING:
  138. LOGWARNING(message);
  139. break;
  140. default:
  141. LOGINFO(message);
  142. break;
  143. }
  144. }
  145. else
  146. {
  147. // In retained mode, ignore info messages
  148. if ((msg->type == asMSGTYPE_ERROR) || (msg->type == asMSGTYPE_WARNING))
  149. mLogMessages += message + "\n";
  150. }
  151. }
  152. asIScriptContext* ScriptEngine::getScriptFileContext(unsigned nestingLevel) const
  153. {
  154. if (nestingLevel >= mScriptFileContexts.size())
  155. return 0;
  156. return mScriptFileContexts[nestingLevel];
  157. }