Skip to content

Commit

Permalink
Change table access to optionally not create tables if they do not exist
Browse files Browse the repository at this point in the history
  Fixes table Get operations previously created tables and sub tables without option to not
  • Loading branch information
dcat52 committed Mar 8, 2022
1 parent 79dfaa8 commit 2104cf1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
36 changes: 28 additions & 8 deletions src/buzz/argos/buzz_loop_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@ void BuzzPut(buzzvm_t t_vm,
/****************************************/
/****************************************/

void BuzzTableOpen(buzzvm_t t_vm,
const std::string& str_var) {
bool BuzzTableOpen(buzzvm_t t_vm,
const std::string& str_var,
bool b_create) {
buzzvm_pushs(t_vm, buzzvm_string_register(t_vm, str_var.c_str(), 1));
buzzvm_dup(t_vm);
buzzvm_gload(t_vm);
if(!buzzobj_istable(buzzvm_stack_at(t_vm, 1))) {
if(!b_create) {
return false;
}
buzzvm_pop(t_vm);
buzzvm_pusht(t_vm);
}
return true;
}

/****************************************/
Expand Down Expand Up @@ -168,49 +173,64 @@ void BuzzTablePut(buzzvm_t t_vm,
/****************************************/
/****************************************/

void BuzzTableOpenNested(buzzvm_t t_vm,
int n_key) {
bool BuzzTableOpenNested(buzzvm_t t_vm,
int n_key,
bool b_create) {
buzzvm_dup(t_vm);
buzzvm_pushi(t_vm, n_key);
buzzvm_push(t_vm, buzzvm_stack_at(t_vm, 2));
buzzvm_pushi(t_vm, n_key);
buzzvm_tget(t_vm);
if(!buzzobj_istable(buzzvm_stack_at(t_vm, 1))) {
if(!b_create) {
return false;
}
buzzvm_pop(t_vm);
buzzvm_pusht(t_vm);
}
return true;
}

/****************************************/
/****************************************/

void BuzzTableOpenNested(buzzvm_t t_vm,
float f_key) {
bool BuzzTableOpenNested(buzzvm_t t_vm,
float f_key,
bool b_create) {
buzzvm_dup(t_vm);
buzzvm_pushf(t_vm, f_key);
buzzvm_push(t_vm, buzzvm_stack_at(t_vm, 2));
buzzvm_pushf(t_vm, f_key);
buzzvm_tget(t_vm);
if(!buzzobj_istable(buzzvm_stack_at(t_vm, 1))) {
if(!b_create) {
return false;
}
buzzvm_pop(t_vm);
buzzvm_pusht(t_vm);
}
return true;
}

/****************************************/
/****************************************/

void BuzzTableOpenNested(buzzvm_t t_vm,
const std::string& str_key) {
bool BuzzTableOpenNested(buzzvm_t t_vm,
const std::string& str_key,
bool b_create) {
buzzvm_dup(t_vm);
buzzvm_pushs(t_vm, buzzvm_string_register(t_vm, str_key.c_str(), 0));
buzzvm_push(t_vm, buzzvm_stack_at(t_vm, 2));
buzzvm_pushs(t_vm, buzzvm_string_register(t_vm, str_key.c_str(), 0));
buzzvm_tget(t_vm);
if(!buzzobj_istable(buzzvm_stack_at(t_vm, 1))) {
if(!b_create) {
return false;
}
buzzvm_pop(t_vm);
buzzvm_pusht(t_vm);
}
return true;
}

/****************************************/
Expand Down
28 changes: 20 additions & 8 deletions src/buzz/argos/buzz_loop_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ void BuzzPut(buzzvm_t t_vm,
*
* @param t_vm The Buzz VM.
* @param str_var The variable name that stores the table.
* @param b_create Make the table if does not exists [default = true].
* @return bool if successfully opened the table.
* @see BuzzTableClose
*/
void BuzzTableOpen(buzzvm_t t_vm,
const std::string& str_var);
bool BuzzTableOpen(buzzvm_t t_vm,
const std::string& str_var,
bool b_create = true);

/**
* Closes the currently open table and stores it as a global variable.
Expand Down Expand Up @@ -221,10 +224,13 @@ void BuzzTablePut(buzzvm_t t_vm,
*
* @param t_vm The Buzz VM.
* @param n_key The key.
* @param b_create Make the table if does not exists [default = true].
* @return bool if successfully opened the table.
* @see BuzzTableCloseNested
*/
void BuzzTableOpenNested(buzzvm_t t_vm,
int n_key);
bool BuzzTableOpenNested(buzzvm_t t_vm,
int n_key,
bool b_create = true);

/**
* Opens a table nested in the currently open table.
Expand All @@ -234,10 +240,13 @@ void BuzzTableOpenNested(buzzvm_t t_vm,
*
* @param t_vm The Buzz VM.
* @param f_key The key.
* @param b_create Make the table if does not exists [default = true].
* @return bool if successfully opened the table.
* @see BuzzTableCloseNested
*/
void BuzzTableOpenNested(buzzvm_t t_vm,
float f_key);
bool BuzzTableOpenNested(buzzvm_t t_vm,
float f_key,
bool b_create = true);

/**
* Opens a table nested in the currently open table.
Expand All @@ -247,10 +256,13 @@ void BuzzTableOpenNested(buzzvm_t t_vm,
*
* @param t_vm The Buzz VM.
* @param str_key The key.
* @param b_create Make the table if does not exists [default = true].
* @return bool if successfully opened the table.
* @see BuzzTableCloseNested
*/
void BuzzTableOpenNested(buzzvm_t t_vm,
const std::string& str_key);
bool BuzzTableOpenNested(buzzvm_t t_vm,
const std::string& str_key,
bool b_create = true);

/**
* Closes the currently open table and stores it in the parent table.
Expand Down

0 comments on commit 2104cf1

Please sign in to comment.