8 # define alloca __builtin_alloca
10 # define alloca __alloca
11 #elif defined _MSC_VER
13 # define alloca _alloca
19 void *alloca (size_t);
23 #include "embryo_private.h"
25 #define STRSET(ep, par, str) { \
26 Embryo_Cell *___cptr; \
27 if ((___cptr = embryo_data_address_get(ep, par))) { \
28 embryo_data_string_set(ep, str, ___cptr); \
31 /* exported args api */
34 _embryo_args_numargs(Embryo_Program *ep, Embryo_Cell *params __UNUSED__)
40 hdr = (Embryo_Header *)ep->base;
41 data = ep->base + (int)hdr->dat;
42 bytes = *(Embryo_Cell *)(data + (int)ep->frm +
43 (2 * sizeof(Embryo_Cell)));
44 return bytes / sizeof(Embryo_Cell);
48 _embryo_args_getarg(Embryo_Program *ep, Embryo_Cell *params)
54 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
55 hdr = (Embryo_Header *)ep->base;
56 data = ep->base + (int)hdr->dat;
57 val = *(Embryo_Cell *)(data + (int)ep->frm +
58 (((int)params[1] + 3) * sizeof(Embryo_Cell)));
59 val += params[2] * sizeof(Embryo_Cell);
60 val = *(Embryo_Cell *)(data + (int)val);
65 _embryo_args_setarg(Embryo_Program *ep, Embryo_Cell *params)
71 if (params[0] != (3 * sizeof(Embryo_Cell))) return 0;
72 hdr = (Embryo_Header *)ep->base;
73 data = ep->base + (int)hdr->dat;
74 val = *(Embryo_Cell *)(data + (int)ep->frm +
75 (((int)params[1] + 3) * sizeof(Embryo_Cell)));
76 val += params[2] * sizeof(Embryo_Cell);
77 if ((val < 0) || ((val >= ep->hea) && (val < ep->stk))) return 0;
78 *(Embryo_Cell *)(data + (int)val) = params[3];
83 _embryo_args_getsarg(Embryo_Program *ep, Embryo_Cell *params)
87 Embryo_Cell base_cell;
91 /* params[1] = arg_no */
93 /* params[3] = buflen */
94 if (params[0] != (3 * sizeof(Embryo_Cell))) return 0;
95 if (params[3] <= 0) return 0; /* buflen must be > 0 */
96 hdr = (Embryo_Header *)ep->base;
97 data = ep->base + (int)hdr->dat;
98 base_cell = *(Embryo_Cell *)(data + (int)ep->frm +
99 (((int)params[1] + 3) * sizeof(Embryo_Cell)));
101 s = alloca(params[3]);
103 while (i < params[3])
105 int offset = base_cell + (i * sizeof(Embryo_Cell));
107 s[i] = *(Embryo_Cell *)(data + offset);
112 STRSET(ep, params[2], s);
114 return i - 1; /* characters written minus terminator */
117 /* functions used by the rest of embryo */
120 _embryo_args_init(Embryo_Program *ep)
122 embryo_program_native_call_add(ep, "numargs", _embryo_args_numargs);
123 embryo_program_native_call_add(ep, "getarg", _embryo_args_getarg);
124 embryo_program_native_call_add(ep, "setarg", _embryo_args_setarg);
125 embryo_program_native_call_add(ep, "getfarg", _embryo_args_getarg);
126 embryo_program_native_call_add(ep, "setfarg", _embryo_args_setarg);
127 embryo_program_native_call_add(ep, "getsarg", _embryo_args_getsarg);