feat: implemented the objects module.
This commit is contained in:
parent
7487e458f2
commit
c8df427cca
5 changed files with 53 additions and 9 deletions
|
@ -1,8 +1,22 @@
|
|||
objects := import("objects")
|
||||
fmt := import("fmt")
|
||||
|
||||
new := func(name) {
|
||||
ret := objects.new()
|
||||
ret.name = name
|
||||
|
||||
ret.def("bark", func(self, n){
|
||||
fmt.print(self.name, ": ")
|
||||
for i:=0 ; i<n ; i++ {
|
||||
fmt.print("BARK!")
|
||||
}
|
||||
fmt.println()
|
||||
})
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
|
||||
export {
|
||||
"new": func(name) {
|
||||
return {
|
||||
name: name
|
||||
}
|
||||
}
|
||||
"new": new
|
||||
}
|
||||
|
|
|
@ -2,5 +2,6 @@ fmt := import("fmt")
|
|||
dogs := import("./tests/dogs")
|
||||
|
||||
dog := dogs.new("check")
|
||||
fmt.println(dog)
|
||||
//fmt.println(dog)
|
||||
dog.bark(10)
|
||||
|
||||
|
|
|
@ -6,15 +6,17 @@ import "surdeus.su/core/xgo/xmodules/logx"
|
|||
import "surdeus.su/core/xgo/xmodules/paths"
|
||||
import "surdeus.su/core/xgo/xmodules/httpx"
|
||||
import "surdeus.su/core/xgo/xmodules/htmlx"
|
||||
import "surdeus.su/core/xgo/xmodules/objects"
|
||||
import "github.com/d5/tengo/v2"
|
||||
|
||||
// The main map to import all the implemented modules.
|
||||
var Modules = func() *tengo.ModuleMap {
|
||||
func GetModules() *tengo.ModuleMap {
|
||||
ret := stdlib.GetModuleMap(stdlib.AllModuleNames()...)
|
||||
ret.AddBuiltinModule("cjson", cjson.Module)
|
||||
ret.AddBuiltinModule("log", logx.Module)
|
||||
ret.AddBuiltinModule("paths", paths.Module)
|
||||
ret.AddBuiltinModule("http", httpx.Module)
|
||||
ret.AddBuiltinModule("html", htmlx.Module)
|
||||
ret.Add("objects", objects.GetModule())
|
||||
return ret
|
||||
}()
|
||||
}
|
||||
|
|
27
xmodules/objects/main.go
Normal file
27
xmodules/objects/main.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package objects
|
||||
|
||||
import "github.com/d5/tengo/v2"
|
||||
|
||||
const src = `
|
||||
|
||||
new := func() {
|
||||
self := {}
|
||||
self.def = func(name, method){
|
||||
self[name] = func(...args){
|
||||
return method(self, args...)
|
||||
}
|
||||
}
|
||||
return self
|
||||
}
|
||||
|
||||
export {
|
||||
new: new
|
||||
}
|
||||
`
|
||||
|
||||
func GetModule() tengo.Importable {
|
||||
return &tengo.SourceModule{
|
||||
Src: []byte(src),
|
||||
}
|
||||
}
|
||||
|
|
@ -52,7 +52,7 @@ func Run(flags *mtool.Flags) {
|
|||
return
|
||||
}
|
||||
|
||||
modules := xmodules.Modules
|
||||
modules := xmodules.GetModules()
|
||||
if len(iargs) == 0 {
|
||||
// REPL
|
||||
RunREPL(modules, os.Stdin, os.Stdout)
|
||||
|
|
Loading…
Reference in a new issue