JSVM.h 4.7 KB

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