go.ketch.com/lib/goja@v0.0.1/destruct.go (about)

     1  package goja
     2  
     3  import (
     4  	"go.ketch.com/lib/goja/unistring"
     5  	"reflect"
     6  )
     7  
     8  type destructKeyedSource struct {
     9  	r        *Runtime
    10  	wrapped  Value
    11  	usedKeys map[Value]struct{}
    12  }
    13  
    14  func newDestructKeyedSource(r *Runtime, wrapped Value) *destructKeyedSource {
    15  	return &destructKeyedSource{
    16  		r:       r,
    17  		wrapped: wrapped,
    18  	}
    19  }
    20  
    21  func (r *Runtime) newDestructKeyedSource(wrapped Value) *Object {
    22  	return &Object{
    23  		runtime: r,
    24  		self:    newDestructKeyedSource(r, wrapped),
    25  	}
    26  }
    27  
    28  func (d *destructKeyedSource) w() objectImpl {
    29  	return d.wrapped.ToObject(d.r).self
    30  }
    31  
    32  func (d *destructKeyedSource) recordKey(key Value) {
    33  	if d.usedKeys == nil {
    34  		d.usedKeys = make(map[Value]struct{})
    35  	}
    36  	d.usedKeys[key] = struct{}{}
    37  }
    38  
    39  func (d *destructKeyedSource) sortLen() int {
    40  	return d.w().sortLen()
    41  }
    42  
    43  func (d *destructKeyedSource) sortGet(i int) Value {
    44  	return d.w().sortGet(i)
    45  }
    46  
    47  func (d *destructKeyedSource) swap(i int, i2 int) {
    48  	d.w().swap(i, i2)
    49  }
    50  
    51  func (d *destructKeyedSource) className() string {
    52  	return d.w().className()
    53  }
    54  
    55  func (d *destructKeyedSource) getStr(p unistring.String, receiver Value) Value {
    56  	d.recordKey(stringValueFromRaw(p))
    57  	return d.w().getStr(p, receiver)
    58  }
    59  
    60  func (d *destructKeyedSource) getIdx(p valueInt, receiver Value) Value {
    61  	d.recordKey(p.toString())
    62  	return d.w().getIdx(p, receiver)
    63  }
    64  
    65  func (d *destructKeyedSource) getSym(p *Symbol, receiver Value) Value {
    66  	d.recordKey(p)
    67  	return d.w().getSym(p, receiver)
    68  }
    69  
    70  func (d *destructKeyedSource) getOwnPropStr(u unistring.String) Value {
    71  	d.recordKey(stringValueFromRaw(u))
    72  	return d.w().getOwnPropStr(u)
    73  }
    74  
    75  func (d *destructKeyedSource) getOwnPropIdx(v valueInt) Value {
    76  	d.recordKey(v.toString())
    77  	return d.w().getOwnPropIdx(v)
    78  }
    79  
    80  func (d *destructKeyedSource) getOwnPropSym(symbol *Symbol) Value {
    81  	d.recordKey(symbol)
    82  	return d.w().getOwnPropSym(symbol)
    83  }
    84  
    85  func (d *destructKeyedSource) setOwnStr(p unistring.String, v Value, throw bool) bool {
    86  	return d.w().setOwnStr(p, v, throw)
    87  }
    88  
    89  func (d *destructKeyedSource) setOwnIdx(p valueInt, v Value, throw bool) bool {
    90  	return d.w().setOwnIdx(p, v, throw)
    91  }
    92  
    93  func (d *destructKeyedSource) setOwnSym(p *Symbol, v Value, throw bool) bool {
    94  	return d.w().setOwnSym(p, v, throw)
    95  }
    96  
    97  func (d *destructKeyedSource) setForeignStr(p unistring.String, v, receiver Value, throw bool) (res bool, handled bool) {
    98  	return d.w().setForeignStr(p, v, receiver, throw)
    99  }
   100  
   101  func (d *destructKeyedSource) setForeignIdx(p valueInt, v, receiver Value, throw bool) (res bool, handled bool) {
   102  	return d.w().setForeignIdx(p, v, receiver, throw)
   103  }
   104  
   105  func (d *destructKeyedSource) setForeignSym(p *Symbol, v, receiver Value, throw bool) (res bool, handled bool) {
   106  	return d.w().setForeignSym(p, v, receiver, throw)
   107  }
   108  
   109  func (d *destructKeyedSource) hasPropertyStr(u unistring.String) bool {
   110  	return d.w().hasPropertyStr(u)
   111  }
   112  
   113  func (d *destructKeyedSource) hasPropertyIdx(idx valueInt) bool {
   114  	return d.w().hasPropertyIdx(idx)
   115  }
   116  
   117  func (d *destructKeyedSource) hasPropertySym(s *Symbol) bool {
   118  	return d.w().hasPropertySym(s)
   119  }
   120  
   121  func (d *destructKeyedSource) hasOwnPropertyStr(u unistring.String) bool {
   122  	return d.w().hasOwnPropertyStr(u)
   123  }
   124  
   125  func (d *destructKeyedSource) hasOwnPropertyIdx(v valueInt) bool {
   126  	return d.w().hasOwnPropertyIdx(v)
   127  }
   128  
   129  func (d *destructKeyedSource) hasOwnPropertySym(s *Symbol) bool {
   130  	return d.w().hasOwnPropertySym(s)
   131  }
   132  
   133  func (d *destructKeyedSource) defineOwnPropertyStr(name unistring.String, desc PropertyDescriptor, throw bool) bool {
   134  	return d.w().defineOwnPropertyStr(name, desc, throw)
   135  }
   136  
   137  func (d *destructKeyedSource) defineOwnPropertyIdx(name valueInt, desc PropertyDescriptor, throw bool) bool {
   138  	return d.w().defineOwnPropertyIdx(name, desc, throw)
   139  }
   140  
   141  func (d *destructKeyedSource) defineOwnPropertySym(name *Symbol, desc PropertyDescriptor, throw bool) bool {
   142  	return d.w().defineOwnPropertySym(name, desc, throw)
   143  }
   144  
   145  func (d *destructKeyedSource) deleteStr(name unistring.String, throw bool) bool {
   146  	return d.w().deleteStr(name, throw)
   147  }
   148  
   149  func (d *destructKeyedSource) deleteIdx(idx valueInt, throw bool) bool {
   150  	return d.w().deleteIdx(idx, throw)
   151  }
   152  
   153  func (d *destructKeyedSource) deleteSym(s *Symbol, throw bool) bool {
   154  	return d.w().deleteSym(s, throw)
   155  }
   156  
   157  func (d *destructKeyedSource) toPrimitiveNumber() Value {
   158  	return d.w().toPrimitiveNumber()
   159  }
   160  
   161  func (d *destructKeyedSource) toPrimitiveString() Value {
   162  	return d.w().toPrimitiveString()
   163  }
   164  
   165  func (d *destructKeyedSource) toPrimitive() Value {
   166  	return d.w().toPrimitive()
   167  }
   168  
   169  func (d *destructKeyedSource) assertCallable() (call func(FunctionCall) Value, ok bool) {
   170  	return d.w().assertCallable()
   171  }
   172  
   173  func (d *destructKeyedSource) assertConstructor() func(args []Value, newTarget *Object) *Object {
   174  	return d.w().assertConstructor()
   175  }
   176  
   177  func (d *destructKeyedSource) proto() *Object {
   178  	return d.w().proto()
   179  }
   180  
   181  func (d *destructKeyedSource) setProto(proto *Object, throw bool) bool {
   182  	return d.w().setProto(proto, throw)
   183  }
   184  
   185  func (d *destructKeyedSource) hasInstance(v Value) bool {
   186  	return d.w().hasInstance(v)
   187  }
   188  
   189  func (d *destructKeyedSource) isExtensible() bool {
   190  	return d.w().isExtensible()
   191  }
   192  
   193  func (d *destructKeyedSource) preventExtensions(throw bool) bool {
   194  	return d.w().preventExtensions(throw)
   195  }
   196  
   197  type destructKeyedSourceIter struct {
   198  	d       *destructKeyedSource
   199  	wrapped iterNextFunc
   200  }
   201  
   202  func (i *destructKeyedSourceIter) next() (propIterItem, iterNextFunc) {
   203  	for {
   204  		item, next := i.wrapped()
   205  		if next == nil {
   206  			return item, nil
   207  		}
   208  		i.wrapped = next
   209  		if _, exists := i.d.usedKeys[item.name]; !exists {
   210  			return item, i.next
   211  		}
   212  	}
   213  }
   214  
   215  func (d *destructKeyedSource) iterateStringKeys() iterNextFunc {
   216  	return (&destructKeyedSourceIter{
   217  		d:       d,
   218  		wrapped: d.w().iterateStringKeys(),
   219  	}).next
   220  }
   221  
   222  func (d *destructKeyedSource) iterateSymbols() iterNextFunc {
   223  	return (&destructKeyedSourceIter{
   224  		d:       d,
   225  		wrapped: d.w().iterateSymbols(),
   226  	}).next
   227  }
   228  
   229  func (d *destructKeyedSource) iterateKeys() iterNextFunc {
   230  	return (&destructKeyedSourceIter{
   231  		d:       d,
   232  		wrapped: d.w().iterateKeys(),
   233  	}).next
   234  }
   235  
   236  func (d *destructKeyedSource) export(ctx *objectExportCtx) interface{} {
   237  	return d.w().export(ctx)
   238  }
   239  
   240  func (d *destructKeyedSource) exportType() reflect.Type {
   241  	return d.w().exportType()
   242  }
   243  
   244  func (d *destructKeyedSource) exportToMap(dst reflect.Value, typ reflect.Type, ctx *objectExportCtx) error {
   245  	return d.w().exportToMap(dst, typ, ctx)
   246  }
   247  
   248  func (d *destructKeyedSource) exportToArrayOrSlice(dst reflect.Value, typ reflect.Type, ctx *objectExportCtx) error {
   249  	return d.w().exportToArrayOrSlice(dst, typ, ctx)
   250  }
   251  
   252  func (d *destructKeyedSource) equal(impl objectImpl) bool {
   253  	return d.w().equal(impl)
   254  }
   255  
   256  func (d *destructKeyedSource) stringKeys(all bool, accum []Value) []Value {
   257  	var next iterNextFunc
   258  	if all {
   259  		next = d.iterateStringKeys()
   260  	} else {
   261  		next = (&enumerableIter{
   262  			o:       d.wrapped.ToObject(d.r),
   263  			wrapped: d.iterateStringKeys(),
   264  		}).next
   265  	}
   266  	for item, next := next(); next != nil; item, next = next() {
   267  		accum = append(accum, item.name)
   268  	}
   269  	return accum
   270  }
   271  
   272  func (d *destructKeyedSource) filterUsedKeys(keys []Value) []Value {
   273  	k := 0
   274  	for i, key := range keys {
   275  		if _, exists := d.usedKeys[key]; exists {
   276  			continue
   277  		}
   278  		if k != i {
   279  			keys[k] = key
   280  		}
   281  		k++
   282  	}
   283  	return keys[:k]
   284  }
   285  
   286  func (d *destructKeyedSource) symbols(all bool, accum []Value) []Value {
   287  	return d.filterUsedKeys(d.w().symbols(all, accum))
   288  }
   289  
   290  func (d *destructKeyedSource) keys(all bool, accum []Value) []Value {
   291  	return d.filterUsedKeys(d.w().keys(all, accum))
   292  }
   293  
   294  func (d *destructKeyedSource) _putProp(name unistring.String, value Value, writable, enumerable, configurable bool) Value {
   295  	return d.w()._putProp(name, value, writable, enumerable, configurable)
   296  }
   297  
   298  func (d *destructKeyedSource) _putSym(s *Symbol, prop Value) {
   299  	d.w()._putSym(s, prop)
   300  }
   301  
   302  func (d *destructKeyedSource) getPrivateEnv(typ *privateEnvType, create bool) *privateElements {
   303  	return d.w().getPrivateEnv(typ, create)
   304  }