Skip to content

Commit 2a4998c

Browse files
bnoordhuisjasnell
authored andcommitted
src: don't set non-primitive values on templates
V8 is going to disallow non-primitive values on v8::FunctionTemplate and v8::ObjectTemplate because those can't be shared across contexts. Fixes: #6216 PR-URL: #6228 Reviewed-By: Trevor Norris <[email protected]>
1 parent ba63e40 commit 2a4998c

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

lib/_http_common.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict';
22

3-
const FreeList = require('internal/freelist').FreeList;
4-
const HTTPParser = process.binding('http_parser').HTTPParser;
3+
const binding = process.binding('http_parser');
4+
const methods = binding.methods;
5+
const HTTPParser = binding.HTTPParser;
56

7+
const FreeList = require('internal/freelist').FreeList;
68
const incoming = require('_http_incoming');
79
const IncomingMessage = incoming.IncomingMessage;
810
const readStart = incoming.readStart;
@@ -14,7 +16,7 @@ exports.debug = debug;
1416
exports.CRLF = '\r\n';
1517
exports.chunkExpression = /chunk/i;
1618
exports.continueExpression = /100-continue/i;
17-
exports.methods = HTTPParser.methods;
19+
exports.methods = methods;
1820

1921
const kOnHeaders = HTTPParser.kOnHeaders | 0;
2022
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
@@ -71,7 +73,7 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
7173

7274
if (typeof method === 'number') {
7375
// server only
74-
parser.incoming.method = HTTPParser.methods[method];
76+
parser.incoming.method = methods[method];
7577
} else {
7678
// client only
7779
parser.incoming.statusCode = statusCode;

src/env-inl.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -507,27 +507,25 @@ inline void Environment::SetProtoMethod(v8::Local<v8::FunctionTemplate> that,
507507
const char* name,
508508
v8::FunctionCallback callback) {
509509
v8::Local<v8::Signature> signature = v8::Signature::New(isolate(), that);
510-
v8::Local<v8::Function> function =
511-
NewFunctionTemplate(callback, signature)->GetFunction();
510+
v8::Local<v8::FunctionTemplate> t = NewFunctionTemplate(callback, signature);
512511
// kInternalized strings are created in the old space.
513512
const v8::NewStringType type = v8::NewStringType::kInternalized;
514513
v8::Local<v8::String> name_string =
515514
v8::String::NewFromUtf8(isolate(), name, type).ToLocalChecked();
516-
that->PrototypeTemplate()->Set(name_string, function);
517-
function->SetName(name_string); // NODE_SET_PROTOTYPE_METHOD() compatibility.
515+
that->PrototypeTemplate()->Set(name_string, t);
516+
t->SetClassName(name_string); // NODE_SET_PROTOTYPE_METHOD() compatibility.
518517
}
519518

520519
inline void Environment::SetTemplateMethod(v8::Local<v8::FunctionTemplate> that,
521520
const char* name,
522521
v8::FunctionCallback callback) {
523-
v8::Local<v8::Function> function =
524-
NewFunctionTemplate(callback)->GetFunction();
522+
v8::Local<v8::FunctionTemplate> t = NewFunctionTemplate(callback);
525523
// kInternalized strings are created in the old space.
526524
const v8::NewStringType type = v8::NewStringType::kInternalized;
527525
v8::Local<v8::String> name_string =
528526
v8::String::NewFromUtf8(isolate(), name, type).ToLocalChecked();
529-
that->Set(name_string, function);
530-
function->SetName(name_string); // NODE_SET_METHOD() compatibility.
527+
that->Set(name_string, t);
528+
t->SetClassName(name_string); // NODE_SET_METHOD() compatibility.
531529
}
532530

533531
inline v8::Local<v8::Object> Environment::NewInternalFieldObject() {

src/node.h

+16-5
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,20 @@ NODE_EXTERN void RunAtExit(Environment* env);
240240
while (0)
241241

242242
// Used to be a macro, hence the uppercase name.
243-
template <typename TypeName>
244-
inline void NODE_SET_METHOD(const TypeName& recv,
243+
inline void NODE_SET_METHOD(v8::Local<v8::Template> recv,
244+
const char* name,
245+
v8::FunctionCallback callback) {
246+
v8::Isolate* isolate = v8::Isolate::GetCurrent();
247+
v8::HandleScope handle_scope(isolate);
248+
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
249+
callback);
250+
v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
251+
t->SetClassName(fn_name);
252+
recv->Set(fn_name, t);
253+
}
254+
255+
// Used to be a macro, hence the uppercase name.
256+
inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
245257
const char* name,
246258
v8::FunctionCallback callback) {
247259
v8::Isolate* isolate = v8::Isolate::GetCurrent();
@@ -265,10 +277,9 @@ inline void NODE_SET_PROTOTYPE_METHOD(v8::Local<v8::FunctionTemplate> recv,
265277
v8::Local<v8::Signature> s = v8::Signature::New(isolate, recv);
266278
v8::Local<v8::FunctionTemplate> t =
267279
v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), s);
268-
v8::Local<v8::Function> fn = t->GetFunction();
269-
recv->PrototypeTemplate()->Set(v8::String::NewFromUtf8(isolate, name), fn);
270280
v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
271-
fn->SetName(fn_name);
281+
t->SetClassName(fn_name);
282+
recv->PrototypeTemplate()->Set(v8::String::NewFromUtf8(isolate, name), t);
272283
}
273284
#define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD
274285

src/node_http_parser.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ void InitHttpParser(Local<Object> target,
759759
methods->Set(num, FIXED_ONE_BYTE_STRING(env->isolate(), #string));
760760
HTTP_METHOD_MAP(V)
761761
#undef V
762-
t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "methods"), methods);
762+
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "methods"), methods);
763763

764764
env->SetProtoMethod(t, "close", Parser::Close);
765765
env->SetProtoMethod(t, "execute", Parser::Execute);

test/parallel/test-http-parser.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
require('../common');
33
var assert = require('assert');
44

5-
var HTTPParser = process.binding('http_parser').HTTPParser;
5+
const binding = process.binding('http_parser');
6+
const methods = binding.methods;
7+
const HTTPParser = binding.HTTPParser;
68

79
var CRLF = '\r\n';
810
var REQUEST = HTTPParser.REQUEST;
911
var RESPONSE = HTTPParser.RESPONSE;
1012

11-
var methods = HTTPParser.methods;
12-
1313
var kOnHeaders = HTTPParser.kOnHeaders | 0;
1414
var kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
1515
var kOnBody = HTTPParser.kOnBody | 0;

0 commit comments

Comments
 (0)