-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathArrayAdapter.mm
More file actions
85 lines (71 loc) · 2.42 KB
/
ArrayAdapter.mm
File metadata and controls
85 lines (71 loc) · 2.42 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
#include "ArrayAdapter.h"
#include "DataWrapper.h"
#include "Helpers.h"
#include "Interop.h"
#include "Caches.h"
using namespace tns;
using namespace v8;
@implementation ArrayAdapter {
Isolate* isolate_;
std::shared_ptr<Persistent<Value>> object_;
std::shared_ptr<Caches> cache_;
}
- (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> object = self->object_->Get(self->isolate_).As<Object>();
if (object->IsArray()) {
uint32_t length = object.As<v8::Array>()->Length();
return length;
}
Local<Context> context = self->cache_->GetContext();
Local<v8::Array> propertyNames;
bool success = object->GetPropertyNames(context).ToLocal(&propertyNames);
tns::Assert(success, self->isolate_);
uint32_t length = propertyNames->Length();
return length;
}
- (id)objectAtIndex:(NSUInteger)index {
v8::Locker locker(self->isolate_);
Isolate::Scope isolate_scope(self->isolate_);
HandleScope handle_scope(self->isolate_);
if (!(index < [self count])) {
tns::Assert(false, self->isolate_);
}
Local<Object> object = self->object_->Get(self->isolate_).As<Object>();
Local<Context> context = self->cache_->GetContext();
Local<Value> item;
bool success = object->Get(context, (uint)index).ToLocal(&item);
tns::Assert(success, self->isolate_);
if (item->IsNullOrUndefined()) {
return nil;
}
id value = Interop::ToObject(context, item);
return value;
}
- (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_ = nullptr;
self->object_ = nullptr;
[super dealloc];
}
@end