Skip to content

Commit 3170636

Browse files
ExE-Bossdanielleadams
authored andcommitted
v8: fix native serdes constructors
Fixes: #13326 Refs: #13541 PR-URL: #36549 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent c844d22 commit 3170636

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

lib/v8.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const {
3535
const { Buffer } = require('buffer');
3636
const { validateString } = require('internal/validators');
3737
const {
38-
Serializer: _Serializer,
39-
Deserializer: _Deserializer
38+
Serializer,
39+
Deserializer
4040
} = internalBinding('serdes');
4141

4242
let profiler = {};
@@ -70,13 +70,6 @@ function getHeapSnapshot() {
7070
return new HeapSnapshotStream(handle);
7171
}
7272

73-
// Calling exposed c++ functions directly throws exception as it expected to be
74-
// called with new operator and caused an assert to fire.
75-
// Creating JS wrapper so that it gets caught at JS layer.
76-
class Serializer extends _Serializer { }
77-
78-
class Deserializer extends _Deserializer { }
79-
8073
const {
8174
cachedDataVersionTag,
8275
setFlagsFromString: _setFlagsFromString,

src/node_serdes.cc

+11
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ Maybe<bool> SerializerContext::WriteHostObject(Isolate* isolate,
169169

170170
void SerializerContext::New(const FunctionCallbackInfo<Value>& args) {
171171
Environment* env = Environment::GetCurrent(args);
172+
if (!args.IsConstructCall()) {
173+
return THROW_ERR_CONSTRUCT_CALL_REQUIRED(
174+
env, "Class constructor Serializer cannot be invoked without 'new'");
175+
}
172176

173177
new SerializerContext(env, args.This());
174178
}
@@ -319,6 +323,10 @@ MaybeLocal<Object> DeserializerContext::ReadHostObject(Isolate* isolate) {
319323

320324
void DeserializerContext::New(const FunctionCallbackInfo<Value>& args) {
321325
Environment* env = Environment::GetCurrent(args);
326+
if (!args.IsConstructCall()) {
327+
return THROW_ERR_CONSTRUCT_CALL_REQUIRED(
328+
env, "Class constructor Deserializer cannot be invoked without 'new'");
329+
}
322330

323331
if (!args[0]->IsArrayBufferView()) {
324332
return node::THROW_ERR_INVALID_ARG_TYPE(
@@ -470,6 +478,7 @@ void Initialize(Local<Object> target,
470478
Local<String> serializerString =
471479
FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer");
472480
ser->SetClassName(serializerString);
481+
ser->ReadOnlyPrototype();
473482
target->Set(env->context(),
474483
serializerString,
475484
ser->GetFunction(env->context()).ToLocalChecked()).Check();
@@ -496,6 +505,8 @@ void Initialize(Local<Object> target,
496505

497506
Local<String> deserializerString =
498507
FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer");
508+
des->SetLength(1);
509+
des->ReadOnlyPrototype();
499510
des->SetClassName(deserializerString);
500511
target->Set(env->context(),
501512
deserializerString,

test/parallel/test-v8-serdes.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ const objects = [
2525

2626
const hostObject = new (internalBinding('js_stream').JSStream)();
2727

28-
const serializerTypeError =
29-
/^TypeError: Class constructor Serializer cannot be invoked without 'new'$/;
30-
const deserializerTypeError =
31-
/^TypeError: Class constructor Deserializer cannot be invoked without 'new'$/;
32-
3328
{
3429
const ser = new v8.DefaultSerializer();
3530
ser.writeHeader();
@@ -186,8 +181,16 @@ const deserializerTypeError =
186181
}
187182

188183
{
189-
assert.throws(v8.Serializer, serializerTypeError);
190-
assert.throws(v8.Deserializer, deserializerTypeError);
184+
assert.throws(() => v8.Serializer(), {
185+
constructor: TypeError,
186+
message: "Class constructor Serializer cannot be invoked without 'new'",
187+
code: 'ERR_CONSTRUCT_CALL_REQUIRED'
188+
});
189+
assert.throws(() => v8.Deserializer(), {
190+
constructor: TypeError,
191+
message: "Class constructor Deserializer cannot be invoked without 'new'",
192+
code: 'ERR_CONSTRUCT_CALL_REQUIRED'
193+
});
191194
}
192195

193196

0 commit comments

Comments
 (0)