From 51a632d7d937d9fec00de5a286eb95c6aa35278f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 26 Jul 2013 16:39:22 +0800 Subject: [PATCH] Don't make use of the 'id' property when sending objects. Fixes #43. --- browser/atom/objects-registry.coffee | 13 ++++++++----- browser/atom/rpc-server.coffee | 3 +-- renderer/api/lib/remote.coffee | 8 ++++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/browser/atom/objects-registry.coffee b/browser/atom/objects-registry.coffee index 97e3033e6317..5f4ce62272c8 100644 --- a/browser/atom/objects-registry.coffee +++ b/browser/atom/objects-registry.coffee @@ -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 diff --git a/browser/atom/rpc-server.coffee b/browser/atom/rpc-server.coffee index 27229e9fe249..2e0c9eb9913e 100644 --- a/browser/atom/rpc-server.coffee +++ b/browser/atom/rpc-server.coffee @@ -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 diff --git a/renderer/api/lib/remote.coffee b/renderer/api/lib/remote.coffee index d43feebab17b..b79c8d0697cf 100644 --- a/renderer/api/lib/remote.coffee +++ b/renderer/api/lib/remote.coffee @@ -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