Skip to content

Commit d4e160c

Browse files
sam-githubMylesBorins
authored andcommitted
src: add wrapper for process.emitWarning()
PR-URL: #9139 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Fedor Indutny <[email protected]>
1 parent 553a326 commit d4e160c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/node.cc

+27
Original file line numberDiff line numberDiff line change
@@ -2563,6 +2563,33 @@ void ClearFatalExceptionHandlers(Environment* env) {
25632563
Undefined(env->isolate())).FromJust();
25642564
}
25652565

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

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

src/node_internals.h

+2
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)