]> khamidou — GitHub mirrors - electron.git/commitdiff
Add remote.createFunctionWithReturnValue API.
authorCheng Zhao <zcbenz@gmail.com>
Sun, 25 Aug 2013 09:22:36 +0000 (17:22 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Sun, 25 Aug 2013 09:22:36 +0000 (17:22 +0800)
browser/atom/rpc-server.coffee
renderer/api/lib/remote.coffee
spec/api/ipc.coffee
spec/fixtures/module/call.js [new file with mode: 0644]
spec/fixtures/module/print_name.js [new file with mode: 0644]

index 90c1a1edcd9e2c7f82e565689be7dd02817a3288..acff46ea5878d4528bdbec29d7f56f57417c194d 100644 (file)
@@ -44,10 +44,13 @@ unwrapArgs = (processId, routingId, args) ->
       when 'remote-object' then objectsRegistry.get meta.id
       when 'array' then unwrapArgs processId, routingId, meta.value
       when 'object'
-        ret = {}
+        ret = v8Util.createObjectWithName meta.name
         for member in meta.members
           ret[member.name] = metaToValue(member.value)
         ret
+      when 'function-with-return-value'
+        returnValue = metaToValue meta.value
+        -> returnValue
       when 'function'
         ret = ->
           ipc.sendChannel processId, routingId, 'ATOM_RENDERER_CALLBACK', meta.id, valueToMeta(processId, routingId, arguments)
index 997225a6810ee9d0b74dec59eb49dbe231009a2c..ee124fe232b7f6cd1a722bcb0e467fa16969e1a3 100644 (file)
@@ -13,9 +13,11 @@ wrapArgs = (args) ->
     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 = type: 'object', name: value.constructor.name, members: []
       ret.members.push(name: prop, value: valueToMeta(field)) for prop, field of value
       ret
+    else if typeof value is 'function' and v8Util.getHiddenValue value, 'returnValue'
+      type: 'function-with-return-value', value: valueToMeta(value())
     else if typeof value is 'function'
       type: 'function', id: callbacksRegistry.add(value)
     else
@@ -121,3 +123,9 @@ processCache = null
 exports.__defineGetter__ 'process', ->
   processCache = exports.getGlobal('process') unless processCache?
   processCache
+
+# Create a funtion that will return the specifed value when called in browser.
+exports.createFunctionWithReturnValue = (returnValue) ->
+  func = -> returnValue
+  v8Util.setHiddenValue func, 'returnValue', true
+  func
index c5464d3b4a46a5bbff5eb655d894ce84b0ae4356..1c28bdc03c71aa49deafc1b11da208d521e688f1 100644 (file)
@@ -16,7 +16,14 @@ describe 'ipc', ->
       a = remote.require path.join(fixtures, 'module', 'id.js')
       assert.equal a.id, 1127
 
-  describe 'remote object', ->
+  describe 'remote.createFunctionWithReturnValue', ->
+    it 'should be called in browser synchronously', ->
+      buf = new Buffer('test')
+      call = remote.require path.join(fixtures, 'module', 'call.js')
+      result = call.call remote.createFunctionWithReturnValue(buf)
+      assert.equal result.constructor.name, 'Buffer'
+
+  describe 'remote object in renderer', ->
     it 'can change its properties', ->
       property = remote.require path.join(fixtures, 'module', 'property.js')
       assert.equal property.property, 1127
@@ -28,6 +35,12 @@ describe 'ipc', ->
       # Restore.
       property.property = 1127
 
+  describe 'remote value in browser', ->
+    it 'keeps its constructor name for objects', ->
+      buf = new Buffer('test')
+      print_name = remote.require path.join(fixtures, 'module', 'print_name.js')
+      assert.equal print_name.print(buf), 'Buffer'
+
   describe 'ipc.send', ->
     it 'should work when sending an object containing id property', (done) ->
       obj = id: 1, name: 'ly'
diff --git a/spec/fixtures/module/call.js b/spec/fixtures/module/call.js
new file mode 100644 (file)
index 0000000..552aab2
--- /dev/null
@@ -0,0 +1,3 @@
+exports.call = function(func) {
+  return func();
+}
diff --git a/spec/fixtures/module/print_name.js b/spec/fixtures/module/print_name.js
new file mode 100644 (file)
index 0000000..09ec333
--- /dev/null
@@ -0,0 +1,3 @@
+exports.print = function(obj) {
+  return obj.constructor.name;
+}