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. bool ExecuteMain();
  33. bool ExecuteFunction(const String& functionName);
  34. inline static JSVM* GetJSVM(duk_context* context)
  35. {
  36. return instance_;
  37. }
  38. inline duk_context* GetJSContext() { return ctx_; }
  39. void GC();
  40. JSMetrics* GetMetrics() { return metrics_; }
  41. void DumpJavascriptObjects() {}
  42. #ifdef JSVM_DEBUG
  43. inline void ValidateJSHeapPtr(void* heapptr)
  44. {
  45. assert(removedHeapPtr_.Find(heapptr) == removedHeapPtr_.End());
  46. assert(heapToObject_.Find(heapptr) != heapToObject_.End());
  47. }
  48. #endif
  49. inline void AddObject(void* heapptr, RefCounted* object)
  50. {
  51. assert(!object->JSGetHeapPtr());
  52. object->JSSetHeapPtr(heapptr);
  53. #ifdef JSVM_DEBUG
  54. assert(heapToObject_.Find(heapptr) == heapToObject_.End());
  55. #endif
  56. heapToObject_[heapptr] = object;
  57. #ifdef JSVM_DEBUG
  58. HashMap<void*, void*>::Iterator itr = removedHeapPtr_.Find(heapptr);
  59. if (itr != removedHeapPtr_.End())
  60. removedHeapPtr_.Erase(itr);
  61. #endif
  62. if (object->IsObject())
  63. {
  64. objectAddedData_[ObjectAdded::P_OBJECT] = object;
  65. SendEvent(E_JSOBJECTADDED, objectAddedData_);
  66. }
  67. }
  68. inline void RemoveObject(RefCounted* object)
  69. {
  70. if (object->IsObject())
  71. {
  72. objectRemovedData_[ObjectRemoved::P_OBJECT] = object;
  73. SendEvent(E_JSOBJECTREMOVED, objectRemovedData_);
  74. }
  75. void* heapptr = object->JSGetHeapPtr();
  76. assert(heapptr);
  77. object->JSSetHeapPtr(NULL);
  78. HashMap<void*, RefCounted*>::Iterator hitr = heapToObject_.Find(heapptr);
  79. assert(hitr != heapToObject_.End());
  80. heapToObject_.Erase(hitr);
  81. #ifdef JSVM_DEBUG
  82. assert(removedHeapPtr_.Find(heapptr) == removedHeapPtr_.End());
  83. removedHeapPtr_[heapptr] = heapptr;
  84. #endif
  85. }
  86. inline RefCounted* GetObjectPtr(void* heapptr, bool allowNull = false)
  87. {
  88. #ifdef JSVM_DEBUG
  89. assert(!removedHeapPtr_.Contains(heapptr));
  90. #endif
  91. if (allowNull && !heapToObject_.Contains(heapptr))
  92. {
  93. return NULL;
  94. }
  95. assert(heapToObject_.Contains(heapptr));
  96. #ifdef JSVM_DEBUG
  97. RefCounted* ref = heapToObject_[heapptr];
  98. assert(ref->JSGetHeapPtr() == heapptr);
  99. #endif
  100. return heapToObject_[heapptr];
  101. }
  102. void SetModuleSearchPaths(const String& searchPath)
  103. {
  104. moduleSearchPath_ = searchPath.Split(';');
  105. for (unsigned i = 0; i < moduleSearchPath_.Size(); i++)
  106. {
  107. moduleSearchPath_[i] = AddTrailingSlash(moduleSearchPath_[i]);
  108. }
  109. }
  110. const Vector<String>& GetModuleSearchPaths()
  111. {
  112. return moduleSearchPath_;
  113. }
  114. void SetLastModuleSearchFile(const String& fileName) { lastModuleSearchFilename_ = fileName; }
  115. const String& GetLastModuleSearchFile() { return lastModuleSearchFilename_; }
  116. const String& GetErrorString() { return errorString_; }
  117. void SendJSErrorEvent(const String& filename = String::EMPTY);
  118. private:
  119. void SubscribeToEvents();
  120. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  121. duk_context* ctx_;
  122. HashMap<void*, RefCounted*> heapToObject_;
  123. #ifdef JSVM_DEBUG
  124. // Debugging
  125. HashMap<void*, void*> removedHeapPtr_;
  126. #endif
  127. float gcTime_;
  128. Vector<String> moduleSearchPath_;
  129. String lastModuleSearchFilename_;
  130. String errorString_;
  131. SharedPtr<JSUI> ui_;
  132. VariantMap objectAddedData_;
  133. VariantMap objectRemovedData_;
  134. SharedPtr<JSMetrics> metrics_;
  135. static JSVM* instance_;
  136. };
  137. template<typename T>
  138. T* js_to_class_instance(duk_context* ctx, int index, unsigned classID)
  139. {
  140. if (!duk_is_object(ctx, index))
  141. return NULL;
  142. return (T*) JSVM::GetJSVM(ctx)->GetObjectPtr(duk_get_heapptr(ctx, index));
  143. }
  144. // pushes null if instance is null
  145. // pushes from object store if already wrapped
  146. // pushes a new'd instance with wrapped native
  147. // must be an Object (so downcast works)
  148. inline bool js_push_class_object_instance(duk_context* ctx, const RefCounted *instance, const char* classname = "")
  149. {
  150. if (!instance)
  151. {
  152. duk_push_null(ctx);
  153. return true;
  154. }
  155. int top = duk_get_top(ctx);
  156. if (instance->JSGetHeapPtr())
  157. {
  158. duk_push_heapptr(ctx, instance->JSGetHeapPtr());
  159. assert(duk_is_object(ctx, -1));
  160. return true;
  161. }
  162. duk_push_heap_stash(ctx);
  163. duk_push_pointer(ctx, (void*) instance->GetClassID());
  164. duk_get_prop(ctx, -2);
  165. // if not an object, this instance isn't not a scriptable class
  166. // reset top and return false
  167. if (!duk_is_object(ctx, -1))
  168. {
  169. duk_set_top(ctx, top);
  170. return false;
  171. }
  172. duk_get_prop_index(ctx, -1, 0);
  173. const char* package = duk_require_string(ctx, -1);
  174. duk_get_prop_index(ctx, -2, 1);
  175. const char* jclassname = duk_require_string(ctx, -1);
  176. duk_set_top(ctx, top);
  177. duk_get_global_string(ctx, package);
  178. duk_get_prop_string(ctx, -1, jclassname);
  179. duk_push_pointer(ctx, (void*) instance);
  180. duk_new(ctx, 1);
  181. duk_remove(ctx, -2); // remove Atomic object
  182. assert(duk_is_object(ctx, -1));
  183. assert ((top + 1) == duk_get_top(ctx));
  184. return true;
  185. }
  186. }