Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Add LoadIrepFile() #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gomruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <mruby/array.h>
#include <mruby/class.h>
#include <mruby/compile.h>
#include <mruby/dump.h>
#include <mruby/error.h>
#include <mruby/irep.h>
#include <mruby/gc.h>
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions mruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "unsafe"

// #cgo CFLAGS: -Ivendor/mruby/include
// #cgo LDFLAGS: ${SRCDIR}/libmruby.a -lm
// #include <stdio.h>
// #include <stdlib.h>
// #include "gomruby.h"
import "C"
Expand Down Expand Up @@ -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.
Expand Down