ElementDocumentWrapper.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "ElementDocumentWrapper.h"
  29. #include <Rocket/Core/Stream.h>
  30. #include <Rocket/Core/Python/Utilities.h>
  31. namespace Rocket {
  32. namespace Core {
  33. namespace Python {
  34. ElementDocumentWrapper::ElementDocumentWrapper(PyObject* self, const char* tag) : ElementWrapper< ElementDocument >(self, tag)
  35. {
  36. Rocket::Core::String module_id(32, "document_%x", this);
  37. // Create a new module
  38. module = PyModule_New(module_id.CString());
  39. module_namespace = PyModule_GetDict(module);
  40. // Merging main into the module
  41. PyObject* builtins = PyImport_AddModule("__main__");
  42. PyObject* builtins_dict = PyModule_GetDict( builtins);
  43. PyDict_Merge(module_namespace, builtins_dict, 0);
  44. }
  45. ElementDocumentWrapper::~ElementDocumentWrapper()
  46. {
  47. // Release the module
  48. Py_DECREF(module);
  49. }
  50. void ElementDocumentWrapper::LoadScript(Rocket::Core::Stream* stream, const Rocket::Core::String& source_name)
  51. {
  52. // If theres a source, check if the code is already loaded and just reuse it
  53. if (!source_name.Empty())
  54. {
  55. Rocket::Core::String module_name = Rocket::Core::String(source_name).Replace("/", "_");
  56. module_name = module_name.Replace("\\", "_");
  57. module_name = module_name.Replace(".py", "");
  58. PyObject* modules = PyImport_GetModuleDict();
  59. PyObject* merge_module = PyDict_GetItemString(modules, module_name.CString());
  60. #ifdef ROCKET_DEBUG
  61. // In debug builds, force module to NULL so that scripts are always reloaded
  62. merge_module = NULL;
  63. #else
  64. if (merge_module)
  65. {
  66. Py_INCREF(merge_module);
  67. }
  68. #endif
  69. if (!merge_module)
  70. {
  71. // Compile the code as a python module
  72. Rocket::Core::String source_buffer;
  73. PreprocessCode(source_buffer, stream);
  74. PyObject* code = Py_CompileString(source_buffer.CString(), source_name.CString(), Py_file_input);
  75. if (code)
  76. {
  77. merge_module = PyImport_ExecCodeModule((char*)module_name.CString(), code);
  78. Py_DECREF(code);
  79. }
  80. }
  81. if (merge_module)
  82. {
  83. PyObject* dict = PyModule_GetDict(merge_module);
  84. PyDict_Merge(module_namespace, dict, 0);
  85. Py_DECREF(merge_module);
  86. }
  87. else
  88. {
  89. Rocket::Core::Python::Utilities::PrintError();
  90. }
  91. }
  92. else
  93. {
  94. // Compile directly onto the python module
  95. Rocket::Core::String source_buffer;
  96. PreprocessCode(source_buffer, stream);
  97. PyObject* result = PyRun_String(source_buffer.CString(), Py_file_input, module_namespace, module_namespace);
  98. if ( !result )
  99. {
  100. Rocket::Core::Python::Utilities::PrintError();
  101. }
  102. else
  103. {
  104. Py_DECREF(result);
  105. }
  106. }
  107. }
  108. PyObject* ElementDocumentWrapper::GetModuleNamespace()
  109. {
  110. return module_namespace;
  111. }
  112. void ElementDocumentWrapper::PreprocessCode(Rocket::Core::String &code, Rocket::Core::Stream *stream)
  113. {
  114. // Load in the script
  115. Rocket::Core::String buffer;
  116. stream->Read(buffer, stream->Length());
  117. // Strip comments and build up code
  118. code = "";
  119. size_t i = 0;
  120. size_t line_start = 0;
  121. enum ParseState { START, COMMENT, DATA };
  122. ParseState state = START;
  123. while (i < buffer.Length())
  124. {
  125. // Python doesn't like \r's, strip them
  126. if (buffer[i] == '\r')
  127. {
  128. buffer.Erase(i, 1);
  129. // Make sure we get out if there are no characters left
  130. if (i == buffer.Length())
  131. continue;
  132. }
  133. switch (state)
  134. {
  135. case START:
  136. {
  137. // Check for the start of comments or non whitespace data
  138. if (buffer[i] == '#')
  139. state = COMMENT;
  140. else if (!Rocket::Core::StringUtilities::IsWhitespace(buffer[i]))
  141. state = DATA;
  142. }
  143. break;
  144. default:
  145. {
  146. // If we've hit the end of the line, process as required
  147. if (buffer[i] == '\n')
  148. {
  149. if (state == DATA)
  150. code += buffer.Substring(line_start, i - line_start + 1);
  151. state = START;
  152. line_start = i + 1;
  153. }
  154. }
  155. break;
  156. }
  157. i++;
  158. }
  159. }
  160. }
  161. }
  162. }