forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPyStack.h
190 lines (155 loc) · 4.22 KB
/
PyStack.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
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
// Copyright 2023 Disintar LLP / [email protected]
#include "third-party/pybind11/include/pybind11/pybind11.h"
#include <string>
#include <utility>
#include <optional>
#include "block/block.h"
#include "block/block-parse.h"
#include "vm/dumper.hpp"
#include "crypto/vm/cellslice.h"
#include "crypto/vm/cells/CellBuilder.h"
#include "PyCellSlice.h"
#include "PyCell.h"
#include "PyCellBuilder.h"
#include "crypto/fift/Fift.h"
#include "crypto/fift/IntCtx.h"
#include "crypto/fift/words.h"
#include "td/utils/filesystem.h"
#include "crypto/vm/stack.hpp"
#include "td/utils/optional.h"
#ifndef TON_STACK_H
#define TON_STACK_H
class PyContinuation {
public:
td::Ref<vm::Continuation> cnt;
PyContinuation(PyCellSlice cs) {
cnt = td::Ref<vm::Continuation>();
cnt->deserialize(cs.my_cell_slice);
}
PyContinuation(td::Ref<vm::Continuation> cnt_) {
cnt = std::move(cnt_);
}
std::string type() {
return cnt->type();
}
PyCell serialize() {
vm::CellBuilder cb;
cnt->serialize(cb);
return PyCell(cb.finalize());
}
};
class PyStack;
class PyStackEntry {
public:
PyStackEntry(td::optional<PyCell> cell = td::optional<PyCell>(),
td::optional<PyCellSlice> cell_slice = td::optional<PyCellSlice>(),
td::optional<PyCellSlice> cell_builder = td::optional<PyCellSlice>(),
td::optional<PyContinuation> continuation = td::optional<PyContinuation>(), std::string big_int = "") {
if (cell) {
entry = vm::StackEntry(cell.value().my_cell);
} else if (cell_slice) {
entry = vm::StackEntry(vm::Ref<vm::CellSlice>{false, cell_slice.value().my_cell_slice});
} else if (cell_builder) {
auto cb = td::Ref<vm::CellBuilder>{true};
cb.write().append_cellslice(cell_builder.value().my_cell_slice);
entry = vm::StackEntry(cb);
} else if (!big_int.empty()) {
td::RefInt256 x = td::string_to_int256(big_int);
entry = vm::StackEntry(x);
} else if (continuation) {
entry = vm::StackEntry(continuation.value().cnt);
} else {
entry = vm::StackEntry();
}
};
PyStackEntry(td::Ref<vm::Stack> stk) {
entry = vm::StackEntry(stk);
}
PyStackEntry(vm::StackEntry stk) {
entry = std::move(stk);
}
vm::StackEntry entry;
int type() {
return entry.type();
}
PyCell as_cell() {
return PyCell(entry.as_cell());
}
std::vector<PyStackEntry> as_tuple() {
auto x = entry.as_tuple();
std::vector<PyStackEntry> tmp;
for (const auto& e : *x) {
tmp.push_back(PyStackEntry(e));
}
return tmp;
}
PyContinuation as_cont() {
auto x = entry.as_cont();
return PyContinuation(x);
}
PyCellSlice as_cell_slice() {
auto x = entry.as_slice();
vm::CellBuilder cb;
cb.append_cellslice(x);
td::Ref<vm::Cell> cell = cb.finalize(x->is_special());
return PyCellSlice(std::move(cell));
}
PyCellBuilder as_cell_builder() {
auto x = entry.as_builder();
bool special;
auto cs = vm::load_cell_slice_special(x->finalize_copy(), special);
return PyCellBuilder(cs);
}
std::string as_int() {
auto x = entry.as_int();
return x->to_dec_string();
}
std::string as_string() {
return entry.as_string();
}
PyCell serialize(int mode = 0) {
vm::CellBuilder cb;
auto x = entry.serialize(cb, mode);
if (!x) {
throw std::invalid_argument("Can't serialize object");
}
return PyCell(cb.finalize());
}
};
PyStackEntry make_tuple(std::vector<PyStackEntry> e);
class PyStack {
public:
PyStack() {
stack = vm::Stack();
}
PyStack(vm::Stack x) {
stack = std::move(x);
}
PyStackEntry at(int idx) {
return PyStackEntry(stack.at(idx));
}
void push(PyStackEntry value) {
stack.push(value.entry);
}
PyStackEntry pop() {
return PyStackEntry(stack.pop());
}
PyCell serialize(int mode = 0) {
vm::CellBuilder cb;
auto x = stack.serialize(cb, mode);
if (!x) {
throw std::invalid_argument("Can't serialize object");
}
return PyCell(cb.finalize());
}
bool is_empty() {
return stack.is_empty();
}
int depth() {
return stack.depth();
}
vm::Stack stack;
};
PyStackEntry deserialize_stack_entry(PyCellSlice cs);
PyStack deserialize_stack(PyCellSlice cs);
#endif //TON_STACK_H