#include #include "env-inl.h" #include "node_process.h" #include "util.h" namespace node { using v8::Context; using v8::Function; using v8::HandleScope; using v8::Isolate; using v8::Just; using v8::Local; using v8::Maybe; using v8::MaybeLocal; using v8::NewStringType; using v8::Nothing; using v8::Object; using v8::String; using v8::Value; MaybeLocal ProcessEmit(Environment* env, const char* event, Local message) { // Send message to enable debug in cluster workers Local process = env->process_object(); Isolate* isolate = env->isolate(); Local argv[] = {OneByteString(isolate, event), message}; return MakeCallback(isolate, process, "emit", arraysize(argv), argv, {0, 0}); } Maybe ProcessEmitWarningGeneric(Environment* env, const char* warning, const char* type, const char* code) { HandleScope handle_scope(env->isolate()); Context::Scope context_scope(env->context()); Local process = env->process_object(); Local emit_warning; if (!process->Get(env->context(), env->emit_warning_string()) .ToLocal(&emit_warning)) { return Nothing(); } if (!emit_warning->IsFunction()) return Just(false); int argc = 0; Local args[3]; // warning, type, code // The caller has to be able to handle a failure anyway, so we might as well // do proper error checking for string creation. if (!String::NewFromUtf8(env->isolate(), warning, NewStringType::kNormal) .ToLocal(&args[argc++])) { return Nothing(); } if (type != nullptr) { if (!String::NewFromOneByte(env->isolate(), reinterpret_cast(type), NewStringType::kNormal) .ToLocal(&args[argc++])) { return Nothing(); } if (code != nullptr && !String::NewFromOneByte(env->isolate(), reinterpret_cast(code), NewStringType::kNormal) .ToLocal(&args[argc++])) { return Nothing(); } } // MakeCallback() unneeded because emitWarning is internal code, it calls // process.emit('warning', ...), but does so on the nextTick. if (emit_warning.As() ->Call(env->context(), process, argc, args) .IsEmpty()) { return Nothing(); } return Just(true); } // Call process.emitWarning(str), fmt is a snprintf() format string Maybe ProcessEmitWarning(Environment* env, const char* fmt, ...) { char warning[1024]; va_list ap; va_start(ap, fmt); vsnprintf(warning, sizeof(warning), fmt, ap); va_end(ap); return ProcessEmitWarningGeneric(env, warning); } Maybe ProcessEmitDeprecationWarning(Environment* env, const char* warning, const char* deprecation_code) { return ProcessEmitWarningGeneric( env, warning, "DeprecationWarning", deprecation_code); } } // namespace node