]> khamidou — GitHub mirrors - electron.git/commitdiff
Enable passing objects and arrays when calling remote function.
authorCheng Zhao <zcbenz@gmail.com>
Wed, 14 Aug 2013 13:51:51 +0000 (21:51 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 14 Aug 2013 13:51:51 +0000 (21:51 +0800)
browser/atom/rpc-server.coffee
renderer/api/lib/remote.coffee

index 879a80dcbbe0a3b070bafbaa0e7b0ae61d5834fd..90c1a1edcd9e2c7f82e565689be7dd02817a3288 100644 (file)
@@ -38,10 +38,16 @@ errorToMeta = (error) ->
 
 # Convert array of meta data from renderer into array of real values.
 unwrapArgs = (processId, routingId, args) ->
-  args.map (meta) ->
+  metaToValue = (meta) ->
     switch meta.type
       when 'value' then meta.value
-      when 'object' then objectsRegistry.get meta.id
+      when 'remote-object' then objectsRegistry.get meta.id
+      when 'array' then unwrapArgs processId, routingId, meta.value
+      when 'object'
+        ret = {}
+        for member in meta.members
+          ret[member.name] = metaToValue(member.value)
+        ret
       when 'function'
         ret = ->
           ipc.sendChannel processId, routingId, 'ATOM_RENDERER_CALLBACK', meta.id, valueToMeta(processId, routingId, arguments)
@@ -50,6 +56,8 @@ unwrapArgs = (processId, routingId, args) ->
         ret
       else throw new TypeError("Unknown type: #{meta.type}")
 
+  args.map metaToValue
+
 ipc.on 'ATOM_BROWSER_REQUIRE', (event, processId, routingId, module) ->
   try
     event.result = valueToMeta processId, routingId, require(module)
index ae08d94b31ff148961a27d3c8163c35cb41c4394..997225a6810ee9d0b74dec59eb49dbe231009a2c 100644 (file)
@@ -7,14 +7,22 @@ callbacksRegistry = new CallbacksRegistry
 
 # Convert the arguments object into an array of meta data.
 wrapArgs = (args) ->
-  Array::slice.call(args).map (value) ->
-    if value? and typeof value is 'object' and v8Util.getHiddenValue value, 'atomId'
-      type: 'object', id: v8Util.getHiddenValue value, 'atomId'
+  valueToMeta = (value) ->
+    if Array.isArray value
+      type: 'array', value: wrapArgs(value)
+    else if value? and typeof value is 'object' and v8Util.getHiddenValue value, 'atomId'
+      type: 'remote-object', id: v8Util.getHiddenValue value, 'atomId'
+    else if value? and typeof value is 'object'
+      ret = type: 'object', members: []
+      ret.members.push(name: prop, value: valueToMeta(field)) for prop, field of value
+      ret
     else if typeof value is 'function'
       type: 'function', id: callbacksRegistry.add(value)
     else
       type: 'value', value: value
 
+  Array::slice.call(args).map valueToMeta
+
 # Convert meta data from browser into real value.
 metaToValue = (meta) ->
   switch meta.type