ScriptEngine.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "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->SetMessageCallback(asFUNCTION(messageCallback), this, asCALL_CDECL);
  50. // Register the array and string types, but leave it for the script engine instantiator to install the rest of the API
  51. {
  52. PROFILE(Script_RegisterInbuiltTypes);
  53. LOGDEBUG("Registering array and string types");
  54. registerArray(mAngelScriptEngine);
  55. registerStdString(mAngelScriptEngine);
  56. }
  57. // Create the context for immediate execution
  58. mImmediateContext = createScriptContext();
  59. }
  60. ScriptEngine::~ScriptEngine()
  61. {
  62. LOGINFO("Script engine shut down");
  63. if (mImmediateContext)
  64. {
  65. mImmediateContext->Release();
  66. mImmediateContext = 0;
  67. }
  68. if (mAngelScriptEngine)
  69. {
  70. mAngelScriptEngine->Release();
  71. mAngelScriptEngine = 0;
  72. }
  73. }
  74. asIScriptContext* ScriptEngine::createScriptContext()
  75. {
  76. return mAngelScriptEngine->CreateContext();
  77. }
  78. bool ScriptEngine::execute(const std::string& line)
  79. {
  80. // Note: compiling code each time is slow. Not to be used for performance-critical or repeating activity
  81. PROFILE(Script_ExecuteImmediate);
  82. std::string wrappedLine = "void temp(){\n" + line + ";\n}";
  83. // Create a dummy module for compiling the line
  84. asIScriptModule* module = mAngelScriptEngine->GetModule("temp", asGM_ALWAYS_CREATE);
  85. if (!module)
  86. return false;
  87. // Use the line as the function name to get an easy to understand error message in case of failure
  88. asIScriptFunction *function = 0;
  89. if (module->CompileFunction(line.c_str(), wrappedLine.c_str(), -1, 0, &function) < 0)
  90. return false;
  91. if (mImmediateContext->Prepare(function->GetId()) < 0)
  92. {
  93. function->Release();
  94. return false;
  95. }
  96. bool success = false;
  97. success = mImmediateContext->Execute() >= 0;
  98. function->Release();
  99. return success;
  100. }
  101. void ScriptEngine::garbageCollect()
  102. {
  103. PROFILE(Script_GarbageCollect);
  104. mAngelScriptEngine->GarbageCollect(asGC_ONE_STEP);
  105. }
  106. void ScriptEngine::setLogMode(ScriptLogMode mode)
  107. {
  108. mLogMode = mode;
  109. }
  110. void ScriptEngine::clearLogMessages()
  111. {
  112. mLogMessages.clear();
  113. }
  114. void ScriptEngine::logMessage(const asSMessageInfo* msg)
  115. {
  116. std::string message = std::string(msg->section) + " (" + toString(msg->row) + "," + toString(msg->col) + ") " +
  117. std::string(msg->message);
  118. if (mLogMode == LOGMODE_IMMEDIATE)
  119. {
  120. switch (msg->type)
  121. {
  122. case asMSGTYPE_ERROR:
  123. LOGERROR(message);
  124. break;
  125. case asMSGTYPE_WARNING:
  126. LOGWARNING(message);
  127. break;
  128. default:
  129. LOGINFO(message);
  130. break;
  131. }
  132. }
  133. else
  134. {
  135. // In retained mode, ignore info messages
  136. if ((msg->type == asMSGTYPE_ERROR) || (msg->type == asMSGTYPE_WARNING))
  137. mLogMessages += message + "\n";
  138. }
  139. }