paladin-t/my_basic

Set/Retrieve value in BAS from native application.

invpe opened this issue · 5 comments

invpe commented

I found few helper functions, but there's no documentation on the arguments they intake.
Therefore i few questions.

  1. How can i set a 'test=2' value or pass value from native C application to the BAS ?
  2. How can i retrieve the value of 'test' to process in native application after the BAS is executed ?
 struct mb_interpreter_t* bas = NULL;
  mb_init();
  mb_open(&bas);
  if (mb_load_string(bas, "test = 1+2+3; print test", true) == MB_FUNC_OK)
  {
    int iResult = mb_run(bas, true);

    mb_value_t val0;
    void **l = NULL;
    int iRes = mb_get_value_by_name(bas,l,"test",&val0);
    printf("VALU:%i RES:%i\n", val0.value.integer,iRes); 
  }
  else
    printf("Failed\n");

6
VALU:0 RES:0

  1. What are the arguments voidl** in the following functions ?

MBAPI int mb_get_value_by_name(struct mb_interpreter_t* s, void** l, const char* n, mb_value_t* val);
MBAPI int mb_add_var(struct mb_interpreter_t* s, void** l, const char* n, mb_value_t val, bool_t force);
MBAPI int mb_get_var(struct mb_interpreter_t* s, void** l, void** v, bool_t redir);
MBAPI int mb_get_var_name(struct mb_interpreter_t* s, void* v, char** n);
MBAPI int mb_get_var_value(struct mb_interpreter_t* s, void* v, mb_value_t* val);
MBAPI int mb_set_var_value(struct mb_interpreter_t* s, void* v, mb_value_t val);
invpe commented

Found it myself

 void **l = NULL;
  struct mb_interpreter_t* bas = NULL;
  mb_init();
  mb_open(&bas);
  if (mb_load_string(bas, "test = 1+NATIVE; print test", true) == MB_FUNC_OK)
  {
    
    // Define a value
    mb_value_t valAdd;
    valAdd.type=MB_DT_INT;
    valAdd.value.integer = 1;
    mb_add_var(bas,l,"NATIVE",valAdd,true);

    // Run
    int iResult = mb_run(bas, true);
    printf("\n");


    // get value (NEEDS TO BE UPPERCASE)
    mb_value_t valGet;
    int iRes = mb_get_value_by_name(bas,l,"TEST",&valGet);
    printf("VALU:%i TYPE:%i RES:%i\n", valGet.value.integer,valGet.type,iRes);

  }
  else
    printf("Failed\n");

    mb_close(&bas);
    mb_dispose();

Interesting find. Two questions for @paladin-t maybe, at what point is void** l initialized? In the above example, it's first referenced at mb_add_var, does that write something to l? What does l even stand for? And, why does the name of a variable in mb_get_value_by_name need to be uppercase?

invpe commented

Lets' reopen to get more clarity on the follow up questions

void** l stores the program pointer, it is used to determine what's being executed and what will be next step internally. It is exposed to most API for manual save/restore externally sometimes.

The full flow might be following in some cases:

  1. BASIC calls a native API
  2. the native API calls mb_add_var
  3. if there's any error, mb_add_var reports it with the current program position retrieved from void** l

mb_get_value_by_name expects uppercase due to a historical reason. BASIC is case-insensitive, and MY-BASIC stores almost everything in uppercase, then mb_get_value_by_name was introduced at some timepoint after that.

invpe commented

Thanks very much.