예제 #1
0
파일: mruby.go 프로젝트: mattn/go-mruby
func (m *MRuby) Eval(code string, args ...interface{}) interface{} {
	c := C.CString(code)
	defer C.free(unsafe.Pointer(c))
	x := C.mrbc_context_new(m.mrb)
	defer C.mrbc_context_free(m.mrb, x)
	p := C.mrb_parse_string(m.mrb, c, x)
	n := C.mrb_generate_code(m.mrb, p)
	C.mrb_pool_close((*C.mrb_pool)(p.pool))
	a := C.CString("ARGV")
	defer C.free(unsafe.Pointer(a))
	ARGV := C.mrb_ary_new(m.mrb)
	for i := 0; i < len(args); i++ {
		C.mrb_ary_push(m.mrb, ARGV, go2mruby(m.mrb, args[i]))
	}
	C.mrb_define_global_const(m.mrb, a, ARGV)
	return mruby2go(m.mrb, C.mrb_run(m.mrb, n, C.mrb_top_self(m.mrb)))
}
예제 #2
0
파일: gomruby.go 프로젝트: AlekSi/gomruby
// Loads mruby code. Arguments are exposed as ARGV array.
func (c *LoadContext) Load(code string, args ...interface{}) (res interface{}, err error) {
	l := len(args)
	ARGV := C.mrb_ary_new_capa(c.m.state, C.mrb_int(l))
	for i := 0; i < l; i++ {
		ii := C.mrb_int(i)
		C.mrb_ary_set(c.m.state, ARGV, ii, c.m.mrubyValue(args[ii]))
	}
	C.mrb_define_global_const(c.m.state, argvCS, ARGV)

	codeC := C.CString(code)
	defer C.free(unsafe.Pointer(codeC))
	v := C.mrb_load_string_cxt(c.m.state, codeC, c.context)
	res = c.m.goValue(v)
	if c.m.state.exc != nil {
		v = C.mrb_obj_value(unsafe.Pointer(c.m.state.exc))
		err = errors.New(c.m.inspect(v))
	}
	return
}
예제 #3
0
파일: mruby.go 프로젝트: Gimi/go-mruby
func (m *MRuby) Eval(code string, args ...interface{}) interface{} {
	c := C.CString(code)
	defer C.free(unsafe.Pointer(c))
	p := C.mrb_parse_string(m.mrb, c)
	n := C.mrb_generate_code(m.mrb, p.tree)
	C.mrb_pool_close((*C.mrb_pool)(p.pool))
	a := C.CString("ARGV")
	defer C.free(unsafe.Pointer(a))
	if n >= 0 {
		ARGV := C.mrb_ary_new(m.mrb)
		for i := 0; i < len(args); i++ {
			C.mrb_ary_push(m.mrb, ARGV, go2mruby(m.mrb, args[i]))
		}
		C.mrb_define_global_const(m.mrb, a, ARGV)
		return mruby2go(m.mrb, C.mrb_run(
			m.mrb,
			C.mrb_proc_new(m.mrb, (*C.mrb_irep)(C._get_irep(m.mrb, n))),
			C.mrb_top_self(m.mrb)))
	}
	return C.mrb_nil_value()
}