- Subject: Re: [slang-users] Qualifiers and C
- From: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- Date: Mon, 1 Feb 2021 09:38:00 -0600
Interesting approach, however what if I would want to pass multiple
qualifier-provided information to the back-implementation functions
(editfile_func, moveup_func – the pointers in the ActionTable)? And I would
want to provide them as a via-qualifier passed SLang struct, not multiple
qualifiers separately for each information?
I guess that in the last paragraph you've shown the direction: I could
provide SLang-defined frontend action() function that would be able to
handle the struct qualifier value (as passing structs seem possible at
SLang level, i.e.: variable stru = qualifier("info_in_struct_qualifier")
seems to work fine), to then decompose the struct and pass the obtained
information as the fixed argument list to an intrinsic, C-implemented
function? Or even instantiate and fill a S-Lang struct properly mapped to a
C struct and pass it as a pointer to a C-level c_action() function, that
would do the routing to editfile_func, etc.
I think that a next version of libSLang could have this nice new feature
added: a function to get structs via qualifiers, in C, i.e.:
SLang_get_struct_qualifier(name, out_ptr, default_value_ptr)
It seems to me that the outptr could/should even be opaque.
BTW. Is there a reflection mechanism at S-Lang level or even at C-level? To
get the fields of the struct, something like:
stru = @SomeStruct_Type;
variable fields;
fields = get_struct_fields(stru);
On Sun, 31 Jan 2021 at 21:20, John E. Davis <jed@xxxxxxxxxxx> wrote:
> Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> wrote:
> > Hi,
> > I would like to make a data qualifier for a function that I export with
> > Slirp:
> >
> > action("EditFile"; data="/etc/bashrc");
> >
> > I could have used SLang_get_string_qualifier() for this, which is OK.
> > However I also would like to allow integer qualifiers. like e.g.:
> >
> > action("MoveUp"; data=5)
> >
> > I don't see any function that would allow to test the qualifier type.
> > Should I just first try to get integer with some special default value?
>
> There is no API function that does this.
>
> It seems to me that somewhere you need to map the strings "EditFile"
> and "MoveUp" to the corresponding functions. Also, if the data
> qualifier is not present, default values will have to be provided.
> Here is one suggestion: Regard the following as pseudocode:
>
> typedef struct _Action_Type
> {
> char *action_name;
> int qualifier_type;
> int (*action_funct)(struct _Action_Type *a, void *vdata);
> }
> Action_Type;
>
> static int editfile_func (Action_Type *a, void *vdata)
> {
> char *data = "default";
> if (vdata ! NULL) data = (char *)vdata;
> .
> .
> }
>
> static int moveup_func (Action_Type *a, void *vdata)
> {
> int data = 1;
> if (vdata ! NULL) data = *(int *)vdata;
> .
> .
> }
>
> Action_Type Action_Table [] =
> {
> {"EditFile", SLANG_STRING_TYPE, editfile_func},
> {"MoveUp", SLANG_INT_TYPE, moveup_func},
> .
> .
> {NULL, -1}
> }
>
> void action_intrinsic (char *action_name)
> {
> Action_Type *a;
> a = Action_Table;
> while (a->action_name != NULL)
> {
> if (0 == strcmp (a->action_name, action_name))
> {
> int status;
>
> if (0 == SLang_qualifier_exists ("data"))
> return (*a->action_funct)(a, NULL);
>
> switch (a->qualifier_type)
> {
> int i; char *s;
>
> case SLANG_INT_TYPE:
> if (-1 == SLang_get_int_qualifier (a, &i, 0))
> return -1;
> status = (*a->action_funct)(a, &i);
> break;
>
> case SLANG_STRING_TYPE:
> if (-1 == SLang_get_string_qualifier (a, &s, NULL))
> return -1;
> status = (*a->action_funct)(a, s);
> SLang_free_slstring (s);
> break;
> }
> if (status == -1)
> {
> /* Handle error */
> }
> return;
> }
> a++;
> }
>
> /* Error, action_name not supported */
> }
>
> > Also, is it possible to pass a struct as a qualifiers? How to get such
> > struct on the C side? Like e.g.:
> >
> > stru = @ActionData_Type;
> > stru.filename="/etc/bashrc";
> > stru.mode="rw";
> > action("EditFile"; data=stru);
>
> This is not supported by the API. For complex cases such as this, I
> suggest that you split the function between a high-level "public"
> interface written in slang, and a lower level "private" interface that
> accepts a fixed argument list.
>
> I hope this helps.
> --John
> _______________________________________________
> For list information, visit <http://jedsoft.org/slang/mailinglists.html>.
>
>
--
Sebastian Gniazdowski
[2021 date index]
[2021 thread index]
[Thread Prev] [Thread Next]
[Date Prev] [Date Next]