• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <js_native_api.h>
2 #include "../common.h"
3 
createDate(napi_env env,napi_callback_info info)4 static napi_value createDate(napi_env env, napi_callback_info info) {
5   size_t argc = 1;
6   napi_value args[1];
7   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
8 
9   NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
10 
11   napi_valuetype valuetype0;
12   NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
13 
14   NAPI_ASSERT(env, valuetype0 == napi_number,
15               "Wrong type of arguments. Expects a number as first argument.");
16 
17   double time;
18   NAPI_CALL(env, napi_get_value_double(env, args[0], &time));
19 
20   napi_value date;
21   NAPI_CALL(env, napi_create_date(env, time, &date));
22 
23   return date;
24 }
25 
isDate(napi_env env,napi_callback_info info)26 static napi_value isDate(napi_env env, napi_callback_info info) {
27   napi_value date, result;
28   size_t argc = 1;
29   bool is_date;
30 
31   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &date, NULL, NULL));
32   NAPI_CALL(env, napi_is_date(env, date, &is_date));
33   NAPI_CALL(env, napi_get_boolean(env, is_date, &result));
34 
35   return result;
36 }
37 
getDateValue(napi_env env,napi_callback_info info)38 static napi_value getDateValue(napi_env env, napi_callback_info info) {
39   napi_value date, result;
40   size_t argc = 1;
41   double value;
42 
43   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &date, NULL, NULL));
44   NAPI_CALL(env, napi_get_date_value(env, date, &value));
45   NAPI_CALL(env, napi_create_double(env, value, &result));
46 
47   return result;
48 }
49 
50 EXTERN_C_START
Init(napi_env env,napi_value exports)51 napi_value Init(napi_env env, napi_value exports) {
52   napi_property_descriptor descriptors[] = {
53     DECLARE_NAPI_PROPERTY("createDate", createDate),
54     DECLARE_NAPI_PROPERTY("isDate", isDate),
55     DECLARE_NAPI_PROPERTY("getDateValue", getDateValue),
56   };
57 
58   NAPI_CALL(env, napi_define_properties(
59       env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
60 
61   return exports;
62 }
63 EXTERN_C_END
64