Documentation
¶
Index ¶
- func Decode(out interface{}, v *MrbValue) error
- type ArenaIndex
- type ArgSpec
- type Array
- type Class
- type CompileContext
- type Exception
- type Func
- type Hash
- type Int
- type Mrb
- func (m *Mrb) ArenaRestore(idx ArenaIndex)
- func (m *Mrb) ArenaSave() ArenaIndex
- func (m *Mrb) Class(name string, super *Class) *Class
- func (m *Mrb) Close()
- func (m *Mrb) ConstDefined(name string, scope Value) bool
- func (m *Mrb) DefineClass(name string, super *Class) *Class
- func (m *Mrb) DefineClassUnder(name string, super *Class, outer *Class) *Class
- func (m *Mrb) DefineModule(name string) *Class
- func (m *Mrb) DefineModuleUnder(name string, outer *Class) *Class
- func (m *Mrb) DisableGC()
- func (m *Mrb) EnableGC()
- func (m *Mrb) FalseValue() *MrbValue
- func (m *Mrb) FixnumValue(v int) *MrbValue
- func (m *Mrb) FullGC()
- func (m *Mrb) GetArgs() []*MrbValue
- func (m *Mrb) GetGlobalVariable(name string) *MrbValue
- func (m *Mrb) IncrementalGC()
- func (m *Mrb) KernelModule() *Class
- func (m *Mrb) LiveObjectCount() int
- func (m *Mrb) LoadString(code string) (*MrbValue, error)
- func (m *Mrb) Module(name string) *Class
- func (m *Mrb) NilValue() *MrbValue
- func (m *Mrb) ObjectClass() *Class
- func (m *Mrb) Run(v Value, self Value) (*MrbValue, error)
- func (m *Mrb) RunWithContext(v Value, self Value, stackKeep int) (int, *MrbValue, error)
- func (m *Mrb) SetGlobalVariable(name string, value Value)
- func (m *Mrb) StringValue(s string) *MrbValue
- func (m *Mrb) TopSelf() *MrbValue
- func (m *Mrb) TrueValue() *MrbValue
- func (m *Mrb) Yield(block Value, args ...Value) (*MrbValue, error)
- type MrbValue
- func (v *MrbValue) Array() *Array
- func (v *MrbValue) Call(method string, args ...Value) (*MrbValue, error)
- func (v *MrbValue) CallBlock(method string, args ...Value) (*MrbValue, error)
- func (v *MrbValue) Class() *Class
- func (v *MrbValue) Fixnum() int
- func (v *MrbValue) Float() float64
- func (v *MrbValue) GCProtect()
- func (v *MrbValue) GetInstanceVariable(variable string) *MrbValue
- func (v *MrbValue) Hash() *Hash
- func (v *MrbValue) IsDead() bool
- func (v *MrbValue) Mrb() *Mrb
- func (v *MrbValue) MrbValue(*Mrb) *MrbValue
- func (v *MrbValue) SetInstanceVariable(variable string, value *MrbValue)
- func (v *MrbValue) SetProcTargetClass(c *Class)
- func (v *MrbValue) SingletonClass() *Class
- func (v *MrbValue) String() string
- func (v *MrbValue) Type() ValueType
- type NilType
- type Parser
- type ParserError
- type ParserMessage
- type String
- type Value
- type ValueType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode converts the Ruby value to a Go value.
The Decode process may call Ruby code and may generate Ruby garbage, but it collects all of its own garbage. You don't need to GC around this.
See the tests (decode_test.go) for detailed and specific examples of how this function decodes. Basic examples are also available here and in the README.
For primitives, the decoding process is likely what you expect. For Ruby, this is booleans, strings, fixnums, and floats. These map directly to effectively equivalent Go types: bool, string, int, float64. Hash and Arrays can map directly to maps and slices in Go, and Decode will handle this as you expect.
The only remaining data type in Go is a struct. A struct in Go can map to any object in Ruby. If the data in Ruby is a hash, then the struct keys will map directly to the hash keys. If the data in Ruby is an object, then one of two things will be done. First: if the object responds to the `to_gomruby` function, then this will be called and the resulting value is expected to be a Hash and will be used to decode into the struct. If the object does NOT respond to that function, then any struct fields will invoke the corresponding Ruby method to attain the value.
Note that with structs you can use the `mruby` tag to specify the Hash key or method name to call. Example:
type Foo struct { Field string `mruby:"read_field"` }
Types ¶
type ArenaIndex ¶
type ArenaIndex int
ArenaIndex represents the index into the arena portion of the GC.
See ArenaSave for more information.
type ArgSpec ¶
ArgSpec defines how many arguments a function should take and what kind. Multiple ArgSpecs can be combined using the "|" operator.
func ArgsArg ¶
ArgsArg says the given number of arguments are required and the second number is optional.
type Array ¶
type Array struct {
*MrbValue
}
Array represents an MrbValue that is a Array in Ruby.
A Array can be obtained by calling the Array function on MrbValue.
type Class ¶
type Class struct {
// contains filtered or unexported fields
}
Class is a class in mruby. To obtain a Class, use DefineClass or one of the variants on the Mrb structure.
func (*Class) DefineClassMethod ¶
DefineClassMethod defines a class-level method on the given class.
func (*Class) DefineConst ¶
DefineConst defines a constant within this class.
func (*Class) DefineMethod ¶
DefineMethod defines an instance method on the class.
type CompileContext ¶
type CompileContext struct {
// contains filtered or unexported fields
}
CompileContext represents a context for code compilation.
CompileContexts keep track of things such as filenames, line numbers, as well as some settings for how to parse and execute code.
func NewCompileContext ¶
func NewCompileContext(m *Mrb) *CompileContext
NewCompileContext constructs a *CompileContext from a *Mrb.
func (*CompileContext) CaptureErrors ¶
func (c *CompileContext) CaptureErrors(yes bool)
CaptureErrors toggles the capture errors feature of the parser, which swallows errors. This allows repls and other partial parsing tools (formatters, f.e.) to function.
func (*CompileContext) Close ¶
func (c *CompileContext) Close()
Close the context, freeing any resources associated with it.
This is safe to call once the context has been used for parsing/loading any Ruby code.
func (*CompileContext) Filename ¶
func (c *CompileContext) Filename() string
Filename returns the filename associated with this context.
func (*CompileContext) SetFilename ¶
func (c *CompileContext) SetFilename(f string)
SetFilename sets the filename associated with this compilation context.
Code parsed under this context will be from this file.
type Exception ¶
Exception is a special type of value that represents an error and implements the Error interface.
type Func ¶
Func is the signature of a function in Go that you use to expose to Ruby code.
The first return value is the actual return value for the code.
The second return value is an exception, if any. This will be raised.
type Hash ¶
type Hash struct {
*MrbValue
}
Hash represents an MrbValue that is a Hash in Ruby.
A Hash can be obtained by calling the Hash function on MrbValue.
func (*Hash) Delete ¶
Delete deletes a key from the hash, returning its existing value, or nil if there wasn't a value.
type Mrb ¶
type Mrb struct {
// contains filtered or unexported fields
}
Mrb represents a single instance of mruby.
func NewMrb ¶
func NewMrb() *Mrb
NewMrb creates a new instance of Mrb, representing the state of a single Ruby VM.
When you're finished with the VM, clean up all resources it is using by calling the Close method.
func (*Mrb) ArenaRestore ¶
func (m *Mrb) ArenaRestore(idx ArenaIndex)
ArenaRestore restores the arena index so the objects between the save and this point can be garbage collected in the future.
See ArenaSave for more documentation.
func (*Mrb) ArenaSave ¶
func (m *Mrb) ArenaSave() ArenaIndex
ArenaSave saves the index into the arena.
Restore the arena index later by calling ArenaRestore.
The arena is where objects returned by functions such as LoadString are stored. By saving the index and then later restoring it with ArenaRestore, these objects can be garbage collected. Otherwise, the objects will never be garbage collected.
The recommended usage pattern for memory management is to save the arena index prior to any Ruby execution, to turn the resulting Ruby value into Go values as you see fit, then to restore the arena index so that GC can collect any values.
Of course, when Close() is called, all objects in the arena are garbage collected anyways, so if you're only calling mruby for a short period of time, you might not have to worry about saving/restoring the arena.
func (*Mrb) Class ¶
Class returns the class with the kgiven name and superclass. Note that if you call this with a class that doesn't exist, mruby will abort the application (like a panic, but not a Go panic).
super can be nil, in which case the Object class will be used.
func (*Mrb) Close ¶
func (m *Mrb) Close()
Close a Mrb, this must be called to properly free resources, and should only be called once.
func (*Mrb) ConstDefined ¶
ConstDefined checks if the given constant is defined in the scope.
This should be used, for example, before a call to Class, because a failure in Class will crash your program (by design). You can retrieve the Value of a Class by calling Value().
func (*Mrb) DefineClass ¶
DefineClass defines a new top-level class.
If super is nil, the class will be defined under Object.
func (*Mrb) DefineClassUnder ¶
DefineClassUnder defines a new class under another class.
This is, for example, how you would define the World class in `Hello::World` where Hello is the "outer" class.
func (*Mrb) DefineModule ¶
DefineModule defines a top-level module.
func (*Mrb) DefineModuleUnder ¶
DefineModuleUnder defines a module under another class/module.
func (*Mrb) DisableGC ¶
func (m *Mrb) DisableGC()
DisableGC disables the garbage collector for this mruby instance. It returns true if it was previously disabled.
func (*Mrb) EnableGC ¶
func (m *Mrb) EnableGC()
EnableGC enables the garbage collector for this mruby instance. It returns true if garbage collection was previously disabled.
func (*Mrb) FalseValue ¶
FalseValue returns a Value for "false"
func (*Mrb) FixnumValue ¶
FixnumValue returns a Value for a fixed number.
func (*Mrb) GetArgs ¶
GetArgs returns all the arguments that were given to the currnetly called function (currently on the stack).
func (*Mrb) GetGlobalVariable ¶
GetGlobalVariable returns the value of the global variable by the given name.
func (*Mrb) IncrementalGC ¶
func (m *Mrb) IncrementalGC()
IncrementalGC runs an incremental GC step. It is much less expensive than a FullGC, but must be called multiple times for GC to actually happen.
This function is best called periodically when executing Ruby in the VM many times (thousands of times).
func (*Mrb) KernelModule ¶
KernelModule returns the Kernel top-level module.
func (*Mrb) LiveObjectCount ¶
LiveObjectCount returns the number of objects that have not been collected (aka, alive).
func (*Mrb) LoadString ¶
LoadString loads the given code, executes it, and returns its final value that it might return.
func (*Mrb) Module ¶
Module returns the named module as a *Class. If the module is invalid, NameError is triggered within your program and SIGABRT is sent to the application.
func (*Mrb) ObjectClass ¶
ObjectClass returns the Object top-level class.
func (*Mrb) Run ¶
Run executes the given value, which should be a proc type.
If you're looking to execute code directly a string, look at LoadString.
If self is nil, it is set to the top-level self.
func (*Mrb) RunWithContext ¶
RunWithContext is a context-aware parser (aka, it does not discard state between runs). It returns a magic integer that describes the stack in place, so that it can be re-used on the next call. This is how local variables can traverse ruby parse invocations.
Otherwise, it is very similar in function to Run()
func (*Mrb) SetGlobalVariable ¶
SetGlobalVariable sets the value of the global variable by the given name.
func (*Mrb) StringValue ¶
StringValue returns a Value for a string.
type MrbValue ¶
type MrbValue struct {
// contains filtered or unexported fields
}
MrbValue is a "value" internally in mruby. A "value" is what mruby calls basically anything in Ruby: a class, an object (instance), a variable, etc.
func (*MrbValue) Array ¶
Array returns the Array value of this value. If the Type of the MrbValue is not a TypeArray, then this will panic. If the MrbValue has a `to_a` function, you must call that manually prior to calling this method.
func (*MrbValue) CallBlock ¶
CallBlock is the same as call except that it expects the last argument to be a Proc that will be passed into the function call. It is an error if args is empty or if there is no block on the end.
func (*MrbValue) Fixnum ¶
Fixnum returns the numeric value of this object if the Type() is TypeFixnum. Calling this with any other type will result in undefined behavior.
func (*MrbValue) Float ¶
Float returns the numeric value of this object if the Type() is TypeFloat. Calling this with any other type will result in undefined behavior.
func (*MrbValue) GCProtect ¶
func (v *MrbValue) GCProtect()
GCProtect protects this value from being garbage collected.
func (*MrbValue) GetInstanceVariable ¶
GetInstanceVariable gets an instance variable on this value.
func (*MrbValue) Hash ¶
Hash returns the Hash value of this value. If the Type of the MrbValue is not a ValueTypeHash, then this will panic. If the MrbValue has a `to_h` function, you must call that manually prior to calling this method.
func (*MrbValue) SetInstanceVariable ¶
SetInstanceVariable sets an instance variable on this value.
func (*MrbValue) SetProcTargetClass ¶
SetProcTargetClass sets the target class where a proc will be executed when this value is a proc.
func (*MrbValue) SingletonClass ¶
SingletonClass returns the singleton class (a class isolated just for the scope of the object) for the given value.
type NilType ¶
type NilType [0]byte
NilType is the object representation of NilClass
var Nil NilType
Nil is a constant that can be used as a Nil Value
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a parser for Ruby code.
func NewParser ¶
NewParser initializes the resources for a parser.
Make sure to Close the parser when you're done with it.
func (*Parser) Close ¶
func (p *Parser) Close()
Close releases any resources associated with the parser.
func (*Parser) GenerateCode ¶
GenerateCode takes all the internal parser state and generates executable Ruby code, returning the callable proc.
func (*Parser) Parse ¶
func (p *Parser) Parse(code string, c *CompileContext) ([]*ParserMessage, error)
Parse parses the code in the given context, and returns any warnings or errors from parsing.
The CompileContext can be nil to not set a context.
type ParserError ¶
type ParserError struct {
Errors []*ParserMessage
}
ParserError is an error from the parser.
func (ParserError) Error ¶
func (p ParserError) Error() string
func (ParserError) String ¶
func (p ParserError) String() string
type ParserMessage ¶
ParserMessage represents a message from parsing code: a warning or error.
type Value ¶
Value is an interface that should be implemented by anything that can be represents as an mruby value.
type ValueType ¶
type ValueType uint32
ValueType is an enum of types that a Value can be and is returned by Value.Type().
const ( // TypeFalse is `false` TypeFalse ValueType = iota // TypeFree is ? TypeFree // TypeTrue is `true` TypeTrue // TypeFixnum is fixnums, or integers for this case. TypeFixnum // TypeSymbol is for entities in ruby that look like `:this` TypeSymbol // TypeUndef is a value internal to ruby for uninstantiated vars. TypeUndef // TypeFloat is any floating point number such as 1.2, etc. TypeFloat // TypeCptr is a void* TypeCptr // TypeObject is a standard ruby object, base class of most instantiated objects. TypeObject // TypeClass is the base class of all classes. TypeClass // TypeModule is the base class of all Modules. TypeModule // TypeIClass is ? TypeIClass // TypeSClass is ? TypeSClass // TypeProc are procs (concrete block definitons) TypeProc // TypeArray is [] TypeArray // TypeHash is { } TypeHash // TypeString is "" TypeString // TypeRange is (0..x) TypeRange // TypeException is raised when using the raise keyword TypeException // TypeFile is for objects of the File class TypeFile // TypeEnv is for getenv/setenv etc TypeEnv // TypeData is ? TypeData // TypeFiber is for members of the Fiber class TypeFiber // TypeMaxDefine is ? TypeMaxDefine // TypeNil is nil TypeNil ValueType = 0xffffffff )