Cleanup the code determining value's type

This commit is contained in:
Cheng Zhao 2016-04-20 14:26:49 +09:00
parent d3bff7fffc
commit ff1b7d18f6
2 changed files with 35 additions and 46 deletions

View file

@ -50,50 +50,37 @@ let getObjectPrototype = function (object) {
}
// Convert a real value into meta data.
var valueToMeta = function (sender, value, optimizeSimpleObject) {
var el, i, len, meta
if (optimizeSimpleObject == null) {
optimizeSimpleObject = false
}
meta = {
type: typeof value
}
let valueToMeta = function (sender, value, optimizeSimpleObject = false) {
// Determine the type of value.
let meta = { type: typeof value }
if (Buffer.isBuffer(value)) {
meta.type = 'buffer'
}
if (value === null) {
} else if (value === null) {
meta.type = 'value'
}
if (Array.isArray(value)) {
} else if (Array.isArray(value)) {
meta.type = 'array'
}
if (value instanceof Error) {
} else if (value instanceof Error) {
meta.type = 'error'
}
if (value instanceof Date) {
} else if (value instanceof Date) {
meta.type = 'date'
}
if ((value != null ? value.constructor.name : void 0) === 'Promise') {
meta.type = 'promise'
}
// Treat simple objects as value.
if (optimizeSimpleObject && meta.type === 'object' && v8Util.getHiddenValue(value, 'simple')) {
meta.type = 'value'
}
// Treat the arguments object as array.
if (meta.type === 'object' && (value.hasOwnProperty('callee')) && (value.length != null)) {
meta.type = 'array'
}
if (meta.type === 'array') {
meta.members = []
for (i = 0, len = value.length; i < len; i++) {
el = value[i]
meta.members.push(valueToMeta(sender, el))
} else if (meta.type === 'object') {
// Recognize certain types of objects.
if (value.constructor != null && value.constructor.name === 'Promise') {
meta.type = 'promise'
} else if (value.hasOwnProperty('callee') && value.length != null) {
// Treat the arguments object as array.
meta.type = 'array'
} else if (optimizeSimpleObject && v8Util.getHiddenValue(value, 'simple')) {
// Treat simple objects as value.
meta.type = 'value'
}
}
// Fill the meta object according to value's type.
if (meta.type === 'array') {
meta.members = value.map((el) => valueToMeta(sender, el))
} else if (meta.type === 'object' || meta.type === 'function') {
meta.name = value.constructor.name
meta.name = value.constructor ? value.constructor.name : ''
// 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

View file

@ -45,17 +45,19 @@ var wrapArgs = function (args, visited) {
type: 'date',
value: value.getTime()
}
} else if ((value != null ? value.constructor.name : void 0) === 'Promise') {
return {
type: 'promise',
then: valueToMeta(function (v) { value.then(v) })
}
} else if ((value != null) && typeof value === 'object' && v8Util.getHiddenValue(value, 'atomId')) {
return {
type: 'remote-object',
id: v8Util.getHiddenValue(value, 'atomId')
}
} else if ((value != null) && typeof value === 'object') {
if (value.constructor != null && value.constructor.name === 'Promise') {
return {
type: 'promise',
then: valueToMeta(function (v) { value.then(v) })
}
} else if (v8Util.getHiddenValue(value, 'atomId')) {
return {
type: 'remote-object',
id: v8Util.getHiddenValue(value, 'atomId')
}
}
ret = {
type: 'object',
name: value.constructor.name,