2013-04-24 08:43:01 +00:00
|
|
|
ipc = require 'ipc'
|
|
|
|
path = require 'path'
|
2013-04-26 13:14:29 +00:00
|
|
|
objectsRegistry = require './objects_registry.js'
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-26 15:58:49 +00:00
|
|
|
# Convert a real value into a POD structure which carries information of this
|
|
|
|
# value.
|
|
|
|
class Meta
|
2013-04-27 11:13:24 +00:00
|
|
|
constructor: (processId, routingId, value) ->
|
2013-04-24 08:43:01 +00:00
|
|
|
@type = typeof value
|
|
|
|
@type = 'value' if value is null
|
2013-04-25 08:03:29 +00:00
|
|
|
@type = 'array' if Array.isArray value
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-29 10:24:37 +00:00
|
|
|
# Treat the arguments object as array.
|
|
|
|
@type = 'array' if @type is 'object' and value.callee? and value.length?
|
|
|
|
|
2013-04-25 08:03:29 +00:00
|
|
|
if @type is 'array'
|
|
|
|
@members = []
|
2013-04-27 11:13:24 +00:00
|
|
|
@members.push new Meta(processId, routingId, el) for el in value
|
2013-04-25 08:03:29 +00:00
|
|
|
else if @type is 'object' or @type is 'function'
|
2013-04-24 08:43:01 +00:00
|
|
|
@name = value.constructor.name
|
2013-04-26 15:58:49 +00:00
|
|
|
|
|
|
|
# Reference the original value if it's an object, because when it's
|
|
|
|
# passed to renderer we would assume the renderer keeps a reference of
|
|
|
|
# it.
|
2013-04-27 11:13:24 +00:00
|
|
|
@storeId = objectsRegistry.add processId, routingId, value
|
2013-04-26 15:26:41 +00:00
|
|
|
@id = value.id
|
2013-04-24 08:43:01 +00:00
|
|
|
|
|
|
|
@members = []
|
2013-04-25 08:03:29 +00:00
|
|
|
@members.push { name: prop, type: typeof field } for prop, field of value
|
2013-04-24 08:43:01 +00:00
|
|
|
else
|
|
|
|
@type = 'value'
|
|
|
|
@value = value
|
|
|
|
|
2013-04-29 10:59:34 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_REQUIRE', (event, processId, routingId, module) ->
|
2013-04-26 14:25:30 +00:00
|
|
|
try
|
2013-04-27 11:13:24 +00:00
|
|
|
event.result = new Meta(processId, routingId, require(module))
|
2013-04-26 14:25:30 +00:00
|
|
|
catch e
|
|
|
|
event.result = type: 'error', value: e.message
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-04-30 08:27:14 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_GLOBAL', (event, processId, routingId, name) ->
|
|
|
|
try
|
|
|
|
event.result = new Meta(processId, routingId, global[name])
|
|
|
|
catch e
|
|
|
|
event.result = type: 'error', value: e.message
|
|
|
|
|
2013-05-01 02:59:29 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_RELEASE_RENDER_VIEW', (event, processId, routingId) ->
|
|
|
|
objectsRegistry.clear processId, routingId
|
|
|
|
|
2013-04-29 10:59:34 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_CURRENT_WINDOW', (event, processId, routingId) ->
|
2013-04-27 11:06:41 +00:00
|
|
|
try
|
|
|
|
windows = objectsRegistry.getAllWindows()
|
|
|
|
for window in windows
|
2013-05-03 07:21:41 +00:00
|
|
|
break if window.getProcessId() == processId and
|
|
|
|
window.getRoutingId() == routingId
|
2013-04-27 11:13:24 +00:00
|
|
|
event.result = new Meta(processId, routingId, window)
|
2013-04-27 11:06:41 +00:00
|
|
|
catch e
|
|
|
|
event.result = type: 'error', value: e.message
|
|
|
|
|
2013-04-29 10:59:34 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_CONSTRUCTOR', (event, processId, routingId, id, args) ->
|
2013-04-24 08:43:01 +00:00
|
|
|
try
|
2013-04-26 14:25:30 +00:00
|
|
|
constructor = objectsRegistry.get id
|
2013-04-25 07:12:56 +00:00
|
|
|
# Call new with array of arguments.
|
|
|
|
# http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
|
2013-04-24 08:43:01 +00:00
|
|
|
obj = new (Function::bind.apply(constructor, [null].concat(args)))
|
2013-04-27 11:13:24 +00:00
|
|
|
event.result = new Meta(processId, routingId, obj)
|
2013-04-24 08:43:01 +00:00
|
|
|
catch e
|
|
|
|
event.result = type: 'error', value: e.message
|
|
|
|
|
2013-04-29 10:59:34 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_FUNCTION_CALL', (event, processId, routingId, id, args) ->
|
2013-04-24 08:43:01 +00:00
|
|
|
try
|
2013-04-26 14:25:30 +00:00
|
|
|
func = objectsRegistry.get id
|
2013-04-26 13:14:29 +00:00
|
|
|
ret = func.apply global, args
|
2013-04-27 11:13:24 +00:00
|
|
|
event.result = new Meta(processId, routingId, ret)
|
2013-04-24 08:43:01 +00:00
|
|
|
catch e
|
|
|
|
event.result = type: 'error', value: e.message
|
|
|
|
|
2013-04-29 10:59:34 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_MEMBER_CALL', (event, processId, routingId, id, method, args) ->
|
2013-04-24 08:43:01 +00:00
|
|
|
try
|
2013-04-26 14:25:30 +00:00
|
|
|
obj = objectsRegistry.get id
|
2013-04-24 08:43:01 +00:00
|
|
|
ret = obj[method].apply(obj, args)
|
2013-04-27 11:13:24 +00:00
|
|
|
event.result = new Meta(processId, routingId, ret)
|
2013-04-24 08:43:01 +00:00
|
|
|
catch e
|
|
|
|
event.result = type: 'error', value: e.message
|
|
|
|
|
2013-04-29 10:59:34 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_MEMBER_SET', (event, processId, routingId, id, name, value) ->
|
2013-04-24 08:43:01 +00:00
|
|
|
try
|
2013-04-26 14:25:30 +00:00
|
|
|
obj = objectsRegistry.get id
|
2013-04-26 13:14:29 +00:00
|
|
|
obj[name] = value
|
2013-04-24 08:43:01 +00:00
|
|
|
catch e
|
|
|
|
event.result = type: 'error', value: e.message
|
|
|
|
|
2013-04-29 10:59:34 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_MEMBER_GET', (event, processId, routingId, id, name) ->
|
2013-04-24 08:43:01 +00:00
|
|
|
try
|
2013-04-26 14:25:30 +00:00
|
|
|
obj = objectsRegistry.get id
|
2013-04-27 11:13:24 +00:00
|
|
|
event.result = new Meta(processId, routingId, obj[name])
|
2013-04-24 08:43:01 +00:00
|
|
|
catch e
|
|
|
|
event.result = type: 'error', value: e.message
|
2013-04-25 11:26:31 +00:00
|
|
|
|
2013-04-29 10:59:34 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_REFERENCE', (event, processId, routingId, id) ->
|
2013-04-26 14:25:30 +00:00
|
|
|
try
|
|
|
|
obj = objectsRegistry.get id
|
2013-04-27 11:13:24 +00:00
|
|
|
event.result = new Meta(processId, routingId, obj)
|
2013-04-26 14:25:30 +00:00
|
|
|
catch e
|
|
|
|
event.result = type: 'error', value: e.message
|
|
|
|
|
2013-04-29 10:59:34 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_DEREFERENCE', (processId, routingId, storeId) ->
|
2013-04-27 11:13:24 +00:00
|
|
|
objectsRegistry.remove processId, routingId, storeId
|