JSVM.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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)
  88. {
  89. #ifdef JSVM_DEBUG
  90. assert(!removedHeapPtr_.Contains(heapptr));
  91. #endif
  92. assert(heapToObject_.Contains(heapptr));
  93. #ifdef JSVM_DEBUG
  94. RefCounted* ref = heapToObject_[heapptr];
  95. assert(ref->JSGetHeapPtr() == heapptr);
  96. #endif
  97. return heapToObject_[heapptr];
  98. }
  99. void SetModuleSearchPaths(const String& searchPath)
  100. {
  101. moduleSearchPath_ = searchPath.Split(';');
  102. for (unsigned i = 0; i < moduleSearchPath_.Size(); i++)
  103. {
  104. moduleSearchPath_[i] = AddTrailingSlash(moduleSearchPath_[i]);
  105. }
  106. }
  107. const Vector<String>& GetModuleSearchPaths()
  108. {
  109. return moduleSearchPath_;
  110. }
  111. void SetLastModuleSearchFile(const String& fileName) { lastModuleSearchFilename_ = fileName; }
  112. const String& GetLastModuleSearchFile() { return lastModuleSearchFilename_; }
  113. const String& GetErrorString() { return errorString_; }
  114. void SendJSErrorEvent(const String& filename = String::EMPTY);
  115. private:
  116. bool GenerateComponent(const String& cname, const String& jsfilename, const String& csource);
  117. void InitComponents();
  118. void InitPackageComponents();
  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_get_global_string(ctx, "Atomic");
  163. // will not handle renamed classes
  164. if (instance->IsObject())
  165. duk_get_prop_string(ctx, -1, ((Object*)instance)->GetTypeName().CString());
  166. else
  167. {
  168. duk_get_prop_string(ctx, -1, classname);
  169. }
  170. duk_push_pointer(ctx, (void*) instance);
  171. duk_new(ctx, 1);
  172. duk_remove(ctx, -2); // remove Atomic object
  173. assert(duk_is_object(ctx, -1));
  174. assert ((top + 1) == duk_get_top(ctx));
  175. return true;
  176. }
  177. }