JSVM.h 4.3 KB

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