-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathDictionaryAdapter.mm
More file actions
269 lines (213 loc) · 7.87 KB
/
Copy pathDictionaryAdapter.mm
File metadata and controls
269 lines (213 loc) · 7.87 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#import <Foundation/NSString.h>
#include "DictionaryAdapter.h"
#include "DataWrapper.h"
#include "Helpers.h"
#include "Interop.h"
#include "Caches.h"
using namespace v8;
using namespace tns;
@interface DictionaryAdapterMapKeysEnumerator : NSEnumerator
- (instancetype)initWithMap:(std::shared_ptr<Persistent<Value>>)map isolate:(Isolate*)isolate cache:(std::shared_ptr<Caches>)cache;
@end
@implementation DictionaryAdapterMapKeysEnumerator {
Isolate* isolate_;
uint32_t index_;
std::shared_ptr<Persistent<Value>> map_;
std::shared_ptr<Caches> cache_;
}
- (instancetype)initWithMap:(std::shared_ptr<Persistent<Value>>)map isolate:(Isolate*)isolate cache:(std::shared_ptr<Caches>)cache {
if (self) {
self->isolate_ = isolate;
self->index_ = 0;
self->map_ = map;
self->cache_ = cache;
}
return self;
}
- (id)nextObject {
Isolate* isolate = self->isolate_;
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = self->cache_->GetContext();
Local<v8::Array> array = self->map_->Get(isolate).As<Map>()->AsArray();
if (self->index_ < array->Length() - 1) {
Local<Value> key;
bool success = array->Get(context, self->index_).ToLocal(&key);
tns::Assert(success, isolate);
self->index_ += 2;
NSString* result = tns::ToNSString(self->isolate_, key);
return result;
}
return nil;
}
- (void)dealloc {
self->isolate_ = nullptr;
self->map_ = nil;
self->cache_ = nil;
[super dealloc];
}
@end
@interface DictionaryAdapterObjectKeysEnumerator : NSEnumerator
- (instancetype)initWithProperties:(std::shared_ptr<Persistent<Value>>)dictionary isolate:(Isolate*)isolate cache:(std::shared_ptr<Caches>)cache;
- (Local<v8::Array>)getProperties;
@end
@implementation DictionaryAdapterObjectKeysEnumerator {
Isolate* isolate_;
std::shared_ptr<Persistent<Value>> dictionary_;
NSUInteger index_;
std::shared_ptr<Caches> cache_;
}
- (instancetype)initWithProperties:(std::shared_ptr<Persistent<Value>>)dictionary isolate:(Isolate*)isolate cache:(std::shared_ptr<Caches>)cache {
if (self) {
self->isolate_ = isolate;
self->dictionary_ = dictionary;
self->index_ = 0;
self->cache_ = cache;
}
return self;
}
- (Local<v8::Array>)getProperties {
v8::Locker locker(self->isolate_);
Isolate::Scope isolate_scope(self->isolate_);
EscapableHandleScope handle_scope(self->isolate_);
Local<Context> context = self->cache_->GetContext();
Local<v8::Array> properties;
Local<Object> dictionary = self->dictionary_->Get(self->isolate_).As<Object>();
tns::Assert(dictionary->GetOwnPropertyNames(context).ToLocal(&properties), self->isolate_);
return handle_scope.Escape(properties);
}
- (id)nextObject {
Isolate* isolate = self->isolate_;
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = self->cache_->GetContext();
Local<v8::Array> properties = [self getProperties];
if (self->index_ < properties->Length()) {
Local<Value> value;
bool success = properties->Get(context, (uint)self->index_).ToLocal(&value);
tns::Assert(success, isolate);
self->index_++;
std::string result = tns::ToString(self->isolate_, value);
return [NSString stringWithUTF8String:result.c_str()];
}
return nil;
}
- (NSArray*)allObjects {
Isolate* isolate = self->isolate_;
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = self->cache_->GetContext();
NSMutableArray* array = [NSMutableArray array];
Local<v8::Array> properties = [self getProperties];
for (int i = 0; i < properties->Length(); i++) {
Local<Value> value;
bool success = properties->Get(context, i).ToLocal(&value);
tns::Assert(success, isolate);
std::string result = tns::ToString(self->isolate_, value);
[array addObject:[NSString stringWithUTF8String:result.c_str()]];
}
return array;
}
- (void)dealloc {
self->isolate_ = nullptr;
self->dictionary_ = nil;
self->cache_ = nil;
[super dealloc];
}
@end
@implementation DictionaryAdapter {
Isolate* isolate_;
std::shared_ptr<Persistent<Value>> object_;
std::shared_ptr<Caches> cache_;
NSEnumerator* enumerator_;
}
- (instancetype)initWithJSObject:(Local<Object>)jsObject isolate:(Isolate*)isolate {
if (self) {
self->isolate_ = isolate;
self->cache_ = Caches::Get(isolate);
self->object_ = std::make_shared<Persistent<Value>>(isolate, jsObject);
self->cache_->Instances.emplace(self, self->object_);
tns::SetValue(isolate, jsObject, MakeGarbageCollected<ObjCDataWrapper>(isolate, self));
}
return self;
}
- (NSUInteger)count {
v8::Locker locker(self->isolate_);
Isolate::Scope isolate_scope(self->isolate_);
HandleScope handle_scope(self->isolate_);
Local<Object> obj = self->object_->Get(self->isolate_).As<Object>();
if (obj->IsMap()) {
return obj.As<Map>()->Size();
}
Local<Context> context = self->cache_->GetContext();
Local<v8::Array> properties;
tns::Assert(obj->GetOwnPropertyNames(context).ToLocal(&properties), self->isolate_);
uint32_t length = properties->Length();
return length;
}
- (id)objectForKey:(id)aKey {
Isolate* isolate = self->isolate_;
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = self->cache_->GetContext();
Local<Object> obj = self->object_->Get(self->isolate_).As<Object>();
Local<Value> value;
if ([aKey isKindOfClass:[NSNumber class]]) {
unsigned int key = [aKey unsignedIntValue];
bool success = obj->Get(context, key).ToLocal(&value);
tns::Assert(success, isolate);
} else if ([aKey isKindOfClass:[NSString class]]) {
const char* key = [aKey UTF8String];
Local<v8::String> keyV8Str = tns::ToV8String(isolate, key);
if (obj->IsMap()) {
Local<Map> map = obj.As<Map>();
bool success = map->Get(context, keyV8Str).ToLocal(&value);
tns::Assert(success, isolate);
} else {
bool success = obj->Get(context, keyV8Str).ToLocal(&value);
tns::Assert(success, isolate);
}
} else {
// TODO: unsupported key type
tns::Assert(false, isolate);
}
id result = Interop::ToObject(context, value);
return result;
}
- (NSEnumerator*)keyEnumerator {
v8::Locker locker(self->isolate_);
Isolate::Scope isolate_scope(self->isolate_);
HandleScope handle_scope(self->isolate_);
Local<Value> obj = self->object_->Get(self->isolate_);
if (obj->IsMap()) {
self->enumerator_ = [[[DictionaryAdapterMapKeysEnumerator alloc] initWithMap:self->object_ isolate:self->isolate_ cache:self->cache_] autorelease];
return self->enumerator_;
}
self->enumerator_ = [[[DictionaryAdapterObjectKeysEnumerator alloc] initWithProperties:self->object_ isolate:self->isolate_ cache:self->cache_] autorelease];
return self->enumerator_;
}
- (void)dealloc {
self->cache_->Instances.erase(self);
Local<Value> value = self->object_->Get(self->isolate_);
BaseDataWrapper* wrapper = tns::GetValue(self->isolate_, value);
if (wrapper != nullptr) {
tns::DeleteValue(self->isolate_, value);
delete wrapper;
}
self->object_->Reset();
self->isolate_ = nullptr;
self->cache_ = nil;
self->object_ = nil;
if (self->enumerator_ != nullptr) {
// CFAutorelease(self->enumerator_);
self->enumerator_ = nullptr;
}
self->cache_ = nullptr;
self->object_ = nullptr;
[super dealloc];
}
@end