builtin_weakmap.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package goja
  2. import "sync"
  3. type weakMap struct {
  4. // need to synchronise access to the data map because it may be accessed
  5. // from the finalizer goroutine
  6. sync.Mutex
  7. data map[uintptr]Value
  8. }
  9. type weakMapObject struct {
  10. baseObject
  11. m *weakMap
  12. }
  13. func newWeakMap() *weakMap {
  14. return &weakMap{
  15. data: make(map[uintptr]Value),
  16. }
  17. }
  18. func (wmo *weakMapObject) init() {
  19. wmo.baseObject.init()
  20. wmo.m = newWeakMap()
  21. }
  22. func (wm *weakMap) removePtr(ptr uintptr) {
  23. wm.Lock()
  24. delete(wm.data, ptr)
  25. wm.Unlock()
  26. }
  27. func (wm *weakMap) set(key *Object, value Value) {
  28. refs := key.getWeakCollRefs()
  29. wm.Lock()
  30. wm.data[refs.id()] = value
  31. wm.Unlock()
  32. refs.add(wm)
  33. }
  34. func (wm *weakMap) get(key *Object) Value {
  35. refs := key.weakColls
  36. if refs == nil {
  37. return nil
  38. }
  39. wm.Lock()
  40. ret := wm.data[refs.id()]
  41. wm.Unlock()
  42. return ret
  43. }
  44. func (wm *weakMap) remove(key *Object) bool {
  45. refs := key.weakColls
  46. if refs == nil {
  47. return false
  48. }
  49. id := refs.id()
  50. wm.Lock()
  51. _, exists := wm.data[id]
  52. if exists {
  53. delete(wm.data, id)
  54. }
  55. wm.Unlock()
  56. if exists {
  57. refs.remove(wm)
  58. }
  59. return exists
  60. }
  61. func (wm *weakMap) has(key *Object) bool {
  62. refs := key.weakColls
  63. if refs == nil {
  64. return false
  65. }
  66. id := refs.id()
  67. wm.Lock()
  68. _, exists := wm.data[id]
  69. wm.Unlock()
  70. return exists
  71. }
  72. func (r *Runtime) weakMapProto_delete(call FunctionCall) Value {
  73. thisObj := r.toObject(call.This)
  74. wmo, ok := thisObj.self.(*weakMapObject)
  75. if !ok {
  76. panic(r.NewTypeError("Method WeakMap.prototype.delete called on incompatible receiver %s", thisObj.String()))
  77. }
  78. key, ok := call.Argument(0).(*Object)
  79. if ok && wmo.m.remove(key) {
  80. return valueTrue
  81. }
  82. return valueFalse
  83. }
  84. func (r *Runtime) weakMapProto_get(call FunctionCall) Value {
  85. thisObj := r.toObject(call.This)
  86. wmo, ok := thisObj.self.(*weakMapObject)
  87. if !ok {
  88. panic(r.NewTypeError("Method WeakMap.prototype.get called on incompatible receiver %s", thisObj.String()))
  89. }
  90. var res Value
  91. if key, ok := call.Argument(0).(*Object); ok {
  92. res = wmo.m.get(key)
  93. }
  94. if res == nil {
  95. return _undefined
  96. }
  97. return res
  98. }
  99. func (r *Runtime) weakMapProto_has(call FunctionCall) Value {
  100. thisObj := r.toObject(call.This)
  101. wmo, ok := thisObj.self.(*weakMapObject)
  102. if !ok {
  103. panic(r.NewTypeError("Method WeakMap.prototype.has called on incompatible receiver %s", thisObj.String()))
  104. }
  105. key, ok := call.Argument(0).(*Object)
  106. if ok && wmo.m.has(key) {
  107. return valueTrue
  108. }
  109. return valueFalse
  110. }
  111. func (r *Runtime) weakMapProto_set(call FunctionCall) Value {
  112. thisObj := r.toObject(call.This)
  113. wmo, ok := thisObj.self.(*weakMapObject)
  114. if !ok {
  115. panic(r.NewTypeError("Method WeakMap.prototype.set called on incompatible receiver %s", thisObj.String()))
  116. }
  117. key := r.toObject(call.Argument(0))
  118. wmo.m.set(key, call.Argument(1))
  119. return call.This
  120. }
  121. func (r *Runtime) builtin_newWeakMap(args []Value, proto *Object) *Object {
  122. o := &Object{runtime: r}
  123. wmo := &weakMapObject{}
  124. wmo.class = classWeakMap
  125. wmo.val = o
  126. wmo.extensible = true
  127. o.self = wmo
  128. wmo.prototype = proto
  129. wmo.init()
  130. if len(args) > 0 {
  131. if arg := args[0]; arg != nil && arg != _undefined && arg != _null {
  132. adder := wmo.getStr("set", nil)
  133. iter := r.getIterator(arg, nil)
  134. i0 := valueInt(0)
  135. i1 := valueInt(1)
  136. if adder == r.global.weakMapAdder {
  137. r.iterate(iter, func(item Value) {
  138. itemObj := r.toObject(item)
  139. k := itemObj.self.getIdx(i0, nil)
  140. v := nilSafe(itemObj.self.getIdx(i1, nil))
  141. wmo.m.set(r.toObject(k), v)
  142. })
  143. } else {
  144. adderFn := toMethod(adder)
  145. if adderFn == nil {
  146. panic(r.NewTypeError("WeakMap.set in missing"))
  147. }
  148. r.iterate(iter, func(item Value) {
  149. itemObj := r.toObject(item)
  150. k := itemObj.self.getIdx(i0, nil)
  151. v := itemObj.self.getIdx(i1, nil)
  152. adderFn(FunctionCall{This: o, Arguments: []Value{k, v}})
  153. })
  154. }
  155. }
  156. }
  157. return o
  158. }
  159. func (r *Runtime) createWeakMapProto(val *Object) objectImpl {
  160. o := newBaseObjectObj(val, r.global.ObjectPrototype, classObject)
  161. o._putProp("constructor", r.global.WeakMap, true, false, true)
  162. r.global.weakMapAdder = r.newNativeFunc(r.weakMapProto_set, nil, "set", nil, 2)
  163. o._putProp("set", r.global.weakMapAdder, true, false, true)
  164. o._putProp("delete", r.newNativeFunc(r.weakMapProto_delete, nil, "delete", nil, 1), true, false, true)
  165. o._putProp("has", r.newNativeFunc(r.weakMapProto_has, nil, "has", nil, 1), true, false, true)
  166. o._putProp("get", r.newNativeFunc(r.weakMapProto_get, nil, "get", nil, 1), true, false, true)
  167. o._putSym(symToStringTag, valueProp(asciiString(classWeakMap), false, false, true))
  168. return o
  169. }
  170. func (r *Runtime) createWeakMap(val *Object) objectImpl {
  171. o := r.newNativeFuncObj(val, r.constructorThrower("WeakMap"), r.builtin_newWeakMap, "WeakMap", r.global.WeakMapPrototype, 0)
  172. return o
  173. }
  174. func (r *Runtime) initWeakMap() {
  175. r.global.WeakMapPrototype = r.newLazyObject(r.createWeakMapProto)
  176. r.global.WeakMap = r.newLazyObject(r.createWeakMap)
  177. r.addToGlobal("WeakMap", r.global.WeakMap)
  178. }