git.s-ol.nu janet-2geom / 7d82e09
make compile as a janet module s-ol 2 years ago
7 changed file(s) with 147 addition(s) and 129 deletion(s). Raw diff Collapse all Expand all
0 [submodule "lib2geom"]
1 path = lib2geom
2 url = https://gitlab.com/inkscape/lib2geom.git
+0
-11
Makefile less more
0 all: src/janet.o src/shell.o src/main.o
1 g++ -o main $^ lib/lib2geom.a -ldl -ldouble-conversion -lgsl -lgslcblas -lm
2
3 src/janet.o: src/janet.c
4 gcc -c -o $@ -Iinclude $<
5
6 src/shell.o: src/shell.c
7 gcc -c -o $@ -Iinclude $<
8
9 src/main.o: src/main.cpp
10 g++ -c -o $@ -Iinclude $<
(New empty file)
0 (declare-project :name "geom")
1
2 (os/shell (string/join [
3 "mkdir" "-p" "lib2geom/build" "&&"
4 "cd" "lib2geom/build" "&&"
5 "cmake" ".." "-D2GEOM_BUILD_SHARED:BOOL=ON" "&&"
6 "make" "2geom"] " "))
7 (declare-native :name "geom" :source @["src/point.cpp" "src/main.cpp"]
8 :compiler "g++"
9 :linker "g++"
10 :cflags ["-Wall" "-Wextra" "-Ilib2geom/src" "-Llib2geom/build/src/2geom"]
11 :lflags ["-l2geom" "-ldl" "-ldouble-conversion" "-lgsl" "-lgslcblas" "-lm"])
0 #include <iostream>
1 #include <sstream>
2 #include <2geom/point.h>
3 #include <janet.h>
0 #include "point.h"
41
5 #define JDOC(x) x
2 extern "C" {
63
7
8
9 /*
10 static void 2geom_point_marshal(void *p, JanetMarshalContext *ctx) {
11 janet_marshal_int64(ctx, *((int64_t *)p));
4 JANET_MODULE_ENTRY(JanetTable *env) {
5 janet_lib_geom_point(env);
126 }
137
14 static void 2geom_point_unmarshal(void *p, JanetMarshalContext *ctx) {
15 *((int64_t *)p) = janet_unmarshal_int64(ctx);
168 }
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 }
0 #include "point.h"
1
2 #include <sstream>
3
4 extern "C" Janet geom_point_get(void *p, Janet key);
5
6 extern "C" void geom_point_tostring(void *p, JanetBuffer *buffer) {
7 std::stringstream stream;
8 stream << "<2geom/point " << *static_cast<Geom::Point *>(p) << ">";
9 janet_buffer_push_cstring(buffer, stream.str().c_str());
10 }
11
12
13 const JanetAbstractType geom_point_type = {
14 "2geom/point",
15 NULL,
16 NULL,
17 geom_point_get,
18 NULL,
19 NULL,
20 NULL,
21 geom_point_tostring
22 };
23
24
25 Janet janet_wrap_point(Geom::Point const &x) {
26 Geom::Point *box = (Geom::Point *)janet_abstract(&geom_point_type, sizeof(Geom::Point));
27 *box = (Geom::Point)x;
28 return janet_wrap_abstract(box);
29 }
30
31
32 Geom::Point janet_unwrap_point(Janet x) {
33 if (janet_checktype(x, JANET_ABSTRACT)) {
34 void *abst = janet_unwrap_abstract(x);
35 if (janet_abstract_type(abst) == &geom_point_type)
36 return *(Geom::Point *)abst;
37 }
38
39 janet_panic("not a point");
40 return Geom::Point();
41 }
42
43
44 extern "C" Janet cfun_geom_point_new(int32_t argc, Janet *argv) {
45 janet_fixarity(argc, 2);
46 double x = janet_getnumber(argv, 0);
47 double y = janet_getnumber(argv, 1);
48 return janet_wrap_point(Geom::Point(x, y));
49 }
50
51 const JanetReg it_cfuns[] = {
52 {
53 "point", cfun_geom_point_new,
54 "(geom/point x y)\n\nCreate a Point from x/y coordinate values."
55 },
56 {NULL, NULL, NULL}
57 };
58
59
60 #define OPMETHOD(name, oper) \
61 static Janet cfun_geom_point_##name(int32_t argc, Janet *argv) { \
62 janet_arity(argc, 2, -1); \
63 Geom::Point *box = (Geom::Point *)janet_abstract(&geom_point_type, sizeof(Geom::Point)); \
64 *box = janet_unwrap_point(argv[0]); \
65 for (int i = 1; i < argc; i++) \
66 *box oper##= janet_unwrap_point(argv[i]); \
67 return janet_wrap_abstract(box); \
68 } \
69 \
70 static Janet cfun_geom_point_##name##_mut(int32_t argc, Janet *argv) { \
71 janet_arity(argc, 2, -1); \
72 Geom::Point *box = (Geom::Point *)janet_abstract(&geom_point_type, sizeof(Geom::Point)); \
73 for (int i = 1; i < argc; i++) \
74 *box oper##= janet_unwrap_point(argv[i]); \
75 return janet_wrap_abstract(box); \
76 }
77
78 OPMETHOD(add, +)
79 OPMETHOD(sub, -)
80
81 static JanetMethod geom_point_methods[] = {
82 {"+", cfun_geom_point_add},
83 {"-", cfun_geom_point_sub},
84 {NULL, NULL}
85 };
86
87 extern "C" Janet geom_point_get(void *p, Janet key) {
88 Geom::Point *box = (Geom::Point *)p;
89 if (!janet_checktype(key, JANET_KEYWORD))
90 janet_panicf("expected keyword, got %v", key);
91
92 uint8_t const *keystr = janet_unwrap_keyword(key);
93 if (janet_string_equal(keystr, janet_cstring("x"))) {
94 return janet_wrap_number(box->x());
95 } else if (janet_string_equal(keystr, janet_cstring("y"))) {
96 return janet_wrap_number(box->y());
97 } else {
98 return janet_getmethod(keystr, geom_point_methods);
99 }
100 }
101
102 extern "C" void janet_lib_geom_point(JanetTable *env) {
103 janet_cfuns(env, NULL, it_cfuns);
104 janet_register_abstract_type(&geom_point_type);
105 }
0 #ifndef JANET_2GEOM_POINT_INCLUDED
1 #define JANET_2GEOM_POINT_INCLUDED
2
3 #include <janet.h>
4
5 #ifdef __cplusplus
6 #include <2geom/point.h>
7 Janet janet_wrap_point(Geom::Point const &x);
8 Geom::Point janet_unwrap_point(Janet x);
9
10 extern "C" {
11 #endif
12
13 extern const JanetAbstractType geom_point_type;
14
15 void janet_lib_geom_point(JanetTable *env);
16
17 #ifdef __cplusplus
18 }
19 #endif
20
21 #endif // JANET_2GEOM_POINT_INCLUDED