JSVM.h 6.0 KB

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