JSVM.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include <Atomic/Core/Context.h>
  24. #include <Atomic/Core/Object.h>
  25. #include <Atomic/Container/List.h>
  26. #include <Atomic/IO/Log.h>
  27. #include <Atomic/IO/FileSystem.h>
  28. #include "JSAPI.h"
  29. #include "JSEvents.h"
  30. //#define JSVM_DEBUG
  31. namespace Atomic
  32. {
  33. class JSFile;
  34. class JSUI;
  35. class JSMetrics;
  36. class ATOMIC_API JSVM : public Object
  37. {
  38. friend class JSMetrics;
  39. OBJECT(JSVM);
  40. public:
  41. /// Construct.
  42. JSVM(Context* context);
  43. /// Destruct.
  44. virtual ~JSVM();
  45. void InitJSContext();
  46. bool ExecuteFile(File* file);
  47. // Resources/Scripts/*.js
  48. bool ExecuteScript(const String& scriptPath);
  49. // Resources/Script/main.js
  50. bool ExecuteMain();
  51. bool ExecuteFunction(const String& functionName);
  52. inline static JSVM* GetJSVM(duk_context* context)
  53. {
  54. return instance_;
  55. }
  56. inline duk_context* GetJSContext() { return ctx_; }
  57. void GC();
  58. JSMetrics* GetMetrics() { return metrics_; }
  59. void DumpJavascriptObjects() {}
  60. #ifdef JSVM_DEBUG
  61. inline void ValidateJSHeapPtr(void* heapptr)
  62. {
  63. assert(removedHeapPtr_.Find(heapptr) == removedHeapPtr_.End());
  64. assert(heapToObject_.Find(heapptr) != heapToObject_.End());
  65. }
  66. #endif
  67. inline void AddObject(void* heapptr, RefCounted* object)
  68. {
  69. assert(!object->JSGetHeapPtr());
  70. object->JSSetHeapPtr(heapptr);
  71. #ifdef JSVM_DEBUG
  72. assert(heapToObject_.Find(heapptr) == heapToObject_.End());
  73. #endif
  74. heapToObject_[heapptr] = object;
  75. #ifdef JSVM_DEBUG
  76. HashMap<void*, void*>::Iterator itr = removedHeapPtr_.Find(heapptr);
  77. if (itr != removedHeapPtr_.End())
  78. removedHeapPtr_.Erase(itr);
  79. #endif
  80. if (object->IsObject())
  81. {
  82. objectAddedData_[ObjectAdded::P_OBJECT] = object;
  83. SendEvent(E_JSOBJECTADDED, objectAddedData_);
  84. }
  85. }
  86. inline void RemoveObject(RefCounted* object)
  87. {
  88. if (object->IsObject())
  89. {
  90. objectRemovedData_[ObjectRemoved::P_OBJECT] = object;
  91. SendEvent(E_JSOBJECTREMOVED, objectRemovedData_);
  92. }
  93. void* heapptr = object->JSGetHeapPtr();
  94. assert(heapptr);
  95. object->JSSetHeapPtr(NULL);
  96. HashMap<void*, RefCounted*>::Iterator hitr = heapToObject_.Find(heapptr);
  97. assert(hitr != heapToObject_.End());
  98. heapToObject_.Erase(hitr);
  99. #ifdef JSVM_DEBUG
  100. assert(removedHeapPtr_.Find(heapptr) == removedHeapPtr_.End());
  101. removedHeapPtr_[heapptr] = heapptr;
  102. #endif
  103. }
  104. inline RefCounted* GetObjectPtr(void* heapptr, bool allowNull = false)
  105. {
  106. #ifdef JSVM_DEBUG
  107. assert(!removedHeapPtr_.Contains(heapptr));
  108. #endif
  109. if (allowNull && !heapToObject_.Contains(heapptr))
  110. {
  111. return NULL;
  112. }
  113. assert(heapToObject_.Contains(heapptr));
  114. #ifdef JSVM_DEBUG
  115. RefCounted* ref = heapToObject_[heapptr];
  116. assert(ref->JSGetHeapPtr() == heapptr);
  117. #endif
  118. return heapToObject_[heapptr];
  119. }
  120. void SetModuleSearchPaths(const String& searchPath)
  121. {
  122. moduleSearchPath_ = searchPath.Split(';');
  123. for (unsigned i = 0; i < moduleSearchPath_.Size(); i++)
  124. {
  125. moduleSearchPath_[i] = AddTrailingSlash(moduleSearchPath_[i]);
  126. }
  127. }
  128. const Vector<String>& GetModuleSearchPaths()
  129. {
  130. return moduleSearchPath_;
  131. }
  132. void SetLastModuleSearchFile(const String& fileName) { lastModuleSearchFilename_ = fileName; }
  133. const String& GetLastModuleSearchFile() { return lastModuleSearchFilename_; }
  134. const String& GetErrorString() { return errorString_; }
  135. void SendJSErrorEvent(const String& filename = String::EMPTY);
  136. int GetRealLineNumber(const String& fileName, const int lineNumber);
  137. private:
  138. void SubscribeToEvents();
  139. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  140. duk_context* ctx_;
  141. HashMap<void*, RefCounted*> heapToObject_;
  142. #ifdef JSVM_DEBUG
  143. // Debugging
  144. HashMap<void*, void*> removedHeapPtr_;
  145. #endif
  146. float gcTime_;
  147. Vector<String> moduleSearchPath_;
  148. String lastModuleSearchFilename_;
  149. String errorString_;
  150. SharedPtr<JSUI> ui_;
  151. VariantMap objectAddedData_;
  152. VariantMap objectRemovedData_;
  153. SharedPtr<JSMetrics> metrics_;
  154. static JSVM* instance_;
  155. };
  156. template<typename T>
  157. T* js_to_class_instance(duk_context* ctx, int index, unsigned classID)
  158. {
  159. if (!duk_is_object(ctx, index))
  160. return NULL;
  161. return (T*) JSVM::GetJSVM(ctx)->GetObjectPtr(duk_get_heapptr(ctx, index));
  162. }
  163. // pushes null if instance is null
  164. // pushes from object store if already wrapped
  165. // pushes a new'd instance with wrapped native
  166. // must be an Object (so downcast works)
  167. inline bool js_push_class_object_instance(duk_context* ctx, const RefCounted *instance, const char* classname = "")
  168. {
  169. if (!instance)
  170. {
  171. duk_push_null(ctx);
  172. return true;
  173. }
  174. int top = duk_get_top(ctx);
  175. if (instance->JSGetHeapPtr())
  176. {
  177. duk_push_heapptr(ctx, instance->JSGetHeapPtr());
  178. assert(duk_is_object(ctx, -1));
  179. return true;
  180. }
  181. duk_push_heap_stash(ctx);
  182. duk_push_pointer(ctx, (void*) instance->GetClassID());
  183. duk_get_prop(ctx, -2);
  184. // if not an object, this instance isn't not a scriptable class
  185. // reset top and return false
  186. if (!duk_is_object(ctx, -1))
  187. {
  188. if (instance->IsObject())
  189. {
  190. LOGERRORF("Unable to push class object instance due to missing ClassID: %s", ((Object*)instance)->GetTypeName().CString());
  191. }
  192. else
  193. {
  194. LOGERROR("Unable to push RefCounted instance due to missing ClassID");
  195. }
  196. duk_set_top(ctx, top);
  197. return false;
  198. }
  199. duk_get_prop_index(ctx, -1, 0);
  200. const char* package = duk_require_string(ctx, -1);
  201. duk_get_prop_index(ctx, -2, 1);
  202. const char* jclassname = duk_require_string(ctx, -1);
  203. duk_set_top(ctx, top);
  204. duk_get_global_string(ctx, package);
  205. duk_get_prop_string(ctx, -1, jclassname);
  206. assert(duk_is_function(ctx, -1));
  207. duk_push_pointer(ctx, (void*) instance);
  208. duk_new(ctx, 1);
  209. duk_remove(ctx, -2); // remove Atomic object
  210. assert(duk_is_object(ctx, -1));
  211. assert ((top + 1) == duk_get_top(ctx));
  212. return true;
  213. }
  214. }