v8: add a js class for Serializer/Dserializer

Calling Serializer/Deserializer without new crashes node.
Adding a js class which just inherits cpp bindings.
Added regression tests.

Fixes: https://github.com/nodejs/node/issues/13326
PR-URL: https://github.com/nodejs/node/pull/13541
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Rajaram Gaunker 2017-06-08 01:17:02 -07:00 committed by Anna Henningsen
parent e2617a0388
commit 12fd63d6bb
No known key found for this signature in database
GPG Key ID: D8B9F5AEAE84E4CF
2 changed files with 23 additions and 1 deletions

View File

@ -15,11 +15,21 @@
'use strict';
const { Buffer } = require('buffer');
const { Serializer, Deserializer } = process.binding('serdes');
const {
Serializer: _Serializer,
Deserializer: _Deserializer
} = process.binding('serdes');
const { copy } = process.binding('buffer');
const { objectToString } = require('internal/util');
const { FastBuffer } = require('internal/buffer');
// Calling exposed c++ functions directly throws exception as it expected to be
// called with new operator and caused an assert to fire.
// Creating JS wrapper so that it gets caught at JS layer.
class Serializer extends _Serializer { }
class Deserializer extends _Deserializer { }
const {
cachedDataVersionTag,
setFlagsFromString,

View File

@ -131,3 +131,15 @@ const objects = [
assert.deepStrictEqual(v8.deserialize(buf), expectedResult);
}
{
assert.throws(
() => { v8.Serializer(); },
/^TypeError: Class constructor Serializer cannot be invoked without 'new'$/
);
assert.throws(
() => { v8.Deserializer(); },
/^TypeError: Class constructor Deserializer cannot be invoked without 'new'$/
);
}