globalStore[key] = {} unless globalStore[key]?
globalStore[key]
-exports.add = (process_id, routing_id, obj) ->
+process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) ->
+ # For objects created in browser scripts, keep a weak reference here.
id = globalMap.add obj
+ obj.id = id
+
+exports.add = (process_id, routing_id, obj) ->
+ # Some native types may already been added to globalMap, in that case we
+ # don't add it twice.
+ id = obj.id ? globalMap.add obj
+
store = getStoreForRenderView process_id, routing_id
+
+ # It's possible that a render view may want to get the same remote object
+ # twice, since we only allow one reference of one object per render view,
+ # we throw when the object is already referenced.
+ throw new Error("Object #{id} is already referenced") if store[id]?
+
store[id] = obj
id
-exports.get = (process_id, routing_id, id) ->
+exports.get = (id) ->
globalMap.get id
exports.remove = (process_id, routing_id, id) ->
else if @type is 'object' or @type is 'function'
@name = value.constructor.name
@id = objectsRegistry.add process_id, routing_id, value
- value.id = @id
@members = []
@members.push { name: prop, type: typeof field } for prop, field of value
@value = value
ipc.on 'ATOM_INTERNAL_REQUIRE', (event, process_id, routing_id, module) ->
- event.result = new PlainObject(process_id, routing_id, require(module))
+ try
+ event.result = new PlainObject(process_id, routing_id, require(module))
+ catch e
+ event.result = type: 'error', value: e.message
ipc.on 'ATOM_INTERNAL_CONSTRUCTOR', (event, process_id, routing_id, id, args) ->
try
- constructor = objectsRegistry.get process_id, routing_id, id
+ constructor = objectsRegistry.get id
# Call new with array of arguments.
# http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
obj = new (Function::bind.apply(constructor, [null].concat(args)))
ipc.on 'ATOM_INTERNAL_FUNCTION_CALL', (event, process_id, routing_id, id, args) ->
try
- func = objectsRegistry.get process_id, routing_id, id
+ func = objectsRegistry.get id
ret = func.apply global, args
event.result = new PlainObject(process_id, routing_id, ret)
catch e
ipc.on 'ATOM_INTERNAL_MEMBER_CALL', (event, process_id, routing_id, id, method, args) ->
try
- obj = objectsRegistry.get process_id, routing_id, id
+ obj = objectsRegistry.get id
ret = obj[method].apply(obj, args)
event.result = new PlainObject(process_id, routing_id, ret)
catch e
ipc.on 'ATOM_INTERNAL_MEMBER_SET', (event, process_id, routing_id, id, name, value) ->
try
- obj = objectsRegistry.get process_id, routing_id, id
+ obj = objectsRegistry.get id
obj[name] = value
catch e
event.result = type: 'error', value: e.message
ipc.on 'ATOM_INTERNAL_MEMBER_GET', (event, process_id, routing_id, id, name) ->
try
- obj = objectsRegistry.get process_id, routing_id, id
+ obj = objectsRegistry.get id
event.result = new PlainObject(process_id, routing_id, obj[name])
catch e
event.result = type: 'error', value: e.message
+ipc.on 'ATOM_INTERNAL_GET_OBJECT', (event, process_id, routing_id, id) ->
+ try
+ obj = objectsRegistry.get id
+ event.result = new PlainObject(process_id, routing_id, obj)
+ catch e
+ event.result = type: 'error', value: e.message
+
ipc.on 'ATOM_INTERNAL_DESTROY', (process_id, routing_id, id) ->
objectsRegistry.remove process_id, routing_id, id