diff --git a/class.go b/class.go index c2d3b5e..f4b282b 100644 --- a/class.go +++ b/class.go @@ -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 @@ -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)) } diff --git a/feature_test.go b/feature_test.go index 466d5c5..34219ab 100644 --- a/feature_test.go +++ b/feature_test.go @@ -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(), @@ -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) { diff --git a/features/object.feature b/features/object.feature new file mode 100644 index 0000000..f5e49a9 --- /dev/null +++ b/features/object.feature @@ -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 diff --git a/object.go b/object.go index cea0523..f59f24a 100644 --- a/object.go +++ b/object.go @@ -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) { diff --git a/value.go b/value.go new file mode 100644 index 0000000..7725bd3 --- /dev/null +++ b/value.go @@ -0,0 +1,5 @@ +package mruby + +func NewObjectValue(v any) Value { + return v.(RBasic) +} diff --git a/variable.go b/variable.go index 209cfe7..f167170 100644 --- a/variable.go +++ b/variable.go @@ -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)