Skip to content

Commit c16ebdd

Browse files
src: add setTitle() api reusing process.title path
Add process.setTitle(title) as a new API while reusing the existing process.title assignment behavior instead of introducing a separate native code path. Signed-off-by: Jonathan Lopes <jonathan15989@protonmail.com>
1 parent 5c3ade4 commit c16ebdd

File tree

5 files changed

+14
-17
lines changed

5 files changed

+14
-17
lines changed

doc/api/process.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4332,8 +4332,6 @@ const { setTitle } = require('node:process');
43324332
setTitle('my-service');
43334333
```
43344334
4335-
This function is not supported in [`Worker`][] threads.
4336-
43374335
## `process.traceDeprecation`
43384336
43394337
<!-- YAML

lib/internal/bootstrap/switches/does_not_own_process_state.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { unavailable } = require('internal/process/worker_thread_only');
77

88
process.abort = unavailable('process.abort()');
99
process.chdir = unavailable('process.chdir()');
10-
process.setTitle = unavailable('process.setTitle()');
10+
process.setTitle = wrappedSetTitle;
1111
process.umask = wrappedUmask;
1212
process.cwd = rawMethods.cwd;
1313

@@ -28,6 +28,9 @@ const {
2828
ERR_WORKER_UNSUPPORTED_OPERATION,
2929
},
3030
} = require('internal/errors');
31+
const {
32+
validateString,
33+
} = require('internal/validators');
3134

3235
function wrappedUmask(mask) {
3336
// process.umask() is a read-only operation in workers.
@@ -37,3 +40,8 @@ function wrappedUmask(mask) {
3740

3841
return rawMethods.umask(mask);
3942
}
43+
44+
function wrappedSetTitle(title) {
45+
validateString(title, 'title');
46+
process.title = title;
47+
}

lib/internal/bootstrap/switches/does_own_process_state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,5 @@ function wrappedCwd() {
146146

147147
function wrappedSetTitle(title) {
148148
validateString(title, 'title');
149-
rawMethods.setTitle(title);
149+
process.title = title;
150150
}

src/node_process_methods.cc

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,6 @@ static void Chdir(const FunctionCallbackInfo<Value>& args) {
100100
}
101101
}
102102

103-
static void SetTitle(const FunctionCallbackInfo<Value>& args) {
104-
Environment* env = Environment::GetCurrent(args);
105-
CHECK(env->owns_process_state());
106-
107-
CHECK_EQ(args.Length(), 1);
108-
CHECK(args[0]->IsString());
109-
Utf8Value title(env->isolate(), args[0]);
110-
uv_set_process_title(*title);
111-
}
112-
113103
inline Local<ArrayBuffer> get_fields_array_buffer(
114104
const FunctionCallbackInfo<Value>& args,
115105
size_t index,
@@ -781,7 +771,6 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
781771
SetMethod(isolate, target, "abort", Abort);
782772
SetMethod(isolate, target, "causeSegfault", CauseSegfault);
783773
SetMethod(isolate, target, "chdir", Chdir);
784-
SetMethod(isolate, target, "setTitle", SetTitle);
785774

786775
SetMethod(isolate, target, "umask", Umask);
787776
SetMethod(isolate, target, "memoryUsage", MemoryUsage);
@@ -830,7 +819,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
830819
registry->Register(Abort);
831820
registry->Register(CauseSegfault);
832821
registry->Register(Chdir);
833-
registry->Register(SetTitle);
834822

835823
registry->Register(Umask);
836824
registry->Register(RawDebug);

test/parallel/test-worker-unsupported-things.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ if (!process.env.HAS_STARTED_WORKER) {
1717
const after = before + ' in worker';
1818
process.title = after;
1919
assert.strictEqual(process.title, after);
20+
21+
process.setTitle(`${after} via function`);
22+
assert.strictEqual(process.title, `${after} via function`);
2023
}
2124

2225
{
@@ -34,7 +37,7 @@ if (!process.env.HAS_STARTED_WORKER) {
3437
});
3538
}
3639

37-
const stubs = ['abort', 'chdir', 'setTitle', 'send', 'disconnect'];
40+
const stubs = ['abort', 'chdir', 'send', 'disconnect'];
3841

3942
if (!common.isWindows) {
4043
stubs.push('setuid', 'seteuid', 'setgid',

0 commit comments

Comments
 (0)