package goja

import (
	
	
	

	
)

/*
DynamicObject is an interface representing a handler for a dynamic Object. Such an object can be created
using the Runtime.NewDynamicObject() method.

Note that Runtime.ToValue() does not have any special treatment for DynamicObject. The only way to create
a dynamic object is by using the Runtime.NewDynamicObject() method. This is done deliberately to avoid
silent code breaks when this interface changes.
*/
type DynamicObject interface {
	// Get a property value for the key. May return nil if the property does not exist.
	Get(key string) Value
	// Set a property value for the key. Return true if success, false otherwise.
	Set(key string, val Value) bool
	// Has should return true if and only if the property exists.
	Has(key string) bool
	// Delete the property for the key. Returns true on success (note, that includes missing property).
	Delete(key string) bool
	// Keys returns a list of all existing property keys. There are no checks for duplicates or to make sure
	// that the order conforms to https://262.ecma-international.org/#sec-ordinaryownpropertykeys
	Keys() []string
}

/*
DynamicArray is an interface representing a handler for a dynamic array Object. Such an object can be created
using the Runtime.NewDynamicArray() method.

Any integer property key or a string property key that can be parsed into an int value (including negative
ones) is treated as an index and passed to the trap methods of the DynamicArray. Note this is different from
the regular ECMAScript arrays which only support positive indexes up to 2^32-1.

DynamicArray cannot be sparse, i.e. hasOwnProperty(num) will return true for num >= 0 && num < Len(). Deleting
such a property is equivalent to setting it to undefined. Note that this creates a slight peculiarity because
hasOwnProperty() will still return true, even after deletion.

Note that Runtime.ToValue() does not have any special treatment for DynamicArray. The only way to create
a dynamic array is by using the Runtime.NewDynamicArray() method. This is done deliberately to avoid
silent code breaks when this interface changes.
*/
type DynamicArray interface {
	// Len returns the current array length.
	Len() int
	// Get an item at index idx. Note that idx may be any integer, negative or beyond the current length.
	Get(idx int) Value
	// Set an item at index idx. Note that idx may be any integer, negative or beyond the current length.
	// The expected behaviour when it's beyond length is that the array's length is increased to accommodate
	// the item. All elements in the 'new' section of the array should be zeroed.
	Set(idx int, val Value) bool
	// SetLen is called when the array's 'length' property is changed. If the length is increased all elements in the
	// 'new' section of the array should be zeroed.
	SetLen(int) bool
}

type baseDynamicObject struct {
	val       *Object
	prototype *Object
}

type dynamicObject struct {
	baseDynamicObject
	d DynamicObject
}

type dynamicArray struct {
	baseDynamicObject
	a DynamicArray
}

/*
NewDynamicObject creates an Object backed by the provided DynamicObject handler.

All properties of this Object are Writable, Enumerable and Configurable data properties. Any attempt to define
a property that does not conform to this will fail.

The Object is always extensible and cannot be made non-extensible. Object.preventExtensions() will fail.

The Object's prototype is initially set to Object.prototype, but can be changed using regular mechanisms
(Object.SetPrototype() in Go or Object.setPrototypeOf() in JS).

The Object cannot have own Symbol properties, however its prototype can. If you need an iterator support for
example, you could create a regular object, set Symbol.iterator on that object and then use it as a
prototype. See TestDynamicObjectCustomProto for more details.

Export() returns the original DynamicObject.

This mechanism is similar to ECMAScript Proxy, however because all properties are enumerable and the object
is always extensible there is no need for invariant checks which removes the need to have a target object and
makes it a lot more efficient.
*/
func ( *Runtime) ( DynamicObject) *Object {
	 := &Object{runtime: }
	 := &dynamicObject{
		d: ,
		baseDynamicObject: baseDynamicObject{
			val:       ,
			prototype: .global.ObjectPrototype,
		},
	}
	.self = 
	return 
}

/*
NewSharedDynamicObject is similar to Runtime.NewDynamicObject but the resulting Object can be shared across multiple
Runtimes. The Object's prototype will be null. The provided DynamicObject must be goroutine-safe.
*/
func ( DynamicObject) *Object {
	 := &Object{}
	 := &dynamicObject{
		d: ,
		baseDynamicObject: baseDynamicObject{
			val: ,
		},
	}
	.self = 
	return 
}

/*
NewDynamicArray creates an array Object backed by the provided DynamicArray handler.
It is similar to NewDynamicObject, the differences are:

- the Object is an array (i.e. Array.isArray() will return true and it will have the length property).

- the prototype will be initially set to Array.prototype.

- the Object cannot have any own string properties except for the 'length'.
*/
func ( *Runtime) ( DynamicArray) *Object {
	 := &Object{runtime: }
	 := &dynamicArray{
		a: ,
		baseDynamicObject: baseDynamicObject{
			val:       ,
			prototype: .getArrayPrototype(),
		},
	}
	.self = 
	return 
}

/*
NewSharedDynamicArray is similar to Runtime.NewDynamicArray but the resulting Object can be shared across multiple
Runtimes. The Object's prototype will be null. If you need to run Array's methods on it, use Array.prototype.[...].call(a, ...).
The provided DynamicArray must be goroutine-safe.
*/
func ( DynamicArray) *Object {
	 := &Object{}
	 := &dynamicArray{
		a: ,
		baseDynamicObject: baseDynamicObject{
			val: ,
		},
	}
	.self = 
	return 
}

func (*dynamicObject) () int {
	return 0
}

func (*dynamicObject) ( int) Value {
	return nil
}

func (*dynamicObject) ( int,  int) {
}

func (*dynamicObject) () string {
	return classObject
}

func ( *baseDynamicObject) ( unistring.String,  Value) Value {
	if  := .prototype;  != nil {
		if  == nil {
			return .self.getStr(, .val)
		}
		return .self.getStr(, )
	}
	return nil
}

func ( *dynamicObject) ( unistring.String,  Value) Value {
	 := .d.Get(.String())
	if  == nil {
		return .getParentStr(, )
	}
	return 
}

func ( *baseDynamicObject) ( valueInt,  Value) Value {
	if  := .prototype;  != nil {
		if  == nil {
			return .self.getIdx(, .val)
		}
		return .self.getIdx(, )
	}
	return nil
}

func ( *dynamicObject) ( valueInt,  Value) Value {
	 := .d.Get(.String())
	if  == nil {
		return .getParentIdx(, )
	}
	return 
}

func ( *baseDynamicObject) ( *Symbol,  Value) Value {
	if  := .prototype;  != nil {
		if  == nil {
			return .self.getSym(, .val)
		}
		return .self.getSym(, )
	}
	return nil
}

func ( *dynamicObject) ( unistring.String) Value {
	return .d.Get(.String())
}

func ( *dynamicObject) ( valueInt) Value {
	return .d.Get(.String())
}

func (*baseDynamicObject) (*Symbol) Value {
	return nil
}

func ( *dynamicObject) ( string,  Value,  bool) bool {
	if .d.Set(, ) {
		return true
	}
	typeErrorResult(, "'Set' on a dynamic object returned false")
	return false
}

func ( *baseDynamicObject) ( bool) {
	typeErrorResult(, "Dynamic objects do not support Symbol properties")
}

func ( *dynamicObject) ( unistring.String,  Value,  bool) bool {
	 := .String()
	if !.d.Has() {
		if  := .prototype;  != nil {
			// we know it's foreign because prototype loops are not allowed
			if ,  := .self.setForeignStr(, , .val, );  {
				return 
			}
		}
	}
	return ._set(, , )
}

func ( *dynamicObject) ( valueInt,  Value,  bool) bool {
	 := .String()
	if !.d.Has() {
		if  := .prototype;  != nil {
			// we know it's foreign because prototype loops are not allowed
			if ,  := .self.setForeignIdx(, , .val, );  {
				return 
			}
		}
	}
	return ._set(, , )
}

func ( *baseDynamicObject) ( *Symbol,  Value,  bool) bool {
	if  := .prototype;  != nil {
		// we know it's foreign because prototype loops are not allowed
		if ,  := .self.setForeignSym(, , .val, );  {
			return 
		}
	}
	._setSym()
	return false
}

func ( *baseDynamicObject) ( unistring.String, ,  Value,  bool) ( bool,  bool) {
	if  := .prototype;  != nil {
		if  !=  {
			return .self.setForeignStr(, , , )
		}
		return .self.setOwnStr(, , ), true
	}
	return false, false
}

func ( *dynamicObject) ( unistring.String, ,  Value,  bool) ( bool,  bool) {
	 := .String()
	if !.d.Has() {
		return .setParentForeignStr(, , , )
	}
	return false, false
}

func ( *baseDynamicObject) ( valueInt, ,  Value,  bool) ( bool,  bool) {
	if  := .prototype;  != nil {
		if  !=  {
			return .self.setForeignIdx(, , , )
		}
		return .self.setOwnIdx(, , ), true
	}
	return false, false
}

func ( *dynamicObject) ( valueInt, ,  Value,  bool) ( bool,  bool) {
	 := .String()
	if !.d.Has() {
		return .setParentForeignIdx(, , , )
	}
	return false, false
}

func ( *baseDynamicObject) ( *Symbol, ,  Value,  bool) ( bool,  bool) {
	if  := .prototype;  != nil {
		if  !=  {
			return .self.setForeignSym(, , , )
		}
		return .self.setOwnSym(, , ), true
	}
	return false, false
}

func ( *dynamicObject) ( unistring.String) bool {
	if .hasOwnPropertyStr() {
		return true
	}
	if  := .prototype;  != nil {
		return .self.hasPropertyStr()
	}
	return false
}

func ( *dynamicObject) ( valueInt) bool {
	if .hasOwnPropertyIdx() {
		return true
	}
	if  := .prototype;  != nil {
		return .self.hasPropertyIdx()
	}
	return false
}

func ( *baseDynamicObject) ( *Symbol) bool {
	if  := .prototype;  != nil {
		return .self.hasPropertySym()
	}
	return false
}

func ( *dynamicObject) ( unistring.String) bool {
	return .d.Has(.String())
}

func ( *dynamicObject) ( valueInt) bool {
	return .d.Has(.String())
}

func (*baseDynamicObject) ( *Symbol) bool {
	return false
}

func ( *baseDynamicObject) ( fmt.Stringer,  PropertyDescriptor,  bool) bool {
	if .Getter != nil || .Setter != nil {
		typeErrorResult(, "Dynamic objects do not support accessor properties")
		return false
	}
	if .Writable == FLAG_FALSE {
		typeErrorResult(, "Dynamic object field %q cannot be made read-only", .String())
		return false
	}
	if .Enumerable == FLAG_FALSE {
		typeErrorResult(, "Dynamic object field %q cannot be made non-enumerable", .String())
		return false
	}
	if .Configurable == FLAG_FALSE {
		typeErrorResult(, "Dynamic object field %q cannot be made non-configurable", .String())
		return false
	}
	return true
}

func ( *dynamicObject) ( unistring.String,  PropertyDescriptor,  bool) bool {
	if .checkDynamicObjectPropertyDescr(, , ) {
		return ._set(.String(), .Value, )
	}
	return false
}

func ( *dynamicObject) ( valueInt,  PropertyDescriptor,  bool) bool {
	if .checkDynamicObjectPropertyDescr(, , ) {
		return ._set(.String(), .Value, )
	}
	return false
}

func ( *baseDynamicObject) ( *Symbol,  PropertyDescriptor,  bool) bool {
	._setSym()
	return false
}

func ( *dynamicObject) ( string,  bool) bool {
	if .d.Delete() {
		return true
	}
	typeErrorResult(, "Could not delete property %q of a dynamic object", )
	return false
}

func ( *dynamicObject) ( unistring.String,  bool) bool {
	return ._delete(.String(), )
}

func ( *dynamicObject) ( valueInt,  bool) bool {
	return ._delete(.String(), )
}

func (*baseDynamicObject) ( *Symbol,  bool) bool {
	return true
}

func ( *baseDynamicObject) () ( func(FunctionCall) Value,  bool) {
	return nil, false
}

func ( *baseDynamicObject) ( *vm,  int) {
	panic(.r.NewTypeError("Dynamic object is not callable"))
}

func (*baseDynamicObject) () func( []Value,  *Object) *Object {
	return nil
}

func ( *baseDynamicObject) () *Object {
	return .prototype
}

func ( *baseDynamicObject) ( *Object,  bool) bool {
	.prototype = 
	return true
}

func ( *baseDynamicObject) ( Value) bool {
	panic(newTypeError("Expecting a function in instanceof check, but got a dynamic object"))
}

func (*baseDynamicObject) () bool {
	return true
}

func ( *baseDynamicObject) ( bool) bool {
	typeErrorResult(, "Cannot make a dynamic object non-extensible")
	return false
}

type dynamicObjectPropIter struct {
	o         *dynamicObject
	propNames []string
	idx       int
}

func ( *dynamicObjectPropIter) () (propIterItem, iterNextFunc) {
	for .idx < len(.propNames) {
		 := .propNames[.idx]
		.idx++
		if .o.d.Has() {
			return propIterItem{name: newStringValue(), enumerable: _ENUM_TRUE}, .
		}
	}
	return propIterItem{}, nil
}

func ( *dynamicObject) () iterNextFunc {
	 := .d.Keys()
	return (&dynamicObjectPropIter{
		o:         ,
		propNames: ,
	}).next
}

func ( *baseDynamicObject) () iterNextFunc {
	return func() (propIterItem, iterNextFunc) {
		return propIterItem{}, nil
	}
}

func ( *dynamicObject) () iterNextFunc {
	return .iterateStringKeys()
}

func ( *dynamicObject) ( *objectExportCtx) interface{} {
	return .d
}

func ( *dynamicObject) () reflect.Type {
	return reflect.TypeOf(.d)
}

func ( *baseDynamicObject) ( reflect.Value,  reflect.Type,  *objectExportCtx) error {
	return genericExportToMap(.val, , , )
}

func ( *baseDynamicObject) ( reflect.Value,  reflect.Type,  *objectExportCtx) error {
	return genericExportToArrayOrSlice(.val, , , )
}

func ( *dynamicObject) ( objectImpl) bool {
	if ,  := .(*dynamicObject);  {
		return .d == .d
	}
	return false
}

func ( *dynamicObject) ( bool,  []Value) []Value {
	 := .d.Keys()
	if  := len() + len();  > cap() {
		 := 
		 = make([]Value, len(), )
		copy(, )
	}
	for ,  := range  {
		 = append(, newStringValue())
	}
	return 
}

func (*baseDynamicObject) ( bool,  []Value) []Value {
	return 
}

func ( *dynamicObject) ( bool,  []Value) []Value {
	return .stringKeys(, )
}

func (*baseDynamicObject) ( unistring.String,  Value, , ,  bool) Value {
	return nil
}

func (*baseDynamicObject) ( *Symbol,  Value) {
}

func ( *baseDynamicObject) (*privateEnvType, bool) *privateElements {
	panic(newTypeError("Dynamic objects cannot have private elements"))
}

func ( *baseDynamicObject) () String {
	return stringObjectC
}

func ( *dynamicArray) () int {
	return .a.Len()
}

func ( *dynamicArray) ( int) Value {
	return .a.Get()
}

func ( *dynamicArray) ( int,  int) {
	 := .sortGet()
	 := .sortGet()
	.a.Set(int(), )
	.a.Set(int(), )
}

func ( *dynamicArray) () string {
	return classArray
}

func ( *dynamicArray) ( unistring.String,  Value) Value {
	if  == "length" {
		return intToValue(int64(.a.Len()))
	}
	if ,  := strToInt();  {
		return .a.Get()
	}
	return .getParentStr(, )
}

func ( *dynamicArray) ( valueInt,  Value) Value {
	if  := .getOwnPropIdx();  != nil {
		return 
	}
	return .getParentIdx(, )
}

func ( *dynamicArray) ( unistring.String) Value {
	if  == "length" {
		return &valueProperty{
			value:    intToValue(int64(.a.Len())),
			writable: true,
		}
	}
	if ,  := strToInt();  {
		return .a.Get()
	}
	return nil
}

func ( *dynamicArray) ( valueInt) Value {
	return .a.Get(toIntStrict(int64()))
}

func ( *dynamicArray) ( Value,  bool) bool {
	if .a.SetLen(toIntStrict(.ToInteger())) {
		return true
	}
	typeErrorResult(, "'SetLen' on a dynamic array returned false")
	return false
}

func ( *dynamicArray) ( unistring.String,  Value,  bool) bool {
	if  == "length" {
		return ._setLen(, )
	}
	if ,  := strToInt();  {
		return ._setIdx(, , )
	}
	typeErrorResult(, "Cannot set property %q on a dynamic array", .String())
	return false
}

func ( *dynamicArray) ( int,  Value,  bool) bool {
	if .a.Set(, ) {
		return true
	}
	typeErrorResult(, "'Set' on a dynamic array returned false")
	return false
}

func ( *dynamicArray) ( valueInt,  Value,  bool) bool {
	return ._setIdx(toIntStrict(int64()), , )
}

func ( *dynamicArray) ( unistring.String, ,  Value,  bool) ( bool,  bool) {
	return .setParentForeignStr(, , , )
}

func ( *dynamicArray) ( valueInt, ,  Value,  bool) ( bool,  bool) {
	return .setParentForeignIdx(, , , )
}

func ( *dynamicArray) ( unistring.String) bool {
	if .hasOwnPropertyStr() {
		return true
	}
	if  := .prototype;  != nil {
		return .self.hasPropertyStr()
	}
	return false
}

func ( *dynamicArray) ( valueInt) bool {
	if .hasOwnPropertyIdx() {
		return true
	}
	if  := .prototype;  != nil {
		return .self.hasPropertyIdx()
	}
	return false
}

func ( *dynamicArray) ( int) bool {
	return  >= 0 &&  < .a.Len()
}

func ( *dynamicArray) ( unistring.String) bool {
	if  == "length" {
		return true
	}
	if ,  := strToInt();  {
		return ._has()
	}
	return false
}

func ( *dynamicArray) ( valueInt) bool {
	return ._has(toIntStrict(int64()))
}

func ( *dynamicArray) ( unistring.String,  PropertyDescriptor,  bool) bool {
	if .checkDynamicObjectPropertyDescr(, , ) {
		if ,  := strToInt();  {
			return ._setIdx(, .Value, )
		}
		typeErrorResult(, "Cannot define property %q on a dynamic array", .String())
	}
	return false
}

func ( *dynamicArray) ( valueInt,  PropertyDescriptor,  bool) bool {
	if .checkDynamicObjectPropertyDescr(, , ) {
		return ._setIdx(toIntStrict(int64()), .Value, )
	}
	return false
}

func ( *dynamicArray) ( int,  bool) bool {
	if ._has() {
		._setIdx(, _undefined, )
	}
	return true
}

func ( *dynamicArray) ( unistring.String,  bool) bool {
	if ,  := strToInt();  {
		return ._delete(, )
	}
	if .hasOwnPropertyStr() {
		typeErrorResult(, "Cannot delete property %q on a dynamic array", .String())
		return false
	}
	return true
}

func ( *dynamicArray) ( valueInt,  bool) bool {
	return ._delete(toIntStrict(int64()), )
}

type dynArrayPropIter struct {
	a          DynamicArray
	idx, limit int
}

func ( *dynArrayPropIter) () (propIterItem, iterNextFunc) {
	if .idx < .limit && .idx < .a.Len() {
		 := strconv.Itoa(.idx)
		.idx++
		return propIterItem{name: asciiString(), enumerable: _ENUM_TRUE}, .
	}

	return propIterItem{}, nil
}

func ( *dynamicArray) () iterNextFunc {
	return (&dynArrayPropIter{
		a:     .a,
		limit: .a.Len(),
	}).next
}

func ( *dynamicArray) () iterNextFunc {
	return .iterateStringKeys()
}

func ( *dynamicArray) ( *objectExportCtx) interface{} {
	return .a
}

func ( *dynamicArray) () reflect.Type {
	return reflect.TypeOf(.a)
}

func ( *dynamicArray) ( objectImpl) bool {
	if ,  := .(*dynamicArray);  {
		return  == 
	}
	return false
}

func ( *dynamicArray) ( bool,  []Value) []Value {
	 := .a.Len()
	 := len() + 
	if  {
		++
	}
	if  > cap() {
		 := 
		 = make([]Value, len(), )
		copy(, )
	}
	for  := 0;  < ; ++ {
		 = append(, asciiString(strconv.Itoa()))
	}
	if  {
		 = append(, asciiString("length"))
	}
	return 
}

func ( *dynamicArray) ( bool,  []Value) []Value {
	return .stringKeys(, )
}