-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathDictionaryAdapter.mm
More file actions
282 lines (229 loc) · 8.19 KB
/
DictionaryAdapter.mm
File metadata and controls
282 lines (229 loc) · 8.19 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
270
271
272
273
274
275
276
277
278
279
280
281
282
#import <Foundation/NSString.h>
#include "DictionaryAdapter.h"
#include "DataWrapper.h"
#include "Helpers.h"
#include "Interop.h"
#include "Caches.h"
#include "IsolateWrapper.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 {
IsolateWrapper* wrapper_;
uint32_t index_;
std::shared_ptr<Persistent<Value>> map_;
}
- (instancetype)initWithMap:(std::shared_ptr<Persistent<Value>>)map isolate:(Isolate*)isolate cache:(std::shared_ptr<Caches>)cache {
if (self) {
self->wrapper_ = new IsolateWrapper(isolate);
self->index_ = 0;
self->map_ = map;
}
return self;
}
- (id)nextObject {
if (!wrapper_->IsValid()) {
return nil;
}
Isolate* isolate = wrapper_->Isolate();
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = wrapper_->GetCache()->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(isolate, key);
return result;
}
return nil;
}
- (void)dealloc {
self->map_ = nil;
delete self->wrapper_;
[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 {
IsolateWrapper* wrapper_;
std::shared_ptr<Persistent<Value>> dictionary_;
NSUInteger index_;
}
- (instancetype)initWithProperties:(std::shared_ptr<Persistent<Value>>)dictionary isolate:(Isolate*)isolate cache:(std::shared_ptr<Caches>)cache {
if (self) {
self->wrapper_ = new IsolateWrapper(isolate);
self->dictionary_ = dictionary;
self->index_ = 0;
}
return self;
}
- (Local<v8::Array>)getProperties {
Isolate* isolate = wrapper_->Isolate();
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
EscapableHandleScope handle_scope(isolate);
Local<Context> context = wrapper_->GetCache()->GetContext();
Local<v8::Array> properties;
Local<Object> dictionary = self->dictionary_->Get(isolate).As<Object>();
tns::Assert(dictionary->GetOwnPropertyNames(context).ToLocal(&properties), isolate);
return handle_scope.Escape(properties);
}
- (id)nextObject {
if (!wrapper_->IsValid()) {
return nil;
}
Isolate* isolate = wrapper_->Isolate();
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = wrapper_->GetCache()->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(isolate, value);
return [NSString stringWithUTF8String:result.c_str()];
}
return nil;
}
- (NSArray*)allObjects {
if (!wrapper_->IsValid()) {
return nil;
}
Isolate* isolate = wrapper_->Isolate();
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = wrapper_->GetCache()->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(isolate, value);
[array addObject:[NSString stringWithUTF8String:result.c_str()]];
}
return array;
}
- (void)dealloc {
self->dictionary_ = nil;
delete self->wrapper_;
[super dealloc];
}
@end
@implementation DictionaryAdapter {
IsolateWrapper* wrapper_;
std::shared_ptr<Persistent<Value>> object_;
ObjCDataWrapper* dataWrapper_;
}
- (instancetype)initWithJSObject:(Local<Object>)jsObject isolate:(Isolate*)isolate {
if (self) {
self->wrapper_ = new IsolateWrapper(isolate);
self->object_ = std::make_shared<Persistent<Value>>(isolate, jsObject);
self->wrapper_->GetCache()->Instances.emplace(self, self->object_);
tns::SetValue(isolate, jsObject, (self->dataWrapper_ = new ObjCDataWrapper(self)));
}
return self;
}
- (NSUInteger)count {
if (!wrapper_->IsValid()) {
return 0;
}
Isolate* isolate = wrapper_->Isolate();
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Object> obj = self->object_->Get(isolate).As<Object>();
if (obj->IsMap()) {
return obj.As<Map>()->Size();
}
Local<Context> context = wrapper_->GetCache()->GetContext();
Local<v8::Array> properties;
tns::Assert(obj->GetOwnPropertyNames(context).ToLocal(&properties), isolate);
uint32_t length = properties->Length();
return length;
}
- (id)objectForKey:(id)aKey {
if (!wrapper_->IsValid()) {
return nil;
}
Isolate* isolate = wrapper_->Isolate();
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = wrapper_->GetCache()->GetContext();
Local<Object> obj = self->object_->Get(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 {
if (!wrapper_->IsValid()) {
return nil;
}
Isolate* isolate = wrapper_->Isolate();
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Value> obj = self->object_->Get(isolate);
if (obj->IsMap()) {
return [[[DictionaryAdapterMapKeysEnumerator alloc] initWithMap:self->object_ isolate:isolate cache:wrapper_->GetCache()] autorelease];
}
return [[[DictionaryAdapterObjectKeysEnumerator alloc] initWithProperties:self->object_ isolate:isolate cache:wrapper_->GetCache()] autorelease];
}
- (void)dealloc {
if (wrapper_->IsValid()) {
Isolate* isolate = wrapper_->Isolate();
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
wrapper_->GetCache()->Instances.erase(self);
Local<Value> value = self->object_->Get(isolate);
BaseDataWrapper* wrapper = tns::GetValue(isolate, value);
if (wrapper != nullptr) {
if (wrapper == dataWrapper_) {
dataWrapper_ = nullptr;
}
tns::DeleteValue(isolate, value);
delete wrapper;
}
}
if (dataWrapper_ != nullptr) {
delete dataWrapper_;
}
self->object_ = nullptr;
delete self->wrapper_;
[super dealloc];
}
@end