-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.go
52 lines (41 loc) · 1.13 KB
/
kernel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package mruby
import "fmt"
func (mrb *State) Inspect(obj Value) string {
ret, ok := objectInspect(mrb, obj).(string)
if !ok {
return ""
}
return ret
}
func objectInspect(mrb *State, self Value) Value {
switch v := self.(type) {
case RClass:
name := mrb.ObjectInstanceVariableGet(v, _classname(mrb))
return name
case string:
return fmt.Sprintf("%q", v)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprintf("%d", v)
default:
return anyToString(mrb, self)
}
}
func objectPuts(mrb *State, recv Value) Value {
args := mrb.GetArgv()
fmt.Println(args...)
return args[0]
}
func funcRaise(mrb *State, recv Value) Value {
args := mrb.GetArgv()
mrb.Raise(nil, fmt.Sprint(args...))
return nil
}
func initKernel(mrb *State) (err error) {
mrb.KernelModule = mrb.DefineModuleId(_Kernel(mrb))
mrb.DefineMethodId(mrb.KernelModule, _inspect(mrb), objectInspect)
mrb.DefineMethodId(mrb.ObjectClass, _raise(mrb), funcRaise)
// defined in mruby-io
putsSym := mrb.Intern("puts")
mrb.DefineMethodId(mrb.ObjectClass, putsSym, objectPuts)
return mrb.IncludeModule(mrb.ObjectClass, mrb.KernelModule)
}