JSVM.h 4.3 KB

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