• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <js_native_api.h>
2 #include "../common.h"
3 #include "../entry_point.h"
4 
BaseClass(napi_env env,napi_callback_info info)5 static napi_value BaseClass(napi_env env, napi_callback_info info) {
6   napi_value newTargetArg;
7   NODE_API_CALL(env, napi_get_new_target(env, info, &newTargetArg));
8   napi_value thisArg;
9   NODE_API_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &thisArg, NULL));
10   napi_value undefined;
11   NODE_API_CALL(env, napi_get_undefined(env, &undefined));
12 
13   // this !== new.target since we are being invoked through super()
14   bool result;
15   NODE_API_CALL(env, napi_strict_equals(env, newTargetArg, thisArg, &result));
16   NODE_API_ASSERT(env, !result, "this !== new.target");
17 
18   // new.target !== undefined because we should be called as a new expression
19   NODE_API_ASSERT(env, newTargetArg != NULL, "newTargetArg != NULL");
20   NODE_API_CALL(env, napi_strict_equals(env, newTargetArg, undefined, &result));
21   NODE_API_ASSERT(env, !result, "new.target !== undefined");
22 
23   return thisArg;
24 }
25 
Constructor(napi_env env,napi_callback_info info)26 static napi_value Constructor(napi_env env, napi_callback_info info) {
27   bool result;
28   napi_value newTargetArg;
29   NODE_API_CALL(env, napi_get_new_target(env, info, &newTargetArg));
30   size_t argc = 1;
31   napi_value argv;
32   napi_value thisArg;
33   NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, &argv, &thisArg, NULL));
34   napi_value undefined;
35   NODE_API_CALL(env, napi_get_undefined(env, &undefined));
36 
37   // new.target !== undefined because we should be called as a new expression
38   NODE_API_ASSERT(env, newTargetArg != NULL, "newTargetArg != NULL");
39   NODE_API_CALL(env, napi_strict_equals(env, newTargetArg, undefined, &result));
40   NODE_API_ASSERT(env, !result, "new.target !== undefined");
41 
42   // arguments[0] should be Constructor itself (test harness passed it)
43   NODE_API_CALL(env, napi_strict_equals(env, newTargetArg, argv, &result));
44   NODE_API_ASSERT(env, result, "new.target === Constructor");
45 
46   return thisArg;
47 }
48 
OrdinaryFunction(napi_env env,napi_callback_info info)49 static napi_value OrdinaryFunction(napi_env env, napi_callback_info info) {
50   napi_value newTargetArg;
51   NODE_API_CALL(env, napi_get_new_target(env, info, &newTargetArg));
52 
53   NODE_API_ASSERT(env, newTargetArg == NULL, "newTargetArg == NULL");
54 
55   napi_value _true;
56   NODE_API_CALL(env, napi_get_boolean(env, true, &_true));
57   return _true;
58 }
59 
60 EXTERN_C_START
Init(napi_env env,napi_value exports)61 napi_value Init(napi_env env, napi_value exports) {
62   const napi_property_descriptor desc[] = {
63     DECLARE_NODE_API_PROPERTY("BaseClass", BaseClass),
64     DECLARE_NODE_API_PROPERTY("OrdinaryFunction", OrdinaryFunction),
65     DECLARE_NODE_API_PROPERTY("Constructor", Constructor)
66   };
67   NODE_API_CALL(env, napi_define_properties(env, exports, 3, desc));
68   return exports;
69 }
70 EXTERN_C_END
71