|
@@ -0,0 +1,214 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "log"
|
|
|
+
|
|
|
+ "github.com/aarzilli/golua/lua"
|
|
|
+ "github.com/fiatjaf/lunatico"
|
|
|
+ "github.com/stevedonovan/luar"
|
|
|
+)
|
|
|
+
|
|
|
+type Object struct {
|
|
|
+ meta *MetaData
|
|
|
+}
|
|
|
+
|
|
|
+func NewObject() *Object {
|
|
|
+ return &Object{
|
|
|
+ meta: NewMetaData(),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Safe() bool {
|
|
|
+ return o.meta.Safe()
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Unsafe() {
|
|
|
+ o.meta.Unsafe()
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Id() uint64 {
|
|
|
+ return o.Uint64(".id")
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Name() string {
|
|
|
+ return o.String("name")
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Description() string {
|
|
|
+ return o.String("description")
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Is(key string) bool {
|
|
|
+ return o.Group(key) != 0
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Group(key string) uint32 {
|
|
|
+ return o.Uint32(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Set(key string, val any) {
|
|
|
+ o.meta.Set(key, val)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Has(key string) bool {
|
|
|
+ return o.meta.Has(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) String(key string) string {
|
|
|
+ return o.meta.String(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Int32(key string) int32 {
|
|
|
+ return o.meta.Int32(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Int64(key string) int64 {
|
|
|
+ return o.meta.Int64(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Uint32(key string) uint32 {
|
|
|
+ return o.meta.Uint32(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Uint64(key string) uint64 {
|
|
|
+ return o.meta.Uint64(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Float32(key string) float32 {
|
|
|
+ return o.meta.Float32(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Float64(key string) float64 {
|
|
|
+ return o.meta.Float64(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Bool(key string) bool {
|
|
|
+ return o.meta.Bool(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) VecString(key string) []string {
|
|
|
+ return o.meta.VecString(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) VecInt32(key string) []int32 {
|
|
|
+ return o.meta.VecInt32(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) VecInt64(key string) []int64 {
|
|
|
+ return o.meta.VecInt64(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) VecUint32(key string) []uint32 {
|
|
|
+ return o.meta.VecUint32(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) VecUint64(key string) []uint64 {
|
|
|
+ return o.meta.VecUint64(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) VecFloat32(key string) []float32 {
|
|
|
+ return o.meta.VecFloat32(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) VecFloat64(key string) []float64 {
|
|
|
+ return o.meta.VecFloat64(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) VecBool(key string) []bool {
|
|
|
+ return o.meta.VecBool(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) MapString(key string) map[string]string {
|
|
|
+ return o.meta.MapString(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) MapInt32(key string) map[string]int32 {
|
|
|
+ return o.meta.MapInt32(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) MapInt64(key string) map[string]int64 {
|
|
|
+ return o.meta.MapInt64(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) MapUint32(key string) map[string]uint32 {
|
|
|
+ return o.meta.MapUint32(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) MapUint64(key string) map[string]uint64 {
|
|
|
+ return o.meta.MapUint64(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) MapFloat32(key string) map[string]float32 {
|
|
|
+ return o.meta.MapFloat32(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) MapFloat64(key string) map[string]float64 {
|
|
|
+ return o.meta.MapFloat64(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) MapBool(key string) map[string]bool {
|
|
|
+ return o.meta.MapBool(key)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *Object) Call(key string, param1, param2 *Object) (string, bool) {
|
|
|
+ if !o.Has(key) || o.String(key) == "" {
|
|
|
+ return fmt.Sprintf("missing '%s'", key), false
|
|
|
+ }
|
|
|
+ var (
|
|
|
+ L *lua.State = luar.Init()
|
|
|
+ ERRMSG string = ""
|
|
|
+ RESULT bool = false
|
|
|
+ old_safe bool = o.meta.safe
|
|
|
+ old_p1_safe bool
|
|
|
+ old_p1 bool = param1 != nil
|
|
|
+ old_p2_safe bool
|
|
|
+ old_p2 bool = param2 != nil
|
|
|
+ )
|
|
|
+ if !old_safe {
|
|
|
+ if old_p1 {
|
|
|
+ old_p1_safe = param1.meta.safe
|
|
|
+ }
|
|
|
+ if old_p2 {
|
|
|
+ old_p2_safe = param2.meta.safe
|
|
|
+ }
|
|
|
+ param1.meta.safe = false
|
|
|
+ param2.meta.safe = false
|
|
|
+ }
|
|
|
+ lunatico.SetGlobals(L, map[string]any{
|
|
|
+ "log": log.Printf,
|
|
|
+ "fail": func(msg string, args ...any) {
|
|
|
+ RESULT = false
|
|
|
+ ERRMSG = fmt.Sprintf(msg, args...)
|
|
|
+ },
|
|
|
+ "pass": func() {
|
|
|
+ RESULT = true
|
|
|
+ ERRMSG = ""
|
|
|
+ },
|
|
|
+
|
|
|
+ "call": key,
|
|
|
+ "safe": old_safe,
|
|
|
+
|
|
|
+ "self": o,
|
|
|
+ "param1": param1,
|
|
|
+ "param2": param2,
|
|
|
+ })
|
|
|
+ err := L.DoString(o.String(key))
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Sprintf("'%s': %v", key, err), false
|
|
|
+ }
|
|
|
+ output := lunatico.GetGlobals(L, "self", "param1", "param2")
|
|
|
+ o = output["self"].(*Object)
|
|
|
+ param1 = output["param1"].(*Object)
|
|
|
+ param2 = output["param2"].(*Object)
|
|
|
+ if old_p1 {
|
|
|
+ param1.meta.safe = old_p1_safe
|
|
|
+ }
|
|
|
+ if old_p2 {
|
|
|
+ param2.meta.safe = old_p2_safe
|
|
|
+ }
|
|
|
+ if o.meta.safe != old_safe {
|
|
|
+ o.meta.safe = old_safe
|
|
|
+ }
|
|
|
+ return ERRMSG, RESULT
|
|
|
+}
|