vm

package
v0.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2020 License: MIT Imports: 37 Imported by: 8

Documentation

Index

Constants

View Source
const Version = "0.1.13"

Version stores current Goby version

Variables

View Source
var ConcurrentArrayMethodsForwardingTable = map[string]bool{
	"[]":           false,
	"*":            false,
	"+":            false,
	"[]=":          true,
	"any?":         false,
	"at":           false,
	"clear":        true,
	"concat":       true,
	"count":        false,
	"delete_at":    true,
	"each":         false,
	"each_index":   false,
	"empty?":       false,
	"first":        false,
	"flatten":      false,
	"join":         false,
	"last":         false,
	"length":       false,
	"map":          false,
	"pop":          true,
	"push":         true,
	"reduce":       false,
	"reverse":      false,
	"reverse_each": false,
	"rotate":       false,
	"select":       false,
	"shift":        true,
	"unshift":      true,
	"values_at":    false,
}

ConcurrentArrayMethodsForwardingTable is a pseudo-constant definition of the forwarded methods, mapped to a boolean representing the requirement for a write lock (true) or read lock (false)

We don't implement dig, as it has no concurrency guarantees.

View Source
var DefaultLibPath string

DefaultLibPath is used for overriding vm.libpath build-time.

Functions

func ConvertToGoFuncArgs added in v0.1.11

func ConvertToGoFuncArgs(args []Object) ([]interface{}, error)

ConvertToGoFuncArgs converts Goby's args to Go func's args

func PrintError added in v0.1.3

func PrintError(v *VM)

PrintError prints an error report string given a vm which evaluated to and Error object

func RegisterExternalClass added in v0.1.10

func RegisterExternalClass(name string, c ...ClassLoader)

RegisterExternalClass will add the given class to the global registry of available classes

func VerifyExpected added in v0.1.10

func VerifyExpected(t *testing.T, i int, evaluated Object, expected interface{})

VerifyExpected is a verification helpers for testing

Types

type ArrayObject

type ArrayObject struct {
	*BaseObj
	Elements []Object
	// contains filtered or unexported fields
}

ArrayObject represents an instance from Array class. An array is a collection of different objects that are ordered and indexed. Elements in an array can belong to any class and you can also build a "tuple" within an array. Array objects should always be enumerable.

func (*ArrayObject) Inspect

func (a *ArrayObject) Inspect() string

Inspect delegates to ToString

func (*ArrayObject) Len added in v0.1.11

func (a *ArrayObject) Len() int

Len returns the length of array's elements

func (*ArrayObject) Less added in v0.1.11

func (a *ArrayObject) Less(i, j int) bool

Less is one of the required method to fulfill sortable interface

func (*ArrayObject) Swap added in v0.1.11

func (a *ArrayObject) Swap(i, j int)

Swap is one of the required method to fulfill sortable interface

func (*ArrayObject) ToJSON added in v0.1.11

func (a *ArrayObject) ToJSON(t *Thread) string

ToJSON returns the object's elements as the JSON string format

func (*ArrayObject) ToString added in v0.1.11

func (a *ArrayObject) ToString() string

ToString returns the object's elements as the string format

func (*ArrayObject) Value added in v0.1.0

func (a *ArrayObject) Value() interface{}

Value returns the elements from the object

type BaseObj added in v0.1.11

type BaseObj struct {
	InstanceVariables *environment
	// contains filtered or unexported fields
}

BaseObj ==============================================================

func NewBaseObject added in v0.1.11

func NewBaseObject(c *RClass) *BaseObj

NewBaseObject creates a BaseObj

func (*BaseObj) Class added in v0.1.11

func (b *BaseObj) Class() *RClass

Class will return object's class

func (*BaseObj) ID added in v0.1.12

func (b *BaseObj) ID() int

ID returns the BaseObj's id

func (*BaseObj) InstanceVariableGet added in v0.1.11

func (b *BaseObj) InstanceVariableGet(name string) (Object, bool)

InstanceVariableGet returns an instance variable specified

func (*BaseObj) InstanceVariableSet added in v0.1.11

func (b *BaseObj) InstanceVariableSet(name string, value Object) Object

InstanceVariableSet sets the instance variable specified

func (*BaseObj) SetSingletonClass added in v0.1.11

func (b *BaseObj) SetSingletonClass(c *RClass)

SetSingletonClass sets object's singleton class

func (*BaseObj) SingletonClass added in v0.1.11

func (b *BaseObj) SingletonClass() *RClass

SingletonClass returns object's singleton class

type BlockObject added in v0.1.7

type BlockObject struct {
	*BaseObj
	// contains filtered or unexported fields
}

BlockObject represents an instance of `Block` class. In Goby, block literals can be used to define an "anonymous function" by using the `Block` class.

A block literal consists of `do`-`end` and code snippets between them, containing optional "block parameters" surrounded by `| |` that can be referred to within the block as "block variables".

`Block.new` can take a block literal, returning a "block" object.

You can call `#call` method on the block object to execute the block whenever and wherever you want. You can even pass around the block objects across your codebase.

```ruby bl = Block.new do |array|

array.reduce do |sum, i|
  sum + i
end

end

#=> <Block: REPL>

bl.call([1, 2, 3, 4]) #=> 10 ```

You can even form a `closure` (note that you can do that without using `Block.new`):

```ruby n = 1 bl = Block.new do

n = n + 1

end #=> <Block: REPL> bl.call #=> 2 bl.call #=> 3 bl.call #=> 4 ```

func (*BlockObject) Inspect added in v0.1.11

func (bo *BlockObject) Inspect() string

Inspect delegates to ToString

func (*BlockObject) ToJSON added in v0.1.11

func (bo *BlockObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*BlockObject) ToString added in v0.1.11

func (bo *BlockObject) ToString() string

ToString returns the object's name as the string format

func (*BlockObject) Value added in v0.1.7

func (bo *BlockObject) Value() interface{}

Value returns the object

type BooleanObject

type BooleanObject struct {
	*BaseObj
	// contains filtered or unexported fields
}

BooleanObject represents boolean object in goby and no instance methods are contained within it. `Boolean` class is just a dummy to hold logical `true` and `false` representation and no other active usage. `Boolean.new` is not supported.

Please note that class checking such as `#is_a?(Boolean)` **should be avoided in principle**. `#is_a?` often leads to redundant code. Consider using `respond_to?` first, but actually it is unnecessary in almost all cases.

var (
	// TRUE is shared boolean object that represents true
	TRUE *BooleanObject
	// FALSE is shared boolean object that represents false
	FALSE *BooleanObject
)

func (*BooleanObject) Inspect

func (b *BooleanObject) Inspect() string

Inspect delegates to ToString

func (*BooleanObject) ToJSON added in v0.1.11

func (b *BooleanObject) ToJSON(t *Thread) string

ToJSON just delegates to `ToString`

func (*BooleanObject) ToString added in v0.1.11

func (b *BooleanObject) ToString() string

ToString returns the object's name as the string format

func (*BooleanObject) Value

func (b *BooleanObject) Value() interface{}

Value returns the object

type BuiltinMethodObject added in v0.1.3

type BuiltinMethodObject struct {
	*BaseObj
	Name string
	Fn   builtinMethodBody
}

BuiltinMethodObject represents methods defined in go.

func DefineForwardedConcurrentArrayMethod added in v0.1.6

func DefineForwardedConcurrentArrayMethod(methodName string, requireWriteLock bool) *BuiltinMethodObject

DefineForwardedConcurrentArrayMethod defines methods for ConcurrentArrayObject

func ExternalBuiltinMethod added in v0.1.10

func ExternalBuiltinMethod(name string, m Method) *BuiltinMethodObject

ExternalBuiltinMethod is a function that builds a BuiltinMethodObject from an external function

func (*BuiltinMethodObject) Inspect added in v0.1.11

func (bim *BuiltinMethodObject) Inspect() string

Inspect delegates to ToString

func (*BuiltinMethodObject) ToJSON added in v0.1.11

func (bim *BuiltinMethodObject) ToJSON(t *Thread) string

ToJSON just delegates to `ToString`

func (*BuiltinMethodObject) ToString added in v0.1.11

func (bim *BuiltinMethodObject) ToString() string

ToString returns the object's name as the string format

func (*BuiltinMethodObject) Value added in v0.1.6

func (bim *BuiltinMethodObject) Value() interface{}

Value returns builtin method object's function

type ChannelObject added in v0.0.8

type ChannelObject struct {
	*BaseObj
	Chan         chan int
	ChannelState int
}

ChannelObject represents Goby's "channel", which equips the Golang' channel and works with `thread`. `thread` is actually a "goroutine". A channel object can relay any kind of objects and guarantees thread-safe communications. You should always use channel objects for safe communications between threads. `Channel#new` is available.

Note that channels are not like files and you don't need to explicitly close them (e.g.: exiting a loop). See https://tour.golang.org/concurrency/4

```ruby def f(from)

i = 0
while i < 3 do
  puts(from + ": " + i.to_s)
  i += 1
end

end

f("direct")

c = Channel.new # spawning a channel object

thread do

puts(c.receive)
f("thread")

end

thread do

puts("going")
c.deliver(10)

end

sleep(2) # This is to prevent main program finished before goroutine. ```

Note that the possibility of race conditions still exists. Handle them with care.

```ruby c = Channel.new

i = 0 thread do

i += 1
c.deliver(i)     # sends `i` to channel `c`

end

# If we put a bare `i += 1` here, then it will execute along with other thread, # which will cause a race condition. # The following "receive" is needed to block the main process until thread is finished c.receive i += 1

c.close # Redundant: just for explanation and you don't need to call this here ```

func (*ChannelObject) Inspect added in v0.1.11

func (co *ChannelObject) Inspect() string

Inspect delegates to ToString

func (*ChannelObject) ToJSON added in v0.1.11

func (co *ChannelObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*ChannelObject) ToString added in v0.1.11

func (co *ChannelObject) ToString() string

ToString returns the object's name as the string format

func (*ChannelObject) Value added in v0.1.0

func (co *ChannelObject) Value() interface{}

Value returns the object

type ClassLoader added in v0.1.10

type ClassLoader = func(*VM) error

ClassLoader can be registered with a vm so that it can load this library at vm creation

func NewExternalClassLoader added in v0.1.13

func NewExternalClassLoader(className, libPath string, classMethods, instanceMethods map[string]Method) ClassLoader

NewExternalClassLoader helps define external go classes by generating a class loader function

type ConcurrentArrayObject added in v0.1.6

type ConcurrentArrayObject struct {
	*BaseObj
	InternalArray *ArrayObject

	sync.RWMutex
}

ConcurrentArrayObject is a thread-safe Array, implemented as a wrapper of an ArrayObject, coupled with an R/W mutex.

Arrays returned by any of the methods are in turn thread-safe.

For implementation simplicity, methods are simple redirection, and defined via a table.

func (*ConcurrentArrayObject) Inspect added in v0.1.11

func (cao *ConcurrentArrayObject) Inspect() string

Inspect delegates to ToString

func (*ConcurrentArrayObject) ToJSON added in v0.1.11

func (cao *ConcurrentArrayObject) ToJSON(t *Thread) string

ToJSON returns the object's name as the JSON string format

func (*ConcurrentArrayObject) ToString added in v0.1.11

func (cao *ConcurrentArrayObject) ToString() string

ToString returns the object's name as the string format

func (*ConcurrentArrayObject) Value added in v0.1.6

func (cao *ConcurrentArrayObject) Value() interface{}

Value returns the object

type ConcurrentHashObject added in v0.1.6

type ConcurrentHashObject struct {
	*BaseObj
	// contains filtered or unexported fields
}

ConcurrentHashObject is an implementation of thread-safe associative arrays (Hash).

The implementation internally uses Go's `sync.Map` type, with some advantages and disadvantages:

- it is highly performant and predictable for a certain pattern of usage (`concurrent loops with keys that are stable over time, and either few steady-state stores, or stores localized to one goroutine per key.`); performance and predictability in other conditions are unspecified; - iterations are non-deterministic; during iterations, keys may not be included; - size can't be retrieved; - for the reasons above, the Hash APIs implemented are minimal.

For details, see https://golang.org/pkg/sync/#Map.

```ruby require 'concurrent/hash' hash = Concurrent::Hash.new({ "a": 1, "b": 2 }) hash["a"] # => 1 ```

func (*ConcurrentHashObject) Inspect added in v0.1.11

func (h *ConcurrentHashObject) Inspect() string

Inspect delegates to ToString

func (*ConcurrentHashObject) ToJSON added in v0.1.11

func (h *ConcurrentHashObject) ToJSON(t *Thread) string

ToJSON returns the object's name as the JSON string format

func (*ConcurrentHashObject) ToString added in v0.1.11

func (h *ConcurrentHashObject) ToString() string

ToString returns the object's name as the string format

func (*ConcurrentHashObject) Value added in v0.1.6

func (h *ConcurrentHashObject) Value() interface{}

Value returns the object

type ConcurrentRWLockObject added in v0.1.6

type ConcurrentRWLockObject struct {
	*BaseObj
	// contains filtered or unexported fields
}

ConcurrentRWLockObject is a Readers-Writer Lock (readers can concurrently put a lock, while a writer requires exclusive access).

The implementation internally uses Go's `sync.RWLock` type.

```ruby require 'concurrent/rw_lock' lock = Concurrent::RWLock.new lock.with_read_lock do

# critical section

end lock.with_write_lock do

# critical section

end ```

func (*ConcurrentRWLockObject) Inspect added in v0.1.11

func (lock *ConcurrentRWLockObject) Inspect() string

Inspect delegates to ToString

func (*ConcurrentRWLockObject) ToJSON added in v0.1.11

func (lock *ConcurrentRWLockObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*ConcurrentRWLockObject) ToString added in v0.1.11

func (lock *ConcurrentRWLockObject) ToString() string

ToString returns the object's name as the string format

func (*ConcurrentRWLockObject) Value added in v0.1.6

func (lock *ConcurrentRWLockObject) Value() interface{}

Value returns the object

type Decimal added in v0.1.6

type Decimal = big.Rat

Decimal is a type alias for representing a decimal

type DecimalObject added in v0.1.6

type DecimalObject struct {
	*BaseObj
	// contains filtered or unexported fields
}

DecimalObject represents a comparable decimal number using Go's Rational representation `big.Rat` from math/big package, which consists of a numerator and a denominator with arbitrary size (experimental). By using Decimal you can avoid errors on float type during calculations. To keep accuracy, avoid conversions until all calculations have been finished. The numerator can be 0, but the denominator cannot be 0. Using Decimal for loop counters or like that is not recommended (TBD).

```ruby "3.14".to_d # => 3.14 "-0.7238943".to_d # => -0.7238943 "355/113".to_d # => 3.1415929203539823008849557522123893805309734513274336283185840

a = "16.1".to_d b = "1.1".to_d e = "17.2".to_d a + b # => 0.1 a + b == e # => true

('16.1'.to_d + "1.1".to_d).to_s #=> 17.2 ('16.1'.to_f + "1.1".to_f).to_s #=> 17.200000000000003 ```

- `Decimal.new` is not supported.

func (*DecimalObject) DecimalValue added in v0.1.6

func (d *DecimalObject) DecimalValue() interface{}

DecimalValue is an alias of Value()

func (*DecimalObject) FloatValue added in v0.1.6

func (d *DecimalObject) FloatValue() float64

FloatValue is a float interface

func (*DecimalObject) Inspect added in v0.1.11

func (d *DecimalObject) Inspect() string

Inspect delegates to ToString

func (*DecimalObject) IntegerValue added in v0.1.6

func (d *DecimalObject) IntegerValue() int

IntegerValue returns integer part of decimal

func (*DecimalObject) ToJSON added in v0.1.11

func (d *DecimalObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*DecimalObject) ToString added in v0.1.11

func (d *DecimalObject) ToString() string

ToString returns the object's approximate float value as the string format.

func (*DecimalObject) Value added in v0.1.6

func (d *DecimalObject) Value() interface{}

Value returns the object

type Diggable added in v0.1.6

type Diggable interface {
	// contains filtered or unexported methods
}

Diggable represents a class that support the #dig method.

type Error

type Error struct {
	*BaseObj

	Type string
	// contains filtered or unexported fields
}

Error class is actually a special struct to hold internal error types with messages. Goby developers need not to take care of the struct. Goby maintainers should consider using the appropriate error type. Cannot create instances of Error class, or inherit Error class.

The type of internal errors:

see vm/errors/error.go.

func (*Error) Inspect

func (e *Error) Inspect() string

Inspect delegates to ToString

func (*Error) Message

func (e *Error) Message() string

Message prints the error's message and its stack traces

func (*Error) ToJSON added in v0.1.11

func (e *Error) ToJSON(t *Thread) string

ToJSON just delegates to `ToString`

func (*Error) ToString added in v0.1.11

func (e *Error) ToString() string

ToString returns the object's name as the string format

func (*Error) Value added in v0.1.6

func (e *Error) Value() interface{}

Value is equivalent to ToString

type FileObject added in v0.0.6

type FileObject struct {
	*BaseObj
	File *os.File
}

FileObject is a special type that contains file pointer so we can keep track on target file. Using `File.open` with block is recommended because the instance (block variable) automatically closes.

```ruby File.open("/tmp/goby/out.txt", "w", 0755) do |f|

a = f.read
f.write(a + "12345")

end # f automatically closes ```

func (*FileObject) Inspect added in v0.0.6

func (f *FileObject) Inspect() string

Inspect delegates to ToString

func (*FileObject) ToJSON added in v0.1.11

func (f *FileObject) ToJSON(t *Thread) string

ToJSON just delegates to `ToString`

func (*FileObject) ToString added in v0.1.11

func (f *FileObject) ToString() string

ToString returns the object's name as the string format

func (*FileObject) Value added in v0.1.6

func (f *FileObject) Value() interface{}

Value returns file object's string format

type Float added in v0.1.6

type Float = big.Float

Float type alias for representing a bitfloat

type FloatObject added in v0.1.6

type FloatObject struct {
	*BaseObj
	// contains filtered or unexported fields
}

FloatObject represents an inexact real number using the native architecture's double-precision floating point representation.

```ruby 1.1 + 1.1 # => 2.2 2.1 * 2.1 # => 4.41 ```

- `Float.new` is not supported.

func (*FloatObject) Inspect added in v0.1.11

func (f *FloatObject) Inspect() string

Inspect delegates to ToString

func (*FloatObject) ToJSON added in v0.1.11

func (f *FloatObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*FloatObject) ToString added in v0.1.11

func (f *FloatObject) ToString() string

ToString returns the object's value as the string format, in non exponential format (straight number, without exponent `E<exp>`).

func (*FloatObject) Value added in v0.1.6

func (f *FloatObject) Value() interface{}

Value returns the object

type GoMap added in v0.1.6

type GoMap struct {
	*BaseObj
	// contains filtered or unexported fields
}

GoMap ...

func (*GoMap) Inspect added in v0.1.11

func (m *GoMap) Inspect() string

Inspect delegates to ToString

func (*GoMap) ToJSON added in v0.1.11

func (m *GoMap) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*GoMap) ToString added in v0.1.11

func (m *GoMap) ToString() string

ToString returns the object's name as the string format

func (*GoMap) Value added in v0.1.6

func (m *GoMap) Value() interface{}

Value returns the object

type GoObject added in v0.1.0

type GoObject struct {
	*BaseObj
	// contains filtered or unexported fields
}

GoObject ...

func (*GoObject) Inspect added in v0.1.11

func (s *GoObject) Inspect() string

Inspect delegates to ToString

func (*GoObject) ToJSON added in v0.1.11

func (s *GoObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*GoObject) ToString added in v0.1.11

func (s *GoObject) ToString() string

ToString returns the object's name as the string format

func (*GoObject) Value added in v0.1.0

func (s *GoObject) Value() interface{}

Value returns the object

type HashObject

type HashObject struct {
	*BaseObj
	Pairs map[string]Object

	// See `[]` and `[]=` for the operational explanation of the default value.
	Default Object
}

HashObject represents hash instances. Hash is a collection of key-value pair, which works like a dictionary. Hash literal is represented with curly brackets `{ }` like `{ key: value }`. Each key of the hash is unique and cannot be duplicate within the hash. Adding a leading space and a trailing space within curly brackets are preferable.

- **Key:** an alphanumeric word that starts with alphabet, without containing space and punctuations. Underscore `_` can also be used within the key. In hash literals, only a symbol literals such as `symbol:` can be used as a key. String literal like "mickey mouse" cannot be used as a key in hash literals. (String and symbol are equivalent in Goby)

Retrieving a value via `[]`, you can use both symbol literals or string literals as keys.

```ruby a = { balthazar1: 100 } # valid b = { 2melchior: 200 } # invalid b = { "casper": 200 } # invalid x = 'balthazar1'

a["balthazar1"] # => 100 a[balthazar1:] # => 100 a[x] # => 100 a[balthazar1] # => error ```

- **value:** String literals and objects (Integer, String, Array, Hash, nil, etc) can be used.

**Note:** - The order of key-value pairs are **not** preserved. - Operator `=>` is not supported. - `Hash.new` is not supported.

func (*HashObject) Inspect

func (h *HashObject) Inspect() string

Inspect delegates to ToString

func (*HashObject) ToJSON added in v0.1.11

func (h *HashObject) ToJSON(t *Thread) string

ToJSON returns the object's name as the JSON string format

func (*HashObject) ToString added in v0.1.11

func (h *HashObject) ToString() string

ToString returns the object's name as the string format

func (*HashObject) Value added in v0.1.0

func (h *HashObject) Value() interface{}

Value returns the object

type Int added in v0.1.6

type Int = big.Int

Int is a type alias for representing a bigint

type IntegerObject

type IntegerObject struct {
	*BaseObj
	// contains filtered or unexported fields
}

IntegerObject represents number objects which can bring into mathematical calculations.

```ruby 1 + 1 # => 2 2 * 2 # => 4 ```

- `Integer.new` is not supported.

func (*IntegerObject) Inspect

func (i *IntegerObject) Inspect() string

Inspect delegates to ToString

func (*IntegerObject) ToJSON added in v0.1.11

func (i *IntegerObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*IntegerObject) ToString added in v0.1.11

func (i *IntegerObject) ToString() string

ToString returns the object's name as the string format

func (*IntegerObject) Value

func (i *IntegerObject) Value() interface{}

Value returns the object

type Match added in v0.1.6

type Match = regexp2.Match

Match is an alias of regexp2.Match

type MatchDataObject added in v0.1.6

type MatchDataObject struct {
	*BaseObj
	// contains filtered or unexported fields
}

MatchDataObject represents the match data returned by a regular expression matching operation. You can use named-captures via `(?<name>)`.

```ruby 'abcd'.match(Regexp.new('(b.)')) #=> #<MatchData 0:"bc" 1:"bc">

'abcd'.match(Regexp.new('a(?<first>b)(?<second>c)')) #=> #<MatchData 0:"abc" first:"b" second:"c"> ```

- `MatchData.new` is not supported.

func (*MatchDataObject) Inspect added in v0.1.11

func (m *MatchDataObject) Inspect() string

Inspect delegates to ToString

func (*MatchDataObject) ToJSON added in v0.1.11

func (m *MatchDataObject) ToJSON(t *Thread) string

ToJSON returns a `{ captureNumber: captureValue }` JSON-encoded string

func (*MatchDataObject) ToString added in v0.1.11

func (m *MatchDataObject) ToString() string

ToString returns a string representation of the object

func (*MatchDataObject) Value added in v0.1.6

func (m *MatchDataObject) Value() interface{}

Value redirects to ToString()

type Method

type Method = func(receiver Object, line int, t *Thread, args []Object) Object

Method is a callable function

type MethodObject added in v0.0.5

type MethodObject struct {
	*BaseObj
	Name string
	// contains filtered or unexported fields
}

MethodObject represents methods defined using goby.

func (*MethodObject) Inspect added in v0.0.5

func (m *MethodObject) Inspect() string

Inspect delegates to ToString

func (*MethodObject) ToJSON added in v0.1.11

func (m *MethodObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*MethodObject) ToString added in v0.1.11

func (m *MethodObject) ToString() string

ToString returns the object's name as the string format

func (*MethodObject) Value added in v0.1.6

func (m *MethodObject) Value() interface{}

Value returns method object's string format

type NullObject added in v0.0.5

type NullObject struct {
	*BaseObj
}

NullObject (`nil`) represents the null value in Goby. `nil` is convert into `null` when exported to JSON format. - `Null.new` is not supported.

var (
	// NULL represents Goby's null objects.
	NULL *NullObject
)

func (*NullObject) Inspect added in v0.0.5

func (n *NullObject) Inspect() string

Inspect returns string "nil" instead of "" like ToString

func (*NullObject) ToJSON added in v0.1.11

func (n *NullObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*NullObject) ToString added in v0.1.11

func (n *NullObject) ToString() string

ToString returns the object's name as the string format

func (*NullObject) Value added in v0.1.0

func (n *NullObject) Value() interface{}

Value returns the object

type Numeric added in v0.1.6

type Numeric interface {
	// contains filtered or unexported methods
}

Numeric currently represents a class that support some numeric conversions. At this stage, it's not meant to be a Goby class in a strict sense, but only a convenient interface.

type Object

type Object interface {
	Class() *RClass
	Value() interface{}
	SingletonClass() *RClass
	SetSingletonClass(*RClass)

	ToString() string
	Inspect() string
	ToJSON(t *Thread) string
	ID() int
	InstanceVariableGet(string) (Object, bool)
	InstanceVariableSet(string, Object) Object
	// contains filtered or unexported methods
}

Object represents all objects in Goby, including Array, Integer or even Method and Error.

func ExecAndReturn added in v0.1.10

func ExecAndReturn(t *testing.T, src string) Object

ExecAndReturn is a test helper

type Pointer

type Pointer struct {
	Target Object
	// contains filtered or unexported fields
}

Pointer is used to point to an object. Variables should hold pointer instead of holding a object directly.

type RClass

type RClass struct {
	// Name is the class's name
	Name string
	// Methods contains its instances' methods
	Methods *environment

	*BaseObj
	// contains filtered or unexported fields
}

RClass represents normal (not built in) class object

func (*RClass) Inspect added in v0.1.11

func (c *RClass) Inspect() string

Inspect delegates to ToString

func (*RClass) ReturnName added in v0.0.9

func (c *RClass) ReturnName() string

ReturnName returns the object's name as the string format

func (*RClass) ToJSON added in v0.1.11

func (c *RClass) ToJSON(t *Thread) string

ToJSON just delegates to `ToString`

func (*RClass) ToString added in v0.1.11

func (c *RClass) ToString() string

ToString returns the object's name as the string format

func (*RClass) Value added in v0.1.6

func (c *RClass) Value() interface{}

Value returns class itself

type RObject

type RObject struct {
	*BaseObj
	InitializeMethod *MethodObject
}

RObject represents any non built-in class's instance.

func (*RObject) Inspect

func (ro *RObject) Inspect() string

Inspect delegates to ToString

func (*RObject) ToJSON added in v0.1.11

func (ro *RObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*RObject) ToString added in v0.1.11

func (ro *RObject) ToString() string

ToString returns the object's name as the string format

func (*RObject) Value added in v0.1.6

func (ro *RObject) Value() interface{}

Value returns object's string format

type RangeObject added in v0.0.9

type RangeObject struct {
	*BaseObj
	Start int
	End   int
}

RangeObject is the built in range class Range represents an interval: a set of values from the beginning to the end specified. Currently, only Integer objects or integer literal are supported.

```ruby r = 0 (1..(1+4)).each do |i|

puts(r = r + i)

end ```

```ruby r = 0 a = 1 b = 5 (a..b).each do |i|

r = r + i

end ```

func (*RangeObject) Inspect added in v0.1.11

func (ro *RangeObject) Inspect() string

Inspect delegates to ToString

func (*RangeObject) ToJSON added in v0.1.11

func (ro *RangeObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*RangeObject) ToString added in v0.1.11

func (ro *RangeObject) ToString() string

ToString returns the object's name as the string format

func (*RangeObject) Value added in v0.1.6

func (ro *RangeObject) Value() interface{}

Value returns range object's string format

type Regexp added in v0.1.6

type Regexp = regexp2.Regexp

Regexp is an alias of regexp2.Regexp

type RegexpObject added in v0.1.6

type RegexpObject struct {
	*BaseObj
	// contains filtered or unexported fields
}

RegexpObject represents regexp instances, which of the type is actually string. Regexp object holds regexp strings. Powered by: github.com/dlclark/regexp2 The regexp2 package had been ported from .NET Framework's regexp library, which is PCRE-compatible and is almost all equivalent to Ruby's Onigmo regexp library.

```ruby a = Regexp.new("orl") a.match?("Hello World") #=> true a.match?("Hello Regexp") #=> false

b = Regexp.new("😏") b.match?("🤡 😏 😐") #=> true b.match?("😝 😍 😊") #=> false

c = Regexp.new("居(ら(?=れ)|さ(?=せ)|る|ろ|れ(?=[ばる])|よ|(?=な[いかくけそ]|ま[しすせ]|そう|た|て))") c.match?("居られればいいのに") #=> true c.match?("居ずまいを正す") #=> false ```

**Note:**

- Currently, manipulations are based upon Golang's Unicode manipulations. - Currently, UTF-8 encoding is assumed based upon Golang's string manipulation, but the encoding is not actually specified(TBD). - `Regexp.new` is exceptionally supported.

**To Goby maintainers**: avoid using Go's standard regexp package (slow and not rich). Consider the faster `Trim` or `Split` etc in Go's "strings" package first, or just use the dlclark/regexp2 instead. ToDo: Regexp literals with '/.../'

func (*RegexpObject) Inspect added in v0.1.11

func (r *RegexpObject) Inspect() string

Inspect delegates to ToString

func (*RegexpObject) ToJSON added in v0.1.11

func (r *RegexpObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*RegexpObject) ToString added in v0.1.11

func (r *RegexpObject) ToString() string

ToString returns the object's name as the string format

func (*RegexpObject) Value added in v0.1.6

func (r *RegexpObject) Value() interface{}

Value returns the object

type Stack added in v0.1.10

type Stack struct {

	// Although every thread has its own stack, vm's main thread still can be accessed by other threads.
	// This is why we need a lock in stack
	// TODO: Find a way to fix this instead of put lock on every stack.
	sync.RWMutex
	// contains filtered or unexported fields
}

Stack is a basic stack implementation

func (*Stack) Pop added in v0.1.10

func (s *Stack) Pop() *Pointer

Pop an element off the top of the stack

func (*Stack) Push added in v0.1.10

func (s *Stack) Push(v *Pointer)

Push an element to the top of the stack

func (*Stack) Set added in v0.1.10

func (s *Stack) Set(index int, pointer *Pointer)

Set a value at a given index in the stack. TODO: Maybe we should be checking for size before we do this.

type StringObject

type StringObject struct {
	*BaseObj
	// contains filtered or unexported fields
}

StringObject represents string instances. String object holds and manipulates a sequence of characters. String objects may be created using as string literals or symbol literals. Double or single quotations can be used for representation.

```ruby a = "Three" b = 'zero' c = '漢' d = 'Tiếng Việt' e = "😏️️" f = :symbol ```

**Note:**

- Currently, manipulations are based upon Golang's Unicode manipulations. - Currently, UTF-8 encoding is assumed based upon Golang's string manipulation, but the encoding is not actually specified(TBD). - `String.new` is not supported.

func (*StringObject) Inspect

func (s *StringObject) Inspect() string

Inspect wraps ToString with double quotes

func (*StringObject) ToJSON added in v0.1.11

func (s *StringObject) ToJSON(t *Thread) string

ToJSON just delegates to ToString

func (*StringObject) ToString added in v0.1.11

func (s *StringObject) ToString() string

ToString returns the object's name as the string format

func (*StringObject) Value

func (s *StringObject) Value() interface{}

Value returns the object

type Thread added in v0.1.10

type Thread struct {

	// data Stack
	Stack Stack
	// contains filtered or unexported fields
}

Thread is the context needed for a single thread of execution

func (*Thread) BlockGiven added in v0.1.11

func (t *Thread) BlockGiven() bool

BlockGiven returns whethe or not we have a block frame below us in the stack

func (*Thread) VM added in v0.1.10

func (t *Thread) VM() *VM

VM returns the vm of the thread

func (*Thread) Yield added in v0.1.11

func (t *Thread) Yield(args ...Object) *Pointer

Yield to a call frame

type VM

type VM struct {
	// contains filtered or unexported fields
}

VM represents a stack based virtual machine.

func InitIssueReportVM added in v0.1.3

func InitIssueReportVM(dir string, args []string) (*VM, error)

InitIssueReportVM initializes a vm in test mode for issue reporting

func New

func New(fileDir string, args []string) (vm *VM, e error)

New initializes a vm to initialize state and returns it.

func (*VM) ExecInstructions added in v0.0.9

func (vm *VM) ExecInstructions(sets []*bytecode.InstructionSet, fn string)

ExecInstructions accepts a sequence of bytecodes and use vm to evaluate them.

func (*VM) GetExecResult

func (vm *VM) GetExecResult() Object

GetExecResult returns stack's top most value. Normally it's used in tests.

func (*VM) GetREPLResult added in v0.0.9

func (vm *VM) GetREPLResult() string

GetREPLResult returns strings that should be showed after each evaluation.

func (*VM) InitArrayObject added in v0.1.10

func (vm *VM) InitArrayObject(elements []Object) *ArrayObject

InitArrayObject returns a new object with the given elemnts

func (*VM) InitErrorObject added in v0.1.10

func (vm *VM) InitErrorObject(errorType string, sourceLine int, format string, args ...interface{}) *Error

InitErrorObject initializes and returns Error object

func (*VM) InitForREPL added in v0.0.9

func (vm *VM) InitForREPL()

InitForREPL does following things: - Initialize instruction sets' index tables - Set vm to REPL mode - Create and push main object frame

func (*VM) InitHashObject added in v0.1.10

func (vm *VM) InitHashObject(pairs map[string]Object) *HashObject

InitHashObject creates a HashObject

func (*VM) InitIntegerObject added in v0.1.10

func (vm *VM) InitIntegerObject(value int) *IntegerObject

InitIntegerObject initializes IntegerObject

func (*VM) InitNoMethodError added in v0.1.11

func (vm *VM) InitNoMethodError(sourceLine int, methodName string, receiver Object) *Error

InitNoMethodError is to print unsupported method errors. This is exported for using from sub-packages.

func (*VM) InitObjectFromGoType added in v0.1.10

func (v *VM) InitObjectFromGoType(value interface{}) Object

InitObjectFromGoType creates an object based on Go's type

func (*VM) InitStringObject added in v0.1.10

func (vm *VM) InitStringObject(value string) *StringObject

InitStringObject creates a StringObject

func (*VM) REPLExec added in v0.0.9

func (vm *VM) REPLExec(sets []*bytecode.InstructionSet)

REPLExec executes instructions differently from normal program execution.

func (*VM) SetClassISIndexTable added in v0.0.9

func (vm *VM) SetClassISIndexTable(fn filename)

SetClassISIndexTable adds new instruction set's index table to vm.classISIndexTables

func (*VM) SetMethodISIndexTable added in v0.0.9

func (vm *VM) SetMethodISIndexTable(fn filename)

SetMethodISIndexTable adds new instruction set's index table to vm.methodISIndexTables

func (*VM) TopLevelClass added in v0.1.11

func (vm *VM) TopLevelClass(cn string) *RClass

TopLevelClass returns a specified top-level class (stored under the Object constant)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL