Esempio n. 1
0
// Creates new load context. Panics if it's not possible. Filename is used in error messages.
func (m *MRuby) NewLoadContext(filename string) (context *LoadContext) {
	ctx := C.mrbc_context_new(m.state)
	if ctx == nil {
		panic(errors.New("gomruby bug: failed to create mruby context"))
	}

	if filename != "" {
		fn := C.CString(filename)
		C.mrbc_filename(m.state, ctx, fn)
		C.free(unsafe.Pointer(fn))
	}

	context = &LoadContext{ctx, m}
	return
}
Esempio n. 2
0
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)))
}
Esempio n. 3
0
func NewCompileContext(m *Mrb) *CompileContext {
	return &CompileContext{
		ctx: C.mrbc_context_new(m.state),
		mrb: m,
	}
}