Skip to content

Commit

Permalink
chore: vendor go-mruby
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Aug 28, 2024
1 parent 005350e commit 6249e65
Show file tree
Hide file tree
Showing 33 changed files with 3,928 additions and 9 deletions.
10 changes: 3 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,16 @@ build-gobench:
build-embedded:
go build -tags "mrb gops" -ldflags $(LD_FLAGS) -o $(EMBEDDEDDIST) cmd/embedded-cable/main.go

download-mruby:
go mod download github.com/mitchellh/go-mruby

prepare-mruby: download-mruby
cd $$(go list -m -f '{{.Dir}}' github.com/mitchellh/go-mruby) && \
prepare-mruby:
cd ./vendorlib/go-mruby && \
MRUBY_COMMIT=$(MRUBY_VERSION) MRUBY_CONFIG=$(MRUBY_CONFIG) make libmruby.a || \
(sed -i '' 's/{ :verbose => $$verbose }/verbose: $$verbose/g' ./mruby-build/mruby/Rakefile && \
MRUBY_COMMIT=$(MRUBY_VERSION) MRUBY_CONFIG=$(MRUBY_CONFIG) make libmruby.a)

upgrade-mruby: clean-mruby prepare-mruby

clean-mruby:
cd $$(go list -m -f '{{.Dir}}' github.com/mitchellh/go-mruby) && \
rm -rf vendor/mruby
rm -rf vendorlib/go-mruby/mruby-build

build-all-mruby:
env $(GOBUILD) -tags mrb -o "dist/anycable-go-$(VERSION)-mrb-macos-amd64" cmd/anycable-go/main.go
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ require (
google.golang.org/grpc v1.60.1
)

// use vendored go-mruby
replace github.com/mitchellh/go-mruby => ./vendorlib/go-mruby

require github.com/sony/gobreaker v0.5.0

require (
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
github.com/mitchellh/go-mruby v0.0.0-20200315023956-207cedc21542 h1:/MjcGU93aaORB6Mydh9Q4D/oOim9BoR4jtpaAOgVZLQ=
github.com/mitchellh/go-mruby v0.0.0-20200315023956-207cedc21542/go.mod h1:TpwfcXhxDvAzz7wUcsTWu+FCaWGGLyyVZrL6sdkvK8k=
github.com/nats-io/jwt/v2 v2.5.3 h1:/9SWvzc6hTfamcgXJ3uYRpgj+QuY2aLNqRiqrKcrpEo=
github.com/nats-io/jwt/v2 v2.5.3/go.mod h1:iysuPemFcc7p4IoYots3IuELSI4EDe9Y0bQMe+I3Bf4=
github.com/nats-io/nats-server/v2 v2.10.7 h1:f5VDy+GMu7JyuFA0Fef+6TfulfCs5nBTgq7MMkFJx5Y=
Expand Down
3 changes: 3 additions & 0 deletions vendorlib/go-mruby/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build_config.rb
libmruby.a
mruby-build
7 changes: 7 additions & 0 deletions vendorlib/go-mruby/.travis.yml
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
21 changes: 21 additions & 0 deletions vendorlib/go-mruby/LICENSE
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.
35 changes: 35 additions & 0 deletions vendorlib/go-mruby/Makefile
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
112 changes: 112 additions & 0 deletions vendorlib/go-mruby/README.md
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())
}
```
52 changes: 52 additions & 0 deletions vendorlib/go-mruby/args.go
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)
}
31 changes: 31 additions & 0 deletions vendorlib/go-mruby/array.go
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
}
43 changes: 43 additions & 0 deletions vendorlib/go-mruby/array_test.go
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)
}
}
Loading

0 comments on commit 6249e65

Please sign in to comment.