JSVM.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. bool GenerateComponent(const String& cname, const String& jsfilename, const String& csource);
  121. void InitComponents();
  122. void InitPackageComponents();
  123. void SubscribeToEvents();
  124. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  125. duk_context* ctx_;
  126. HashMap<void*, RefCounted*> heapToObject_;
  127. #ifdef JSVM_DEBUG
  128. // Debugging
  129. HashMap<void*, void*> removedHeapPtr_;
  130. #endif
  131. float gcTime_;
  132. Vector<String> moduleSearchPath_;
  133. String lastModuleSearchFilename_;
  134. String errorString_;
  135. SharedPtr<JSUI> ui_;
  136. VariantMap objectAddedData_;
  137. VariantMap objectRemovedData_;
  138. SharedPtr<JSMetrics> metrics_;
  139. static JSVM* instance_;
  140. };
  141. template<typename T>
  142. T* js_to_class_instance(duk_context* ctx, int index, unsigned classID)
  143. {
  144. if (!duk_is_object(ctx, index))
  145. return NULL;
  146. return (T*) JSVM::GetJSVM(ctx)->GetObjectPtr(duk_get_heapptr(ctx, index));
  147. }
  148. // pushes null if instance is null
  149. // pushes from object store if already wrapped
  150. // pushes a new'd instance with wrapped native
  151. // must be an Object (so downcast works)
  152. inline bool js_push_class_object_instance(duk_context* ctx, const RefCounted *instance, const char* classname = "")
  153. {
  154. if (!instance)
  155. {
  156. duk_push_null(ctx);
  157. return true;
  158. }
  159. int top = duk_get_top(ctx);
  160. if (instance->JSGetHeapPtr())
  161. {
  162. duk_push_heapptr(ctx, instance->JSGetHeapPtr());
  163. assert(duk_is_object(ctx, -1));
  164. return true;
  165. }
  166. // will not handle renamed classes
  167. if (instance->IsObject())
  168. {
  169. Object *obj = (Object*) instance;
  170. void* uniqueClassID = (void *) obj->GetTypeName().CString();
  171. duk_push_heap_stash(ctx);
  172. duk_push_pointer(ctx, uniqueClassID);
  173. duk_get_prop(ctx, -2);
  174. const char* package = duk_require_string(ctx, -1);
  175. duk_pop_2(ctx);
  176. duk_get_global_string(ctx, package);
  177. duk_get_prop_string(ctx, -1, ((Object*)instance)->GetTypeName().CString());
  178. }
  179. else
  180. {
  181. duk_get_global_string(ctx, "Atomic");
  182. duk_get_prop_string(ctx, -1, classname);
  183. }
  184. duk_push_pointer(ctx, (void*) instance);
  185. duk_new(ctx, 1);
  186. duk_remove(ctx, -2); // remove Atomic object
  187. assert(duk_is_object(ctx, -1));
  188. assert ((top + 1) == duk_get_top(ctx));
  189. return true;
  190. }
  191. }