Skip to content

Commit 1935625

Browse files
committed
src: disallow constructor behaviour for native methods
Disallow constructor behaviour and setting up prototypes for native methods that are not constructors (i.e. make them behave like ES6 class methods). PR-URL: #26700 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 11f8024 commit 1935625

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/env-inl.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -883,9 +883,7 @@ inline void Environment::SetMethod(v8::Local<v8::Object> that,
883883
v8::Local<v8::Context> context = isolate()->GetCurrentContext();
884884
v8::Local<v8::Function> function =
885885
NewFunctionTemplate(callback, v8::Local<v8::Signature>(),
886-
// TODO(TimothyGu): Investigate if SetMethod is ever
887-
// used for constructors.
888-
v8::ConstructorBehavior::kAllow,
886+
v8::ConstructorBehavior::kThrow,
889887
v8::SideEffectType::kHasSideEffect)
890888
->GetFunction(context)
891889
.ToLocalChecked();
@@ -903,9 +901,7 @@ inline void Environment::SetMethodNoSideEffect(v8::Local<v8::Object> that,
903901
v8::Local<v8::Context> context = isolate()->GetCurrentContext();
904902
v8::Local<v8::Function> function =
905903
NewFunctionTemplate(callback, v8::Local<v8::Signature>(),
906-
// TODO(TimothyGu): Investigate if SetMethod is ever
907-
// used for constructors.
908-
v8::ConstructorBehavior::kAllow,
904+
v8::ConstructorBehavior::kThrow,
909905
v8::SideEffectType::kHasNoSideEffect)
910906
->GetFunction(context)
911907
.ToLocalChecked();

test/parallel/test-process-chdir.js

+5
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ const err = {
4242
};
4343
common.expectsError(function() { process.chdir({}); }, err);
4444
common.expectsError(function() { process.chdir(); }, err);
45+
46+
// Check that our built-in methods do not have a prototype/constructor behaviour
47+
// if they don't need to. This could be tested for any of our C++ methods.
48+
assert.strictEqual(process.cwd.prototype, undefined);
49+
assert.throws(() => new process.cwd(), TypeError);

0 commit comments

Comments
 (0)