Skip to content

Commit 213134f

Browse files
committed
src: add wrapper for process.emitWarning()
PR-URL: #9139 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Fedor Indutny <[email protected]>
1 parent d71dd9b commit 213134f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/node.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,6 +2562,33 @@ void ClearFatalExceptionHandlers(Environment* env) {
25622562
Undefined(env->isolate())).FromJust();
25632563
}
25642564

2565+
// Call process.emitWarning(str), fmt is a snprintf() format string
2566+
void ProcessEmitWarning(Environment* env, const char* fmt, ...) {
2567+
char warning[1024];
2568+
va_list ap;
2569+
2570+
va_start(ap, fmt);
2571+
vsnprintf(warning, sizeof(warning), fmt, ap);
2572+
va_end(ap);
2573+
2574+
HandleScope handle_scope(env->isolate());
2575+
Context::Scope context_scope(env->context());
2576+
2577+
Local<Object> process = env->process_object();
2578+
MaybeLocal<Value> emit_warning = process->Get(env->context(),
2579+
FIXED_ONE_BYTE_STRING(env->isolate(), "emitWarning"));
2580+
Local<Value> arg = node::OneByteString(env->isolate(), warning);
2581+
2582+
Local<Value> f;
2583+
2584+
if (!emit_warning.ToLocal(&f)) return;
2585+
if (!f->IsFunction()) return;
2586+
2587+
// MakeCallback() unneeded, because emitWarning is internal code, it calls
2588+
// process.emit('warning', ..), but does so on the nextTick.
2589+
f.As<v8::Function>()->Call(process, 1, &arg);
2590+
}
2591+
25652592

25662593
static void Binding(const FunctionCallbackInfo<Value>& args) {
25672594
Environment* env = Environment::GetCurrent(args);

src/node_internals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ void AppendExceptionLine(Environment* env,
129129

130130
NO_RETURN void FatalError(const char* location, const char* message);
131131

132+
void ProcessEmitWarning(Environment* env, const char* fmt, ...);
133+
132134
v8::Local<v8::Value> BuildStatsObject(Environment* env, const uv_stat_t* s);
133135

134136
void SetupProcessObject(Environment* env,

0 commit comments

Comments
 (0)