github.com/primecitizens/pcz/std@v0.2.1/ffi/js/object.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 package js 5 6 import ( 7 "github.com/primecitizens/pcz/std/ffi/js/bindings" 8 ) 9 10 func New(constructor Func[Any], args ...Ref) Object { 11 return Object{ 12 ref: bindings.New( 13 constructor.ref, 14 SliceData(args), 15 SizeU(len(args)), 16 ), 17 } 18 } 19 20 // TODO 21 type Record[T any] struct { 22 ref bindings.Ref 23 } 24 25 func (r Record[T]) FromRef(ref Ref) Record[T] { 26 return Record[T]{ 27 ref: bindings.Ref(ref), 28 } 29 } 30 31 func (r Record[T]) Ref() Ref { 32 return Ref(r.ref) 33 } 34 35 func (r Record[T]) Once() Record[T] { 36 bindings.Once(r.ref) 37 return r 38 } 39 40 func (r Record[T]) Free() { 41 bindings.Free(r.ref) 42 } 43 44 // An Object is a reference to a js object. 45 type Object struct { 46 ref bindings.Ref 47 } 48 49 func (o Object) FromRef(ref Ref) Object { 50 return Object{ 51 ref: bindings.Ref(ref), 52 } 53 } 54 55 func (o Object) Ref() Ref { 56 return Ref(o.ref) 57 } 58 59 func (o Object) Once() Object { 60 bindings.Once(o.ref) 61 return o 62 } 63 64 func (o Object) Free() { 65 bindings.Free(o.ref) 66 } 67 68 func (o Object) Prototype() (proto Object, hasPrototype bool) { 69 r := bindings.GetPrototype(o.ref) 70 if r == bindings.Ref(Null) { 71 return 72 } 73 74 proto.ref = r 75 hasPrototype = true 76 return 77 } 78 79 // SetPrototype sets o's prototype to proto 80 // 81 // When free is ture, proto is freed after the call. 82 func (o Object) SetPrototype(free bool, proto Ref) bool { 83 return bindings.Ref(True) == bindings.SetPrototype( 84 o.ref, 85 bindings.Ref(Bool(free)), 86 bindings.Ref(proto), 87 ) 88 } 89 90 type PropDesc uint32 91 92 const ( 93 PropDesc_exists PropDesc = 1 << iota 94 PropDesc_writable // 95 PropDesc_configurable // 96 PropDesc_enumerable // 97 PropDesc_getter // 98 PropDesc_setter // 99 ) 100 101 func (o Object) PropDesc(name string) PropDesc { 102 return PropDesc(bindings.GetPropDesc( 103 o.ref, 104 StringData(name), 105 SizeU(len(name)), 106 )) 107 } 108 109 func (o Object) DefineProp( 110 name string, 111 flags PropDesc, 112 getter Func[func() Any], 113 setter Func[func(Any) bool], 114 ) bool { 115 return bindings.Ref(True) == bindings.DefineProp( 116 o.ref, 117 StringData(name), 118 SizeU(len(name)), 119 uint32(flags), 120 getter.ref, 121 setter.ref, 122 ) 123 } 124 125 func (o Object) DeleteProp(name string) bool { 126 return bindings.Ref(True) == bindings.DeleteProp( 127 o.ref, 128 StringData(name), 129 SizeU(len(name)), 130 ) 131 } 132 133 func (o Object) Prop(name string) Any { 134 return Any{ 135 ref: bindings.Prop( 136 o.ref, 137 StringData(name), 138 SizeU(len(name)), 139 ), 140 } 141 } 142 143 func (o Object) StringProp(name string) String { 144 return o.Prop(name).AsString() 145 } 146 147 func (o Object) NumProp(name string) float64 { 148 return bindings.NumProp( 149 o.ref, 150 StringData(name), 151 SizeU(len(name)), 152 ) 153 } 154 155 func (o Object) BoolProp(name string) bool { 156 return bindings.Ref(True) == bindings.BoolProp( 157 o.ref, 158 StringData(name), 159 SizeU(len(name)), 160 ) 161 } 162 163 func (o Object) PropByString(name String) Any { 164 return Any{ 165 ref: bindings.PropByString(o.ref, name.ref), 166 } 167 } 168 169 func (o Object) StringPropByString(name String) String { 170 return o.PropByString(name).AsString() 171 } 172 173 func (o Object) NumPropByString(name String) float64 { 174 return bindings.NumPropByString(o.ref, name.ref) 175 } 176 177 func (o Object) BoolPropByString(name String) bool { 178 return bindings.Ref(True) == bindings.BoolPropByString(o.ref, name.ref) 179 } 180 181 func (o Object) SetProp(name string, free bool, val Ref) bool { 182 return bindings.Ref(True) == bindings.SetProp( 183 o.ref, 184 StringData(name), 185 SizeU(len(name)), 186 bindings.Ref(Bool(free)), 187 bindings.Ref(val), 188 ) 189 } 190 191 func (o Object) SetNumProp(name string, val float64) bool { 192 return bindings.Ref(True) == bindings.SetNumProp( 193 o.ref, 194 StringData(name), 195 SizeU(len(name)), 196 val, 197 ) 198 } 199 200 func (o Object) SetBoolProp(name string, val bool) bool { 201 return bindings.Ref(True) == bindings.SetBoolProp( 202 o.ref, 203 StringData(name), 204 SizeU(len(name)), 205 bindings.Ref(Bool(val)), 206 ) 207 } 208 209 func (o Object) SetStringProp(name, val string) bool { 210 return bindings.Ref(True) == bindings.SetStringProp( 211 o.ref, 212 StringData(name), 213 SizeU(len(name)), 214 StringData(val), 215 SizeU(len(val)), 216 ) 217 } 218 219 func (o Object) SetPropByString(prop String, free bool, val Ref) bool { 220 return bindings.Ref(True) == bindings.SetPropByString( 221 o.ref, 222 prop.ref, 223 bindings.Ref(Bool(free)), 224 bindings.Ref(val), 225 ) 226 } 227 228 func (o Object) SetNumPropByString(prop String, val float64) bool { 229 return bindings.Ref(True) == bindings.SetNumPropByString( 230 o.ref, prop.ref, val, 231 ) 232 } 233 234 func (o Object) SetBoolPropByString(s String, val bool) bool { 235 return bindings.Ref(True) == bindings.SetBoolPropByString( 236 o.ref, 237 s.ref, 238 bindings.Ref(Bool(val)), 239 ) 240 } 241 242 func (o Object) SetStringPropByString(name String, free bool, val String) bool { 243 return bindings.Ref(True) == bindings.SetStringPropByString( 244 o.ref, 245 name.ref, 246 bindings.Ref(Bool(free)), 247 val.ref, 248 ) 249 }