diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2019-11-28 15:04:24 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2019-11-28 15:04:24 +0000 |
| commit | 7d82e09523ddc023da8df7bb6a0504f10c26cb92 (patch) | |
| tree | 4594d8fc710fcf5162fb7fc0640cee12edfb0720 /src/main.cpp | |
| parent | initial prototype (diff) | |
| download | janet-2geom-7d82e09523ddc023da8df7bb6a0504f10c26cb92.tar.gz janet-2geom-7d82e09523ddc023da8df7bb6a0504f10c26cb92.zip | |
make compile as a janet module
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 122 |
1 files changed, 4 insertions, 118 deletions
diff --git a/src/main.cpp b/src/main.cpp index fefeb6e..e0be6fd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,123 +1,9 @@ -#include <iostream> -#include <sstream> -#include <2geom/point.h> -#include <janet.h> +#include "point.h" -#define JDOC(x) x +extern "C" { - - -/* -static void 2geom_point_marshal(void *p, JanetMarshalContext *ctx) { - janet_marshal_int64(ctx, *((int64_t *)p)); -} - -static void 2geom_point_unmarshal(void *p, JanetMarshalContext *ctx) { - *((int64_t *)p) = janet_unmarshal_int64(ctx); -} -*/ - -extern "C" Janet geom_point_get(void *p, Janet key); - -extern "C" void geom_point_tostring(void *p, JanetBuffer *buffer) { - std::stringstream stream; - stream << "<2geom/point " << *static_cast<Geom::Point *>(p) << ">"; - janet_buffer_push_cstring(buffer, stream.str().c_str()); -} - - -static const JanetAbstractType geom_point_type = { - "2geom/point", - NULL, - NULL, - geom_point_get, - NULL, // geom_point_set, - NULL, // geom_point_marshal, - NULL, // geom_point_unmarshal, - geom_point_tostring -}; - - -Janet janet_wrap_point(Geom::Point const &x) { - Geom::Point *box = (Geom::Point *)janet_abstract(&geom_point_type, sizeof(Geom::Point)); - *box = (Geom::Point)x; - return janet_wrap_abstract(box); -} - - -Geom::Point janet_unwrap_point(Janet x) { - if (janet_checktype(x, JANET_ABSTRACT)) { - void *abst = janet_unwrap_abstract(x); - if (janet_abstract_type(abst) == &geom_point_type) - return *(Geom::Point *)abst; - } - - janet_panic("not a point"); - return Geom::Point(); -} - - -extern "C" Janet cfun_geom_point_new(int32_t argc, Janet *argv) { - janet_fixarity(argc, 2); - double x = janet_getnumber(argv, 0); - double y = janet_getnumber(argv, 1); - return janet_wrap_point(Geom::Point(x, y)); -} - -const JanetReg it_cfuns[] = { - { - "geom/point", cfun_geom_point_new, - JDOC("(geom/point x y)\n\n" - "Create a boxed Point from x/y coordinate values.") - }, - {NULL, NULL, NULL} -}; - - -#define OPMETHOD(name, oper) \ -static Janet cfun_geom_point_##name(int32_t argc, Janet *argv) { \ - janet_arity(argc, 2, -1); \ - Geom::Point *box = (Geom::Point *)janet_abstract(&geom_point_type, sizeof(Geom::Point)); \ - *box = janet_unwrap_point(argv[0]); \ - for (int i = 1; i < argc; i++) \ - *box oper##= janet_unwrap_point(argv[i]); \ - return janet_wrap_abstract(box); \ -} \ - \ -static Janet cfun_geom_point_##name##_mut(int32_t argc, Janet *argv) { \ - janet_arity(argc, 2, -1); \ - Geom::Point *box = (Geom::Point *)janet_abstract(&geom_point_type, sizeof(Geom::Point)); \ - for (int i = 1; i < argc; i++) \ - *box oper##= janet_unwrap_point(argv[i]); \ - return janet_wrap_abstract(box); \ -} - -OPMETHOD(add, +) -OPMETHOD(sub, -) - -static JanetMethod geom_point_methods[] = { - {"+", cfun_geom_point_add}, - {"-", cfun_geom_point_sub}, - {NULL, NULL} -}; - - -extern "C" Janet geom_point_get(void *p, Janet key) { - Geom::Point *box = (Geom::Point *)p; - if (!janet_checktype(key, JANET_KEYWORD)) - janet_panicf("expected keyword, got %v", key); - - uint8_t const *keystr = janet_unwrap_keyword(key); - if (janet_string_equal(keystr, janet_cstring("x"))) { - return janet_wrap_number(box->x()); - } else if (janet_string_equal(keystr, janet_cstring("y"))) { - return janet_wrap_number(box->y()); - } else { - return janet_getmethod(keystr, geom_point_methods); - } +JANET_MODULE_ENTRY(JanetTable *env) { + janet_lib_geom_point(env); } -extern "C" void register_janet_stuff(JanetTable *env) { - janet_cfuns(env, NULL, it_cfuns); - janet_register_abstract_type(&geom_point_type); } |
