package goja

type weakMap uint64

type weakMapObject struct {
	baseObject
	m weakMap
}

func ( *weakMapObject) () {
	.baseObject.init()
	.m = weakMap(.val.runtime.genId())
}

func ( weakMap) ( *Object,  Value) {
	.getWeakRefs()[] = 
}

func ( weakMap) ( *Object) Value {
	return .weakRefs[]
}

func ( weakMap) ( *Object) bool {
	if ,  := .weakRefs[];  {
		delete(.weakRefs, )
		return true
	}
	return false
}

func ( weakMap) ( *Object) bool {
	,  := .weakRefs[]
	return 
}

func ( *Runtime) ( FunctionCall) Value {
	 := .toObject(.This)
	,  := .self.(*weakMapObject)
	if ! {
		panic(.NewTypeError("Method WeakMap.prototype.delete called on incompatible receiver %s", .objectproto_toString(FunctionCall{This: })))
	}
	,  := .Argument(0).(*Object)
	if  && .m.remove() {
		return valueTrue
	}
	return valueFalse
}

func ( *Runtime) ( FunctionCall) Value {
	 := .toObject(.This)
	,  := .self.(*weakMapObject)
	if ! {
		panic(.NewTypeError("Method WeakMap.prototype.get called on incompatible receiver %s", .objectproto_toString(FunctionCall{This: })))
	}
	var  Value
	if ,  := .Argument(0).(*Object);  {
		 = .m.get()
	}
	if  == nil {
		return _undefined
	}
	return 
}

func ( *Runtime) ( FunctionCall) Value {
	 := .toObject(.This)
	,  := .self.(*weakMapObject)
	if ! {
		panic(.NewTypeError("Method WeakMap.prototype.has called on incompatible receiver %s", .objectproto_toString(FunctionCall{This: })))
	}
	,  := .Argument(0).(*Object)
	if  && .m.has() {
		return valueTrue
	}
	return valueFalse
}

func ( *Runtime) ( FunctionCall) Value {
	 := .toObject(.This)
	,  := .self.(*weakMapObject)
	if ! {
		panic(.NewTypeError("Method WeakMap.prototype.set called on incompatible receiver %s", .objectproto_toString(FunctionCall{This: })))
	}
	 := .toObject(.Argument(0))
	.m.set(, .Argument(1))
	return .This
}

func ( *Runtime) ( string) *Object {
	return .NewTypeError("Constructor %s requires 'new'", )
}

func ( *Runtime) ( []Value,  *Object) *Object {
	if  == nil {
		panic(.needNew("WeakMap"))
	}
	 := .getPrototypeFromCtor(, .global.WeakMap, .global.WeakMapPrototype)
	 := &Object{runtime: }

	 := &weakMapObject{}
	.class = classObject
	.val = 
	.extensible = true
	.self = 
	.prototype = 
	.init()
	if len() > 0 {
		if  := [0];  != nil &&  != _undefined &&  != _null {
			 := .getStr("set", nil)
			 := toMethod()
			if  == nil {
				panic(.NewTypeError("WeakMap.set in missing"))
			}
			 := .getIterator(, nil)
			 := valueInt(0)
			 := valueInt(1)
			if  == .global.weakMapAdder {
				.iterate(func( Value) {
					 := .toObject()
					 := .self.getIdx(, nil)
					 := nilSafe(.self.getIdx(, nil))
					.m.set(.toObject(), )
				})
			} else {
				.iterate(func( Value) {
					 := .toObject()
					 := .self.getIdx(, nil)
					 := .self.getIdx(, nil)
					(FunctionCall{This: , Arguments: []Value{, }})
				})
			}
		}
	}
	return 
}

func ( *Runtime) ( *Object) objectImpl {
	 := newBaseObjectObj(, .global.ObjectPrototype, classObject)

	._putProp("constructor", .getWeakMap(), true, false, true)
	.global.weakMapAdder = .newNativeFunc(.weakMapProto_set, "set", 2)
	._putProp("set", .global.weakMapAdder, true, false, true)
	._putProp("delete", .newNativeFunc(.weakMapProto_delete, "delete", 1), true, false, true)
	._putProp("has", .newNativeFunc(.weakMapProto_has, "has", 1), true, false, true)
	._putProp("get", .newNativeFunc(.weakMapProto_get, "get", 1), true, false, true)

	._putSym(SymToStringTag, valueProp(asciiString(classWeakMap), false, false, true))

	return 
}

func ( *Runtime) ( *Object) objectImpl {
	 := .newNativeConstructOnly(, .builtin_newWeakMap, .getWeakMapPrototype(), "WeakMap", 0)

	return 
}

func ( *Runtime) () *Object {
	 := .global.WeakMapPrototype
	if  == nil {
		 = &Object{runtime: }
		.global.WeakMapPrototype = 
		.self = .createWeakMapProto()
	}
	return 
}

func ( *Runtime) () *Object {
	 := .global.WeakMap
	if  == nil {
		 = &Object{runtime: }
		.global.WeakMap = 
		.self = .createWeakMap()
	}
	return 
}