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
use super::{ForeignFunctionCallbackResult, ForeignFunctionContext};
use stoffel_vm_types::core_types::{ArrayRef, ObjectRef, TableRef, Value};
impl<'a> ForeignFunctionContext<'a> {
/// Semantically read a table field through the configured memory backend.
///
/// This uses a mutable memory borrow so ORAM-style backends can update
/// their internal state during logical reads.
pub fn read_table_field(
&mut self,
table_ref: TableRef,
key: &Value,
) -> ForeignFunctionCallbackResult<Option<Value>> {
Ok(self.services.try_read_table_field(table_ref, key)?)
}
/// Semantically read an array length through the configured memory backend.
///
/// This uses a mutable memory borrow so ORAM-style backends can update
/// their internal state during logical length reads.
pub fn read_array_ref_len(
&mut self,
array_ref: ArrayRef,
) -> ForeignFunctionCallbackResult<usize> {
self.read_array_len(array_ref)
}
/// Semantically read an array length through the configured memory backend.
///
/// This uses a mutable memory borrow so ORAM-style backends can update
/// their internal state during logical length reads.
pub fn read_array_len(&mut self, array_ref: ArrayRef) -> ForeignFunctionCallbackResult<usize> {
Ok(self.services.try_read_array_ref_len(array_ref)?)
}
/// Semantically read an object length through the configured memory backend.
///
/// This uses a mutable memory borrow so ORAM-style backends can update
/// their internal state during logical object-shape reads.
pub fn read_object_ref_len(
&mut self,
object_ref: ObjectRef,
) -> ForeignFunctionCallbackResult<usize> {
self.read_object_len(object_ref)
}
/// Semantically read an object length through the configured memory backend.
///
/// This uses a mutable memory borrow so ORAM-style backends can update
/// their internal state during logical object-shape reads.
pub fn read_object_len(
&mut self,
object_ref: ObjectRef,
) -> ForeignFunctionCallbackResult<usize> {
Ok(self.services.try_read_object_ref_len(object_ref)?)
}
/// Semantically read object entries through the configured memory backend.
///
/// This uses a mutable memory borrow so ORAM-style backends can update
/// their internal state during logical object reads.
pub fn read_object_ref_entries(
&mut self,
object_ref: ObjectRef,
limit: usize,
) -> ForeignFunctionCallbackResult<Vec<(Value, Value)>> {
self.read_object_entries(object_ref, limit)
}
/// Semantically read object entries through the configured memory backend.
///
/// This uses a mutable memory borrow so ORAM-style backends can update
/// their internal state during logical object reads.
pub fn read_object_entries(
&mut self,
object_ref: ObjectRef,
limit: usize,
) -> ForeignFunctionCallbackResult<Vec<(Value, Value)>> {
Ok(self
.services
.try_read_object_ref_entries(object_ref, limit)?)
}
/// Semantically read a VM byte array (`Value::Array` of `Value::U8`).
pub fn read_byte_array(&mut self, value: &Value) -> ForeignFunctionCallbackResult<Vec<u8>> {
Ok(self.services.read_byte_array(value)?)
}
/// Create a VM byte array (`Value::Array` of `Value::U8`) from raw bytes.
pub fn create_byte_array(&mut self, bytes: &[u8]) -> ForeignFunctionCallbackResult<Value> {
Ok(self.services.create_byte_array(bytes)?)
}
/// Create a VM table object and return its typed handle.
pub fn create_object_ref(&mut self) -> ForeignFunctionCallbackResult<ObjectRef> {
Ok(self.services.create_object_ref()?)
}
/// Create a VM table array and return its typed handle.
pub fn create_array_ref(&mut self, capacity: usize) -> ForeignFunctionCallbackResult<ArrayRef> {
Ok(self.services.create_array_ref(capacity)?)
}
/// Create a VM table object through the configured memory backend.
pub fn create_object(&mut self) -> ForeignFunctionCallbackResult<Value> {
Ok(Value::from(self.create_object_ref()?))
}
/// Create a VM table array through the configured memory backend.
pub fn create_array(&mut self, capacity: usize) -> ForeignFunctionCallbackResult<Value> {
Ok(Value::from(self.create_array_ref(capacity)?))
}
/// Set a field on a VM table through the configured memory backend.
pub fn set_table_field(
&mut self,
table_ref: TableRef,
key: Value,
value: Value,
) -> ForeignFunctionCallbackResult<()> {
Ok(self.services.set_table_field(table_ref, key, value)?)
}
/// Append values to a VM array through the configured memory backend.
pub fn push_array_ref_values(
&mut self,
array_ref: ArrayRef,
values: &[Value],
) -> ForeignFunctionCallbackResult<usize> {
self.push_array_values(array_ref, values)
}
/// Append values to a VM array through the configured memory backend.
pub fn push_array_values(
&mut self,
array_ref: ArrayRef,
values: &[Value],
) -> ForeignFunctionCallbackResult<usize> {
Ok(self.services.push_array_ref_values(array_ref, values)?)
}
/// Append this call's argument tail directly to a VM array.
pub fn push_array_args_from(
&mut self,
array_ref: ArrayRef,
start: usize,
function: &'static str,
) -> ForeignFunctionCallbackResult<usize> {
let values = self
.args
.get(start..)
.ok_or_else(|| format!("{function} missing arguments starting at {start}"))?;
Ok(self.services.push_array_ref_values(array_ref, values)?)
}
/// Remove and return an array value by index, shifting following values left.
pub fn pop_array_ref_value(
&mut self,
array_ref: ArrayRef,
index: usize,
) -> ForeignFunctionCallbackResult<Option<Value>> {
Ok(self.services.pop_array_ref_value(array_ref, index)?)
}
/// Insert an array value by index, shifting current and following values right.
pub fn insert_array_ref_value(
&mut self,
array_ref: ArrayRef,
index: usize,
value: Value,
) -> ForeignFunctionCallbackResult<usize> {
Ok(self
.services
.insert_array_ref_value(array_ref, index, value)?)
}
/// Replace all VM-visible array elements.
pub fn replace_array_ref_values(
&mut self,
array_ref: ArrayRef,
values: Vec<Value>,
) -> ForeignFunctionCallbackResult<usize> {
Ok(self.services.replace_array_ref_values(array_ref, values)?)
}
/// Remove all VM-visible array elements.
pub fn clear_array_ref(&mut self, array_ref: ArrayRef) -> ForeignFunctionCallbackResult<()> {
Ok(self.services.clear_array_ref(array_ref)?)
}
/// Reverse VM-visible array elements in place.
pub fn reverse_array_ref(&mut self, array_ref: ArrayRef) -> ForeignFunctionCallbackResult<()> {
Ok(self.services.reverse_array_ref(array_ref)?)
}
/// Create a VM object and populate it with fields.
pub fn create_object_with_fields<I>(
&mut self,
fields: I,
) -> ForeignFunctionCallbackResult<Value>
where
I: IntoIterator<Item = (Value, Value)>,
{
let object_ref = self.create_object_ref()?;
let table_ref = TableRef::from(object_ref);
for (key, value) in fields {
self.set_table_field(table_ref, key, value)?;
}
Ok(Value::from(object_ref))
}
}