forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinding.c
More file actions
104 lines (88 loc) · 3.66 KB
/
Copy pathbinding.c
File metadata and controls
104 lines (88 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <node_api.h>
#include <stdio.h>
#include <stdlib.h>
#define NODE_API_CALL(call) \
do { \
napi_status status = call; \
if (status != napi_ok) { \
fprintf(stderr, #call " failed: %d\n", status); \
abort(); \
} \
} while (0)
#define ABORT_IF_FALSE(condition) \
if (!(condition)) { \
fprintf(stderr, #condition " failed\n"); \
abort(); \
}
static napi_value Runner(napi_env env,
napi_callback_info info,
napi_property_attributes attr) {
napi_value argv[2], undefined, js_array_length, start, end;
size_t argc = 2;
napi_valuetype val_type = napi_undefined;
bool is_array = false;
uint32_t array_length = 0;
napi_value* native_array;
// Validate params and retrieve start and end function.
NODE_API_CALL(napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
ABORT_IF_FALSE(argc == 2);
NODE_API_CALL(napi_typeof(env, argv[0], &val_type));
ABORT_IF_FALSE(val_type == napi_object);
NODE_API_CALL(napi_is_array(env, argv[1], &is_array));
ABORT_IF_FALSE(is_array);
NODE_API_CALL(napi_get_array_length(env, argv[1], &array_length));
NODE_API_CALL(napi_get_named_property(env, argv[0], "start", &start));
NODE_API_CALL(napi_typeof(env, start, &val_type));
ABORT_IF_FALSE(val_type == napi_function);
NODE_API_CALL(napi_get_named_property(env, argv[0], "end", &end));
NODE_API_CALL(napi_typeof(env, end, &val_type));
ABORT_IF_FALSE(val_type == napi_function);
NODE_API_CALL(napi_get_undefined(env, &undefined));
NODE_API_CALL(napi_create_uint32(env, array_length, &js_array_length));
// Copy objects into a native array.
native_array = malloc(array_length * sizeof(*native_array));
for (uint32_t idx = 0; idx < array_length; idx++) {
NODE_API_CALL(napi_get_element(env, argv[1], idx, &native_array[idx]));
}
const napi_property_descriptor desc = {
"prop", NULL, NULL, NULL, NULL, js_array_length, attr, NULL};
// Start the benchmark.
napi_call_function(env, argv[0], start, 0, NULL, NULL);
for (uint32_t idx = 0; idx < array_length; idx++) {
NODE_API_CALL(napi_define_properties(env, native_array[idx], 1, &desc));
}
// Conclude the benchmark.
NODE_API_CALL(
napi_call_function(env, argv[0], end, 1, &js_array_length, NULL));
free(native_array);
return undefined;
}
static napi_value RunFastPath(napi_env env, napi_callback_info info) {
return Runner(env, info, napi_writable | napi_enumerable | napi_configurable);
}
static napi_value RunSlowPath(napi_env env, napi_callback_info info) {
return Runner(env, info, napi_writable | napi_enumerable);
}
NAPI_MODULE_INIT() {
napi_property_descriptor props[] = {
{"runFastPath",
NULL,
RunFastPath,
NULL,
NULL,
NULL,
napi_writable | napi_configurable | napi_enumerable,
NULL},
{"runSlowPath",
NULL,
RunSlowPath,
NULL,
NULL,
NULL,
napi_writable | napi_configurable | napi_enumerable,
NULL},
};
NODE_API_CALL(napi_define_properties(
env, exports, sizeof(props) / sizeof(*props), props));
return exports;
}