-
Notifications
You must be signed in to change notification settings - Fork 11
/
mirrors.c
224 lines (204 loc) · 7.08 KB
/
mirrors.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "gracelib.h"
// mirrors.c defines a Grace module "mirrors" which can be compiled for
// dynamic or static linking. The module contains one method:
// reflect(obj : Any) -> Mirror
// Mirrors support three methods:
// methods -> List<MirrorMethod>
// getMethod(name : String) -> MirrorMethod
// annotations -> List<Any>
// MirrorMethods support four methods:
// name -> String
// request(arglists : List<List<Any>>) -> Any
// partcount -> Number
// paramcounts -> List of Numbers
// A sample use might be:
// mirrors.reflect(1).getMethod("+").request([[2]]) == 3
Object mirrors_module = NULL;
ClassData MirrorClass;
ClassData MirrorMethodClass;
Method *findmethodsimple(Object self, const char *name);
struct MirrorObject {
OBJECT_HEADER;
Object obj;
};
struct MirrorMethodObject {
OBJECT_HEADER;
Method *method;
Object obj;
};
Object MirrorMethod_asString(Object self, int nparams, int *argcv, Object *argv,
int flags) {
struct MirrorMethodObject *s = (struct MirrorMethodObject*)self;
char buf[strlen(s->method->name) + 12];
strcpy(buf, "<Method \"");
strcat(buf, s->method->name);
strcat(buf, "\">");
return alloc_String(buf);
}
Object MirrorMethod_name(Object self, int nparams, int *argcv, Object *argv,
int flags) {
struct MirrorMethodObject *s = (struct MirrorMethodObject*)self;
return alloc_String(s->method->name);
}
Object MirrorMethod_partcount(Object self, int nparams, int *argcv,
Object *argv, int flags) {
struct MirrorMethodObject *s = (struct MirrorMethodObject*)self;
if (!s->method->type)
return alloc_Float64(-1);
return alloc_Float64(s->method->type->nparts);
}
Object MirrorMethod_paramcounts(Object self, int nparams, int *argcv,
Object *argv, int flags) {
struct MirrorMethodObject *s = (struct MirrorMethodObject*)self;
int i;
if (!s->method->type)
return alloc_done();
gc_pause();
Object l = alloc_List();
int cargcv[] = {1};
Object carg;
for (i=0; i<s->method->type->nparts; i++) {
carg = alloc_Float64(s->method->type->argcv[i]);
callmethod(l, "push", 1, cargcv, &carg);
}
gc_unpause();
return l;
}
Object MirrorMethod_request(Object self, int nparts, int *argcv, Object *argv,
int flags) {
struct MirrorMethodObject *s = (struct MirrorMethodObject*)self;
Object partsl = argv[0];
int cparts = integerfromAny(callmethod(partsl, "size", 0, NULL, NULL));
int i = 0;
int size = 0;
int cargcv[cparts];
Object partsiter = callmethod(partsl, "iter", 0, NULL, NULL);
while (istrue(callmethod(partsiter, "havemore", 0, NULL, NULL))) {
Object argsl = callmethod(partsiter, "next", 0, NULL, NULL);
cargcv[i] = integerfromAny(callmethod(argsl, "size", 0, NULL, NULL));
size += cargcv[i];
i++;
}
Object cargv[size];
i = 0;
partsiter = callmethod(partsl, "iter", 0, NULL, NULL);
while (istrue(callmethod(partsiter, "havemore", 0, NULL, NULL))) {
Object argsl = callmethod(partsiter, "next", 0, NULL, NULL);
Object argsiter = callmethod(argsl, "iter", 0, NULL, NULL);
while (istrue(callmethod(argsiter, "havemore", 0, NULL, NULL))) {
Object o = callmethod(argsiter, "next", 0, NULL, NULL);
cargv[i] = o;
i++;
}
}
Object rv = callmethod(s->obj, s->method->name, cparts, cargcv,
cargv);
return rv;
}
Object alloc_MirrorMethod(Method *method, Object obj) {
if (MirrorMethodClass == NULL) {
MirrorMethodClass = alloc_class("MirrorMethod", 5);
add_Method(MirrorMethodClass, "request", &MirrorMethod_request);
add_Method(MirrorMethodClass, "name", &MirrorMethod_name);
add_Method(MirrorMethodClass, "asString", &MirrorMethod_asString);
add_Method(MirrorMethodClass, "partcount", &MirrorMethod_partcount);
add_Method(MirrorMethodClass, "paramcounts", &MirrorMethod_paramcounts);
}
Object o = alloc_obj(sizeof(struct MirrorMethodObject)
- sizeof(struct Object), MirrorMethodClass);
struct MirrorMethodObject *p = (struct MirrorMethodObject*)o;
p->obj = obj;
p->method = method;
return o;
}
Object Mirror_getMethod(Object self, int nparams, int *argcv, Object *argv,
int flags) {
struct MirrorObject *s = (struct MirrorObject*)self;
Object o = s->obj;
Method *m = findmethodsimple(o, grcstring(argv[0]));
if (m == NULL) {
gracedie("no such method '%s' found by mirror\n", grcstring(argv[0]));
}
return alloc_MirrorMethod(m, o);
}
Object Mirror_methods(Object self, int nparams, int *argcv, Object *args,
int flags) {
struct MirrorObject *s = (struct MirrorObject*)self;
Object o = s->obj;
ClassData c = o->class;
Method *m;
gc_pause();
Object l = alloc_List();
Object arg;
int tmp = 1;
int i;
for (i=0; i<c->nummethods; i++) {
m = &c->methods[i];
arg = alloc_MirrorMethod(m, o);
callmethod(l, "push", 1, &tmp, &arg);
}
gc_unpause();
return l;
}
Object Mirror_annotations(Object self, int nparams, int *argcv, Object *args,
int flags) {
struct MirrorObject *s = (struct MirrorObject*)self;
Object o = s->obj;
ClassData c = o->class;
if (strncmp(c->name, "Object", 6) != 0) {
gracedie("cannot request annotations of non-user object\n");
}
struct UserObject *uo = (struct UserObject*)o;
gc_pause();
Object l = alloc_List();
Object arg;
int tmp = 1;
int i;
for (i = 0; i < uo->numannotations; i++) {
arg = uo->annotations[i];
callmethod(l, "push", 1, &tmp, &arg);
}
gc_unpause();
return l;
}
Object alloc_mirror(Object obj) {
if (MirrorClass == NULL) {
MirrorClass = alloc_class("Mirror", 3);
add_Method(MirrorClass, "methods", &Mirror_methods);
add_Method(MirrorClass, "getMethod", &Mirror_getMethod);
add_Method(MirrorClass, "annotations", &Mirror_annotations);
}
Object o = alloc_obj(sizeof(Object), MirrorClass);
struct MirrorObject *p = (struct MirrorObject*)o;
p->obj = obj;
return o;
}
Object mirrors_reflect(Object self, int nparams, int *argcv, Object *args,
int flags) {
if (nparams != 1)
gracedie("mirrors.reflect requires one argument");
return alloc_mirror(args[0]);
}
Object mirrors_loadDynamicModule(Object self, int nparams, int *argcv,
Object *argv, int flags) {
if (nparams != 1)
gracedie("mirrors.loadDynamicModule requires one argument");
char *s = grcstring(argv[0]);
return dlmodule(s);
}
// Create and return a Grace object with all the above functions as methods.
Object module_mirrors_init() {
if (mirrors_module != NULL)
return mirrors_module;
ClassData c = alloc_class("Module<mirrors>", 12);
add_Method(c, "reflect", &mirrors_reflect);
add_Method(c, "loadDynamicModule", &mirrors_loadDynamicModule);
Object o = alloc_newobj(0, c);
mirrors_module = o;
gc_root(mirrors_module);
return o;
}