Browse Source

update jerry_callbacks.c jerry_message.c

Signed-off-by: yangfasheng <yangfasheng@rt-thread.com>
yangfasheng 7 years ago
parent
commit
24965490cb
2 changed files with 19 additions and 17 deletions
  1. 6 1
      rtthread-port/jerry_callbacks.c
  2. 13 16
      rtthread-port/jerry_message.c

+ 6 - 1
rtthread-port/jerry_callbacks.c

@@ -128,7 +128,11 @@ rt_bool_t js_send_callback(struct js_callback *callback, const void *args, uint3
     if (jmc)
     if (jmc)
     {
     {
         jmc->callback = callback;
         jmc->callback = callback;
-        jmc->args = (void *)args;
+        jmc->args = rt_malloc(size);
+        if (jmc->args && args)
+        {
+            memcpy(jmc->args, args, size);
+        }
         jmc->size = size;
         jmc->size = size;
 
 
         if (_js_mq_func)
         if (_js_mq_func)
@@ -138,6 +142,7 @@ rt_bool_t js_send_callback(struct js_callback *callback, const void *args, uint3
 
 
         if (ret == RT_FALSE)
         if (ret == RT_FALSE)
         {
         {
+            rt_free(jmc->args);
             rt_free(jmc);
             rt_free(jmc);
         }
         }
     }
     }

+ 13 - 16
rtthread-port/jerry_message.c

@@ -25,24 +25,21 @@ rt_bool_t js_message_send(const char *name, rt_uint8_t *data, rt_uint32_t size)
 
 
     if (js_message_cb)
     if (js_message_cb)
     {
     {
-        struct js_message *msg = rt_malloc(sizeof(struct js_message));
-        if (msg)
+        struct js_message msg;
+
+        msg.name = rt_strdup(name);
+        msg.size = size;
+        msg.data = rt_malloc(msg.size);
+        if (msg.data)
         {
         {
-            msg->name = rt_strdup(name);
-            msg->size = size;
-            msg->data = rt_malloc(msg->size);
-            if (msg->data)
-            {
-                rt_memcpy(msg->data, data, size);
-                ret = js_send_callback(js_message_cb, msg, sizeof(struct js_message));
-            }
+            rt_memcpy(msg.data, data, size);
+            ret = js_send_callback(js_message_cb, &msg, sizeof(struct js_message));
+        }
 
 
-            if (!ret)
-            {
-                rt_free(msg->data);
-                rt_free(msg->name);
-                rt_free(msg);
-            }
+        if (!ret)
+        {
+            rt_free(msg.data);
+            rt_free(msg.name);
         }
         }
     }
     }