From 818738ce840be9e6c0463fe43dd40d392b5f6168 Mon Sep 17 00:00:00 2001 From: Todd Wolfson Date: Fri, 17 Mar 2017 08:21:37 -0700 Subject: [PATCH 1/3] :bug: Add toString support to remote functions --- lib/renderer/api/remote.js | 3 +++ spec/api-ipc-spec.js | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/lib/renderer/api/remote.js b/lib/renderer/api/remote.js index 8277e16c2182..60a34785be75 100644 --- a/lib/renderer/api/remote.js +++ b/lib/renderer/api/remote.js @@ -175,6 +175,9 @@ const proxyFunctionProperties = function (remoteMemberFunction, metaId, name) { return true }, get: (target, property, receiver) => { + if (property === 'toString' && typeof target.toString === 'function') { + return target.toString.bind(target) + } if (!target.hasOwnProperty(property)) loadRemoteProperties() return target[property] }, diff --git a/spec/api-ipc-spec.js b/spec/api-ipc-spec.js index e812eb5fef2a..f86d06507c1b 100644 --- a/spec/api-ipc-spec.js +++ b/spec/api-ipc-spec.js @@ -161,6 +161,11 @@ describe('ipc module', function () { assert.equal(typeof remote.clipboard.readText, 'function') assert.equal(typeof remote.shell.openExternal, 'function') }) + + it('returns toString() of original function via toString()', function () { + var readText = remote.clipboard.readText + assert(readText.toString().startsWith('function')) + }) }) describe('remote object in renderer', function () { From 648d3324fb84fd243850f55ea1a22f5499a76527 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 17 Mar 2017 10:28:23 -0700 Subject: [PATCH 2/3] Add spec for remote function with toString property --- spec/api-ipc-spec.js | 5 ++++- spec/fixtures/module/to-string-non-function.js | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/module/to-string-non-function.js diff --git a/spec/api-ipc-spec.js b/spec/api-ipc-spec.js index f86d06507c1b..47f2eef21685 100644 --- a/spec/api-ipc-spec.js +++ b/spec/api-ipc-spec.js @@ -163,8 +163,11 @@ describe('ipc module', function () { }) it('returns toString() of original function via toString()', function () { - var readText = remote.clipboard.readText + const {readText} = remote.clipboard assert(readText.toString().startsWith('function')) + + var {functionWithToStringProperty} = remote.require(path.join(fixtures, 'module', 'to-string-non-function.js')) + assert.equal(functionWithToStringProperty.toString, 'hello') }) }) diff --git a/spec/fixtures/module/to-string-non-function.js b/spec/fixtures/module/to-string-non-function.js new file mode 100644 index 000000000000..3e91921037c5 --- /dev/null +++ b/spec/fixtures/module/to-string-non-function.js @@ -0,0 +1,4 @@ +function hello () { +} +hello.toString = 'hello' +module.exports = {functionWithToStringProperty: hello} From c50b5184933b764ecb5bef7641a9a9db8980f9db Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 17 Mar 2017 10:29:07 -0700 Subject: [PATCH 3/3] Check toString after loading remote properties --- lib/renderer/api/remote.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/renderer/api/remote.js b/lib/renderer/api/remote.js index 60a34785be75..5059dd69f37d 100644 --- a/lib/renderer/api/remote.js +++ b/lib/renderer/api/remote.js @@ -175,11 +175,16 @@ const proxyFunctionProperties = function (remoteMemberFunction, metaId, name) { return true }, get: (target, property, receiver) => { - if (property === 'toString' && typeof target.toString === 'function') { - return target.toString.bind(target) - } if (!target.hasOwnProperty(property)) loadRemoteProperties() - return target[property] + const value = target[property] + + // Bind toString to target if it is a function to avoid + // Function.prototype.toString is not generic errors + if (property === 'toString' && typeof value === 'function') { + return value.bind(target) + } + + return value }, ownKeys: (target) => { loadRemoteProperties()