From eb51e080e538116d592d00249545a07da54a731e Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 25 Jul 2016 16:30:40 +0900 Subject: [PATCH 1/4] spec: TypeArray should work in remote --- spec/api-ipc-spec.js | 15 ++++++++++----- spec/fixtures/module/print_name.js | 8 ++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/spec/api-ipc-spec.js b/spec/api-ipc-spec.js index bd1a7e3d86b..01ab4fa0ff2 100644 --- a/spec/api-ipc-spec.js +++ b/spec/api-ipc-spec.js @@ -142,20 +142,25 @@ describe('ipc module', 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 () { - var buf = new Buffer('test') - var printName = remote.require(print) + const buf = new Buffer('test') assert.equal(printName.print(buf), 'Buffer') }) it('supports instanceof Date', function () { - var now = new Date() - var printName = remote.require(print) + const now = new Date() assert.equal(printName.print(now), 'Date') assert.deepEqual(printName.echo(now), now) }) + + it('supports TypedArray', function () { + const values = [1, 2, 3, 4] + const typedArray = printName.typedArray(values) + assert.equal(values.toString(), typedArray.toString()) + }) }) describe('remote promise', function () { diff --git a/spec/fixtures/module/print_name.js b/spec/fixtures/module/print_name.js index db4f71d407b..fff59200eda 100644 --- a/spec/fixtures/module/print_name.js +++ b/spec/fixtures/module/print_name.js @@ -5,3 +5,11 @@ exports.print = function (obj) { exports.echo = function (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 +} From 1c9421bc89a4b3ec909bf519d03ea16edad33cab Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 25 Jul 2016 16:39:09 +0900 Subject: [PATCH 2/4] Use ArrayBuffer.isView to detect Buffer and ArrayBuffer --- filenames.gypi | 1 - lib/browser/rpc-server.js | 8 ++------ lib/common/api/exports/electron.js | 5 ----- lib/common/api/is-typed-array.js | 17 ----------------- lib/renderer/api/remote.js | 11 ++--------- 5 files changed, 4 insertions(+), 38 deletions(-) delete mode 100644 lib/common/api/is-typed-array.js diff --git a/filenames.gypi b/filenames.gypi index d5381e15b30..a3a4b6c808f 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -44,7 +44,6 @@ 'lib/common/api/deprecate.js', 'lib/common/api/deprecations.js', 'lib/common/api/is-promise.js', - 'lib/common/api/is-typed-array.js', 'lib/common/api/exports/electron.js', 'lib/common/api/native-image.js', 'lib/common/api/shell.js', diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index 6e9d80ed21a..1fd20bf6e64 100644 --- a/lib/browser/rpc-server.js +++ b/lib/browser/rpc-server.js @@ -2,7 +2,7 @@ const electron = require('electron') const v8Util = process.atomBinding('v8_util') -const {ipcMain, isPromise, isTypedArray, webContents} = electron +const {ipcMain, isPromise, webContents} = electron const objectsRegistry = require('./objects-registry') @@ -59,12 +59,10 @@ let valueToMeta = function (sender, value, optimizeSimpleObject = false) { // Recognize certain types of objects. if (value === null) { meta.type = 'value' - } else if (Buffer.isBuffer(value)) { + } else if (ArrayBuffer.isView(value)) { meta.type = 'buffer' } else if (Array.isArray(value)) { meta.type = 'array' - } else if (isTypedArray(value)) { - meta.type = 'typed-array' } else if (value instanceof Error) { meta.type = 'error' } else if (value instanceof Date) { @@ -151,8 +149,6 @@ const unwrapArgs = function (sender, args) { return unwrapArgs(sender, meta.value) case 'buffer': return new Buffer(meta.value) - case 'typed-array': - return Buffer.from(meta.value) case 'date': return new Date(meta.value) case 'promise': diff --git a/lib/common/api/exports/electron.js b/lib/common/api/exports/electron.js index 4539649c7fa..efa44d452cc 100644 --- a/lib/common/api/exports/electron.js +++ b/lib/common/api/exports/electron.js @@ -48,11 +48,6 @@ exports.defineProperties = function (exports) { get: function () { return require('../is-promise') } - }, - isTypedArray: { - get: function () { - return require('../is-typed-array') - } } }) } diff --git a/lib/common/api/is-typed-array.js b/lib/common/api/is-typed-array.js deleted file mode 100644 index 8b63a0cb1b7..00000000000 --- a/lib/common/api/is-typed-array.js +++ /dev/null @@ -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') - ) -} diff --git a/lib/renderer/api/remote.js b/lib/renderer/api/remote.js index ae6564fe0f2..960044cc6c3 100644 --- a/lib/renderer/api/remote.js +++ b/lib/renderer/api/remote.js @@ -1,7 +1,7 @@ 'use strict' const v8Util = process.atomBinding('v8_util') -const {ipcRenderer, isPromise, isTypedArray, CallbacksRegistry} = require('electron') +const {ipcRenderer, isPromise, CallbacksRegistry} = require('electron') const callbacksRegistry = new CallbacksRegistry() @@ -30,16 +30,11 @@ const wrapArgs = function (args, visited) { } visited.delete(value) return meta - } else if (Buffer.isBuffer(value)) { + } else if (ArrayBuffer.isView(value)) { return { type: 'buffer', 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) { return { type: 'date', @@ -169,8 +164,6 @@ const metaToValue = function (meta) { return results case 'buffer': return new Buffer(meta.value) - case 'typed-array': - return Buffer.from(meta.value) case 'promise': return Promise.resolve({ then: metaToValue(meta.then) From 9ddb62c42a79798f1e719ec2c5d3c5304299a777 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 25 Jul 2016 16:40:22 +0900 Subject: [PATCH 3/4] Use Buffer.from instead of deprecated new Buffer --- lib/browser/rpc-server.js | 2 +- lib/renderer/api/remote.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index 1fd20bf6e64..d240561837e 100644 --- a/lib/browser/rpc-server.js +++ b/lib/browser/rpc-server.js @@ -148,7 +148,7 @@ const unwrapArgs = function (sender, args) { case 'array': return unwrapArgs(sender, meta.value) case 'buffer': - return new Buffer(meta.value) + return Buffer.from(meta.value) case 'date': return new Date(meta.value) case 'promise': diff --git a/lib/renderer/api/remote.js b/lib/renderer/api/remote.js index 960044cc6c3..20d8ea574cb 100644 --- a/lib/renderer/api/remote.js +++ b/lib/renderer/api/remote.js @@ -163,7 +163,7 @@ const metaToValue = function (meta) { } return results case 'buffer': - return new Buffer(meta.value) + return Buffer.from(meta.value) case 'promise': return Promise.resolve({ then: metaToValue(meta.then) From 7a6196642340be0620a9366d5fd0cfa2e16ab6d8 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 25 Jul 2016 16:41:20 +0900 Subject: [PATCH 4/4] Use deepEqual for comparing type arrays --- spec/api-ipc-spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/api-ipc-spec.js b/spec/api-ipc-spec.js index 01ab4fa0ff2..83413e08f06 100644 --- a/spec/api-ipc-spec.js +++ b/spec/api-ipc-spec.js @@ -159,7 +159,7 @@ describe('ipc module', function () { it('supports TypedArray', function () { const values = [1, 2, 3, 4] const typedArray = printName.typedArray(values) - assert.equal(values.toString(), typedArray.toString()) + assert.deepEqual(values, typedArray) }) })