-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSObject.mm
More file actions
86 lines (67 loc) · 1.72 KB
/
JSObject.mm
File metadata and controls
86 lines (67 loc) · 1.72 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
#include "JSObject.h"
#include "ObjCBridge.h"
#include "js_native_api.h"
#import <Foundation/Foundation.h>
@interface JSObject : NSObject {
napi_env env;
napi_ref ref;
nativescript::ObjCBridgeState* bridgeState;
}
- (instancetype)initWithEnv:(napi_env)env value:(napi_value)value;
- (void)removeFromBridgeState;
- (napi_value)value;
@end
void JSObject_finalize(napi_env, void* data, void*) {
JSObject* obj = (JSObject*)data;
[obj removeFromBridgeState];
[obj release];
}
@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 = nativescript::ObjCBridgeState::InstanceData(env);
if (bridgeState != nullptr) {
bridgeState->objectRefs[self] = ref;
}
return self;
}
- (void)removeFromBridgeState {
if (bridgeState == nullptr) {
return;
}
bridgeState->objectRefs.erase(self);
bridgeState = nullptr;
}
- (napi_value)value {
napi_value result;
napi_get_reference_value(env, ref, &result);
return result;
}
- (void)dealloc {
napi_delete_reference(env, ref);
[super dealloc];
}
@end
@protocol Test
@optional
@property(nonatomic, readonly) bool optionalString;
@end
namespace nativescript {
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 nativescript