ipc = require 'ipc'
path = require 'path'
objectsRegistry = require './objects_registry.js'
-
-metaToArgs = (metas) ->
- metas.map (meta) ->
- if meta.type is 'value'
- meta.value
- else if meta.type is 'remoteObject'
- objectsRegistry.get meta.id
+v8_util = process.atomBinding 'v8_util'
+
+unwrapArgs = (processId, routingId, args) ->
+ args.map (arg) ->
+ if arg.type is 'value'
+ arg.value
+ else if arg.type is 'remoteObject'
+ objectsRegistry.get arg.id
+ else if arg.type is 'function'
+ ret = ->
+ ipc.sendChannel processId, routingId, 'ATOM_RENDERER_CALLBACK', arg.id, new Meta(processId, routingId, arguments)
+ v8_util.setDestructor ret, ->
+ ipc.sendChannel processId, routingId, 'ATOM_RENDERER_RELEASE_CALLBACK', arg.id
+ ret
else
- throw new TypeError("Unknown type: #{meta.type}")
+ throw new TypeError("Unknown type: #{arg.type}")
# Convert a real value into a POD structure which carries information of this
# value.
ipc.on 'ATOM_BROWSER_CONSTRUCTOR', (event, processId, routingId, id, args) ->
try
- args = metaToArgs args
+ args = unwrapArgs processId, routingId, args
constructor = objectsRegistry.get id
# Call new with array of arguments.
# http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
ipc.on 'ATOM_BROWSER_FUNCTION_CALL', (event, processId, routingId, id, args) ->
try
- args = metaToArgs args
+ args = unwrapArgs processId, routingId, args
func = objectsRegistry.get id
ret = func.apply global, args
event.result = new Meta(processId, routingId, ret)
ipc.on 'ATOM_BROWSER_MEMBER_CALL', (event, processId, routingId, id, method, args) ->
try
- args = metaToArgs args
+ args = unwrapArgs processId, routingId, args
obj = objectsRegistry.get id
ret = obj[method].apply(obj, args)
event.result = new Meta(processId, routingId, ret)
ipc = require 'ipc'
v8_util = process.atomBinding 'v8_util'
+nextCallbackId = 0
+storedCallbacks = {}
+
+storeCallback = (callback) ->
+ ++nextCallbackId
+ storedCallbacks[nextCallbackId] = callback
+ nextCallbackId
+
+makeCallback = (id, args) ->
+ storedCallbacks[id].apply global, args
+
+releaseCallback = (id) ->
+ delete storedCallbacks[id]
+
# Transform the arguments passed to browser into list of descriptions.
#
# This function assumes an array is passed, and it only converts remote objects
# and functions, other types of values are passed as it is.
-argsToMeta = (args) ->
- args.map (value) ->
+wrapArgs = (args) ->
+ Array::slice.call(args).map (value) ->
if typeof value is 'object' and v8_util.getHiddenValue value, 'isRemoteObject'
type: 'remoteObject', id: value.id
else if typeof value is 'function'
- throw new TypeError('functions is not supported yet')
+ type: 'function', id: storeCallback(value)
else
type: 'value', value: value
constructor: ->
if @constructor == RemoteFunction
# Constructor call.
- obj = ipc.sendChannelSync 'ATOM_BROWSER_CONSTRUCTOR', meta.id, Array::slice.call(arguments)
+ obj = ipc.sendChannelSync 'ATOM_BROWSER_CONSTRUCTOR', meta.id, wrapArgs(arguments)
# Returning object in constructor will replace constructed object
# with the returned object.
return metaToValue obj
else
# Function call.
- ret = ipc.sendChannelSync 'ATOM_BROWSER_FUNCTION_CALL', meta.id, Array::slice.call(arguments)
+ ret = ipc.sendChannelSync 'ATOM_BROWSER_FUNCTION_CALL', meta.id, wrapArgs(arguments)
return metaToValue ret
else
ret = v8_util.createObjectWithName meta.name
if member.type is 'function'
ret[member.name] = ->
# Call member function.
- ret = ipc.sendChannelSync 'ATOM_BROWSER_MEMBER_CALL', meta.id, member.name, Array::slice.call(arguments)
+ ret = ipc.sendChannelSync 'ATOM_BROWSER_MEMBER_CALL', meta.id, member.name, wrapArgs(arguments)
metaToValue ret
else
ret.__defineSetter__ member.name, (value) ->
ret
+ipc.on 'ATOM_RENDERER_CALLBACK', (id, args) ->
+ makeCallback id, metaToValue(args)
+
+ipc.on 'ATOM_RENDERER_RELEASE_CALLBACK', (id) ->
+ releaseCallback id
+
# Release all resources of current render view when it's going to be unloaded.
window.addEventListener 'unload', (event) ->
ipc.sendChannelSync 'ATOM_BROWSER_RELEASE_RENDER_VIEW'