-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSObject.mm
More file actions
74 lines (57 loc) · 1.5 KB
/
JSObject.mm
File metadata and controls
74 lines (57 loc) · 1.5 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
#include "JSObject.h"
#include "ObjCBridge.h"
#include "js_native_api.h"
#import <Foundation/Foundation.h>
void JSObject_finalize(napi_env, void *data, void *) {
id obj = (id)data;
[obj release];
}
@interface JSObject : NSObject {
napi_env env;
napi_ref ref;
objc_bridge::ObjCBridgeState *bridgeState;
}
- (instancetype)initWithEnv:(napi_env)env value:(napi_value)value;
- (napi_value)value;
@end
@implementation JSObject
- (instancetype)initWithEnv:(napi_env)_env value:(napi_value)value {
[super init];
self->env = _env;
napi_add_finalizer(env, value, self, JSObject_finalize, nullptr, &ref);
uint32_t result;
napi_reference_ref(env, ref, &result);
napi_wrap(env, value, self, nullptr, nullptr, nullptr);
bridgeState = objc_bridge::ObjCBridgeState::InstanceData(env);
bridgeState->objectRefs[self] = ref;
return self;
}
- (napi_value)value {
napi_value result;
napi_get_reference_value(env, ref, &result);
return result;
}
- (void)dealloc {
napi_delete_reference(env, ref);
bridgeState->objectRefs.erase(self);
[super dealloc];
}
@end
@protocol Test
@optional
@property(nonatomic, readonly) bool optionalString;
@end
namespace objc_bridge {
id jsObjectToId(napi_env env, napi_value value) {
return [[JSObject alloc] initWithEnv:env value:value];
}
napi_value idToJsObject(napi_env env, id obj) {
if (obj == nil) {
return nullptr;
}
if ([obj isKindOfClass:[JSObject class]]) {
return [((JSObject *)obj) value];
}
return nil;
}
} // namespace objc_bridge