feat: implemented the objects module.

This commit is contained in:
Andrey Parhomenko 2024-06-15 01:00:26 +05:00
parent 7487e458f2
commit c8df427cca
5 changed files with 53 additions and 9 deletions

View file

@ -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 { export {
"new": func(name) { "new": new
return {
name: name
}
}
} }

View file

@ -2,5 +2,6 @@ fmt := import("fmt")
dogs := import("./tests/dogs") dogs := import("./tests/dogs")
dog := dogs.new("check") dog := dogs.new("check")
fmt.println(dog) //fmt.println(dog)
dog.bark(10)

View file

@ -6,15 +6,17 @@ import "surdeus.su/core/xgo/xmodules/logx"
import "surdeus.su/core/xgo/xmodules/paths" import "surdeus.su/core/xgo/xmodules/paths"
import "surdeus.su/core/xgo/xmodules/httpx" import "surdeus.su/core/xgo/xmodules/httpx"
import "surdeus.su/core/xgo/xmodules/htmlx" import "surdeus.su/core/xgo/xmodules/htmlx"
import "surdeus.su/core/xgo/xmodules/objects"
import "github.com/d5/tengo/v2" import "github.com/d5/tengo/v2"
// The main map to import all the implemented modules. // The main map to import all the implemented modules.
var Modules = func() *tengo.ModuleMap { func GetModules() *tengo.ModuleMap {
ret := stdlib.GetModuleMap(stdlib.AllModuleNames()...) ret := stdlib.GetModuleMap(stdlib.AllModuleNames()...)
ret.AddBuiltinModule("cjson", cjson.Module) ret.AddBuiltinModule("cjson", cjson.Module)
ret.AddBuiltinModule("log", logx.Module) ret.AddBuiltinModule("log", logx.Module)
ret.AddBuiltinModule("paths", paths.Module) ret.AddBuiltinModule("paths", paths.Module)
ret.AddBuiltinModule("http", httpx.Module) ret.AddBuiltinModule("http", httpx.Module)
ret.AddBuiltinModule("html", htmlx.Module) ret.AddBuiltinModule("html", htmlx.Module)
ret.Add("objects", objects.GetModule())
return ret return ret
}() }

27
xmodules/objects/main.go Normal file
View 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),
}
}

View file

@ -52,7 +52,7 @@ func Run(flags *mtool.Flags) {
return return
} }
modules := xmodules.Modules modules := xmodules.GetModules()
if len(iargs) == 0 { if len(iargs) == 0 {
// REPL // REPL
RunREPL(modules, os.Stdin, os.Stdout) RunREPL(modules, os.Stdin, os.Stdout)