ScriptEngine.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "Log.h"
  25. #include "Profiler.h"
  26. #include "RegisterArray.h"
  27. #include "RegisterStdString.h"
  28. #include "ScriptEngine.h"
  29. #include "ScriptFile.h"
  30. #include "StringUtils.h"
  31. #include <angelscript.h>
  32. #include "DebugNew.h"
  33. void messageCallback(const asSMessageInfo* msg, void* param)
  34. {
  35. ScriptEngine* engine = static_cast<ScriptEngine*>(param);
  36. engine->logMessage(msg);
  37. }
  38. ScriptEngine::ScriptEngine() :
  39. mAngelScriptEngine(0),
  40. mImmediateContext(0)
  41. {
  42. mAngelScriptEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
  43. if (!mAngelScriptEngine)
  44. EXCEPTION("Could not create AngelScript engine");
  45. LOGINFO("Script engine created");
  46. mAngelScriptEngine->SetUserData(this);
  47. mAngelScriptEngine->SetEngineProperty(asEP_USE_CHARACTER_LITERALS, true);
  48. mAngelScriptEngine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, true);
  49. mAngelScriptEngine->SetEngineProperty(asEP_ALLOW_IMPLICIT_HANDLE_TYPES, true);
  50. mAngelScriptEngine->SetEngineProperty(asEP_BUILD_WITHOUT_LINE_CUES, true);
  51. mAngelScriptEngine->SetMessageCallback(asFUNCTION(messageCallback), this, asCALL_CDECL);
  52. // Register the array and string types, but leave it for the script engine instantiator to install the rest of the API
  53. {
  54. PROFILE(Script_RegisterInbuiltTypes);
  55. LOGDEBUG("Registering array and string types");
  56. registerArray(mAngelScriptEngine);
  57. registerStdString(mAngelScriptEngine);
  58. }
  59. // Create the context for immediate execution
  60. mImmediateContext = mAngelScriptEngine->CreateContext();
  61. // Create the function/method contexts
  62. for (unsigned i = 0 ; i < MAX_SCRIPT_NESTING_LEVEL; ++i)
  63. mScriptFileContexts.push_back(mAngelScriptEngine->CreateContext());
  64. }
  65. ScriptEngine::~ScriptEngine()
  66. {
  67. LOGINFO("Script engine shut down");
  68. if (mImmediateContext)
  69. {
  70. mImmediateContext->Release();
  71. mImmediateContext = 0;
  72. }
  73. for (unsigned i = 0 ; i < MAX_SCRIPT_NESTING_LEVEL; ++i)
  74. {
  75. if (mScriptFileContexts[i])
  76. mScriptFileContexts[i]->Release();
  77. }
  78. mScriptFileContexts.clear();
  79. if (mAngelScriptEngine)
  80. {
  81. mAngelScriptEngine->Release();
  82. mAngelScriptEngine = 0;
  83. }
  84. }
  85. bool ScriptEngine::execute(const std::string& line)
  86. {
  87. // Note: compiling code each time is slow. Not to be used for performance-critical or repeating activity
  88. PROFILE(Script_ExecuteImmediate);
  89. std::string wrappedLine = "void f(){\n" + line + ";\n}";
  90. // If necessary, create a dummy module for compiling the line
  91. asIScriptModule* module = 0;
  92. if (mImmediateScriptFile)
  93. module = mImmediateScriptFile->getScriptModule();
  94. if (!module)
  95. module = mAngelScriptEngine->GetModule("ExecuteImmediate", asGM_CREATE_IF_NOT_EXISTS);
  96. if (!module)
  97. return false;
  98. asIScriptFunction *function = 0;
  99. if (module->CompileFunction("", 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. mAngelScriptEngine->GarbageCollect(asGC_ONE_STEP);
  119. }
  120. void ScriptEngine::setImmediateScriptFile(ScriptFile* file)
  121. {
  122. mImmediateScriptFile = file;
  123. }
  124. void ScriptEngine::setLogMode(ScriptLogMode mode)
  125. {
  126. mLogMode = mode;
  127. }
  128. void ScriptEngine::clearLogMessages()
  129. {
  130. mLogMessages.clear();
  131. }
  132. void ScriptEngine::logMessage(const asSMessageInfo* msg)
  133. {
  134. std::string message = std::string(msg->section) + " (" + toString(msg->row) + "," + toString(msg->col) + ") " +
  135. std::string(msg->message);
  136. if (mLogMode == LOGMODE_IMMEDIATE)
  137. {
  138. switch (msg->type)
  139. {
  140. case asMSGTYPE_ERROR:
  141. LOGERROR(message);
  142. break;
  143. case asMSGTYPE_WARNING:
  144. LOGWARNING(message);
  145. break;
  146. default:
  147. LOGINFO(message);
  148. break;
  149. }
  150. }
  151. else
  152. {
  153. // In retained mode, ignore info messages
  154. if ((msg->type == asMSGTYPE_ERROR) || (msg->type == asMSGTYPE_WARNING))
  155. mLogMessages += message + "\n";
  156. }
  157. }
  158. asIScriptContext* ScriptEngine::getScriptFileContext(unsigned nestingLevel) const
  159. {
  160. if (nestingLevel >= mScriptFileContexts.size())
  161. return 0;
  162. return mScriptFileContexts[nestingLevel];
  163. }