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'
IDWeakMap = require 'id-weak-map'
v8Util = process.atomBinding 'v8_util'
class ObjectsStore
@stores = {}
constructor: ->
@nextId = 0
@nextId = 1
@objects = []
getNextId: ->
@ -41,8 +42,7 @@ class ObjectsStore
objectsWeakMap = new IDWeakMap
objectsWeakMap.add = (obj) ->
id = IDWeakMap::add.call this, obj
Object.defineProperty obj, 'id',
enumerable: true, writable: false, value: id
v8Util.setHiddenValue obj, 'atomId', id
id
windowsWeakMap = new IDWeakMap
@ -63,13 +63,16 @@ process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) ->
exports.add = (processId, routingId, obj) ->
# Some native objects may already been added to objectsWeakMap, be care not
# 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
# where the object is stored. The caller can later dereference the object
# with the storeId.
store = ObjectsStore.forRenderView processId, routingId
store.add obj
storeId = store.add obj
[id, storeId]
exports.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
# passed to renderer we would assume the renderer keeps a reference of
# it.
meta.storeId = objectsRegistry.add processId, routingId, value
meta.id = value.id
[meta.id, meta.storeId] = objectsRegistry.add processId, routingId, value
meta.members = []
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.
wrapArgs = (args) ->
Array::slice.call(args).map (value) ->
if value? and typeof value is 'object' and v8Util.getHiddenValue value, 'isRemoteObject'
type: 'object', id: value.id
if value? and typeof value is 'object' and v8Util.getHiddenValue value, 'atomId'
type: 'object', id: v8Util.getHiddenValue value, 'atomId'
else if typeof value is 'function'
type: 'function', id: callbacksRegistry.add(value)
else
@ -68,8 +68,8 @@ metaToValue = (meta) ->
return unless currentContextExist
ipc.sendChannel 'ATOM_BROWSER_DEREFERENCE', meta.storeId
# Mark this is a remote object.
v8Util.setHiddenValue ret, 'isRemoteObject', true
# Remember object's id.
v8Util.setHiddenValue ret, 'atomId', meta.id
ret