ScriptEngine.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. std::string message = std::string(msg->section) + " (" + toString(msg->row) + "," + toString(msg->col) + ") " + std::string(msg->message);
  36. switch (msg->type)
  37. {
  38. case asMSGTYPE_ERROR:
  39. LOGERROR(message);
  40. break;
  41. case asMSGTYPE_WARNING:
  42. LOGWARNING(message);
  43. break;
  44. default:
  45. LOGINFO(message);
  46. break;
  47. }
  48. }
  49. ScriptEngine::ScriptEngine() :
  50. mAngelScriptEngine(0),
  51. mImmediateContext(0)
  52. {
  53. mAngelScriptEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
  54. if (!mAngelScriptEngine)
  55. EXCEPTION("Could not create AngelScript engine");
  56. LOGINFO("Script engine created");
  57. mAngelScriptEngine->SetUserData(this);
  58. mAngelScriptEngine->SetEngineProperty(asEP_USE_CHARACTER_LITERALS, true);
  59. mAngelScriptEngine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, true);
  60. mAngelScriptEngine->SetMessageCallback(asFUNCTION(messageCallback), 0, asCALL_CDECL);
  61. // Register the array and string types, but leave it for the script engine instantiator to install the rest of the API
  62. {
  63. PROFILE(Script_RegisterInbuiltTypes);
  64. LOGDEBUG("Registering array and string types");
  65. registerArray(mAngelScriptEngine);
  66. registerStdString(mAngelScriptEngine);
  67. }
  68. // Create the context for immediate execution
  69. mImmediateContext = createScriptContext();
  70. }
  71. ScriptEngine::~ScriptEngine()
  72. {
  73. LOGINFO("Script engine shut down");
  74. if (mImmediateContext)
  75. {
  76. mImmediateContext->Release();
  77. mImmediateContext = 0;
  78. }
  79. if (mAngelScriptEngine)
  80. {
  81. mAngelScriptEngine->Release();
  82. mAngelScriptEngine = 0;
  83. }
  84. }
  85. asIScriptContext* ScriptEngine::createScriptContext()
  86. {
  87. return mAngelScriptEngine->CreateContext();
  88. }
  89. bool ScriptEngine::execute(const std::string& line)
  90. {
  91. // Note: compiling code each time is slow. Not to be used for performance-critical or repeating activity
  92. PROFILE(Script_ExecuteImmediate);
  93. std::string wrappedLine = "void temp(){\n" + line + ";\n}";
  94. // Create a dummy module for compiling the line
  95. asIScriptModule* module = mAngelScriptEngine->GetModule("temp", asGM_ALWAYS_CREATE);
  96. if (!module)
  97. return false;
  98. // Use the line as the function name to get an easy to understand error message in case of failure
  99. asIScriptFunction *function = 0;
  100. if (module->CompileFunction(line.c_str(), wrappedLine.c_str(), -1, 0, &function) < 0)
  101. return false;
  102. if (mImmediateContext->Prepare(function->GetId()) < 0)
  103. {
  104. function->Release();
  105. return false;
  106. }
  107. bool success = false;
  108. success = mImmediateContext->Execute() >= 0;
  109. function->Release();
  110. return success;
  111. }
  112. void ScriptEngine::garbageCollect()
  113. {
  114. PROFILE(Script_GarbageCollect);
  115. mAngelScriptEngine->GarbageCollect(asGC_ONE_STEP);
  116. }