Skip to content

Commit

Permalink
Add DefineConstById to register basic objects
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Jan 4, 2024
1 parent 6bc21c0 commit 8329f02
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
9 changes: 8 additions & 1 deletion class.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package mruby
type methodTable map[Symbol]*Method
type mt = methodTable

var _ RBasic = &RClass{}

type RClass struct {
RBasic
object
super *RClass
mt
iv
Expand Down Expand Up @@ -84,4 +86,9 @@ func initClass(mrb *State) {
basicObject.class = classClass
objectClass.class = classClass
moduleClass.class = classClass

mrb.DefineConstById(basicObject, mrb.Intern("BasicObject"), NewObjectValue(basicObject))
mrb.DefineConstById(objectClass, mrb.Intern("Object"), NewObjectValue(objectClass))
mrb.DefineConstById(objectClass, mrb.Intern("Module"), NewObjectValue(moduleClass))
mrb.DefineConstById(objectClass, mrb.Intern("Class"), NewObjectValue(classClass))
}
10 changes: 10 additions & 0 deletions feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ func (feat *RubyFeature) thereShouldReturnSymbol(expected string) error {
return nil
}

func (feat *RubyFeature) thereShouldReturnObject() error {
_, ok := feat.ret.(mruby.RBasic)
if !ok {
return fmt.Errorf("expected object, got %T", feat.ret)
}

return nil
}

func InitializeScenario(s *godog.ScenarioContext) {
feat := RubyFeature{
mrb: mruby.New(),
Expand All @@ -107,6 +116,7 @@ func InitializeScenario(s *godog.ScenarioContext) {
s.Step(`^there should return nil$`, feat.thereShouldReturnNil)
s.Step(`^there should return string "([^"]*)"$`, feat.thereShouldReturnString)
s.Step(`^there should return symbol "([^"]*)"$`, feat.thereShouldReturnSymbol)
s.Step(`^there should return object$`, feat.thereShouldReturnObject)
}

func TestFeatures(t *testing.T) {
Expand Down
21 changes: 21 additions & 0 deletions features/object.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: Object
Scenario: I can get Object class
When I execute ruby code:
"""
Object
"""
Then there should return object

Scenario: I can get Module class
When I execute ruby code:
"""
Module
"""
Then there should return object

Scenario: I can get Class class
When I execute ruby code:
"""
Class
"""
Then there should return object
14 changes: 12 additions & 2 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ package mruby

import "fmt"

type RBasic struct {
type object struct {
class *RClass
}

type RBasic interface {
Class() *RClass
}

var _ RBasic = &RObject{}

type RObject struct {
RBasic
object
}

func (obj *object) Class() *RClass {
return nil
}

func initObject(mrb *State) {
Expand Down
5 changes: 5 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mruby

func NewObjectValue(v any) Value {
return v.(RBasic)
}
4 changes: 4 additions & 0 deletions variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ func (iv ivTable) Set(sym Symbol, val Value) {
iv[sym] = val
}

func (mrb *State) DefineConstById(klass *RClass, name Symbol, val Value) {
klass.Set(name, val)
}

func (mrb *State) VmGetConst(sym Symbol) Value {
klass := mrb.ObjectClass
return klass.Get(sym)
Expand Down

0 comments on commit 8329f02

Please sign in to comment.