ElementDocumentWrapper.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "../../../Include/Rocket/Core/Stream.h"
  30. #include "../../../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. // Insert the document global
  45. PyDict_SetItemString(module_namespace, "document", self);
  46. }
  47. ElementDocumentWrapper::~ElementDocumentWrapper()
  48. {
  49. // Release the module
  50. Py_DECREF(module);
  51. }
  52. void ElementDocumentWrapper::LoadScript(Rocket::Core::Stream* stream, const Rocket::Core::String& source_name)
  53. {
  54. // If theres a source, check if the code is already loaded and just reuse it
  55. if (!source_name.Empty())
  56. {
  57. Rocket::Core::String module_name = Rocket::Core::String(source_name).Replace("/", "_");
  58. module_name = module_name.Replace("\\", "_");
  59. module_name = module_name.Replace(".py", "");
  60. PyObject* modules = PyImport_GetModuleDict();
  61. PyObject* merge_module = PyDict_GetItemString(modules, module_name.CString());
  62. #ifdef ROCKET_DEBUG
  63. // In debug builds, force module to NULL so that scripts are always reloaded
  64. merge_module = NULL;
  65. #else
  66. if (merge_module)
  67. {
  68. Py_INCREF(merge_module);
  69. }
  70. #endif
  71. if (!merge_module)
  72. {
  73. // Compile the code as a python module
  74. Rocket::Core::String source_buffer;
  75. PreprocessCode(source_buffer, stream);
  76. PyObject* code = Py_CompileString(source_buffer.CString(), source_name.CString(), Py_file_input);
  77. if (code)
  78. {
  79. merge_module = PyImport_ExecCodeModule((char*)module_name.CString(), code);
  80. Py_DECREF(code);
  81. }
  82. }
  83. if (merge_module)
  84. {
  85. PyObject* dict = PyModule_GetDict(merge_module);
  86. PyDict_Merge(module_namespace, dict, 0);
  87. Py_DECREF(merge_module);
  88. }
  89. else
  90. {
  91. Rocket::Core::Python::Utilities::PrintError();
  92. }
  93. }
  94. else
  95. {
  96. // Compile directly onto the python module
  97. Rocket::Core::String source_buffer;
  98. PreprocessCode(source_buffer, stream);
  99. PyObject* result = PyRun_String(source_buffer.CString(), Py_file_input, module_namespace, module_namespace);
  100. if ( !result )
  101. {
  102. Rocket::Core::Python::Utilities::PrintError();
  103. }
  104. else
  105. {
  106. Py_DECREF(result);
  107. }
  108. }
  109. }
  110. PyObject* ElementDocumentWrapper::GetModuleNamespace()
  111. {
  112. return module_namespace;
  113. }
  114. void ElementDocumentWrapper::PreprocessCode(Rocket::Core::String &code, Rocket::Core::Stream *stream)
  115. {
  116. // Load in the script
  117. Rocket::Core::String buffer;
  118. stream->Read(buffer, stream->Length());
  119. // Strip comments and build up code
  120. code = "";
  121. size_t i = 0;
  122. size_t line_start = 0;
  123. enum ParseState { START, COMMENT, DATA };
  124. ParseState state = START;
  125. while (i < buffer.Length())
  126. {
  127. // Python doesn't like \r's, strip them
  128. if (buffer[i] == '\r')
  129. {
  130. buffer.Erase(i, 1);
  131. // Make sure we get out if there are no characters left
  132. if (i == buffer.Length())
  133. continue;
  134. }
  135. switch (state)
  136. {
  137. case START:
  138. {
  139. // Check for the start of comments or non whitespace data
  140. if (buffer[i] == '#')
  141. state = COMMENT;
  142. else if (!Rocket::Core::StringUtilities::IsWhitespace(buffer[i]))
  143. state = DATA;
  144. }
  145. break;
  146. default:
  147. {
  148. // If we've hit the end of the line, process as required
  149. if (buffer[i] == '\n')
  150. {
  151. if (state == DATA)
  152. code += buffer.Substring(line_start, i - line_start + 1);
  153. state = START;
  154. line_start = i + 1;
  155. }
  156. }
  157. break;
  158. }
  159. i++;
  160. }
  161. }
  162. }
  163. }
  164. }