github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/value_noscalar.go (about) 1 // +build noscalar 2 // 3 // This implementation of Value disables special casing of ints, floats and 4 // bools. It causes more memory allocations. 5 6 package runtime 7 8 // A Value is a runtime value. 9 type Value struct { 10 iface interface{} 11 } 12 13 // AsValue returns a Value for the passed interface. 14 func AsValue(i interface{}) Value { 15 return Value{iface: i} 16 } 17 18 // Interface turns the Value into an interface. 19 func (v Value) Interface() interface{} { 20 return v.iface 21 } 22 23 // IntValue returns a Value holding the given arg. 24 func IntValue(n int64) Value { 25 return Value{iface: n} 26 } 27 28 // FloatValue returns a Value holding the given arg. 29 func FloatValue(f float64) Value { 30 return Value{iface: f} 31 } 32 33 // BoolValue returns a Value holding the given arg. 34 func BoolValue(b bool) Value { 35 return Value{iface: b} 36 } 37 38 // StringValue returns a Value holding the given arg. 39 func StringValue(s string) Value { 40 return Value{iface: s} 41 } 42 43 // TableValue returns a Value holding the given arg. 44 func TableValue(t *Table) Value { 45 return Value{iface: t} 46 } 47 48 // FunctionValue returns a Value holding the given arg. 49 func FunctionValue(c Callable) Value { 50 return Value{iface: c} 51 } 52 53 // ContValue returns a Value holding the given arg. 54 func ContValue(c Cont) Value { 55 return Value{iface: c} 56 } 57 58 // ArrayValue returns a Value holding the given arg. 59 func ArrayValue(a []Value) Value { 60 return Value{iface: a} 61 } 62 63 // CodeValue returns a Value holding the given arg. 64 func CodeValue(c *Code) Value { 65 return Value{iface: c} 66 } 67 68 // ThreadValue returns a Value holding the given arg. 69 func ThreadValue(t *Thread) Value { 70 return Value{iface: t} 71 } 72 73 // LightUserDataValue returns a Value holding the given arg. 74 func LightUserDataValue(d LightUserData) Value { 75 return Value{iface: d} 76 } 77 78 // UserDataValue returns a Value holding the given arg. 79 func UserDataValue(u *UserData) Value { 80 return Value{iface: u} 81 } 82 83 // NilValue is a value holding Nil. 84 var NilValue = Value{} 85 86 // Type returns the ValueType of v. 87 func (v Value) Type() ValueType { 88 if v.iface == nil { 89 return NilType 90 } 91 switch v.iface.(type) { 92 case int64: 93 return IntType 94 case float64: 95 return FloatType 96 case bool: 97 return BoolType 98 case string: 99 return StringType 100 case *Table: 101 return TableType 102 case *Code: 103 return CodeType 104 case Callable: 105 return FunctionType 106 case *Thread: 107 return ThreadType 108 case *UserData: 109 return UserDataType 110 default: 111 return UnknownType 112 } 113 } 114 115 // NumberType return the ValueType of v if it is a number, otherwise 116 // UnknownType. 117 func (v Value) NumberType() ValueType { 118 switch v.iface.(type) { 119 case int64: 120 return IntType 121 case float64: 122 return FloatType 123 } 124 return UnknownType 125 } 126 127 // AsInt returns v as a int64 (or panics). 128 func (v Value) AsInt() int64 { 129 return v.iface.(int64) 130 } 131 132 // AsFloat returns v as a float64 (or panics). 133 func (v Value) AsFloat() float64 { 134 return v.iface.(float64) 135 } 136 137 // AsBool returns v as a bool (or panics). 138 func (v Value) AsBool() bool { 139 return v.iface.(bool) 140 } 141 142 // AsString returns v as a string (or panics). 143 func (v Value) AsString() string { 144 return v.iface.(string) 145 } 146 147 // AsTable returns v as a *Table (or panics). 148 func (v Value) AsTable() *Table { 149 return v.iface.(*Table) 150 } 151 152 // AsCont returns v as a Cont (or panics). 153 func (v Value) AsCont() Cont { 154 return v.iface.(Cont) 155 } 156 157 // AsArray returns v as a [] (or panics). 158 func (v Value) AsArray() []Value { 159 return v.iface.([]Value) 160 } 161 162 // AsClosure returns v as a *Closure (or panics). 163 func (v Value) AsClosure() *Closure { 164 return v.iface.(*Closure) 165 } 166 167 // AsCode returns v as a *Code (or panics). 168 func (v Value) AsCode() *Code { 169 return v.iface.(*Code) 170 } 171 172 // AsUserData returns v as a *UserData (or panics). 173 func (v Value) AsUserData() *UserData { 174 return v.iface.(*UserData) 175 } 176 177 // AsFunction returns v as a Callable (or panics). 178 func (v Value) AsFunction() Callable { 179 return v.iface.(Callable) 180 } 181 182 // TryInt converts v to type int64 if possible (ok is false otherwise). 183 func (v Value) TryInt() (n int64, ok bool) { 184 n, ok = v.iface.(int64) 185 return 186 } 187 188 // TryFloat converts v to type float64 if possible (ok is false otherwise). 189 func (v Value) TryFloat() (n float64, ok bool) { 190 n, ok = v.iface.(float64) 191 return 192 } 193 194 // TryString converts v to type string if possible (ok is false otherwise). 195 func (v Value) TryString() (s string, ok bool) { 196 s, ok = v.iface.(string) 197 return 198 } 199 200 // TryCallable converts v to type Callable if possible (ok is false otherwise). 201 func (v Value) TryCallable() (c Callable, ok bool) { 202 c, ok = v.iface.(Callable) 203 return 204 } 205 206 // TryClosure converts v to type *Closure if possible (ok is false otherwise). 207 func (v Value) TryClosure() (c *Closure, ok bool) { 208 c, ok = v.iface.(*Closure) 209 return 210 } 211 212 // TryThread converts v to type *Thread if possible (ok is false otherwise). 213 func (v Value) TryThread() (t *Thread, ok bool) { 214 t, ok = v.iface.(*Thread) 215 return 216 } 217 218 // TryTable converts v to type *Table if possible (ok is false otherwise). 219 func (v Value) TryTable() (t *Table, ok bool) { 220 t, ok = v.iface.(*Table) 221 return 222 } 223 224 // TryUserData converts v to type *UserData if possible (ok is false otherwise). 225 func (v Value) TryUserData() (u *UserData, ok bool) { 226 u, ok = v.iface.(*UserData) 227 return 228 } 229 230 // TryBool converts v to type bool if possible (ok is false otherwise). 231 func (v Value) TryBool() (b bool, ok bool) { 232 b, ok = v.iface.(bool) 233 return 234 } 235 236 // TryCont converts v to type Cont if possible (ok is false otherwise). 237 func (v Value) TryCont() (c Cont, ok bool) { 238 c, ok = v.iface.(Cont) 239 return 240 } 241 242 // TryCode converts v to type *Code if possible (ok is false otherwise). 243 func (v Value) TryCode() (c *Code, ok bool) { 244 c, ok = v.iface.(*Code) 245 return 246 } 247 248 // IsNil returns true if v is nil. 249 func (v Value) IsNil() bool { 250 return v.iface == nil 251 }