-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathfromiterator.h
More file actions
156 lines (132 loc) · 3.36 KB
/
fromiterator.h
File metadata and controls
156 lines (132 loc) · 3.36 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
/**
* FromIterator.h
*
* Class that turns a PHP traversable object into something that
* is iterable in a javascript environment too.
*
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
* @copyright 2025 Copernica BV
*/
/**
* Include guard
*/
#pragma once
/**
* Dependencies
*/
#include "fromphp.h"
/**
* Begin of namespace
*/
namespace JS {
/**
* Class definition
*/
class FromIterator
{
private:
/**
* The variable that represents the iterator
* @var v8::Local<v8::Object>
*/
v8::Local<v8::Object> _iterator;
/**
* Structure that holds the data associated with the iterator,
* and that is added via a symbol in the javascript-object
*/
class Data
{
private:
/**
* Reference to the isolate
* @var v8::Isolate
*/
v8::Isolate *_isolate;
/**
* The PHP space Iterator object
* @var Php::Value
*/
Php::Value _value;
public:
/**
* Constructor
* @param isolate
* @param iterator
*/
Data(v8::Isolate *isolate, const Php::Value &value) :
_isolate(isolate), _value(value) {}
/**
* Destructor
*/
virtual ~Data() = default;
/**
* Is the iterator still valid?
* @return bool
*/
bool valid() { return _value.call("valid"); }
/**
* Is the iterator done?
* @return bool
*/
bool done() { return !valid(); }
/**
* Get the current value
* @return Php::Value
*/
Php::Value value() { return _value.call("current"); }
/**
* Get the current value
* @return v8::Local
*/
v8::Local<v8::Value> current() { return FromPhp(_isolate, _value.call("current")); }
/**
* Proceed to the next record
* @return bool
*/
bool next() { _value.call("next"); return valid(); }
};
/**
* Helper method to get access to the underlying data
* @param isolate
* @param obj
* @return Data*
*/
static Data *restore(v8::Isolate *isolate, const v8::Local<v8::Object> &obj);
/**
* Destruct internally stored data
* @param isolate
* @param obj
* @return Data*
*/
static void destruct(v8::Isolate *isolate, const v8::Local<v8::Object> &obj);
/**
* Method that is called by v8 when the next item is requested
* @param args
*/
static void nxtmethod(const v8::FunctionCallbackInfo<v8::Value> &args);
/**
* Method that is called when the iterator leaps out prematurely
* @param args
*/
static void retmethod(const v8::FunctionCallbackInfo<v8::Value> &args);
public:
/**
* Constructor
* @param isolate the isolate
* @param value iterable php object
*/
FromIterator(v8::Isolate *isolate, const Php::Value &value);
/**
* Private destructor (object is self-destructing)
*/
virtual ~FromIterator() = default;
/**
* Cast to the local object
* @return v8::Local<v8::Object>
*/
v8::Local<v8::Object> &value() { return _iterator; }
};
/**
* End of namespace
*/
}