2
0

LuaCompiler.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "LuaCompiler.h"
  24. #include "LuaResource.h"
  25. #include "TempAllocator.h"
  26. #include "DynamicString.h"
  27. #include "Filesystem.h"
  28. #include "OS.h"
  29. namespace crown
  30. {
  31. #ifdef WINDOWS
  32. #define LUAJIT "luajit.exe"
  33. #else
  34. #define LUAJIT "./luajit"
  35. #endif
  36. //-----------------------------------------------------------------------------
  37. LuaCompiler::LuaCompiler()
  38. : m_luajit_blob_size(0), m_luajit_blob(NULL)
  39. {
  40. }
  41. //-----------------------------------------------------------------------------
  42. LuaCompiler::~LuaCompiler()
  43. {
  44. }
  45. //-----------------------------------------------------------------------------
  46. size_t LuaCompiler::compile_impl(Filesystem& fs, const char* resource_path)
  47. {
  48. TempAllocator1024 alloc;
  49. DynamicString res_abs_path(alloc);
  50. TempAllocator1024 alloc2;
  51. DynamicString bc_abs_path(alloc2);
  52. fs.get_absolute_path(resource_path, res_abs_path);
  53. fs.get_absolute_path("bc.tmp", bc_abs_path);
  54. const char* luajit[] =
  55. {
  56. LUAJIT,
  57. "-b",
  58. res_abs_path.c_str(),
  59. bc_abs_path.c_str(),
  60. NULL
  61. };
  62. os::execute_process(luajit);
  63. File* bc = fs.open(bc_abs_path.c_str(), FOM_READ);
  64. if (bc != NULL)
  65. {
  66. m_luajit_blob_size = bc->size();
  67. m_luajit_blob = (char*) default_allocator().allocate(m_luajit_blob_size);
  68. bc->read(m_luajit_blob, m_luajit_blob_size);
  69. fs.close(bc);
  70. fs.delete_file(bc_abs_path.c_str());
  71. }
  72. else
  73. {
  74. Log::e("Error while reading luajit bytecode");
  75. return 0;
  76. }
  77. return sizeof(LuaHeader) + m_luajit_blob_size;
  78. }
  79. //-----------------------------------------------------------------------------
  80. void LuaCompiler::write_impl(File* out_file)
  81. {
  82. LuaHeader header;
  83. header.version = LUA_RESOURCE_VERSION;
  84. header.size = m_luajit_blob_size;
  85. out_file->write((char*)&header, sizeof(LuaHeader));
  86. out_file->write((char*)m_luajit_blob, m_luajit_blob_size);
  87. default_allocator().deallocate(m_luajit_blob);
  88. m_luajit_blob_size = 0;
  89. m_luajit_blob = NULL;
  90. }
  91. } // namespace crown