Ver código fonte

Only create JS event variant map object when a subscriber needs it

Josh Engebretson 10 anos atrás
pai
commit
b3faa701c6

+ 1 - 2
Source/AtomicJS/Javascript/JSCore.cpp

@@ -83,8 +83,6 @@ static int Object_SubscribeToEvent(duk_context* ctx)
     return 0;
 }
 
-// so we don't keep allocating these
-static VariantMap sendEventVMap;
 static int Object_SendEvent(duk_context* ctx)
 {
     int top = duk_get_top(ctx);
@@ -102,6 +100,7 @@ static int Object_SendEvent(duk_context* ctx)
     {
         if (duk_is_object(ctx, 1)) {
 
+            VariantMap sendEventVMap;
             js_object_to_variantmap(ctx, 1, sendEventVMap);
 
             sender->SendEvent(duk_to_string(ctx, 0), sendEventVMap);

+ 19 - 29
Source/AtomicJS/Javascript/JSEventHelper.cpp

@@ -20,37 +20,18 @@ JSEventDispatcher::~JSEventDispatcher()
 
 void JSEventDispatcher::BeginSendEvent(Context* context, Object* sender, StringHash eventType, VariantMap& eventData)
 {
-    JSVM* vm = JSVM::GetJSVM(NULL);
-
-    if (!vm)
-        return;
-
-    if (!jsEvents_.Contains(eventType))
-        return;
-
-    duk_context* ctx = vm->GetJSContext();
-
-    duk_push_global_stash(ctx);
-    duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_VARIANTMAP_CACHE);
-
-    duk_push_pointer(ctx, (void*) &eventData);
-    js_push_variantmap(ctx, eventData);
-    duk_put_prop(ctx, -3);
-
-    duk_pop_2(ctx);
-
 }
 
 void JSEventDispatcher::EndSendEvent(Context* context, Object* sender, StringHash eventType, VariantMap& eventData)
 {
+    if (!jsEvents_.Contains(eventType))
+        return;
+
     JSVM* vm = JSVM::GetJSVM(NULL);
 
     if (!vm)
         return;
 
-    if (!jsEvents_.Contains(eventType))
-        return;
-
     duk_context* ctx = vm->GetJSContext();
 
     duk_push_global_stash(ctx);
@@ -107,22 +88,31 @@ void JSEventHelper::HandleEvent(StringHash eventType, VariantMap& eventData)
 
     if (duk_is_function(ctx, -1))
     {
-        assert(duk_is_object(ctx, -1));
-
+        // look in the variant map cache
         duk_push_global_stash(ctx);
         duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_VARIANTMAP_CACHE);
         duk_push_pointer(ctx, (void*) &eventData);
         duk_get_prop(ctx, -2);
 
-        duk_remove(ctx, -2); // vmap cache
-        duk_remove(ctx, -2); // global stash
-
         if (!duk_is_object(ctx, -1))
         {
-            duk_set_top(ctx, top);
-            return;
+            // pop result
+            duk_pop(ctx);
+
+            // we need to push a new variant map and store to cache
+            // the cache object will be cleared at the send end in  the
+            // global listener above
+            js_push_variantmap(ctx, eventData);
+            duk_push_pointer(ctx, (void*) &eventData);
+            duk_dup(ctx, -2);
+            duk_put_prop(ctx, -4);
+
         }
 
+        duk_remove(ctx, -2); // vmap cache
+        duk_remove(ctx, -2); // global stash
+
+
         if (duk_pcall(ctx, 1) != 0)
         {
             vm->SendJSErrorEvent();