-
Notifications
You must be signed in to change notification settings - Fork 1
/
method.cc
81 lines (71 loc) · 1.69 KB
/
method.cc
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
#define ICI_CORE
#include "method.h"
#include "fwd.h"
#include "null.h"
#include "primes.h"
#include "str.h"
#ifndef NOPROFILE
#include "profile.h"
#endif
namespace ici
{
/*
* Returns a new ICI method object that combines the given 'subject' object
* (typically a struct) with the given 'callable' object (typically a
* function). A method is also a callable object.
*
* Returns nullptr on error, usual conventions.
*
* This --func-- forms part of the --ici-api--.
*/
method *new_method(object *subject, object *callable)
{
method *m;
if ((m = ici_talloc(method)) == nullptr)
{
return nullptr;
}
set_tfnz(m, TC_METHOD, 0, 1, 0);
m->m_subject = subject;
m->m_callable = callable;
rego(m);
return m;
}
size_t method_type::mark(object *o)
{
auto m = methodof(o);
return type::mark(m) + ici_mark(m->m_subject) + ici_mark(m->m_callable);
}
object *method_type::fetch(object *o, object *k)
{
auto m = methodof(o);
if (k == SS(subject))
{
return m->m_subject;
}
if (k == SS(callable))
{
return m->m_callable;
}
return null;
}
int method_type::call(object *o, object *)
{
auto m = methodof(o);
if (!m->m_callable->can_call())
{
char n1[objnamez];
char n2[objnamez];
return set_error("attempt to call %s:%s", ici::objname(n1, m->m_subject), ici::objname(n2, m->m_callable));
}
return m->m_callable->call(m->m_subject);
}
void method_type::objname(object *o, char p[objnamez])
{
char n1[objnamez];
char n2[objnamez];
ici::objname(n1, methodof(o)->m_subject);
ici::objname(n2, methodof(o)->m_callable);
sprintf(p, "(%.13s:%.13s)", n1, n2);
}
} // namespace ici