-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMisc.h
145 lines (134 loc) · 3.86 KB
/
Misc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#if 1
/* File: Miscellaneous
* Various functions to help interface tinypy.
*/
inline tp_obj _tp_dcall(tp_vm *tp, tp_obj fnc(tp_vm *tp)) {
return fnc(tp);
}
inline tp_obj _tp_tcall(tp_vm *tp, tp_obj fnc) {
if (fnc.fnc.ftype & 2) {
_tp_list_insert(tp, tp->params.list.val, 0, fnc.fnc.info->self);
}
return _tp_dcall(tp, (tp_obj(*)(tp_vm *))fnc.fnc.cfnc);
}
inline tp_obj tp_fnc_new(tp_vm *tp, int t, void *v, tp_obj c, tp_obj s, tp_obj g) {
tp_obj r = { TP_FNC };
_tp_fnc *info = (_tp_fnc*)calloc(sizeof(_tp_fnc), 1);
info->code = c;
info->self = s;
info->globals = g;
r.fnc.ftype = t;
r.fnc.info = info;
r.fnc.cfnc = v;
return tp_track(tp, r);
}
inline tp_obj tp_def(tp_vm *tp, tp_obj code, tp_obj g) {
tp_obj r = tp_fnc_new(tp, 1, 0, code, tp_None, g);
return r;
}
/* Function: tp_fnc
* Creates a new tinypy function object.
*
* This is how you can create a tinypy function object which, when called in
* the script, calls the provided C function.
*/
inline tp_obj tp_fnc(tp_vm *tp, tp_obj v(tp_vm *tp)) {
return tp_fnc_new(tp, 0, v, tp_None, tp_None, tp_None);
}
inline tp_obj tp_method(tp_vm *tp, tp_obj self, tp_obj v(tp_vm *tp)) {
return tp_fnc_new(tp, 2, v, tp_None, self, tp_None);
}
/* Function: tp_data
* Creates a new data object.
*
* Parameters:
* magic - An integer number associated with the data type. This can be used
* to check the type of data objects.
* v - A pointer to user data. Only the pointer is stored in the object,
* you keep all responsibility for the data it points to.
*
*
* Returns:
* The new data object.
*
* Public fields:
* The following fields can be access in a data object:
*
* magic - An integer number stored in the object.
* val - The data pointer of the object.
* info->free - If not NULL, a callback function called when the object gets
* destroyed.
*
* Example:
* > void *__free__(tp_vm *tp, tp_obj self)
* > {
* > free(self.data.val);
* > }
* >
* > tp_obj my_obj = tp_data(tp_vm *tp, 0, my_ptr);
* > my_obj.data.info->free = __free__;
*/
inline tp_obj tp_data(tp_vm *tp, int magic, void *v) {
tp_obj r = { TP_DATA };
r.data.info = (_tp_data*)calloc(sizeof(_tp_data), 1);
r.data.val = v;
r.data.magic = magic;
return tp_track(tp, r);
}
/* Function: tp_params
* Initialize the tinypy parameters.
*
* When you are calling a tinypy function, you can use this to initialize the
* list of parameters getting passed to it. Usually, you may want to use
* <tp_params_n> or <tp_params_v>.
*/
inline tp_obj tp_params(tp_vm *tp) {
tp_obj r;
tp->params = tp->_params.list.val->items[tp->cur];
r = tp->_params.list.val->items[tp->cur];
r.list.val->len = 0;
return r;
}
/* Function: tp_params_n
* Specify a list of objects as function call parameters.
*
* See also: <tp_params>, <tp_params_v>
*
* Parameters:
* n - The number of parameters.
* argv - A list of n tinypy objects, which will be passed as parameters.
*
* Returns:
* The parameters list. You may modify it before performing the function call.
*/
inline tp_obj tp_params_n(tp_vm *tp, int n, tp_obj argv[]) {
tp_obj r = tp_params(tp);
int i; for (i = 0; i < n; i++) { _tp_list_append(tp, r.list.val, argv[i]); }
return r;
}
/* Function: tp_params_v
* Pass parameters for a tinypy function call.
*
* When you want to call a tinypy method, then you use this to pass parameters
* to it.
*
* Parameters:
* n - The number of variable arguments following.
* ... - Pass n tinypy objects, which a subsequently called tinypy method will
* receive as parameters.
*
* Returns:
* A tinypy list object representing the current call parameters. You can modify
* the list before doing the function call.
*/
inline tp_obj tp_params_v(tp_vm *tp, int n, ...) {
int i;
tp_obj r = tp_params(tp);
va_list a; va_start(a, n);
for (i = 0; i < n; i++) {
_tp_list_append(tp, r.list.val, va_arg(a, tp_obj));
}
va_end(a);
return r;
}
#endif