Skip to content

Commit 2104cf1

Browse files
committed
Change table access to optionally not create tables if they do not exist
Fixes table Get operations previously created tables and sub tables without option to not
1 parent 79dfaa8 commit 2104cf1

2 files changed

Lines changed: 48 additions & 16 deletions

File tree

src/buzz/argos/buzz_loop_functions.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,20 @@ void BuzzPut(buzzvm_t t_vm,
4949
/****************************************/
5050
/****************************************/
5151

52-
void BuzzTableOpen(buzzvm_t t_vm,
53-
const std::string& str_var) {
52+
bool BuzzTableOpen(buzzvm_t t_vm,
53+
const std::string& str_var,
54+
bool b_create) {
5455
buzzvm_pushs(t_vm, buzzvm_string_register(t_vm, str_var.c_str(), 1));
5556
buzzvm_dup(t_vm);
5657
buzzvm_gload(t_vm);
5758
if(!buzzobj_istable(buzzvm_stack_at(t_vm, 1))) {
59+
if(!b_create) {
60+
return false;
61+
}
5862
buzzvm_pop(t_vm);
5963
buzzvm_pusht(t_vm);
6064
}
65+
return true;
6166
}
6267

6368
/****************************************/
@@ -168,49 +173,64 @@ void BuzzTablePut(buzzvm_t t_vm,
168173
/****************************************/
169174
/****************************************/
170175

171-
void BuzzTableOpenNested(buzzvm_t t_vm,
172-
int n_key) {
176+
bool BuzzTableOpenNested(buzzvm_t t_vm,
177+
int n_key,
178+
bool b_create) {
173179
buzzvm_dup(t_vm);
174180
buzzvm_pushi(t_vm, n_key);
175181
buzzvm_push(t_vm, buzzvm_stack_at(t_vm, 2));
176182
buzzvm_pushi(t_vm, n_key);
177183
buzzvm_tget(t_vm);
178184
if(!buzzobj_istable(buzzvm_stack_at(t_vm, 1))) {
185+
if(!b_create) {
186+
return false;
187+
}
179188
buzzvm_pop(t_vm);
180189
buzzvm_pusht(t_vm);
181190
}
191+
return true;
182192
}
183193

184194
/****************************************/
185195
/****************************************/
186196

187-
void BuzzTableOpenNested(buzzvm_t t_vm,
188-
float f_key) {
197+
bool BuzzTableOpenNested(buzzvm_t t_vm,
198+
float f_key,
199+
bool b_create) {
189200
buzzvm_dup(t_vm);
190201
buzzvm_pushf(t_vm, f_key);
191202
buzzvm_push(t_vm, buzzvm_stack_at(t_vm, 2));
192203
buzzvm_pushf(t_vm, f_key);
193204
buzzvm_tget(t_vm);
194205
if(!buzzobj_istable(buzzvm_stack_at(t_vm, 1))) {
206+
if(!b_create) {
207+
return false;
208+
}
195209
buzzvm_pop(t_vm);
196210
buzzvm_pusht(t_vm);
197211
}
212+
return true;
198213
}
199214

200215
/****************************************/
201216
/****************************************/
202217

203-
void BuzzTableOpenNested(buzzvm_t t_vm,
204-
const std::string& str_key) {
218+
bool BuzzTableOpenNested(buzzvm_t t_vm,
219+
const std::string& str_key,
220+
bool b_create) {
205221
buzzvm_dup(t_vm);
206222
buzzvm_pushs(t_vm, buzzvm_string_register(t_vm, str_key.c_str(), 0));
207223
buzzvm_push(t_vm, buzzvm_stack_at(t_vm, 2));
208224
buzzvm_pushs(t_vm, buzzvm_string_register(t_vm, str_key.c_str(), 0));
209225
buzzvm_tget(t_vm);
210226
if(!buzzobj_istable(buzzvm_stack_at(t_vm, 1))) {
227+
if(!b_create) {
228+
return false;
229+
}
211230
buzzvm_pop(t_vm);
212231
buzzvm_pusht(t_vm);
213232
}
233+
return true;
214234
}
215235

216236
/****************************************/

src/buzz/argos/buzz_loop_functions.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ void BuzzPut(buzzvm_t t_vm,
7171
*
7272
* @param t_vm The Buzz VM.
7373
* @param str_var The variable name that stores the table.
74+
* @param b_create Make the table if does not exists [default = true].
75+
* @return bool if successfully opened the table.
7476
* @see BuzzTableClose
7577
*/
76-
void BuzzTableOpen(buzzvm_t t_vm,
77-
const std::string& str_var);
78+
bool BuzzTableOpen(buzzvm_t t_vm,
79+
const std::string& str_var,
80+
bool b_create = true);
7881

7982
/**
8083
* Closes the currently open table and stores it as a global variable.
@@ -221,10 +224,13 @@ void BuzzTablePut(buzzvm_t t_vm,
221224
*
222225
* @param t_vm The Buzz VM.
223226
* @param n_key The key.
227+
* @param b_create Make the table if does not exists [default = true].
228+
* @return bool if successfully opened the table.
224229
* @see BuzzTableCloseNested
225230
*/
226-
void BuzzTableOpenNested(buzzvm_t t_vm,
227-
int n_key);
231+
bool BuzzTableOpenNested(buzzvm_t t_vm,
232+
int n_key,
233+
bool b_create = true);
228234

229235
/**
230236
* Opens a table nested in the currently open table.
@@ -234,10 +240,13 @@ void BuzzTableOpenNested(buzzvm_t t_vm,
234240
*
235241
* @param t_vm The Buzz VM.
236242
* @param f_key The key.
243+
* @param b_create Make the table if does not exists [default = true].
244+
* @return bool if successfully opened the table.
237245
* @see BuzzTableCloseNested
238246
*/
239-
void BuzzTableOpenNested(buzzvm_t t_vm,
240-
float f_key);
247+
bool BuzzTableOpenNested(buzzvm_t t_vm,
248+
float f_key,
249+
bool b_create = true);
241250

242251
/**
243252
* Opens a table nested in the currently open table.
@@ -247,10 +256,13 @@ void BuzzTableOpenNested(buzzvm_t t_vm,
247256
*
248257
* @param t_vm The Buzz VM.
249258
* @param str_key The key.
259+
* @param b_create Make the table if does not exists [default = true].
260+
* @return bool if successfully opened the table.
250261
* @see BuzzTableCloseNested
251262
*/
252-
void BuzzTableOpenNested(buzzvm_t t_vm,
253-
const std::string& str_key);
263+
bool BuzzTableOpenNested(buzzvm_t t_vm,
264+
const std::string& str_key,
265+
bool b_create = true);
254266

255267
/**
256268
* Closes the currently open table and stores it in the parent table.

0 commit comments

Comments
 (0)