Repository navigation
Expand file tree
/
Copy pathObjCBridge.mm
More file actions
227 lines (191 loc) · 6.39 KB
/
Copy pathObjCBridge.mm
File metadata and controls
227 lines (191 loc) · 6.39 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "ObjCBridge.h"
#include "AutoreleasePool.h"
#include "Block.h"
#include "Class.h"
#include "ClassMember.h"
#include "Enum.h"
#include "InlineFunctions.h"
#include "Interop.h"
#include "Metadata.h"
#include "MetadataReader.h"
#include "NativeScript.h"
#include "Object.h"
#include "ObjectRef.h"
#include "Struct.h"
#include "TypeConv.h"
#include "Variable.h"
#include "js_native_api.h"
#include "js_native_api_types.h"
#include "node_api_util.h"
#import <Foundation/Foundation.h>
#include <mach-o/dyld.h>
#include <mach-o/getsect.h>
#include <objc/runtime.h>
#ifdef EMBED_METADATA_SIZE
const unsigned char __attribute__((section("__objc_metadata,__objc_metadata")))
#if defined(__aarch64__)
embedded_metadata[EMBED_METADATA_SIZE] = "NSMDSectionHeaderARM";
#else
embedded_metadata[EMBED_METADATA_SIZE] = "NSMDSectionHeaderX86";
#endif
#endif
namespace objc_bridge {
void finalize_bridge_data(napi_env env, void *data, void *hint) {
auto bridgeState = (ObjCBridgeState *)data;
delete bridgeState;
}
MDMetadataReader *loadMetadataFromFile(const char *metadata_path) {
if (metadata_path == nullptr) {
metadata_path = "metadata.nsmd";
}
auto f =
fopen(metadata_path == nullptr ? "metadata.nsmd" : metadata_path, "r");
if (f == nullptr) {
fprintf(stderr, "metadata.nsmd not found\n");
exit(1);
}
fseek(f, 0, SEEK_END);
auto size = ftell(f);
fseek(f, 0, SEEK_SET);
auto buffer = (uint8_t *)malloc(size);
fread(buffer, 1, size, f);
fclose(f);
return new MDMetadataReader(buffer, size);
}
ObjCBridgeState::ObjCBridgeState(napi_env env, const char *metadata_path) {
napi_set_instance_data(env, this, finalize_bridge_data, nil);
self_dl = dlopen(nullptr, RTLD_NOW);
#ifdef EMBED_METADATA_SIZE
if (metadata_path != nullptr) {
metadata = loadMetadataFromFile(metadata_path);
} else {
metadata =
new MDMetadataReader((void *)embedded_metadata, EMBED_METADATA_SIZE);
}
#else
unsigned long segmentSize = 0;
auto segmentData =
getsegmentdata((const mach_header_64 *)_dyld_get_image_header(0),
"__objc_metadata", &segmentSize);
if (segmentData != nullptr) {
metadata = new MDMetadataReader(segmentData, segmentSize);
} else {
metadata = loadMetadataFromFile(metadata_path);
}
#endif
objc_autoreleasePool = objc_autoreleasePoolPush();
}
ObjCBridgeState::~ObjCBridgeState() {
objc_autoreleasePoolPop(objc_autoreleasePool);
delete metadata;
dlclose(self_dl);
}
napi_value ObjCBridgeState::proxyNativeObject(napi_env env, napi_value object,
id nativeObject) {
NAPI_PREAMBLE
napi_value factory = get_ref_value(env, createNativeProxy);
napi_value transferOwnershipFunc =
get_ref_value(env, this->transferOwnershipToNative);
napi_value result, global;
napi_value args[3] = {object, nullptr, transferOwnershipFunc};
napi_get_boolean(env, [nativeObject isKindOfClass:NSArray.class], &args[1]);
napi_get_global(env, &global);
napi_call_function(env, global, factory, 3, args, &result);
// We need to wrap the proxied object separately except for Hermes,
// We'll just ignore the error there.
napi_wrap(env, result, nativeObject, nullptr, nullptr, nullptr);
napi_ref ref = nullptr;
NAPI_GUARD(napi_add_finalizer(env, result, nativeObject, finalize_objc_object,
this, &ref)) {
NAPI_THROW_LAST_ERROR
return nullptr;
}
objectRefs[nativeObject] = ref;
return result;
}
} // namespace objc_bridge
using namespace objc_bridge;
NAPI_FUNCTION(getArrayBuffer) {
NAPI_CALLBACK_BEGIN(2)
void *ptr = Pointer::unwrap(env, argv[0])->data;
int64_t length;
napi_get_value_int64(env, argv[1], &length);
napi_value arrayBuffer;
napi_create_external_arraybuffer(env, ptr, length, nullptr, nullptr,
&arrayBuffer);
return arrayBuffer;
}
NAPI_FUNCTION(init) {
NAPI_CALLBACK_BEGIN(1)
napi_valuetype type;
napi_typeof(env, argv[0], &type);
const char *metadata_path = nullptr;
if (type == napi_string) {
size_t len;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len);
metadata_path = (char *)malloc(len + 1);
napi_get_value_string_utf8(env, argv[0], (char *)metadata_path, len + 1,
&len);
}
objc_bridge_init(env, metadata_path);
return nullptr;
}
NAPI_EXPORT NAPI_MODULE_REGISTER {
const napi_property_descriptor property = NAPI_FUNCTION_DESC(init);
napi_define_properties(env, exports, 1, &property);
return exports;
}
NAPI_EXPORT void objc_bridge_init(void *_env, const char *metadata_path) {
napi_env env = (napi_env)_env;
ObjCBridgeState *bridgeState = new ObjCBridgeState(env, metadata_path);
napi_value objc;
napi_create_object(env, &objc);
const napi_property_descriptor objcProperties[] = {
NAPI_FUNCTION_DESC(registerClass), NAPI_FUNCTION_DESC(registerBlock),
NAPI_FUNCTION_DESC(import), NAPI_FUNCTION_DESC(autoreleasepool),
NAPI_FUNCTION_DESC(getArrayBuffer),
};
napi_define_properties(env, objc, 5, objcProperties);
napi_value global;
napi_get_global(env, &global);
const napi_property_descriptor globalProperties[] = {
{
.utf8name = "objc",
.attributes = napi_enumerable,
.getter = nullptr,
.setter = nullptr,
.value = objc,
.data = nullptr,
.method = nullptr,
},
{
.utf8name = "ObjectRef",
.attributes = napi_enumerable,
.getter = nullptr,
.setter = nullptr,
.method = nullptr,
.data = nullptr,
.value = defineObjectRefClass(env),
},
{
.utf8name = "NativeClass",
.attributes = napi_enumerable,
.getter = nullptr,
.setter = nullptr,
.method = JS_registerClass,
.data = nullptr,
.value = nullptr,
}};
napi_define_properties(env, global, 3, globalProperties);
initProxyFactory(env, bridgeState);
initFastEnumeratorIteratorFactory(env, bridgeState);
registerInterop(env, global);
registerInlineFunctions(env);
bridgeState->registerVarGlobals(env, global);
bridgeState->registerEnumGlobals(env, global);
bridgeState->registerStructGlobals(env, global);
bridgeState->registerUnionGlobals(env, global);
bridgeState->registerFunctionGlobals(env, global);
bridgeState->registerClassGlobals(env, global);
bridgeState->registerProtocolGlobals(env, global);
}