-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSObject.mm
More file actions
78 lines (61 loc) · 1.71 KB
/
Copy pathJSObject.mm
File metadata and controls
78 lines (61 loc) · 1.71 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
#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;
uint64_t bridgeStateToken;
}
- (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_create_reference(env, value, 1, &ref);
napi_wrap(env, value, self, nullptr, nullptr, nullptr);
bridgeState = nativescript::ObjCBridgeState::InstanceData(env);
bridgeStateToken = bridgeState != nullptr ? bridgeState->lifetimeToken : 0;
return self;
}
- (napi_value)value {
if (env == nullptr || ref == nullptr) {
return nullptr;
}
napi_value result;
napi_get_reference_value(env, ref, &result);
return result;
}
- (void)dealloc {
if (env != nullptr && ref != nullptr &&
(bridgeState == nullptr || nativescript::IsBridgeStateLive(bridgeState, bridgeStateToken))) {
nativescript::DeleteReferenceOnOwningThread(env, bridgeState, bridgeStateToken, ref);
ref = nullptr;
}
bridgeState = nullptr;
bridgeStateToken = 0;
env = nullptr;
[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] autorelease];
}
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