Go (golang) bindings to mruby.
Switch branches/tags
Nothing to show
Clone or download
erikh Merge pull request #69 from fabian-f/Instance&GlobalVariables
Expose mrb_iv_get/set, mrb_gv_get/set to Go in a consistent fashion.
Latest commit cd6a04a Oct 3, 2018
Permalink
Failed to load latest commit information.
.gitignore Some initial work Mar 15, 2014
.travis.yml Makefile,mruby_test.go: add megacheck linter; fix checks Sep 14, 2018
LICENSE Create LICENSE Feb 11, 2017
Makefile Makefile,mruby_test.go: add megacheck linter; fix checks Sep 14, 2018
README.md Adjust readme to accomodate for dependencies Feb 11, 2017
args.go Fix an issue where the args list would get corrupted if Procs were used. Dec 22, 2016
array.go Implements checkException helper to internally clear exception state Mar 7, 2016
array_test.go Address linting issues to get tests passing with go1.10 Feb 19, 2018
class.go Several fixes to get code golint and go vet clean Oct 5, 2016
class_test.go Implements checkException helper to internally clear exception state Mar 7, 2016
context.go Introduce CompileContext.CaptureErrors to enable error swallowing in … Dec 10, 2016
context_test.go CompileContext Mar 17, 2014
decode.go Several fixes to get code golint and go vet clean Oct 5, 2016
decode_test.go decode: can decode into interfaces Jul 14, 2015
examples_test.go decode example Jul 14, 2015
func.go Fix call exception propagation Jan 9, 2017
func_test.go Implements checkException helper to internally clear exception state Mar 7, 2016
gc_test.go gc_test.go: not-passing-yet tests for IsDead and three new GC-related… Nov 28, 2016
go.mod go.mod,go.sum: get golint into go modules Sep 14, 2018
go.sum go.mod,go.sum: get golint into go modules Sep 14, 2018
golint.sh Integrate golint into build tooling Oct 23, 2016
gomruby.h Added *MrbValue.GetInstanceVariable, *MrbValue.SetInstanceVariable, *… Sep 26, 2018
hash.go Implements checkException helper to internally clear exception state Mar 7, 2016
hash_test.go Address linting issues to get tests passing with go1.10 Feb 19, 2018
mruby.go Added *MrbValue.GetInstanceVariable, *MrbValue.SetInstanceVariable, *… Sep 26, 2018
mruby_test.go Fixed linter issue with test case. Sep 28, 2018
parser.go Correct method/variable definitions in a few places to satisfy golint Oct 23, 2016
parser_test.go Address linting issues to get tests passing with go1.10 Feb 19, 2018
value.go Added *MrbValue.GetInstanceVariable, *MrbValue.SetInstanceVariable, *… Sep 26, 2018
value_test.go Address linting issues to get tests passing with go1.10 Feb 19, 2018
value_type.go value_type.go: replace iota comments with descriptions of each type Oct 23, 2016

README.md

mruby Library for Go Build Status

go-mruby provides mruby bindings for Go. This allows Go applications to run a lightweight embedded Ruby VM. Using the mruby library, Go applications can call Ruby code, and Ruby code can call Go code (that is properly exposed)!

At the time of writing, this is the most comprehensive mruby library for Go by far. It is also the only mruby library for Go that enables exposing Go functions to Ruby as well as being able to generically convert complex Ruby types into Go types. Our goal is to implement all of the mruby API.

Project Status: The major portions of the mruby API are implemented, but the mruby API is huge. If there is something that is missing, please issue a pull request and I'd be happy to add it! We're also not yet ready to promise API backwards compatibility on a Go-level, but we're getting there.

Installation

Installation is a little trickier than a standard Go library, but not by much. You can't simply go get this library, unfortunately. This is because mruby must first be built. We don't ship a pre-built version of mruby because the build step of mruby is important in customizing what aspects of the standard library you want available, as well as any other extensions.

To build mruby, we've made it very easy. You will need the following packages available on your host operating system:

  • bison
  • flex
  • ruby 2.x

Then just type:

$ make

This will download mruby, compile it, and run the tests for go-mruby, verifying that your build is functional. By default, go-mruby will download and build a default version of mruby, but this is customizable.

Compiling/installing the go-mruby library should work on Linux, Mac OS X, and Windows. On Windows, msys is the only supported build toolchain (same as Go itself).

Due to this linking, it is strongly recommended that you vendor this repository and bake our build system into your process.

Customizing the mruby Compilation

You can customize the mruby compilation by setting a couple environmental variables prior to calling make:

  • MRUBY_COMMIT is the git ref that will be checked out for mruby. This defaults to to a recently tagged version. Many versions before 1.2.0 do not work with go-mruby. It is recommend you explicitly set this to a ref that works for you to avoid any changes in this library later.

  • MRUBY_CONFIG is the path to a build_config.rb file used to configure how mruby is built. If this is not set, go-mruby will use the default build config that comes with mruby. You can learn more about configuring the mruby build here.

Usage

go-mruby exposes the mruby API in a way that is idiomatic Go, so that it is comfortable to use by a standard Go programmer without having intimate knowledge of how mruby works.

For usage examples and documentation, please see the go-mruby GoDoc, which we keep up to date and full of examples.

For a quick taste of what using go-mruby looks like, though, we provide an example below:

package main

import (
	"fmt"
	"github.com/mitchellh/go-mruby"
)

func main() {
	mrb := mruby.NewMrb()
	defer mrb.Close()

	// Our custom function we'll expose to Ruby. The first return
	// value is what to return from the func and the second is an
	// exception to raise (if any).
	addFunc := func(m *mruby.Mrb, self *mruby.MrbValue) (mruby.Value, mruby.Value) {
		args := m.GetArgs()
		return mruby.Int(args[0].Fixnum() + args[1].Fixnum()), nil
	}

	// Lets define a custom class and a class method we can call.
	class := mrb.DefineClass("Example", nil)
	class.DefineClassMethod("add", addFunc, mruby.ArgsReq(2))

	// Let's call it and inspect the result
	result, err := mrb.LoadString(`Example.add(12, 30)`)
	if err != nil {
		panic(err.Error())
	}

	// This will output "Result: 42"
	fmt.Printf("Result: %s\n", result.String())
}