remote: handle circular reference in wrapArgs

This commit is contained in:
deepak1556 2015-06-15 19:11:14 +05:30
parent 8b8a6aea74
commit 193f95a888

View file

@ -5,18 +5,29 @@ CallbacksRegistry = require 'callbacks-registry'
callbacksRegistry = new CallbacksRegistry callbacksRegistry = new CallbacksRegistry
# Check for circular reference.
isCircular = (field, visited) ->
if typeof field is 'object'
if field in visited
return true
visited.push field
return false
# Convert the arguments object into an array of meta data. # Convert the arguments object into an array of meta data.
wrapArgs = (args) -> wrapArgs = (args, visited=[]) ->
valueToMeta = (value) -> valueToMeta = (value) ->
if Array.isArray value if Array.isArray value
type: 'array', value: wrapArgs(value) type: 'array', value: wrapArgs(value, visited)
else if Buffer.isBuffer value else if Buffer.isBuffer value
type: 'buffer', value: Array::slice.call(value, 0) type: 'buffer', value: Array::slice.call(value, 0)
else if value? and typeof value is 'object' and v8Util.getHiddenValue value, 'atomId' else if value? and typeof value is 'object' and v8Util.getHiddenValue value, 'atomId'
type: 'remote-object', id: v8Util.getHiddenValue value, 'atomId' type: 'remote-object', id: v8Util.getHiddenValue value, 'atomId'
else if value? and typeof value is 'object' else if value? and typeof value is 'object'
ret = type: 'object', name: value.constructor.name, members: [] ret = type: 'object', name: value.constructor.name, members: []
ret.members.push(name: prop, value: valueToMeta(field)) for prop, field of value for prop, field of value
ret.members.push
name: prop
value: valueToMeta(if isCircular(field, visited) then null else field)
ret ret
else if typeof value is 'function' and v8Util.getHiddenValue value, 'returnValue' else if typeof value is 'function' and v8Util.getHiddenValue value, 'returnValue'
type: 'function-with-return-value', value: valueToMeta(value()) type: 'function-with-return-value', value: valueToMeta(value())