@@ -147,12 +147,15 @@ using v8::HandleScope;
147
147
using v8::HeapStatistics;
148
148
using v8::Integer;
149
149
using v8::Isolate;
150
+ using v8::Just;
150
151
using v8::Local;
151
152
using v8::Locker;
153
+ using v8::Maybe;
152
154
using v8::MaybeLocal;
153
155
using v8::Message;
154
156
using v8::Name;
155
157
using v8::NamedPropertyHandlerConfiguration;
158
+ using v8::Nothing;
156
159
using v8::Null;
157
160
using v8::Number;
158
161
using v8::Object;
@@ -2277,8 +2280,11 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
2277
2280
}
2278
2281
if (mp->nm_version == -1 ) {
2279
2282
if (env->EmitNapiWarning ()) {
2280
- ProcessEmitWarning (env, " N-API is an experimental feature and could "
2281
- " change at any time." );
2283
+ if (ProcessEmitWarning (env, " N-API is an experimental feature and could "
2284
+ " change at any time." ).IsNothing ()) {
2285
+ dlib.Close ();
2286
+ return ;
2287
+ }
2282
2288
}
2283
2289
} else if (mp->nm_version != NODE_MODULE_VERSION) {
2284
2290
char errmsg[1024 ];
@@ -2425,33 +2431,83 @@ static void OnMessage(Local<Message> message, Local<Value> error) {
2425
2431
FatalException (Isolate::GetCurrent (), error, message);
2426
2432
}
2427
2433
2434
+ static Maybe<bool > ProcessEmitWarningGeneric (Environment* env,
2435
+ const char * warning,
2436
+ const char * type = nullptr ,
2437
+ const char * code = nullptr ) {
2438
+ HandleScope handle_scope (env->isolate ());
2439
+ Context::Scope context_scope (env->context ());
2440
+
2441
+ Local<Object> process = env->process_object ();
2442
+ Local<Value> emit_warning;
2443
+ if (!process->Get (env->context (),
2444
+ env->emit_warning_string ()).ToLocal (&emit_warning)) {
2445
+ return Nothing<bool >();
2446
+ }
2447
+
2448
+ if (!emit_warning->IsFunction ()) return Just (false );
2449
+
2450
+ int argc = 0 ;
2451
+ Local<Value> args[3 ]; // warning, type, code
2452
+
2453
+ // The caller has to be able to handle a failure anyway, so we might as well
2454
+ // do proper error checking for string creation.
2455
+ if (!String::NewFromUtf8 (env->isolate (),
2456
+ warning,
2457
+ v8::NewStringType::kNormal ).ToLocal (&args[argc++])) {
2458
+ return Nothing<bool >();
2459
+ }
2460
+ if (type != nullptr ) {
2461
+ if (!String::NewFromOneByte (env->isolate (),
2462
+ reinterpret_cast <const uint8_t *>(type),
2463
+ v8::NewStringType::kNormal )
2464
+ .ToLocal (&args[argc++])) {
2465
+ return Nothing<bool >();
2466
+ }
2467
+ if (code != nullptr &&
2468
+ !String::NewFromOneByte (env->isolate (),
2469
+ reinterpret_cast <const uint8_t *>(code),
2470
+ v8::NewStringType::kNormal )
2471
+ .ToLocal (&args[argc++])) {
2472
+ return Nothing<bool >();
2473
+ }
2474
+ }
2475
+
2476
+ // MakeCallback() unneeded because emitWarning is internal code, it calls
2477
+ // process.emit('warning', ...), but does so on the nextTick.
2478
+ if (emit_warning.As <Function>()->Call (env->context (),
2479
+ process,
2480
+ argc,
2481
+ args).IsEmpty ()) {
2482
+ return Nothing<bool >();
2483
+ }
2484
+ return Just (true );
2485
+ }
2486
+
2487
+
2428
2488
// Call process.emitWarning(str), fmt is a snprintf() format string
2429
- void ProcessEmitWarning (Environment* env, const char * fmt, ...) {
2489
+ Maybe< bool > ProcessEmitWarning (Environment* env, const char * fmt, ...) {
2430
2490
char warning[1024 ];
2431
2491
va_list ap;
2432
2492
2433
2493
va_start (ap, fmt);
2434
2494
vsnprintf (warning, sizeof (warning), fmt, ap);
2435
2495
va_end (ap);
2436
2496
2437
- HandleScope handle_scope (env->isolate ());
2438
- Context::Scope context_scope (env->context ());
2439
-
2440
- Local<Object> process = env->process_object ();
2441
- MaybeLocal<Value> emit_warning = process->Get (env->context (),
2442
- FIXED_ONE_BYTE_STRING (env->isolate (), " emitWarning" ));
2443
- Local<Value> arg = node::OneByteString (env->isolate (), warning);
2444
-
2445
- Local<Value> f;
2497
+ return ProcessEmitWarningGeneric (env, warning);
2498
+ }
2446
2499
2447
- if (!emit_warning.ToLocal (&f)) return ;
2448
- if (!f->IsFunction ()) return ;
2449
2500
2450
- // MakeCallback() unneeded, because emitWarning is internal code, it calls
2451
- // process.emit('warning', ..), but does so on the nextTick.
2452
- f.As <v8::Function>()->Call (process, 1 , &arg);
2501
+ Maybe<bool > ProcessEmitDeprecationWarning (Environment* env,
2502
+ const char * warning,
2503
+ const char * deprecation_code) {
2504
+ return ProcessEmitWarningGeneric (env,
2505
+ warning,
2506
+ " DeprecationWarning" ,
2507
+ deprecation_code);
2453
2508
}
2454
2509
2510
+
2455
2511
static bool PullFromCache (Environment* env,
2456
2512
const FunctionCallbackInfo<Value>& args,
2457
2513
Local<String> module ,
0 commit comments