Browse Source

VM can now be accessed from Javascript, adding simple metrics system (Object instance count for leak detection)

Josh Engebretson 10 years ago
parent
commit
48a36c77c3

+ 2 - 2
Source/AtomicJS/JSBind/modules/Javascript.json

@@ -1,5 +1,5 @@
 {
 	"name" : "Javascript",
 	"sources" : ["Javascript"],
-	"classes" : ["JSComponent"]
-}
+	"classes" : ["JSVM", "JSComponent", "JSMetrics"]
+}

+ 10 - 0
Source/AtomicJS/Javascript/JSAtomic.cpp

@@ -82,6 +82,13 @@ static int js_assert(duk_context* ctx)
     return 0;
 }
 
+static int js_atomic_GetVM(duk_context* ctx)
+{
+    JSVM* vm = JSVM::GetJSVM(ctx);
+    js_push_class_object_instance(ctx, vm);
+    return 1;
+}
+
 
 static int js_atomic_GetEngine(duk_context* ctx)
 {
@@ -239,6 +246,9 @@ void jsapi_init_atomic(JSVM* vm)
     duk_put_prop_index(ctx, -2, JS_GLOBALSTASH_INDEX_NODE_REGISTRY);
     duk_pop(ctx);
 
+    duk_push_c_function(ctx, js_atomic_GetVM, 0);
+    duk_put_prop_string(ctx, -2, "getVM");
+
     duk_push_c_function(ctx, js_atomic_GetEngine, 0);
     duk_put_prop_string(ctx, -2, "getEngine");
 

+ 60 - 0
Source/AtomicJS/Javascript/JSMetrics.cpp

@@ -0,0 +1,60 @@
+
+#include "JSVM.h"
+#include "JSMetrics.h"
+
+namespace Atomic
+{
+
+JSMetrics::JSMetrics(Context* context, JSVM *vm) : Object(context),
+    vm_(vm)
+{
+
+}
+
+JSMetrics::~JSMetrics()
+{
+
+}
+
+void JSMetrics::Dump()
+{
+    List<String>::ConstIterator itr = classNames_.Begin();
+    while (itr != classNames_.End())
+    {
+        const String& classname = *itr;
+        LOGINFOF("%s %i", classname.CString(), classInstances_[classname] );
+        itr++;
+    }
+
+}
+
+void JSMetrics::Capture()
+{
+    classNames_.Clear();
+    classInstances_.Clear();
+
+    HashMap<void*, RefCounted*>::ConstIterator itr = vm_->heapToObject_.Begin();
+    while (itr != vm_->heapToObject_.End())
+    {
+        String classname = "RefCounted";
+        if (itr->second_->IsObject())
+        {
+            classname = ((Object*) itr->second_)->GetTypeName();
+        }
+
+        if (!classNames_.Contains(classname))
+        {
+            classNames_.Push(classname);
+            classInstances_[classname] = 1;
+        }
+        else
+        {
+            classInstances_[classname]++;
+        }
+
+        itr++;
+    }
+
+}
+
+}

+ 41 - 0
Source/AtomicJS/Javascript/JSMetrics.h

@@ -0,0 +1,41 @@
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+// Please see LICENSE.md in repository root for license information
+// https://github.com/AtomicGameEngine/AtomicGameEngine
+
+#pragma once
+
+#include <Atomic/Core/Context.h>
+#include <Atomic/Core/Object.h>
+#include <Atomic/Container/List.h>
+
+namespace Atomic
+{
+
+class JSVM;
+
+class JSMetrics : public Object
+{
+    OBJECT(JSMetrics);
+
+public:
+
+    /// Construct.
+    JSMetrics(Context* context, JSVM* vm);
+    /// Destruct.
+    virtual ~JSMetrics();
+
+    void Capture();
+
+    void Dump();
+
+private:
+
+    WeakPtr<JSVM> vm_;
+
+    // Object
+    List<String> classNames_;
+    HashMap<StringHash, int> classInstances_;
+
+};
+
+}

+ 3 - 0
Source/AtomicJS/Javascript/JSVM.cpp

@@ -18,6 +18,7 @@
 #include "JSVM.h"
 #include "JSAtomic.h"
 #include "JSUI.h"
+#include "JSMetrics.h"
 
 namespace Atomic
 {
@@ -32,6 +33,8 @@ JSVM::JSVM(Context* context) :
     assert(!instance_);
 
     instance_ = this;
+
+    metrics_ = new JSMetrics(context, this);
 }
 
 JSVM::~JSVM()

+ 8 - 0
Source/AtomicJS/Javascript/JSVM.h

@@ -20,9 +20,13 @@ namespace Atomic
 
 class JSFile;
 class JSUI;
+class JSMetrics;
 
 class ATOMIC_API JSVM : public Object
 {
+
+    friend class JSMetrics;
+
     OBJECT(JSVM);
 
 public:
@@ -51,6 +55,8 @@ public:
 
     inline duk_context* GetJSContext() { return ctx_; }
 
+    JSMetrics* GetMetrics() { return metrics_; }
+
     void DumpJavascriptObjects() {}
 
 #ifdef JSVM_DEBUG
@@ -174,6 +180,8 @@ private:
     VariantMap objectAddedData_;
     VariantMap objectRemovedData_;
 
+    SharedPtr<JSMetrics> metrics_;
+
     static JSVM* instance_;
 
 };