JSVM.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 ATOMIC_API JSVM : public Object
  17. {
  18. OBJECT(JSVM);
  19. public:
  20. /// Construct.
  21. JSVM(Context* context);
  22. /// Destruct.
  23. virtual ~JSVM();
  24. void InitJSContext();
  25. bool ExecuteFile(File* file);
  26. // Resources/Scripts/*.js
  27. bool ExecuteScript(const String& scriptPath);
  28. // Resources/Script/main.js
  29. // Catches not requiring AtomicGame, etc
  30. bool ExecuteMain();
  31. bool ExecuteFunction(const String& functionName);
  32. inline static JSVM* GetJSVM(duk_context* context)
  33. {
  34. return instance_;
  35. }
  36. inline duk_context* GetJSContext() { return ctx_; }
  37. void DumpJavascriptObjects() {}
  38. #ifdef JSVM_DEBUG
  39. inline void ValidateJSHeapPtr(void* heapptr)
  40. {
  41. assert(removedHeapPtr_.Find(heapptr) == removedHeapPtr_.End());
  42. assert(heapToObject_.Find(heapptr) != heapToObject_.End());
  43. }
  44. #endif
  45. inline void AddObject(void* heapptr, RefCounted* object)
  46. {
  47. assert(!object->JSGetHeapPtr());
  48. object->JSSetHeapPtr(heapptr);
  49. #ifdef JSVM_DEBUG
  50. assert(heapToObject_.Find(heapptr) == heapToObject_.End());
  51. #endif
  52. heapToObject_[heapptr] = object;
  53. #ifdef JSVM_DEBUG
  54. HashMap<void*, void*>::Iterator itr = removedHeapPtr_.Find(heapptr);
  55. if (itr != removedHeapPtr_.End())
  56. removedHeapPtr_.Erase(itr);
  57. #endif
  58. if (object->IsObject())
  59. {
  60. objectAddedData_[ObjectAdded::P_OBJECT] = object;
  61. SendEvent(E_JSOBJECTADDED, objectAddedData_);
  62. }
  63. }
  64. inline void RemoveObject(RefCounted* object)
  65. {
  66. if (object->IsObject())
  67. {
  68. objectRemovedData_[ObjectRemoved::P_OBJECT] = object;
  69. SendEvent(E_JSOBJECTREMOVED, objectRemovedData_);
  70. }
  71. void* heapptr = object->JSGetHeapPtr();
  72. assert(heapptr);
  73. object->JSSetHeapPtr(NULL);
  74. HashMap<void*, RefCounted*>::Iterator hitr = heapToObject_.Find(heapptr);
  75. assert(hitr != heapToObject_.End());
  76. heapToObject_.Erase(hitr);
  77. #ifdef JSVM_DEBUG
  78. assert(removedHeapPtr_.Find(heapptr) == removedHeapPtr_.End());
  79. removedHeapPtr_[heapptr] = heapptr;
  80. #endif
  81. }
  82. inline RefCounted* GetObjectPtr(void* heapptr)
  83. {
  84. #ifdef JSVM_DEBUG
  85. assert(!removedHeapPtr_.Contains(heapptr));
  86. #endif
  87. assert(heapToObject_.Contains(heapptr));
  88. #ifdef JSVM_DEBUG
  89. RefCounted* ref = heapToObject_[heapptr];
  90. assert(ref->JSGetHeapPtr() == heapptr);
  91. #endif
  92. return heapToObject_[heapptr];
  93. }
  94. void SetModuleSearchPath(const String& searchPath)
  95. {
  96. moduleSearchPath_ = searchPath;
  97. }
  98. void SetLastModuleSearchFile(const String& fileName) { lastModuleSearchFilename_ = fileName; }
  99. const String& GetLastModuleSearchFile() { return lastModuleSearchFilename_; }
  100. const String& GetErrorString() { return errorString_; }
  101. void SendJSErrorEvent(const String& filename = String::EMPTY);
  102. private:
  103. bool GenerateComponent(const String& cname, const String& jsfilename, const String& csource);
  104. void InitComponents();
  105. void InitPackageComponents();
  106. void SubscribeToEvents();
  107. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  108. static int js_module_search(duk_context* ctx);
  109. duk_context* ctx_;
  110. HashMap<void*, RefCounted*> heapToObject_;
  111. #ifdef JSVM_DEBUG
  112. // Debugging
  113. HashMap<void*, void*> removedHeapPtr_;
  114. #endif
  115. float gcTime_;
  116. String moduleSearchPath_;
  117. String lastModuleSearchFilename_;
  118. String errorString_;
  119. SharedPtr<JSUI> ui_;
  120. VariantMap objectAddedData_;
  121. VariantMap objectRemovedData_;
  122. static JSVM* instance_;
  123. };
  124. template<typename T>
  125. T* js_to_class_instance(duk_context* ctx, int index, unsigned classID)
  126. {
  127. if (!duk_is_object(ctx, index))
  128. return NULL;
  129. return (T*) JSVM::GetJSVM(ctx)->GetObjectPtr(duk_get_heapptr(ctx, index));
  130. }
  131. // pushes null if instance is null
  132. // pushes from object store if already wrapped
  133. // pushes a new'd instance with wrapped native
  134. // must be an Object (so downcast works)
  135. inline bool js_push_class_object_instance(duk_context* ctx, const RefCounted *instance, const char* classname = "")
  136. {
  137. if (!instance)
  138. {
  139. duk_push_null(ctx);
  140. return true;
  141. }
  142. int top = duk_get_top(ctx);
  143. if (instance->JSGetHeapPtr())
  144. {
  145. duk_push_heapptr(ctx, instance->JSGetHeapPtr());
  146. assert(duk_is_object(ctx, -1));
  147. return true;
  148. }
  149. duk_get_global_string(ctx, "Atomic");
  150. // will not handle renamed classes
  151. if (instance->IsObject())
  152. duk_get_prop_string(ctx, -1, ((Object*)instance)->GetTypeName().CString());
  153. else
  154. {
  155. duk_get_prop_string(ctx, -1, classname);
  156. }
  157. duk_push_pointer(ctx, (void*) instance);
  158. duk_new(ctx, 1);
  159. duk_remove(ctx, -2); // remove Atomic object
  160. assert(duk_is_object(ctx, -1));
  161. assert ((top + 1) == duk_get_top(ctx));
  162. return true;
  163. }
  164. }