-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
3,928 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
build_config.rb | ||
libmruby.a | ||
mruby-build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language: go | ||
sudo: required | ||
go: | ||
- "1.14" | ||
- "1.13" | ||
install: sudo apt-get install build-essential g++ bison flex | ||
script: make all staticcheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Mitchell Hashimoto | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
MRUBY_COMMIT ?= 1.2.0 | ||
MRUBY_VENDOR_DIR ?= mruby-build | ||
|
||
all: libmruby.a test | ||
|
||
clean: | ||
rm -rf ${MRUBY_VENDOR_DIR} | ||
rm -f libmruby.a | ||
|
||
gofmt: | ||
@echo "Checking code with gofmt.." | ||
gofmt -s *.go >/dev/null | ||
|
||
lint: | ||
GO111MODULE=off go get golang.org/x/lint/golint | ||
golint ./... | ||
|
||
staticcheck: | ||
GO111MODULE=off go get honnef.co/go/tools/cmd/staticcheck | ||
staticcheck ./... | ||
|
||
libmruby.a: ${MRUBY_VENDOR_DIR}/mruby | ||
cd ${MRUBY_VENDOR_DIR}/mruby && ${MAKE} | ||
cp ${MRUBY_VENDOR_DIR}/mruby/build/host/lib/libmruby.a . | ||
|
||
${MRUBY_VENDOR_DIR}/mruby: | ||
mkdir -p ${MRUBY_VENDOR_DIR} | ||
git clone https://github.com/mruby/mruby.git ${MRUBY_VENDOR_DIR}/mruby | ||
cd ${MRUBY_VENDOR_DIR}/mruby && git reset --hard && git clean -fdx | ||
cd ${MRUBY_VENDOR_DIR}/mruby && git checkout ${MRUBY_COMMIT} | ||
|
||
test: libmruby.a gofmt lint | ||
go test -v | ||
|
||
.PHONY: all clean libmruby.a test lint staticcheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# mruby Library for Go [![Build Status](https://travis-ci.org/mitchellh/go-mruby.svg?branch=master)](https://travis-ci.org/mitchellh/go-mruby) | ||
|
||
go-mruby provides [mruby](https://github.com/mruby/mruby) bindings for | ||
[Go](http://golang.org). 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](https://github.com/mruby/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](https://github.com/mruby/mruby/tree/master/doc/guides/compile.md). | ||
|
||
## 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](http://godoc.org/github.com/mitchellh/go-mruby), 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: | ||
|
||
```go | ||
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()) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package mruby | ||
|
||
import "sync" | ||
|
||
// #include "gomruby.h" | ||
import "C" | ||
|
||
// ArgSpec defines how many arguments a function should take and | ||
// what kind. Multiple ArgSpecs can be combined using the "|" | ||
// operator. | ||
type ArgSpec C.mrb_aspec | ||
|
||
// ArgsAny allows any number of arguments. | ||
func ArgsAny() ArgSpec { | ||
return ArgSpec(C._go_MRB_ARGS_ANY()) | ||
} | ||
|
||
// ArgsArg says the given number of arguments are required and | ||
// the second number is optional. | ||
func ArgsArg(r, o int) ArgSpec { | ||
return ArgSpec(C._go_MRB_ARGS_ARG(C.int(r), C.int(o))) | ||
} | ||
|
||
// ArgsBlock says it takes a block argument. | ||
func ArgsBlock() ArgSpec { | ||
return ArgSpec(C._go_MRB_ARGS_BLOCK()) | ||
} | ||
|
||
// ArgsNone says it takes no arguments. | ||
func ArgsNone() ArgSpec { | ||
return ArgSpec(C._go_MRB_ARGS_NONE()) | ||
} | ||
|
||
// ArgsReq says that the given number of arguments are required. | ||
func ArgsReq(n int) ArgSpec { | ||
return ArgSpec(C._go_MRB_ARGS_REQ(C.int(n))) | ||
} | ||
|
||
// ArgsOpt says that the given number of arguments are optional. | ||
func ArgsOpt(n int) ArgSpec { | ||
return ArgSpec(C._go_MRB_ARGS_OPT(C.int(n))) | ||
} | ||
|
||
// The global accumulator when Mrb.GetArgs is called. There is a | ||
// global lock around this so that the access to it is safe. | ||
var getArgAccumulator []C.mrb_value | ||
var getArgLock = new(sync.Mutex) | ||
|
||
//export goGetArgAppend | ||
func goGetArgAppend(v C.mrb_value) { | ||
getArgAccumulator = append(getArgAccumulator, v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package mruby | ||
|
||
// #include "gomruby.h" | ||
import "C" | ||
|
||
// Array represents an MrbValue that is a Array in Ruby. | ||
// | ||
// A Array can be obtained by calling the Array function on MrbValue. | ||
type Array struct { | ||
*MrbValue | ||
} | ||
|
||
// Len returns the length of the array. | ||
func (v *Array) Len() int { | ||
return int(C.mrb_ary_len(v.state, v.value)) | ||
} | ||
|
||
// Get gets an element form the Array by index. | ||
// | ||
// This does not copy the element. This is a pointer/reference directly | ||
// to the element in the array. | ||
func (v *Array) Get(idx int) (*MrbValue, error) { | ||
result := C.mrb_ary_entry(v.value, C.mrb_int(idx)) | ||
|
||
val := newValue(v.state, result) | ||
if val.Type() == TypeNil { | ||
val = nil | ||
} | ||
|
||
return val, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package mruby | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestArray(t *testing.T) { | ||
mrb := NewMrb() | ||
defer mrb.Close() | ||
|
||
value, err := mrb.LoadString(`["foo", "bar", "baz", false]`) | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
v := value.Array() | ||
|
||
// Len | ||
if n := v.Len(); n != 4 { | ||
t.Fatalf("bad: %d", n) | ||
} | ||
|
||
// Get | ||
value, err = v.Get(1) | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
if value.String() != "bar" { | ||
t.Fatalf("bad: %s", value) | ||
} | ||
|
||
// Get bool | ||
value, err = v.Get(3) | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
if valType := value.Type(); valType != TypeFalse { | ||
t.Fatalf("bad type: %v", valType) | ||
} | ||
if value.String() != "false" { | ||
t.Fatalf("bad: %s", value) | ||
} | ||
} |
Oops, something went wrong.