diff --git a/gomruby.h b/gomruby.h index c2c1b49..8fcddc6 100644 --- a/gomruby.h +++ b/gomruby.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -72,6 +73,12 @@ static mrb_value _go_mrb_load_string(mrb_state *mrb, const char *s) { GOMRUBY_EXC_PROTECT_END } +static mrb_value _go_mrb_load_irep_file(mrb_state *mrb, FILE *fp) { + GOMRUBY_EXC_PROTECT_START + result = mrb_load_irep_file(mrb, fp); + GOMRUBY_EXC_PROTECT_END +} + static mrb_value _go_mrb_yield_argv(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv) { GOMRUBY_EXC_PROTECT_START result = mrb_yield_argv(mrb, b, argc, argv); diff --git a/mruby.go b/mruby.go index b5565f5..5e782ab 100644 --- a/mruby.go +++ b/mruby.go @@ -4,6 +4,7 @@ import "unsafe" // #cgo CFLAGS: -Ivendor/mruby/include // #cgo LDFLAGS: ${SRCDIR}/libmruby.a -lm +// #include // #include // #include "gomruby.h" import "C" @@ -187,6 +188,26 @@ func (m *Mrb) LoadString(code string) (*MrbValue, error) { return newValue(m.state, value), nil } +// LoadIrepFile loads the given compiled file (.mrb) path, executes it, and returns its final +// value that it might return. +func (m *Mrb) LoadIrepFile(path string) (*MrbValue, error) { + c1 := C.CString(path) + defer C.free(unsafe.Pointer(c1)) + + c2 := C.CString("r") + defer C.free(unsafe.Pointer(c2)) + + fp := C.fopen(c1, c2) + defer C.fclose(fp) + + value := C._go_mrb_load_irep_file(m.state, fp) + if exc := checkException(m.state); exc != nil { + return nil, exc + } + + return newValue(m.state, value), nil +} + // Run executes the given value, which should be a proc type. // // If you're looking to execute code directly a string, look at LoadString.