JSVM.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #pragma once
  5. #include <Atomic/Core/Context.h>
  6. #include <Atomic/Core/Object.h>
  7. #include <Atomic/Container/List.h>
  8. #include <Atomic/IO/Log.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include "JSAPI.h"
  11. #include "JSEvents.h"
  12. //#define JSVM_DEBUG
  13. namespace Atomic
  14. {
  15. class JSFile;
  16. class JSUI;
  17. class JSMetrics;
  18. class ATOMIC_API JSVM : public Object
  19. {
  20. friend class JSMetrics;
  21. OBJECT(JSVM);
  22. public:
  23. /// Construct.
  24. JSVM(Context* context);
  25. /// Destruct.
  26. virtual ~JSVM();
  27. void InitJSContext();
  28. bool ExecuteFile(File* file);
  29. // Resources/Scripts/*.js
  30. bool ExecuteScript(const String& scriptPath);
  31. // Resources/Script/main.js
  32. // Catches not requiring AtomicGame, etc
  33. bool ExecuteMain();
  34. bool ExecuteFunction(const String& functionName);
  35. inline static JSVM* GetJSVM(duk_context* context)
  36. {
  37. return instance_;
  38. }
  39. inline duk_context* GetJSContext() { return ctx_; }
  40. void GC();
  41. JSMetrics* GetMetrics() { return metrics_; }
  42. void DumpJavascriptObjects() {}
  43. #ifdef JSVM_DEBUG
  44. inline void ValidateJSHeapPtr(void* heapptr)
  45. {
  46. assert(removedHeapPtr_.Find(heapptr) == removedHeapPtr_.End());
  47. assert(heapToObject_.Find(heapptr) != heapToObject_.End());
  48. }
  49. #endif
  50. inline void AddObject(void* heapptr, RefCounted* object)
  51. {
  52. assert(!object->JSGetHeapPtr());
  53. object->JSSetHeapPtr(heapptr);
  54. #ifdef JSVM_DEBUG
  55. assert(heapToObject_.Find(heapptr) == heapToObject_.End());
  56. #endif
  57. heapToObject_[heapptr] = object;
  58. #ifdef JSVM_DEBUG
  59. HashMap<void*, void*>::Iterator itr = removedHeapPtr_.Find(heapptr);
  60. if (itr != removedHeapPtr_.End())
  61. removedHeapPtr_.Erase(itr);
  62. #endif
  63. if (object->IsObject())
  64. {
  65. objectAddedData_[ObjectAdded::P_OBJECT] = object;
  66. SendEvent(E_JSOBJECTADDED, objectAddedData_);
  67. }
  68. }
  69. inline void RemoveObject(RefCounted* object)
  70. {
  71. if (object->IsObject())
  72. {
  73. objectRemovedData_[ObjectRemoved::P_OBJECT] = object;
  74. SendEvent(E_JSOBJECTREMOVED, objectRemovedData_);
  75. }
  76. void* heapptr = object->JSGetHeapPtr();
  77. assert(heapptr);
  78. object->JSSetHeapPtr(NULL);
  79. HashMap<void*, RefCounted*>::Iterator hitr = heapToObject_.Find(heapptr);
  80. assert(hitr != heapToObject_.End());
  81. heapToObject_.Erase(hitr);
  82. #ifdef JSVM_DEBUG
  83. assert(removedHeapPtr_.Find(heapptr) == removedHeapPtr_.End());
  84. removedHeapPtr_[heapptr] = heapptr;
  85. #endif
  86. }
  87. inline RefCounted* GetObjectPtr(void* heapptr, bool allowNull = false)
  88. {
  89. #ifdef JSVM_DEBUG
  90. assert(!removedHeapPtr_.Contains(heapptr));
  91. #endif
  92. if (allowNull && !heapToObject_.Contains(heapptr))
  93. {
  94. return NULL;
  95. }
  96. assert(heapToObject_.Contains(heapptr));
  97. #ifdef JSVM_DEBUG
  98. RefCounted* ref = heapToObject_[heapptr];
  99. assert(ref->JSGetHeapPtr() == heapptr);
  100. #endif
  101. return heapToObject_[heapptr];
  102. }
  103. void SetModuleSearchPaths(const String& searchPath)
  104. {
  105. moduleSearchPath_ = searchPath.Split(';');
  106. for (unsigned i = 0; i < moduleSearchPath_.Size(); i++)
  107. {
  108. moduleSearchPath_[i] = AddTrailingSlash(moduleSearchPath_[i]);
  109. }
  110. }
  111. const Vector<String>& GetModuleSearchPaths()
  112. {
  113. return moduleSearchPath_;
  114. }
  115. void SetLastModuleSearchFile(const String& fileName) { lastModuleSearchFilename_ = fileName; }
  116. const String& GetLastModuleSearchFile() { return lastModuleSearchFilename_; }
  117. const String& GetErrorString() { return errorString_; }
  118. void SendJSErrorEvent(const String& filename = String::EMPTY);
  119. private:
  120. void SubscribeToEvents();
  121. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  122. duk_context* ctx_;
  123. HashMap<void*, RefCounted*> heapToObject_;
  124. #ifdef JSVM_DEBUG
  125. // Debugging
  126. HashMap<void*, void*> removedHeapPtr_;
  127. #endif
  128. float gcTime_;
  129. Vector<String> moduleSearchPath_;
  130. String lastModuleSearchFilename_;
  131. String errorString_;
  132. SharedPtr<JSUI> ui_;
  133. VariantMap objectAddedData_;
  134. VariantMap objectRemovedData_;
  135. SharedPtr<JSMetrics> metrics_;
  136. static JSVM* instance_;
  137. };
  138. template<typename T>
  139. T* js_to_class_instance(duk_context* ctx, int index, unsigned classID)
  140. {
  141. if (!duk_is_object(ctx, index))
  142. return NULL;
  143. return (T*) JSVM::GetJSVM(ctx)->GetObjectPtr(duk_get_heapptr(ctx, index));
  144. }
  145. // pushes null if instance is null
  146. // pushes from object store if already wrapped
  147. // pushes a new'd instance with wrapped native
  148. // must be an Object (so downcast works)
  149. inline bool js_push_class_object_instance(duk_context* ctx, const RefCounted *instance, const char* classname = "")
  150. {
  151. if (!instance)
  152. {
  153. duk_push_null(ctx);
  154. return true;
  155. }
  156. int top = duk_get_top(ctx);
  157. if (instance->JSGetHeapPtr())
  158. {
  159. duk_push_heapptr(ctx, instance->JSGetHeapPtr());
  160. assert(duk_is_object(ctx, -1));
  161. return true;
  162. }
  163. // will not handle renamed classes
  164. if (instance->IsObject())
  165. {
  166. Object *obj = (Object*) instance;
  167. void* uniqueClassID = (void *) obj->GetTypeName().CString();
  168. duk_push_heap_stash(ctx);
  169. duk_push_pointer(ctx, uniqueClassID);
  170. duk_get_prop(ctx, -2);
  171. const char* package = duk_require_string(ctx, -1);
  172. duk_pop_2(ctx);
  173. duk_get_global_string(ctx, package);
  174. duk_get_prop_string(ctx, -1, ((Object*)instance)->GetTypeName().CString());
  175. }
  176. else
  177. {
  178. duk_get_global_string(ctx, "Atomic");
  179. duk_get_prop_string(ctx, -1, classname);
  180. }
  181. duk_push_pointer(ctx, (void*) instance);
  182. duk_new(ctx, 1);
  183. duk_remove(ctx, -2); // remove Atomic object
  184. assert(duk_is_object(ctx, -1));
  185. assert ((top + 1) == duk_get_top(ctx));
  186. return true;
  187. }
  188. }