tinyscript
tinyscript copied to clipboard
Documentation / Example for Strings in C-Function Calls
Hi,
we are trying to use a tiny script with a string in a parameter call.
it should be like
test( "info" )
would this work in tiny script at all? Sincerely Felix
Unfortunately tinyscript only allows numbers in expressions and function calls right now.
I have solved this by having a (non)standard library with a list data type. Then I have a set of functions exposed to Tinyscript, like list_new, list_dup, list_free, list_pop, list_get, list_push, list_push_ (for pushing two items in one call), list_push__, list_set, list_size, list_truncate. Combined with https://github.com/totalspectrum/tinyscript/pull/2 I can relatively easily build strings, and then just have tiny wrappers that take the list struct I have and do NUL-termination and passing of the data buffer to the actual function.
typedef struct ts_list {
Val size;
uint8_t * data;
Val capacity;
} ts_list;
If there's an interest in having this distributed with the language (I'd guess behind a compilation flag so as to keep the core properly tiny), I'd be willing to open a pull request. I also have helpers for working with double
and CBOR
values.
@daniellandau : Having a list data structure sounds like it would be very useful, so yes, please open a pull request. Thanks!
Now at https://github.com/totalspectrum/tinyscript/pull/6
To answer the original question in more detail.
You still can't
test("info")
But you can now
var arg = list_new(5)
list_push__(arg, 'i', 'n', 'f')
list_push_(arg, 'o', 0)
test(arg)
And if you have a pre-existing C function
void test(char * arg);
You have to define a wrapper
void ts_test(ts_list * arg) {
if (arg->size > 0 && arg->data[arg->size - 1] == '\0')
test(arg->data)
}
...
TinyScript_Define("test", CFUNC(1), (Val)ts_test);
Or something to that effect, like doing NUL-termination on C-side etc.