builtin_weakmap.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package goja
  2. type weakMap uint64
  3. type weakMapObject struct {
  4. baseObject
  5. m weakMap
  6. }
  7. func (wmo *weakMapObject) init() {
  8. wmo.baseObject.init()
  9. wmo.m = weakMap(wmo.val.runtime.genId())
  10. }
  11. func (wm weakMap) set(key *Object, value Value) {
  12. key.getWeakRefs()[wm] = value
  13. }
  14. func (wm weakMap) get(key *Object) Value {
  15. return key.weakRefs[wm]
  16. }
  17. func (wm weakMap) remove(key *Object) bool {
  18. if _, exists := key.weakRefs[wm]; exists {
  19. delete(key.weakRefs, wm)
  20. return true
  21. }
  22. return false
  23. }
  24. func (wm weakMap) has(key *Object) bool {
  25. _, exists := key.weakRefs[wm]
  26. return exists
  27. }
  28. func (r *Runtime) weakMapProto_delete(call FunctionCall) Value {
  29. thisObj := r.toObject(call.This)
  30. wmo, ok := thisObj.self.(*weakMapObject)
  31. if !ok {
  32. panic(r.NewTypeError("Method WeakMap.prototype.delete called on incompatible receiver %s", thisObj.String()))
  33. }
  34. key, ok := call.Argument(0).(*Object)
  35. if ok && wmo.m.remove(key) {
  36. return valueTrue
  37. }
  38. return valueFalse
  39. }
  40. func (r *Runtime) weakMapProto_get(call FunctionCall) Value {
  41. thisObj := r.toObject(call.This)
  42. wmo, ok := thisObj.self.(*weakMapObject)
  43. if !ok {
  44. panic(r.NewTypeError("Method WeakMap.prototype.get called on incompatible receiver %s", thisObj.String()))
  45. }
  46. var res Value
  47. if key, ok := call.Argument(0).(*Object); ok {
  48. res = wmo.m.get(key)
  49. }
  50. if res == nil {
  51. return _undefined
  52. }
  53. return res
  54. }
  55. func (r *Runtime) weakMapProto_has(call FunctionCall) Value {
  56. thisObj := r.toObject(call.This)
  57. wmo, ok := thisObj.self.(*weakMapObject)
  58. if !ok {
  59. panic(r.NewTypeError("Method WeakMap.prototype.has called on incompatible receiver %s", thisObj.String()))
  60. }
  61. key, ok := call.Argument(0).(*Object)
  62. if ok && wmo.m.has(key) {
  63. return valueTrue
  64. }
  65. return valueFalse
  66. }
  67. func (r *Runtime) weakMapProto_set(call FunctionCall) Value {
  68. thisObj := r.toObject(call.This)
  69. wmo, ok := thisObj.self.(*weakMapObject)
  70. if !ok {
  71. panic(r.NewTypeError("Method WeakMap.prototype.set called on incompatible receiver %s", thisObj.String()))
  72. }
  73. key := r.toObject(call.Argument(0))
  74. wmo.m.set(key, call.Argument(1))
  75. return call.This
  76. }
  77. func (r *Runtime) needNew(name string) *Object {
  78. return r.NewTypeError("Constructor %s requires 'new'", name)
  79. }
  80. func (r *Runtime) getPrototypeFromCtor(newTarget, defCtor, defProto *Object) *Object {
  81. if newTarget == defCtor {
  82. return defProto
  83. }
  84. proto := newTarget.self.getStr("prototype", nil)
  85. if obj, ok := proto.(*Object); ok {
  86. return obj
  87. }
  88. return defProto
  89. }
  90. func (r *Runtime) builtin_newWeakMap(args []Value, newTarget *Object) *Object {
  91. if newTarget == nil {
  92. panic(r.needNew("WeakMap"))
  93. }
  94. proto := r.getPrototypeFromCtor(newTarget, r.global.WeakMap, r.global.WeakMapPrototype)
  95. o := &Object{runtime: r}
  96. wmo := &weakMapObject{}
  97. wmo.class = classWeakMap
  98. wmo.val = o
  99. wmo.extensible = true
  100. o.self = wmo
  101. wmo.prototype = proto
  102. wmo.init()
  103. if len(args) > 0 {
  104. if arg := args[0]; arg != nil && arg != _undefined && arg != _null {
  105. adder := wmo.getStr("set", nil)
  106. iter := r.getIterator(arg, nil)
  107. i0 := valueInt(0)
  108. i1 := valueInt(1)
  109. if adder == r.global.weakMapAdder {
  110. iter.iterate(func(item Value) {
  111. itemObj := r.toObject(item)
  112. k := itemObj.self.getIdx(i0, nil)
  113. v := nilSafe(itemObj.self.getIdx(i1, nil))
  114. wmo.m.set(r.toObject(k), v)
  115. })
  116. } else {
  117. adderFn := toMethod(adder)
  118. if adderFn == nil {
  119. panic(r.NewTypeError("WeakMap.set in missing"))
  120. }
  121. iter.iterate(func(item Value) {
  122. itemObj := r.toObject(item)
  123. k := itemObj.self.getIdx(i0, nil)
  124. v := itemObj.self.getIdx(i1, nil)
  125. adderFn(FunctionCall{This: o, Arguments: []Value{k, v}})
  126. })
  127. }
  128. }
  129. }
  130. return o
  131. }
  132. func (r *Runtime) createWeakMapProto(val *Object) objectImpl {
  133. o := newBaseObjectObj(val, r.global.ObjectPrototype, classObject)
  134. o._putProp("constructor", r.global.WeakMap, true, false, true)
  135. r.global.weakMapAdder = r.newNativeFunc(r.weakMapProto_set, nil, "set", nil, 2)
  136. o._putProp("set", r.global.weakMapAdder, true, false, true)
  137. o._putProp("delete", r.newNativeFunc(r.weakMapProto_delete, nil, "delete", nil, 1), true, false, true)
  138. o._putProp("has", r.newNativeFunc(r.weakMapProto_has, nil, "has", nil, 1), true, false, true)
  139. o._putProp("get", r.newNativeFunc(r.weakMapProto_get, nil, "get", nil, 1), true, false, true)
  140. o._putSym(SymToStringTag, valueProp(asciiString(classWeakMap), false, false, true))
  141. return o
  142. }
  143. func (r *Runtime) createWeakMap(val *Object) objectImpl {
  144. o := r.newNativeConstructOnly(val, r.builtin_newWeakMap, r.global.WeakMapPrototype, "WeakMap", 0)
  145. return o
  146. }
  147. func (r *Runtime) initWeakMap() {
  148. r.global.WeakMapPrototype = r.newLazyObject(r.createWeakMapProto)
  149. r.global.WeakMap = r.newLazyObject(r.createWeakMap)
  150. r.addToGlobal("WeakMap", r.global.WeakMap)
  151. }