Skip to content

Commit 295ef48

Browse files
committed
cleanup
1 parent fbdef0f commit 295ef48

File tree

3 files changed

+24
-60
lines changed

3 files changed

+24
-60
lines changed

include/Class.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ class BridgedClass {
2424

2525
BridgedClass *superclass;
2626

27+
napi_env env;
2728
napi_ref constructor;
2829
napi_ref prototype;
2930

3031
BridgedClass() {}
3132
BridgedClass(napi_env env, MDSectionOffset offset);
33+
~BridgedClass();
3234
};
3335

3436
} // namespace objc_bridge

src/Class.mm

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,6 @@
209209
return nullptr;
210210
}
211211

212-
// Common function used to define Objective-C properties & methods on a JS
213-
// constructor/prototype. This is used for both the main constructor/prototype,
214-
// as well as supercall constructor/prototype.
215-
216212
std::string NativeObjectName = "NativeObject";
217213

218214
// Bridge an Objective-C class to JavaScript on the fly. Runtime introspection
@@ -227,6 +223,8 @@
227223
BridgedClass::BridgedClass(napi_env env, MDSectionOffset offset) {
228224
NAPI_PREAMBLE
229225

226+
this->env = env;
227+
230228
metadataOffset = offset;
231229

232230
auto bridgeData = ObjCBridgeData::InstanceData(env);
@@ -267,18 +265,10 @@
267265
(void *)this, 0, nil, &constructor);
268266

269267
if (nativeClass != nil) {
270-
NAPI_GUARD(
271-
napi_wrap(env, constructor, (void *)nativeClass, nil, nil, nil)) {
272-
NAPI_THROW_LAST_ERROR
273-
return;
274-
}
268+
napi_wrap(env, constructor, (void *)nativeClass, nil, nil, nil);
275269
}
276270

277-
NAPI_GUARD(
278-
napi_get_named_property(env, constructor, "prototype", &prototype)) {
279-
NAPI_THROW_LAST_ERROR
280-
return;
281-
}
271+
napi_get_named_property(env, constructor, "prototype", &prototype);
282272

283273
napi_value superConstructor = nil, superPrototype = nil;
284274

@@ -310,24 +300,10 @@
310300
superclass = nullptr;
311301
}
312302

313-
napi_value classExternal;
314-
NAPI_GUARD(napi_create_external(env, (void *)nativeClass, nil, nil,
315-
&classExternal)) {
316-
NAPI_THROW_LAST_ERROR
317-
return;
318-
}
319-
320-
NAPI_GUARD(
321-
napi_set_named_property(env, constructor, "__class__", classExternal)) {
322-
NAPI_THROW_LAST_ERROR
323-
return;
324-
}
325-
326303
this->constructor = make_ref(env, constructor);
327304
this->prototype = make_ref(env, prototype);
328305

329306
if (isNativeObject) {
330-
// Define custom inspect property.
331307
napi_property_descriptor property = {
332308
.utf8name = nil,
333309
.name = jsSymbolFor(env, "nodejs.util.inspect.custom"),
@@ -340,7 +316,6 @@
340316
};
341317

342318
napi_define_properties(env, prototype, 1, &property);
343-
// napi_define_properties(env, constructor, 1, &property);
344319

345320
napi_value global, Symbol, SymbolDispose;
346321
napi_get_global(env, &global);
@@ -464,4 +439,9 @@
464439
}
465440
}
466441

467-
} // namespace objc_bridge
442+
BridgedClass::~BridgedClass() {
443+
napi_delete_reference(env, constructor);
444+
napi_delete_reference(env, prototype);
445+
}
446+
447+
} // namespace objc_bridge

src/CustomClass.mm

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,17 @@ void addProtocol(napi_env env, ObjCBridgeData *bridgeData, Class cls,
125125
extern char name_buf[512];
126126

127127
void ObjCBridgeData::registerClass(napi_env env, napi_value constructor) {
128-
napi_value classExternal, prototype, registered;
129-
napi_get_named_property(env, constructor, "__class__", &classExternal);
128+
napi_value prototype;
130129
napi_get_named_property(env, constructor, "prototype", &prototype);
131130

131+
napi_value superConstructor;
132+
napi_get_prototype(env, constructor, &superConstructor);
132133
Class superClassNative = nullptr;
133-
napi_get_value_external(env, classExternal, (void **)&superClassNative);
134+
napi_unwrap(env, superConstructor, (void **)&superClassNative);
134135

135136
if (superClassNative == nullptr) {
137+
// If the class does not inherit from a native class,
138+
// by default it inherits from NSObject.
136139
superClassNative = [NSObject class];
137140
}
138141

@@ -152,18 +155,9 @@ void addProtocol(napi_env env, ObjCBridgeData *bridgeData, Class cls,
152155

153156
napi_value properties;
154157

155-
napi_value jsGlobal, jsObject, jsObjectGetOwnPropertyNames;
156-
napi_get_global(env, &jsGlobal);
157-
napi_get_named_property(env, jsGlobal, "Object", &jsObject);
158-
napi_get_named_property(env, jsObject, "getOwnPropertyNames",
159-
&jsObjectGetOwnPropertyNames);
160-
161-
napi_call_function(env, jsObject, jsObjectGetOwnPropertyNames, 1, &prototype,
162-
&properties);
163-
164-
// napi_get_all_property_names(env, prototype, napi_key_own_only,
165-
// napi_key_skip_symbols,
166-
// napi_key_numbers_to_strings, &properties);
158+
napi_get_all_property_names(env, prototype, napi_key_own_only,
159+
napi_key_skip_symbols,
160+
napi_key_numbers_to_strings, &properties);
167161

168162
uint32_t propertyCount = 0;
169163
napi_get_array_length(env, properties, &propertyCount);
@@ -180,12 +174,9 @@ void addProtocol(napi_env env, ObjCBridgeData *bridgeData, Class cls,
180174
napi_get_named_property(env, constructor, "ObjCExposedMethods",
181175
&exposedMethods);
182176

183-
// napi_get_all_property_names(
184-
// env, exposedMethods, napi_key_own_only, napi_key_skip_symbols,
185-
// napi_key_numbers_to_strings, &exposedMethodNames);
186-
187-
napi_call_function(env, jsObject, jsObjectGetOwnPropertyNames, 1,
188-
&exposedMethods, &exposedMethodNames);
177+
napi_get_all_property_names(
178+
env, exposedMethods, napi_key_own_only, napi_key_skip_symbols,
179+
napi_key_numbers_to_strings, &exposedMethodNames);
189180

190181
uint32_t exposedMethodCount = 0;
191182
napi_get_array_length(env, exposedMethodNames, &exposedMethodCount);
@@ -284,17 +275,8 @@ void addProtocol(napi_env env, ObjCBridgeData *bridgeData, Class cls,
284275

285276
classesByPointer[cls] = bridgedClass;
286277

287-
void *alreadyWrapped;
288-
napi_unwrap(env, constructor, &alreadyWrapped);
289-
if (alreadyWrapped != nullptr) {
290-
napi_remove_wrap(env, constructor, &alreadyWrapped);
291-
}
292278
napi_wrap(env, constructor, (void *)cls, nullptr, nullptr, nullptr);
293279

294-
napi_value external;
295-
napi_create_external(env, (void *)cls, nullptr, nullptr, &external);
296-
napi_set_named_property(env, constructor, "__class__", external);
297-
298280
napi_value jsTrue;
299281
napi_get_boolean(env, true, &jsTrue);
300282
napi_set_named_property(env, constructor, "__objc_msgSendSuper__", jsTrue);

0 commit comments

Comments
 (0)