From 9d34354a80bb73c7311b28caf7af4ad67aab048a Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 3 Aug 2022 22:00:27 +0000 Subject: [PATCH] (no comment) --- source/engine/mrbffi.c | 35 +++++++++++++++++++++++++++++++++++ source/engine/mrbffi.h | 6 ++++++ 2 files changed, 41 insertions(+) create mode 100644 source/engine/mrbffi.c create mode 100644 source/engine/mrbffi.h diff --git a/source/engine/mrbffi.c b/source/engine/mrbffi.c new file mode 100644 index 0000000..8160949 --- /dev/null +++ b/source/engine/mrbffi.c @@ -0,0 +1,35 @@ +#include "mrbffi.h" + +#include "mruby.h" +#include "mruby/compile.h" + +#include "script.h" + +extern mrb_state *mrb; + +int fib(int n) { + if (n < 2) return n; + + return fib(n-1) + fib(n-2); +} + +/* FFI */ + +mrb_value mrb_fib(mrb_state *mrb, mrb_value self) { + int n; + mrb_get_args(mrb, "i", &n); + return mrb_fixnum_value(fib(n)); +} + +mrb_value mrb_load(mrb_state *mrb, mrb_value self) { + char *path; + mrb_get_args(mrb, "z!", &path); + script_dofile(path); + return self; +} + +void ffi_load() { + mrb_define_method(mrb, mrb->object_class, "fib", mrb_fib, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, mrb->object_class, "load", mrb_load, MRB_ARGS_REQ(1)); +} + diff --git a/source/engine/mrbffi.h b/source/engine/mrbffi.h new file mode 100644 index 0000000..0b2e9dd --- /dev/null +++ b/source/engine/mrbffi.h @@ -0,0 +1,6 @@ +#ifndef MRBFFI_H +#define MRBFFI_H + +void ffi_load(); + +#endif \ No newline at end of file