@@ -148,12 +148,15 @@ using v8::HandleScope;
148
148
using v8::HeapStatistics;
149
149
using v8::Integer;
150
150
using v8::Isolate;
151
+ using v8::Just;
151
152
using v8::Local;
152
153
using v8::Locker;
154
+ using v8::Maybe;
153
155
using v8::MaybeLocal;
154
156
using v8::Message;
155
157
using v8::Name;
156
158
using v8::NamedPropertyHandlerConfiguration;
159
+ using v8::Nothing;
157
160
using v8::Null;
158
161
using v8::Number;
159
162
using v8::Object;
@@ -2605,8 +2608,11 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
2605
2608
}
2606
2609
if (mp->nm_version == -1 ) {
2607
2610
if (env->EmitNapiWarning ()) {
2608
- ProcessEmitWarning (env, " N-API is an experimental feature and could "
2609
- " change at any time." );
2611
+ if (ProcessEmitWarning (env, " N-API is an experimental feature and could "
2612
+ " change at any time." ).IsNothing ()) {
2613
+ dlib.Close ();
2614
+ return ;
2615
+ }
2610
2616
}
2611
2617
} else if (mp->nm_version != NODE_MODULE_VERSION) {
2612
2618
char errmsg[1024 ];
@@ -2753,33 +2759,83 @@ static void OnMessage(Local<Message> message, Local<Value> error) {
2753
2759
FatalException (Isolate::GetCurrent (), error, message);
2754
2760
}
2755
2761
2762
+ static Maybe<bool > ProcessEmitWarningGeneric (Environment* env,
2763
+ const char * warning,
2764
+ const char * type = nullptr ,
2765
+ const char * code = nullptr ) {
2766
+ HandleScope handle_scope (env->isolate ());
2767
+ Context::Scope context_scope (env->context ());
2768
+
2769
+ Local<Object> process = env->process_object ();
2770
+ Local<Value> emit_warning;
2771
+ if (!process->Get (env->context (),
2772
+ env->emit_warning_string ()).ToLocal (&emit_warning)) {
2773
+ return Nothing<bool >();
2774
+ }
2775
+
2776
+ if (!emit_warning->IsFunction ()) return Just (false );
2777
+
2778
+ int argc = 0 ;
2779
+ Local<Value> args[3 ]; // warning, type, code
2780
+
2781
+ // The caller has to be able to handle a failure anyway, so we might as well
2782
+ // do proper error checking for string creation.
2783
+ if (!String::NewFromUtf8 (env->isolate (),
2784
+ warning,
2785
+ v8::NewStringType::kNormal ).ToLocal (&args[argc++])) {
2786
+ return Nothing<bool >();
2787
+ }
2788
+ if (type != nullptr ) {
2789
+ if (!String::NewFromOneByte (env->isolate (),
2790
+ reinterpret_cast <const uint8_t *>(type),
2791
+ v8::NewStringType::kNormal )
2792
+ .ToLocal (&args[argc++])) {
2793
+ return Nothing<bool >();
2794
+ }
2795
+ if (code != nullptr &&
2796
+ !String::NewFromOneByte (env->isolate (),
2797
+ reinterpret_cast <const uint8_t *>(code),
2798
+ v8::NewStringType::kNormal )
2799
+ .ToLocal (&args[argc++])) {
2800
+ return Nothing<bool >();
2801
+ }
2802
+ }
2803
+
2804
+ // MakeCallback() unneeded because emitWarning is internal code, it calls
2805
+ // process.emit('warning', ...), but does so on the nextTick.
2806
+ if (emit_warning.As <Function>()->Call (env->context (),
2807
+ process,
2808
+ argc,
2809
+ args).IsEmpty ()) {
2810
+ return Nothing<bool >();
2811
+ }
2812
+ return Just (true );
2813
+ }
2814
+
2815
+
2756
2816
// Call process.emitWarning(str), fmt is a snprintf() format string
2757
- void ProcessEmitWarning (Environment* env, const char * fmt, ...) {
2817
+ Maybe< bool > ProcessEmitWarning (Environment* env, const char * fmt, ...) {
2758
2818
char warning[1024 ];
2759
2819
va_list ap;
2760
2820
2761
2821
va_start (ap, fmt);
2762
2822
vsnprintf (warning, sizeof (warning), fmt, ap);
2763
2823
va_end (ap);
2764
2824
2765
- HandleScope handle_scope (env->isolate ());
2766
- Context::Scope context_scope (env->context ());
2767
-
2768
- Local<Object> process = env->process_object ();
2769
- MaybeLocal<Value> emit_warning = process->Get (env->context (),
2770
- FIXED_ONE_BYTE_STRING (env->isolate (), " emitWarning" ));
2771
- Local<Value> arg = node::OneByteString (env->isolate (), warning);
2772
-
2773
- Local<Value> f;
2825
+ return ProcessEmitWarningGeneric (env, warning);
2826
+ }
2774
2827
2775
- if (!emit_warning.ToLocal (&f)) return ;
2776
- if (!f->IsFunction ()) return ;
2777
2828
2778
- // MakeCallback() unneeded, because emitWarning is internal code, it calls
2779
- // process.emit('warning', ..), but does so on the nextTick.
2780
- f.As <v8::Function>()->Call (process, 1 , &arg);
2829
+ Maybe<bool > ProcessEmitDeprecationWarning (Environment* env,
2830
+ const char * warning,
2831
+ const char * deprecation_code) {
2832
+ return ProcessEmitWarningGeneric (env,
2833
+ warning,
2834
+ " DeprecationWarning" ,
2835
+ deprecation_code);
2781
2836
}
2782
2837
2838
+
2783
2839
static bool PullFromCache (Environment* env,
2784
2840
const FunctionCallbackInfo<Value>& args,
2785
2841
Local<String> module,
0 commit comments