git.s-ol.nu janet-2geom / master
get/set point[0] and point[1] s-ol 2 years ago
1 changed file(s) with 46 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
22 #include <sstream>
33
44 extern "C" Janet geom_point_get(void *p, Janet key);
5 extern "C" void geom_point_set(void *p, Janet key, Janet value);
56
67 extern "C" void geom_point_tostring(void *p, JanetBuffer *buffer) {
78 std::stringstream stream;
1516 NULL,
1617 NULL,
1718 geom_point_get,
18 NULL,
19 geom_point_set,
1920 NULL,
2021 NULL,
2122 geom_point_tostring
6970 }
7071
7172 extern "C" Janet cfun_geom_point_new(int32_t argc, Janet *argv) {
72 janet_fixarity(argc, 2);
73 double x = janet_getnumber(argv, 0);
74 double y = janet_getnumber(argv, 1);
73 janet_arity(argc, 0, 2);
74 double x = 0;
75 double y = 0;
76 if (argc == 2) {
77 x = janet_getnumber(argv, 0);
78 y = janet_getnumber(argv, 1);
79 } else if (argc == 1) {
80 x = janet_getnumber(argv, 0);
81 y = x;
82 }
7583 return janet_wrap_point(Geom::Point(x, y));
7684 }
7785
241249
242250 extern "C" Janet geom_point_get(void *p, Janet key) {
243251 Geom::Point *box = (Geom::Point *)p;
244 if (!janet_checktype(key, JANET_KEYWORD))
245 janet_panicf("expected keyword, got %v", key);
246
247 return janet_getmethod(janet_unwrap_keyword(key), geom_point_methods);
252
253 if (janet_checktype(key, JANET_KEYWORD))
254 return janet_getmethod(janet_unwrap_keyword(key), geom_point_methods);
255
256 if (!janet_checksize(key)) janet_panic("expected size as key");
257
258 size_t const index = (size_t) janet_unwrap_number(key);
259 switch (index) {
260 case 0:
261 return janet_wrap_number((*box)[0]);
262 case 1:
263 return janet_wrap_number((*box)[1]);
264 default:
265 return janet_wrap_nil();
266 }
267 }
268
269 extern "C" void geom_point_set(void *p, Janet key, Janet value) {
270 Geom::Point *box = (Geom::Point *)p;
271
272 if (!janet_checksize(key)) janet_panic("expected size as key");
273 if (!janet_checktype(key, JANET_NUMBER)) janet_panic("expected number as value");
274
275 size_t const index = (size_t) janet_unwrap_number(key);
276 switch (index) {
277 case 0:
278 (*box)[0] = janet_unwrap_number(value);
279 break;
280 case 1:
281 (*box)[1] = janet_unwrap_number(value);
282 break;
283 default:
284 janet_panic("index out of bounds");
285 }
248286 }
249287
250288 extern "C" void janet_lib_geom_point(JanetTable *env) {