JSVM.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. duk_context* ctx_;
  113. HashMap<void*, RefCounted*> heapToObject_;
  114. #ifdef JSVM_DEBUG
  115. // Debugging
  116. HashMap<void*, void*> removedHeapPtr_;
  117. #endif
  118. float gcTime_;
  119. String moduleSearchPath_;
  120. String lastModuleSearchFilename_;
  121. String errorString_;
  122. SharedPtr<JSUI> ui_;
  123. VariantMap objectAddedData_;
  124. VariantMap objectRemovedData_;
  125. SharedPtr<JSMetrics> metrics_;
  126. static JSVM* instance_;
  127. };
  128. template<typename T>
  129. T* js_to_class_instance(duk_context* ctx, int index, unsigned classID)
  130. {
  131. if (!duk_is_object(ctx, index))
  132. return NULL;
  133. return (T*) JSVM::GetJSVM(ctx)->GetObjectPtr(duk_get_heapptr(ctx, index));
  134. }
  135. // pushes null if instance is null
  136. // pushes from object store if already wrapped
  137. // pushes a new'd instance with wrapped native
  138. // must be an Object (so downcast works)
  139. inline bool js_push_class_object_instance(duk_context* ctx, const RefCounted *instance, const char* classname = "")
  140. {
  141. if (!instance)
  142. {
  143. duk_push_null(ctx);
  144. return true;
  145. }
  146. int top = duk_get_top(ctx);
  147. if (instance->JSGetHeapPtr())
  148. {
  149. duk_push_heapptr(ctx, instance->JSGetHeapPtr());
  150. assert(duk_is_object(ctx, -1));
  151. return true;
  152. }
  153. duk_get_global_string(ctx, "Atomic");
  154. // will not handle renamed classes
  155. if (instance->IsObject())
  156. duk_get_prop_string(ctx, -1, ((Object*)instance)->GetTypeName().CString());
  157. else
  158. {
  159. duk_get_prop_string(ctx, -1, classname);
  160. }
  161. duk_push_pointer(ctx, (void*) instance);
  162. duk_new(ctx, 1);
  163. duk_remove(ctx, -2); // remove Atomic object
  164. assert(duk_is_object(ctx, -1));
  165. assert ((top + 1) == duk_get_top(ctx));
  166. return true;
  167. }
  168. }