From 12eccd73305890a3f3b38a70bb12ca3885789add Mon Sep 17 00:00:00 2001 From: Piotr Kobzda Date: Tue, 2 Jan 2018 20:25:44 +0100 Subject: [PATCH] Function as target object allowed --- src/weakref.cc | 2 +- test/create.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/weakref.cc b/src/weakref.cc index 626327e..8c921de 100644 --- a/src/weakref.cc +++ b/src/weakref.cc @@ -8,7 +8,7 @@ namespace { class ObjectInfo : public ObjectWrap { public: ObjectInfo(const CallbackInfo& args) : ObjectWrap(args) { - if (!args[0].IsObject()) + if (!args[0].IsObject() && !args[0].IsFunction()) throw Error::New(Env(), "target should be object"); if (!args[1].IsFunction()) throw Error::New(Env(), "callback should be function"); diff --git a/test/create.js b/test/create.js index ffc9308..af3b988 100644 --- a/test/create.js +++ b/test/create.js @@ -14,10 +14,29 @@ describe('create()', function() { null, undefined, 'foo', + Symbol(), ].forEach((val) => { assert.throws(function() { weak.create(val); }); }); }); + + it('should accept "object" values', function() { + [ + {}, + function() {}, + () => {}, + [], + Buffer(''), + new ArrayBuffer(10), + new Int32Array(new ArrayBuffer(12)), + Promise.resolve(), + new WeakMap(), + ].forEach((val) => { + assert.doesNotThrow(() => { + assert.ok(weak.create(val)); + }, Error, String(val)); + }); + }); });