JSVM.h 4.9 KB

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