JSVM.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "JSAPI.h"
  10. #include "JSEvents.h"
  11. //#define JSVM_DEBUG
  12. namespace Atomic
  13. {
  14. class JSFile;
  15. class JSUI;
  16. class JSMetrics;
  17. class ATOMIC_API JSVM : public Object
  18. {
  19. friend class JSMetrics;
  20. OBJECT(JSVM);
  21. public:
  22. /// Construct.
  23. JSVM(Context* context);
  24. /// Destruct.
  25. virtual ~JSVM();
  26. void InitJSContext();
  27. bool ExecuteFile(File* file);
  28. // Resources/Scripts/*.js
  29. bool ExecuteScript(const String& scriptPath);
  30. // Resources/Script/main.js
  31. // Catches not requiring AtomicGame, etc
  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)
  87. {
  88. #ifdef JSVM_DEBUG
  89. assert(!removedHeapPtr_.Contains(heapptr));
  90. #endif
  91. assert(heapToObject_.Contains(heapptr));
  92. #ifdef JSVM_DEBUG
  93. RefCounted* ref = heapToObject_[heapptr];
  94. assert(ref->JSGetHeapPtr() == heapptr);
  95. #endif
  96. return heapToObject_[heapptr];
  97. }
  98. void SetModuleSearchPath(const String& searchPath)
  99. {
  100. moduleSearchPath_ = searchPath;
  101. }
  102. void SetLastModuleSearchFile(const String& fileName) { lastModuleSearchFilename_ = fileName; }
  103. const String& GetLastModuleSearchFile() { return lastModuleSearchFilename_; }
  104. const String& GetErrorString() { return errorString_; }
  105. void SendJSErrorEvent(const String& filename = String::EMPTY);
  106. private:
  107. bool GenerateComponent(const String& cname, const String& jsfilename, const String& csource);
  108. void InitComponents();
  109. void InitPackageComponents();
  110. void SubscribeToEvents();
  111. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  112. static int js_module_search(duk_context* ctx);
  113. duk_context* ctx_;
  114. HashMap<void*, RefCounted*> heapToObject_;
  115. #ifdef JSVM_DEBUG
  116. // Debugging
  117. HashMap<void*, void*> removedHeapPtr_;
  118. #endif
  119. float gcTime_;
  120. String moduleSearchPath_;
  121. String lastModuleSearchFilename_;
  122. String errorString_;
  123. SharedPtr<JSUI> ui_;
  124. VariantMap objectAddedData_;
  125. VariantMap objectRemovedData_;
  126. SharedPtr<JSMetrics> metrics_;
  127. static JSVM* instance_;
  128. };
  129. template<typename T>
  130. T* js_to_class_instance(duk_context* ctx, int index, unsigned classID)
  131. {
  132. if (!duk_is_object(ctx, index))
  133. return NULL;
  134. return (T*) JSVM::GetJSVM(ctx)->GetObjectPtr(duk_get_heapptr(ctx, index));
  135. }
  136. // pushes null if instance is null
  137. // pushes from object store if already wrapped
  138. // pushes a new'd instance with wrapped native
  139. // must be an Object (so downcast works)
  140. inline bool js_push_class_object_instance(duk_context* ctx, const RefCounted *instance, const char* classname = "")
  141. {
  142. if (!instance)
  143. {
  144. duk_push_null(ctx);
  145. return true;
  146. }
  147. int top = duk_get_top(ctx);
  148. if (instance->JSGetHeapPtr())
  149. {
  150. duk_push_heapptr(ctx, instance->JSGetHeapPtr());
  151. assert(duk_is_object(ctx, -1));
  152. return true;
  153. }
  154. duk_get_global_string(ctx, "Atomic");
  155. // will not handle renamed classes
  156. if (instance->IsObject())
  157. duk_get_prop_string(ctx, -1, ((Object*)instance)->GetTypeName().CString());
  158. else
  159. {
  160. duk_get_prop_string(ctx, -1, classname);
  161. }
  162. duk_push_pointer(ctx, (void*) instance);
  163. duk_new(ctx, 1);
  164. duk_remove(ctx, -2); // remove Atomic object
  165. assert(duk_is_object(ctx, -1));
  166. assert ((top + 1) == duk_get_top(ctx));
  167. return true;
  168. }
  169. }