Don't make use of the 'id' property when sending objects.

Fixes #43.
This commit is contained in:
Cheng Zhao 2013-07-26 16:39:22 +08:00
parent 511b2a4422
commit 51a632d7d9
3 changed files with 13 additions and 11 deletions

View file

@ -1,11 +1,12 @@
BrowserWindow = require 'browser-window' BrowserWindow = require 'browser-window'
IDWeakMap = require 'id-weak-map' IDWeakMap = require 'id-weak-map'
v8Util = process.atomBinding 'v8_util'
class ObjectsStore class ObjectsStore
@stores = {} @stores = {}
constructor: -> constructor: ->
@nextId = 0 @nextId = 1
@objects = [] @objects = []
getNextId: -> getNextId: ->
@ -41,8 +42,7 @@ class ObjectsStore
objectsWeakMap = new IDWeakMap objectsWeakMap = new IDWeakMap
objectsWeakMap.add = (obj) -> objectsWeakMap.add = (obj) ->
id = IDWeakMap::add.call this, obj id = IDWeakMap::add.call this, obj
Object.defineProperty obj, 'id', v8Util.setHiddenValue obj, 'atomId', id
enumerable: true, writable: false, value: id
id id
windowsWeakMap = new IDWeakMap windowsWeakMap = new IDWeakMap
@ -63,13 +63,16 @@ process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) ->
exports.add = (processId, routingId, obj) -> exports.add = (processId, routingId, obj) ->
# Some native objects may already been added to objectsWeakMap, be care not # Some native objects may already been added to objectsWeakMap, be care not
# to add it twice. # to add it twice.
objectsWeakMap.add obj unless obj.id? and objectsWeakMap.has obj.id objectsWeakMap.add obj unless v8Util.getHiddenValue obj, 'atomId'
id = v8Util.getHiddenValue obj, 'atomId'
# Store and reference the object, then return the storeId which points to # Store and reference the object, then return the storeId which points to
# where the object is stored. The caller can later dereference the object # where the object is stored. The caller can later dereference the object
# with the storeId. # with the storeId.
store = ObjectsStore.forRenderView processId, routingId store = ObjectsStore.forRenderView processId, routingId
store.add obj storeId = store.add obj
[id, storeId]
exports.get = (id) -> exports.get = (id) ->
objectsWeakMap.get id objectsWeakMap.get id

View file

@ -22,8 +22,7 @@ valueToMeta = (processId, routingId, value) ->
# Reference the original value if it's an object, because when it's # 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 # passed to renderer we would assume the renderer keeps a reference of
# it. # it.
meta.storeId = objectsRegistry.add processId, routingId, value [meta.id, meta.storeId] = objectsRegistry.add processId, routingId, value
meta.id = value.id
meta.members = [] meta.members = []
meta.members.push {name: prop, type: typeof field} for prop, field of value meta.members.push {name: prop, type: typeof field} for prop, field of value

View file

@ -8,8 +8,8 @@ callbacksRegistry = new CallbacksRegistry
# Convert the arguments object into an array of meta data. # Convert the arguments object into an array of meta data.
wrapArgs = (args) -> wrapArgs = (args) ->
Array::slice.call(args).map (value) -> Array::slice.call(args).map (value) ->
if value? and typeof value is 'object' and v8Util.getHiddenValue value, 'isRemoteObject' if value? and typeof value is 'object' and v8Util.getHiddenValue value, 'atomId'
type: 'object', id: value.id type: 'object', id: v8Util.getHiddenValue value, 'atomId'
else if typeof value is 'function' else if typeof value is 'function'
type: 'function', id: callbacksRegistry.add(value) type: 'function', id: callbacksRegistry.add(value)
else else
@ -68,8 +68,8 @@ metaToValue = (meta) ->
return unless currentContextExist return unless currentContextExist
ipc.sendChannel 'ATOM_BROWSER_DEREFERENCE', meta.storeId ipc.sendChannel 'ATOM_BROWSER_DEREFERENCE', meta.storeId
# Mark this is a remote object. # Remember object's id.
v8Util.setHiddenValue ret, 'isRemoteObject', true v8Util.setHiddenValue ret, 'atomId', meta.id
ret ret