Merge pull request #4981 from atom/member-object-reference

Remote object should be referenced by its members
This commit is contained in:
Cheng Zhao 2016-04-01 16:11:21 +09:00
commit 6f7a98d7c9
2 changed files with 29 additions and 8 deletions

View file

@ -91,8 +91,9 @@ var wrapArgs = function (args, visited) {
}
// Populate object's members from descriptors.
// The |ref| will be kept referenced by |members|.
// This matches |getObjectMemebers| in rpc-server.
let setObjectMembers = function (object, metaId, members) {
let setObjectMembers = function (ref, object, metaId, members) {
for (let member of members) {
if (object.hasOwnProperty(member.name)) continue
@ -109,9 +110,16 @@ let setObjectMembers = function (object, metaId, members) {
return metaToValue(ret)
}
}
descriptor.writable = true
descriptor.get = function () {
remoteMemberFunction.ref = ref // The member should reference its object.
return remoteMemberFunction
}
// Enable monkey-patch the method
descriptor.set = function (value) {
remoteMemberFunction = value
return value
}
descriptor.configurable = true
descriptor.value = remoteMemberFunction
} else if (member.type === 'get') {
descriptor.get = function () {
return metaToValue(ipcRenderer.sendSync('ATOM_BROWSER_MEMBER_GET', metaId, member.name))
@ -132,11 +140,11 @@ let setObjectMembers = function (object, metaId, members) {
// Populate object's prototype from descriptor.
// This matches |getObjectPrototype| in rpc-server.
let setObjectPrototype = function (object, metaId, descriptor) {
let setObjectPrototype = function (ref, object, metaId, descriptor) {
if (descriptor === null) return
let proto = {}
setObjectMembers(proto, metaId, descriptor.members)
setObjectPrototype(proto, metaId, descriptor.proto)
setObjectMembers(ref, proto, metaId, descriptor.members)
setObjectPrototype(ref, proto, metaId, descriptor.proto)
Object.setPrototypeOf(object, proto)
}
@ -191,9 +199,9 @@ let metaToValue = function (meta) {
}
// Populate delegate members.
setObjectMembers(ret, meta.id, meta.members)
setObjectMembers(ret, ret, meta.id, meta.members)
// Populate delegate prototype.
setObjectPrototype(ret, meta.id, meta.proto)
setObjectPrototype(ret, ret, meta.id, meta.proto)
// Set constructor.name to object's name.
Object.defineProperty(ret.constructor, 'name', { value: meta.name })

View file

@ -73,6 +73,12 @@ describe('ipc module', function () {
assert.equal(delete remoteFunctions.aFunction, true)
})
it('is referenced by its members', function () {
let stringify = remote.getGlobal('JSON').stringify
gc();
stringify({})
});
})
describe('remote value in browser', function () {
@ -143,6 +149,13 @@ describe('ipc module', function () {
assert(!proto.hasOwnProperty('method'))
assert(Object.getPrototypeOf(proto).hasOwnProperty('method'))
})
it('is referenced by methods in prototype chain', function () {
let method = derived.method
derived = null
gc()
assert.equal(method(), 'method')
});
})
describe('ipc.sender.send', function () {