mirror of
https://github.com/robertkrimen/otto
synced 2025-10-12 20:27:30 +08:00

* No more _stash * Now using a "virtual table" system via _objectClass * Make Array.concat GoArray compatible (via .isArray()) Fix #16
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package otto
|
|
|
|
import (
|
|
"strconv"
|
|
"unicode/utf16"
|
|
)
|
|
|
|
type _stringObject struct {
|
|
value Value
|
|
value16 []uint16
|
|
}
|
|
|
|
func (runtime *_runtime) newStringObject(value Value) *_object {
|
|
value = toValue(toString(value))
|
|
value16 := utf16Of(value.value.(string))
|
|
|
|
self := runtime.newClassObject("String")
|
|
self.defineProperty("length", toValue(len(value16)), 0, false)
|
|
self.objectClass = _classString
|
|
self.value = &_stringObject{
|
|
value: value,
|
|
value16: value16,
|
|
}
|
|
return self
|
|
}
|
|
|
|
func (self *_object) stringValue() (string, *_stringObject) {
|
|
value, valid := self.value.(*_stringObject)
|
|
if valid {
|
|
return value.value.value.(string), value
|
|
}
|
|
return "", nil
|
|
}
|
|
|
|
func (self *_object) stringValue16() []uint16 {
|
|
_, value := self.stringValue()
|
|
if value != nil {
|
|
return value.value16
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func utf16Of(value string) []uint16 {
|
|
return utf16.Encode([]rune(value))
|
|
}
|
|
|
|
func stringEnumerate(self *_object, each func(string)) {
|
|
length := len(self.stringValue16())
|
|
for index := 0; index < length; index += 1 {
|
|
each(strconv.FormatInt(int64(index), 10))
|
|
}
|
|
objectEnumerate(self, each)
|
|
}
|
|
|
|
func stringGetOwnProperty(self *_object, name string) *_property {
|
|
if property := objectGetOwnProperty(self, name); property != nil {
|
|
return property
|
|
}
|
|
index := stringToArrayIndex(name)
|
|
if index >= 0 {
|
|
value16 := self.stringValue16()
|
|
if int(index) < len(value16) {
|
|
return &_property{toValue(string(value16[index])), 0}
|
|
}
|
|
}
|
|
return nil
|
|
}
|