1
0
mirror of https://github.com/robertkrimen/otto synced 2025-09-28 18:45:22 +08:00
otto/type_string.go
2012-10-05 18:47:53 -07:00

85 lines
1.6 KiB
Go

package otto
import (
"strconv"
)
func (runtime *_runtime) newStringObject(value Value) *_object {
self := runtime.newPrimitiveObject("String", toValue(toString(value)))
self._propertyStash = newStringStash(toString(value), self._propertyStash)
return self
}
type _stringStash struct {
value string
_stash
}
func newStringStash(value string, stash _stash) *_stringStash {
self := &_stringStash{
value,
stash,
}
return self
}
func (self *_stringStash) CanRead(name string) bool {
// .length
if name == "length" {
return true
}
// .0, .1, .2, ...
index := stringToArrayIndex(name)
if index >= 0 && index < int64(len(self.value)) {
return true
}
return self._stash.CanRead(name)
}
func (self *_stringStash) Read(name string) Value {
// .length
if name == "length" {
return toValue(len(string(self.value)))
}
// .0, .1, .2, ...
index := stringToArrayIndex(name)
if index >= 0 && index < int64(len(self.value)) {
return toValue(string(self.value[index]))
}
return self._stash.Read(name)
}
func (self *_stringStash) property(name string) *_property {
// .length
if name == "length" {
return &_property{
toValue(len(string(self.value))),
0, // -Write -Enumerate -Configure
}
}
// .0, .1, .2, ...
index := stringToArrayIndex(name)
if index >= 0 && index < int64(len(self.value)) {
return &_property{
toValue(string(self.value[index])),
0, // -Write -Enumerate -Configure
}
}
return self._stash.property(name)
}
func (self *_stringStash) Enumerate(each func(string)) {
// .0, .1, .2, ...
for index, _ := range self.value {
name := strconv.FormatInt(int64(index), 10)
each(name)
}
self._stash.Enumerate(each)
}