|
0 |
#include <iostream>
|
|
1 |
#include <sstream>
|
|
2 |
#include <2geom/point.h>
|
|
3 |
#include <janet.h>
|
|
4 |
|
|
5 |
#define JDOC(x) x
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
/*
|
|
10 |
static void 2geom_point_marshal(void *p, JanetMarshalContext *ctx) {
|
|
11 |
janet_marshal_int64(ctx, *((int64_t *)p));
|
|
12 |
}
|
|
13 |
|
|
14 |
static void 2geom_point_unmarshal(void *p, JanetMarshalContext *ctx) {
|
|
15 |
*((int64_t *)p) = janet_unmarshal_int64(ctx);
|
|
16 |
}
|
|
17 |
*/
|
|
18 |
|
|
19 |
extern "C" Janet geom_point_get(void *p, Janet key);
|
|
20 |
|
|
21 |
extern "C" void geom_point_tostring(void *p, JanetBuffer *buffer) {
|
|
22 |
std::stringstream stream;
|
|
23 |
stream << "<2geom/point " << *static_cast<Geom::Point *>(p) << ">";
|
|
24 |
janet_buffer_push_cstring(buffer, stream.str().c_str());
|
|
25 |
}
|
|
26 |
|
|
27 |
|
|
28 |
static const JanetAbstractType geom_point_type = {
|
|
29 |
"2geom/point",
|
|
30 |
NULL,
|
|
31 |
NULL,
|
|
32 |
geom_point_get,
|
|
33 |
NULL, // geom_point_set,
|
|
34 |
NULL, // geom_point_marshal,
|
|
35 |
NULL, // geom_point_unmarshal,
|
|
36 |
geom_point_tostring
|
|
37 |
};
|
|
38 |
|
|
39 |
|
|
40 |
Janet janet_wrap_point(Geom::Point const &x) {
|
|
41 |
Geom::Point *box = (Geom::Point *)janet_abstract(&geom_point_type, sizeof(Geom::Point));
|
|
42 |
*box = (Geom::Point)x;
|
|
43 |
return janet_wrap_abstract(box);
|
|
44 |
}
|
|
45 |
|
|
46 |
|
|
47 |
Geom::Point janet_unwrap_point(Janet x) {
|
|
48 |
if (janet_checktype(x, JANET_ABSTRACT)) {
|
|
49 |
void *abst = janet_unwrap_abstract(x);
|
|
50 |
if (janet_abstract_type(abst) == &geom_point_type)
|
|
51 |
return *(Geom::Point *)abst;
|
|
52 |
}
|
|
53 |
|
|
54 |
janet_panic("not a point");
|
|
55 |
return Geom::Point();
|
|
56 |
}
|
|
57 |
|
|
58 |
|
|
59 |
extern "C" Janet cfun_geom_point_new(int32_t argc, Janet *argv) {
|
|
60 |
janet_fixarity(argc, 2);
|
|
61 |
double x = janet_getnumber(argv, 0);
|
|
62 |
double y = janet_getnumber(argv, 1);
|
|
63 |
return janet_wrap_point(Geom::Point(x, y));
|
|
64 |
}
|
|
65 |
|
|
66 |
const JanetReg it_cfuns[] = {
|
|
67 |
{
|
|
68 |
"geom/point", cfun_geom_point_new,
|
|
69 |
JDOC("(geom/point x y)\n\n"
|
|
70 |
"Create a boxed Point from x/y coordinate values.")
|
|
71 |
},
|
|
72 |
{NULL, NULL, NULL}
|
|
73 |
};
|
|
74 |
|
|
75 |
|
|
76 |
#define OPMETHOD(name, oper) \
|
|
77 |
static Janet cfun_geom_point_##name(int32_t argc, Janet *argv) { \
|
|
78 |
janet_arity(argc, 2, -1); \
|
|
79 |
Geom::Point *box = (Geom::Point *)janet_abstract(&geom_point_type, sizeof(Geom::Point)); \
|
|
80 |
*box = janet_unwrap_point(argv[0]); \
|
|
81 |
for (int i = 1; i < argc; i++) \
|
|
82 |
*box oper##= janet_unwrap_point(argv[i]); \
|
|
83 |
return janet_wrap_abstract(box); \
|
|
84 |
} \
|
|
85 |
\
|
|
86 |
static Janet cfun_geom_point_##name##_mut(int32_t argc, Janet *argv) { \
|
|
87 |
janet_arity(argc, 2, -1); \
|
|
88 |
Geom::Point *box = (Geom::Point *)janet_abstract(&geom_point_type, sizeof(Geom::Point)); \
|
|
89 |
for (int i = 1; i < argc; i++) \
|
|
90 |
*box oper##= janet_unwrap_point(argv[i]); \
|
|
91 |
return janet_wrap_abstract(box); \
|
|
92 |
}
|
|
93 |
|
|
94 |
OPMETHOD(add, +)
|
|
95 |
OPMETHOD(sub, -)
|
|
96 |
|
|
97 |
static JanetMethod geom_point_methods[] = {
|
|
98 |
{"+", cfun_geom_point_add},
|
|
99 |
{"-", cfun_geom_point_sub},
|
|
100 |
{NULL, NULL}
|
|
101 |
};
|
|
102 |
|
|
103 |
|
|
104 |
extern "C" Janet geom_point_get(void *p, Janet key) {
|
|
105 |
Geom::Point *box = (Geom::Point *)p;
|
|
106 |
if (!janet_checktype(key, JANET_KEYWORD))
|
|
107 |
janet_panicf("expected keyword, got %v", key);
|
|
108 |
|
|
109 |
uint8_t const *keystr = janet_unwrap_keyword(key);
|
|
110 |
if (janet_string_equal(keystr, janet_cstring("x"))) {
|
|
111 |
return janet_wrap_number(box->x());
|
|
112 |
} else if (janet_string_equal(keystr, janet_cstring("y"))) {
|
|
113 |
return janet_wrap_number(box->y());
|
|
114 |
} else {
|
|
115 |
return janet_getmethod(keystr, geom_point_methods);
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
extern "C" void register_janet_stuff(JanetTable *env) {
|
|
120 |
janet_cfuns(env, NULL, it_cfuns);
|
|
121 |
janet_register_abstract_type(&geom_point_type);
|
|
122 |
}
|