Skip to content

Commit

Permalink
Retry remove-builtin-method-function branch #683
Browse files Browse the repository at this point in the history
- See #683

- Updated some classes such as null.go, too
  • Loading branch information
hachi8833 committed Oct 19, 2018
1 parent e85b986 commit 35ef91a
Show file tree
Hide file tree
Showing 23 changed files with 8,373 additions and 8,465 deletions.
2,532 changes: 1,264 additions & 1,268 deletions vm/array.go

Large diffs are not rendered by default.

128 changes: 62 additions & 66 deletions vm/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,75 +53,71 @@ type BlockObject struct {
}

// Class methods --------------------------------------------------------
func builtinBlockClassMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
// @param block literal
// @return [Block]
Name: "new",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if blockFrame == nil {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Can't initialize block object without block argument")
}

return t.vm.initBlockObject(blockFrame.instructionSet, blockFrame.ep, blockFrame.self)
},
var builtinBlockClassMethods = []*BuiltinMethodObject{
{
// @param block literal
// @return [Block]
Name: "new",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if blockFrame == nil {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Can't initialize block object without block argument")
}

return t.vm.initBlockObject(blockFrame.instructionSet, blockFrame.ep, blockFrame.self)
},
}
},
}

// Instance methods -----------------------------------------------------
func builtinBlockInstanceMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
// Executes the block and returns the result.
// It can take arbitrary number of arguments and passes them to the block arguments of the block object,
// keeping the order of the arguments.
//
// ```ruby
// bl = Block.new do |array|
// array.reduce do |sum, i|
// sum + i
// end
// end
// #=> <Block: REPL>
// bl.call([1, 2, 3, 4]) #=> 10
// ```
//
// TODO: should check if the following behavior is OK or not
// Note that the method does NOT check the number of the arguments and the number of block parameters.
// * if the number of the arguments exceed, the rest will just be truncated:
//
// ```ruby
// p = Block.new do |i, j, k|
// [i, j, k]
// end
// p.call(1, 2, 3, 4, 5) #=> [1, 2, 3]
// ```
//
// * if the number of the block parameters exceeds, the rest will just be filled with `nil`:
//
// ```ruby
// p = Block.new do |i, j, k|
// [i, j, k]
// end
// p.call #=> [nil, nil, nil]
// ```
//
// @param object [Object]...
// @return [Object]
Name: "call",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
block := receiver.(*BlockObject)
c := newNormalCallFrame(block.instructionSet, block.instructionSet.filename, sourceLine)
c.ep = block.ep
c.self = block.self
c.isBlock = true

return t.builtinMethodYield(c, args...).Target
},
var builtinBlockInstanceMethods = []*BuiltinMethodObject{
{
// Executes the block and returns the result.
// It can take arbitrary number of arguments and passes them to the block arguments of the block object,
// keeping the order of the arguments.
//
// ```ruby
// bl = Block.new do |array|
// array.reduce do |sum, i|
// sum + i
// end
// end
// #=> <Block: REPL>
// bl.call([1, 2, 3, 4]) #=> 10
// ```
//
// TODO: should check if the following behavior is OK or not
// Note that the method does NOT check the number of the arguments and the number of block parameters.
// * if the number of the arguments exceed, the rest will just be truncated:
//
// ```ruby
// p = Block.new do |i, j, k|
// [i, j, k]
// end
// p.call(1, 2, 3, 4, 5) #=> [1, 2, 3]
// ```
//
// * if the number of the block parameters exceeds, the rest will just be filled with `nil`:
//
// ```ruby
// p = Block.new do |i, j, k|
// [i, j, k]
// end
// p.call #=> [nil, nil, nil]
// ```
//
// @param object [Object]...
// @return [Object]
Name: "call",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
block := receiver.(*BlockObject)
c := newNormalCallFrame(block.instructionSet, block.instructionSet.filename, sourceLine)
c.ep = block.ep
c.self = block.self
c.isBlock = true

return t.builtinMethodYield(c, args...).Target
},
}
},
}

// Internal functions ===================================================
Expand All @@ -130,8 +126,8 @@ func builtinBlockInstanceMethods() []*BuiltinMethodObject {

func (vm *VM) initBlockClass() *RClass {
class := vm.initializeClass(classes.BlockClass)
class.setBuiltinMethods(builtinBlockClassMethods(), true)
class.setBuiltinMethods(builtinBlockInstanceMethods(), false)
class.setBuiltinMethods(builtinBlockClassMethods, true)
class.setBuiltinMethods(builtinBlockInstanceMethods, false)
return class
}

Expand Down
16 changes: 7 additions & 9 deletions vm/boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ var (
)

// Class methods --------------------------------------------------------
func builtinBooleanClassMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
Name: "new",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
return t.vm.InitNoMethodError(sourceLine, "new", receiver)
},
var builtinBooleanClassMethods = []*BuiltinMethodObject{
{
Name: "new",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
return t.vm.InitNoMethodError(sourceLine, "new", receiver)
},
}
},
}

// Internal functions ===================================================
Expand All @@ -43,7 +41,7 @@ func builtinBooleanClassMethods() []*BuiltinMethodObject {

func (vm *VM) initBoolClass() *RClass {
b := vm.initializeClass(classes.BooleanClass)
b.setBuiltinMethods(builtinBooleanClassMethods(), true)
b.setBuiltinMethods(builtinBooleanClassMethods, true)

TRUE = &BooleanObject{value: true, BaseObj: &BaseObj{class: b}}
FALSE = &BooleanObject{value: false, BaseObj: &BaseObj{class: b}}
Expand Down
Loading

0 comments on commit 35ef91a

Please sign in to comment.