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
+ else
+ throw new TypeError("Unknown type: #{meta.type}")
+
# Convert a real value into a POD structure which carries information of this
# value.
class Meta
ipc.on 'ATOM_BROWSER_CONSTRUCTOR', (event, processId, routingId, id, args) ->
try
+ args = metaToArgs 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
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
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'
+# 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) ->
+ 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')
+ else
+ type: 'value', value: value
+
# Transform the description of value into a value or delegate object.
metaToValue = (meta) ->
switch meta.type
v8_util.setDestructor ret, ->
ipc.sendChannel 'ATOM_BROWSER_DEREFERENCE', meta.storeId
+ # Mark this is a remote object.
+ v8_util.setHiddenValue ret, 'isRemoteObject', true
+
ret
# Release all resources of current render view when it's going to be unloaded.