-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectRef.mm
More file actions
143 lines (117 loc) · 3.52 KB
/
ObjectRef.mm
File metadata and controls
143 lines (117 loc) · 3.52 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "ObjectRef.h"
#include "TypeConv.h"
#include "Util.h"
#include "js_native_api.h"
#include "node_api_util.h"
#import <Foundation/Foundation.h>
namespace nativescript {
void ObjectRef_finalize(napi_env env, void* data, void* hint) { free(data); }
NAPI_FUNCTION(ObjectRef_constructor) {
napi_value jsThis, arg;
size_t argc = 1;
napi_get_cb_info(env, cbinfo, &argc, &arg, &jsThis, nullptr);
auto data = malloc(sizeof(id));
napi_wrap(env, jsThis, data, ObjectRef_finalize, nullptr, nullptr);
napi_valuetype argType;
napi_typeof(env, arg, &argType);
if (argType != napi_undefined && argType != napi_null) {
const char* argenc = "@";
auto conv = TypeConv::Make(env, &argenc);
bool shouldFree;
conv->toNative(env, arg, data, &shouldFree, &shouldFree);
} else {
*(id*)data = nil;
}
return jsThis;
}
NAPI_FUNCTION(ObjectRef_unwrap) {
napi_value jsThis;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr);
void* data;
napi_unwrap(env, jsThis, &data);
id obj = *(id*)data;
if (obj == nil) {
napi_throw_error(env, nullptr, "ObjectRef.unwrap returned nil");
return nullptr;
}
const char* argenc = "@";
auto conv = TypeConv::Make(env, &argenc);
return conv->toJS(env, &obj);
}
NAPI_FUNCTION(ObjectRef_get_value) {
napi_value jsThis;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr);
void* data;
napi_unwrap(env, jsThis, &data);
id obj = *(id*)data;
const char* argenc = "@";
auto conv = TypeConv::Make(env, &argenc);
return conv->toJS(env, &obj);
}
NAPI_FUNCTION(ObjectRef_set_value) {
napi_value jsThis, arg;
size_t argc = 1;
napi_get_cb_info(env, cbinfo, &argc, &arg, &jsThis, nullptr);
void* data;
napi_unwrap(env, jsThis, &data);
const char* argenc = "@";
auto conv = TypeConv::Make(env, &argenc);
bool shouldFree;
conv->toNative(env, arg, data, &shouldFree, &shouldFree);
return nullptr;
}
NAPI_FUNCTION(ObjectRef_customInspect) {
napi_value jsThis;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr);
void* data;
napi_unwrap(env, jsThis, &data);
id obj = *(id*)data;
std::string inspect = "ObjectRef(";
if (obj == nil) {
inspect += "nil";
} else {
inspect += [[obj description] UTF8String];
}
inspect += ")";
napi_value result;
napi_create_string_utf8(env, inspect.c_str(), NAPI_AUTO_LENGTH, &result);
return result;
}
napi_value defineObjectRefClass(napi_env env) {
napi_value result;
const napi_property_descriptor properties[] = {
{
.utf8name = "unwrap",
.name = nullptr,
.method = JS_ObjectRef_unwrap,
.getter = nullptr,
.setter = nullptr,
.value = nullptr,
.attributes = napi_default,
.data = nullptr,
},
{
.utf8name = "value",
.name = nullptr,
.method = nullptr,
.getter = JS_ObjectRef_get_value,
.setter = JS_ObjectRef_set_value,
.value = nullptr,
.attributes = napi_default,
.data = nullptr,
},
{
.utf8name = nullptr,
.name = jsSymbolFor(env, "nodejs.util.inspect.custom"),
.method = JS_ObjectRef_customInspect,
.getter = nullptr,
.setter = nullptr,
.value = nullptr,
.attributes = napi_default,
.data = nullptr,
}};
napi_define_class(env, "ObjectRef", NAPI_AUTO_LENGTH, JS_ObjectRef_constructor, nullptr, 3,
properties, &result);
return result;
}
} // namespace nativescript