-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.array.h
111 lines (104 loc) · 2.48 KB
/
js.array.h
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
#pragma once
#include "js.object.h"
/**
* @brief The js namespace
*
*/
namespace js {
/**
* @brief IndexedProperty
*
* This is a proxy object of indexed property getter and setter
*
*/
class IndexedProperty {
protected:
value_ref_t this_;
value_ref_t index_;
public:
IndexedProperty() {}
IndexedProperty(value_ref_t target, value_ref_t index)
: this_(target), index_(index) {}
IndexedProperty& operator=(const value_ref_t &val) {
JSERR_TO_EXCEPTION(JsSetIndexedProperty(this_, index_, val));
return *this;
}
operator value_ref_t() const {
value_ref_t out;
JsGetIndexedProperty(this_, index_, out.addr());
return out;
}
};
/**
* @brief The accessor of JsArray
*
*/
class array_accessor_ : public object_accessor_<_Array> {
public:
/**
* @brief Get the length of the array
*
* @return uint32_t
*/
uint32_t Length() {
propid_t id = PropertyId("length");
if (!id)
return 0;
value_ref_t length = GetProperty(id);
if (!length.is<_Number>())
return 0;
return GetAs<Int>(length);
}
/**
* @brief Get the indexed property.
*
* Just call JsSetIndexedProperty.
*
* @param index The index of the array to get.
* @return value_ref_t The js value.
*/
value_ref_t GetItem(Int index) {
auto i = Just<Int>(index);
value_ref_t out;
JsGetIndexedProperty(get(), i, out.addr());
return out;
}
/**
* @brief Set the indexed property
*
* Just call JsSetIndexedProperty.
*
* @param index
* @param value
* @return true
* @return false
*/
bool SetItem(Int index, value_ref_t value) {
auto i = Just<Int>(index);
auto err = JsSetIndexedProperty(get(), i, value);
return err == JsNoError;
}
/**
* @brief Set or Get indexed property
*
* @param index The index of the property.
* @return IndexedProperty The getter and setter proxy object.
*/
IndexedProperty operator[](Int index) {
return IndexedProperty(*this, Just<Int>(index));
}
public:
/**
* @brief Create a new array with special length.
*
* @param len
* @return value_ref_t
*/
static value_ref_t Create(uint32_t len) {
value_ref_t out;
JsCreateArray(len, out.addr());
return out;
}
};
using Array = base_value_<array_accessor_>;
}; // namespace js