Merge pull request #6590 from electron/reliable-typed-array

Use ArrayBuffer.isView to detect Buffer and ArrayBuffer
This commit is contained in:
Cheng Zhao 2016-07-25 16:54:54 +09:00 committed by GitHub
commit 72c4952907
7 changed files with 22 additions and 43 deletions

View file

@ -44,7 +44,6 @@
'lib/common/api/deprecate.js', 'lib/common/api/deprecate.js',
'lib/common/api/deprecations.js', 'lib/common/api/deprecations.js',
'lib/common/api/is-promise.js', 'lib/common/api/is-promise.js',
'lib/common/api/is-typed-array.js',
'lib/common/api/exports/electron.js', 'lib/common/api/exports/electron.js',
'lib/common/api/native-image.js', 'lib/common/api/native-image.js',
'lib/common/api/shell.js', 'lib/common/api/shell.js',

View file

@ -2,7 +2,7 @@
const electron = require('electron') const electron = require('electron')
const v8Util = process.atomBinding('v8_util') const v8Util = process.atomBinding('v8_util')
const {ipcMain, isPromise, isTypedArray, webContents} = electron const {ipcMain, isPromise, webContents} = electron
const objectsRegistry = require('./objects-registry') const objectsRegistry = require('./objects-registry')
@ -59,12 +59,10 @@ let valueToMeta = function (sender, value, optimizeSimpleObject = false) {
// Recognize certain types of objects. // Recognize certain types of objects.
if (value === null) { if (value === null) {
meta.type = 'value' meta.type = 'value'
} else if (Buffer.isBuffer(value)) { } else if (ArrayBuffer.isView(value)) {
meta.type = 'buffer' meta.type = 'buffer'
} else if (Array.isArray(value)) { } else if (Array.isArray(value)) {
meta.type = 'array' meta.type = 'array'
} else if (isTypedArray(value)) {
meta.type = 'typed-array'
} else if (value instanceof Error) { } else if (value instanceof Error) {
meta.type = 'error' meta.type = 'error'
} else if (value instanceof Date) { } else if (value instanceof Date) {
@ -150,8 +148,6 @@ const unwrapArgs = function (sender, args) {
case 'array': case 'array':
return unwrapArgs(sender, meta.value) return unwrapArgs(sender, meta.value)
case 'buffer': case 'buffer':
return new Buffer(meta.value)
case 'typed-array':
return Buffer.from(meta.value) return Buffer.from(meta.value)
case 'date': case 'date':
return new Date(meta.value) return new Date(meta.value)

View file

@ -48,11 +48,6 @@ exports.defineProperties = function (exports) {
get: function () { get: function () {
return require('../is-promise') return require('../is-promise')
} }
},
isTypedArray: {
get: function () {
return require('../is-typed-array')
}
} }
}) })
} }

View file

@ -1,17 +0,0 @@
'use strict'
module.exports = function isTypedArray (val) {
return (
val &&
(val instanceof Int8Array ||
val instanceof Int16Array ||
val instanceof Int32Array ||
val instanceof Uint8Array ||
val instanceof Uint8ClampedArray ||
val instanceof Uint16Array ||
val instanceof Uint32Array ||
val instanceof Float32Array ||
val instanceof Float64Array) ||
(Object.prototype.toString.call(val).substr(-6, 5) === 'Array')
)
}

View file

@ -1,7 +1,7 @@
'use strict' 'use strict'
const v8Util = process.atomBinding('v8_util') const v8Util = process.atomBinding('v8_util')
const {ipcRenderer, isPromise, isTypedArray, CallbacksRegistry} = require('electron') const {ipcRenderer, isPromise, CallbacksRegistry} = require('electron')
const callbacksRegistry = new CallbacksRegistry() const callbacksRegistry = new CallbacksRegistry()
@ -30,16 +30,11 @@ const wrapArgs = function (args, visited) {
} }
visited.delete(value) visited.delete(value)
return meta return meta
} else if (Buffer.isBuffer(value)) { } else if (ArrayBuffer.isView(value)) {
return { return {
type: 'buffer', type: 'buffer',
value: Array.prototype.slice.call(value, 0) value: Array.prototype.slice.call(value, 0)
} }
} else if (isTypedArray(value)) {
return {
type: 'typed-array',
value: Array.prototype.slice.call(value)
}
} else if (value instanceof Date) { } else if (value instanceof Date) {
return { return {
type: 'date', type: 'date',
@ -168,8 +163,6 @@ const metaToValue = function (meta) {
} }
return results return results
case 'buffer': case 'buffer':
return new Buffer(meta.value)
case 'typed-array':
return Buffer.from(meta.value) return Buffer.from(meta.value)
case 'promise': case 'promise':
return Promise.resolve({ return Promise.resolve({

View file

@ -142,20 +142,25 @@ describe('ipc module', function () {
}) })
describe('remote value in browser', function () { describe('remote value in browser', function () {
var print = path.join(fixtures, 'module', 'print_name.js') const print = path.join(fixtures, 'module', 'print_name.js')
const printName = remote.require(print)
it('keeps its constructor name for objects', function () { it('keeps its constructor name for objects', function () {
var buf = new Buffer('test') const buf = new Buffer('test')
var printName = remote.require(print)
assert.equal(printName.print(buf), 'Buffer') assert.equal(printName.print(buf), 'Buffer')
}) })
it('supports instanceof Date', function () { it('supports instanceof Date', function () {
var now = new Date() const now = new Date()
var printName = remote.require(print)
assert.equal(printName.print(now), 'Date') assert.equal(printName.print(now), 'Date')
assert.deepEqual(printName.echo(now), now) assert.deepEqual(printName.echo(now), now)
}) })
it('supports TypedArray', function () {
const values = [1, 2, 3, 4]
const typedArray = printName.typedArray(values)
assert.deepEqual(values, typedArray)
})
}) })
describe('remote promise', function () { describe('remote promise', function () {

View file

@ -5,3 +5,11 @@ exports.print = function (obj) {
exports.echo = function (obj) { exports.echo = function (obj) {
return obj return obj
} }
exports.typedArray = function (name) {
const int16 = new Int16Array(name.length)
for (let i = 0; i < name.length; ++i) {
int16[i] = name[i]
}
return int16
}