123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- package goja
- import (
- "github.com/dop251/goja/unistring"
- "reflect"
- )
- type destructKeyedSource struct {
- r *Runtime
- wrapped Value
- usedKeys map[Value]struct{}
- }
- func newDestructKeyedSource(r *Runtime, wrapped Value) *destructKeyedSource {
- return &destructKeyedSource{
- r: r,
- wrapped: wrapped,
- }
- }
- func (r *Runtime) newDestructKeyedSource(wrapped Value) *Object {
- return &Object{
- runtime: r,
- self: newDestructKeyedSource(r, wrapped),
- }
- }
- func (d *destructKeyedSource) w() objectImpl {
- return d.wrapped.ToObject(d.r).self
- }
- func (d *destructKeyedSource) recordKey(key Value) {
- if d.usedKeys == nil {
- d.usedKeys = make(map[Value]struct{})
- }
- d.usedKeys[key] = struct{}{}
- }
- func (d *destructKeyedSource) sortLen() int {
- return d.w().sortLen()
- }
- func (d *destructKeyedSource) sortGet(i int) Value {
- return d.w().sortGet(i)
- }
- func (d *destructKeyedSource) swap(i int, i2 int) {
- d.w().swap(i, i2)
- }
- func (d *destructKeyedSource) className() string {
- return d.w().className()
- }
- func (d *destructKeyedSource) typeOf() String {
- return d.w().typeOf()
- }
- func (d *destructKeyedSource) getStr(p unistring.String, receiver Value) Value {
- d.recordKey(stringValueFromRaw(p))
- return d.w().getStr(p, receiver)
- }
- func (d *destructKeyedSource) getIdx(p valueInt, receiver Value) Value {
- d.recordKey(p.toString())
- return d.w().getIdx(p, receiver)
- }
- func (d *destructKeyedSource) getSym(p *Symbol, receiver Value) Value {
- d.recordKey(p)
- return d.w().getSym(p, receiver)
- }
- func (d *destructKeyedSource) getOwnPropStr(u unistring.String) Value {
- d.recordKey(stringValueFromRaw(u))
- return d.w().getOwnPropStr(u)
- }
- func (d *destructKeyedSource) getOwnPropIdx(v valueInt) Value {
- d.recordKey(v.toString())
- return d.w().getOwnPropIdx(v)
- }
- func (d *destructKeyedSource) getOwnPropSym(symbol *Symbol) Value {
- d.recordKey(symbol)
- return d.w().getOwnPropSym(symbol)
- }
- func (d *destructKeyedSource) setOwnStr(p unistring.String, v Value, throw bool) bool {
- return d.w().setOwnStr(p, v, throw)
- }
- func (d *destructKeyedSource) setOwnIdx(p valueInt, v Value, throw bool) bool {
- return d.w().setOwnIdx(p, v, throw)
- }
- func (d *destructKeyedSource) setOwnSym(p *Symbol, v Value, throw bool) bool {
- return d.w().setOwnSym(p, v, throw)
- }
- func (d *destructKeyedSource) setForeignStr(p unistring.String, v, receiver Value, throw bool) (res bool, handled bool) {
- return d.w().setForeignStr(p, v, receiver, throw)
- }
- func (d *destructKeyedSource) setForeignIdx(p valueInt, v, receiver Value, throw bool) (res bool, handled bool) {
- return d.w().setForeignIdx(p, v, receiver, throw)
- }
- func (d *destructKeyedSource) setForeignSym(p *Symbol, v, receiver Value, throw bool) (res bool, handled bool) {
- return d.w().setForeignSym(p, v, receiver, throw)
- }
- func (d *destructKeyedSource) hasPropertyStr(u unistring.String) bool {
- return d.w().hasPropertyStr(u)
- }
- func (d *destructKeyedSource) hasPropertyIdx(idx valueInt) bool {
- return d.w().hasPropertyIdx(idx)
- }
- func (d *destructKeyedSource) hasPropertySym(s *Symbol) bool {
- return d.w().hasPropertySym(s)
- }
- func (d *destructKeyedSource) hasOwnPropertyStr(u unistring.String) bool {
- return d.w().hasOwnPropertyStr(u)
- }
- func (d *destructKeyedSource) hasOwnPropertyIdx(v valueInt) bool {
- return d.w().hasOwnPropertyIdx(v)
- }
- func (d *destructKeyedSource) hasOwnPropertySym(s *Symbol) bool {
- return d.w().hasOwnPropertySym(s)
- }
- func (d *destructKeyedSource) defineOwnPropertyStr(name unistring.String, desc PropertyDescriptor, throw bool) bool {
- return d.w().defineOwnPropertyStr(name, desc, throw)
- }
- func (d *destructKeyedSource) defineOwnPropertyIdx(name valueInt, desc PropertyDescriptor, throw bool) bool {
- return d.w().defineOwnPropertyIdx(name, desc, throw)
- }
- func (d *destructKeyedSource) defineOwnPropertySym(name *Symbol, desc PropertyDescriptor, throw bool) bool {
- return d.w().defineOwnPropertySym(name, desc, throw)
- }
- func (d *destructKeyedSource) deleteStr(name unistring.String, throw bool) bool {
- return d.w().deleteStr(name, throw)
- }
- func (d *destructKeyedSource) deleteIdx(idx valueInt, throw bool) bool {
- return d.w().deleteIdx(idx, throw)
- }
- func (d *destructKeyedSource) deleteSym(s *Symbol, throw bool) bool {
- return d.w().deleteSym(s, throw)
- }
- func (d *destructKeyedSource) assertCallable() (call func(FunctionCall) Value, ok bool) {
- return d.w().assertCallable()
- }
- func (d *destructKeyedSource) vmCall(vm *vm, n int) {
- d.w().vmCall(vm, n)
- }
- func (d *destructKeyedSource) assertConstructor() func(args []Value, newTarget *Object) *Object {
- return d.w().assertConstructor()
- }
- func (d *destructKeyedSource) proto() *Object {
- return d.w().proto()
- }
- func (d *destructKeyedSource) setProto(proto *Object, throw bool) bool {
- return d.w().setProto(proto, throw)
- }
- func (d *destructKeyedSource) hasInstance(v Value) bool {
- return d.w().hasInstance(v)
- }
- func (d *destructKeyedSource) isExtensible() bool {
- return d.w().isExtensible()
- }
- func (d *destructKeyedSource) preventExtensions(throw bool) bool {
- return d.w().preventExtensions(throw)
- }
- type destructKeyedSourceIter struct {
- d *destructKeyedSource
- wrapped iterNextFunc
- }
- func (i *destructKeyedSourceIter) next() (propIterItem, iterNextFunc) {
- for {
- item, next := i.wrapped()
- if next == nil {
- return item, nil
- }
- i.wrapped = next
- if _, exists := i.d.usedKeys[item.name]; !exists {
- return item, i.next
- }
- }
- }
- func (d *destructKeyedSource) iterateStringKeys() iterNextFunc {
- return (&destructKeyedSourceIter{
- d: d,
- wrapped: d.w().iterateStringKeys(),
- }).next
- }
- func (d *destructKeyedSource) iterateSymbols() iterNextFunc {
- return (&destructKeyedSourceIter{
- d: d,
- wrapped: d.w().iterateSymbols(),
- }).next
- }
- func (d *destructKeyedSource) iterateKeys() iterNextFunc {
- return (&destructKeyedSourceIter{
- d: d,
- wrapped: d.w().iterateKeys(),
- }).next
- }
- func (d *destructKeyedSource) export(ctx *objectExportCtx) interface{} {
- return d.w().export(ctx)
- }
- func (d *destructKeyedSource) exportType() reflect.Type {
- return d.w().exportType()
- }
- func (d *destructKeyedSource) exportToMap(dst reflect.Value, typ reflect.Type, ctx *objectExportCtx) error {
- return d.w().exportToMap(dst, typ, ctx)
- }
- func (d *destructKeyedSource) exportToArrayOrSlice(dst reflect.Value, typ reflect.Type, ctx *objectExportCtx) error {
- return d.w().exportToArrayOrSlice(dst, typ, ctx)
- }
- func (d *destructKeyedSource) equal(impl objectImpl) bool {
- return d.w().equal(impl)
- }
- func (d *destructKeyedSource) stringKeys(all bool, accum []Value) []Value {
- var next iterNextFunc
- if all {
- next = d.iterateStringKeys()
- } else {
- next = (&enumerableIter{
- o: d.wrapped.ToObject(d.r),
- wrapped: d.iterateStringKeys(),
- }).next
- }
- for item, next := next(); next != nil; item, next = next() {
- accum = append(accum, item.name)
- }
- return accum
- }
- func (d *destructKeyedSource) filterUsedKeys(keys []Value) []Value {
- k := 0
- for i, key := range keys {
- if _, exists := d.usedKeys[key]; exists {
- continue
- }
- if k != i {
- keys[k] = key
- }
- k++
- }
- return keys[:k]
- }
- func (d *destructKeyedSource) symbols(all bool, accum []Value) []Value {
- return d.filterUsedKeys(d.w().symbols(all, accum))
- }
- func (d *destructKeyedSource) keys(all bool, accum []Value) []Value {
- return d.filterUsedKeys(d.w().keys(all, accum))
- }
- func (d *destructKeyedSource) _putProp(name unistring.String, value Value, writable, enumerable, configurable bool) Value {
- return d.w()._putProp(name, value, writable, enumerable, configurable)
- }
- func (d *destructKeyedSource) _putSym(s *Symbol, prop Value) {
- d.w()._putSym(s, prop)
- }
- func (d *destructKeyedSource) getPrivateEnv(typ *privateEnvType, create bool) *privateElements {
- return d.w().getPrivateEnv(typ, create)
- }
|