• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <js_native_api.h>
2 #include "common.h"
3 
4 #include <stdio.h>
5 
add_returned_status(napi_env env,const char * key,napi_value object,char * expected_message,napi_status expected_status,napi_status actual_status)6 void add_returned_status(napi_env env,
7                          const char* key,
8                          napi_value object,
9                          char* expected_message,
10                          napi_status expected_status,
11                          napi_status actual_status) {
12 
13   char napi_message_string[100] = "";
14   napi_value prop_value;
15 
16   if (actual_status != expected_status) {
17     snprintf(napi_message_string, sizeof(napi_message_string), "Invalid status [%d]", actual_status);
18   }
19 
20   NAPI_CALL_RETURN_VOID(env,
21                         napi_create_string_utf8(
22                             env,
23                             (actual_status == expected_status ?
24                                 expected_message :
25                                 napi_message_string),
26                             NAPI_AUTO_LENGTH,
27                             &prop_value));
28   NAPI_CALL_RETURN_VOID(env,
29                         napi_set_named_property(env,
30                                                 object,
31                                                 key,
32                                                 prop_value));
33 }
34 
add_last_status(napi_env env,const char * key,napi_value return_value)35 void add_last_status(napi_env env, const char* key, napi_value return_value) {
36     napi_value prop_value;
37     const napi_extended_error_info* p_last_error;
38     NAPI_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error));
39 
40     NAPI_CALL_RETURN_VOID(env,
41         napi_create_string_utf8(env,
42                                 (p_last_error->error_message == NULL ?
43                                     "napi_ok" :
44                                     p_last_error->error_message),
45                                 NAPI_AUTO_LENGTH,
46                                 &prop_value));
47     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env,
48                                                        return_value,
49                                                        key,
50                                                        prop_value));
51 }
52