builtin_set.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package goja
  2. type setObject struct {
  3. baseObject
  4. m *orderedMap
  5. }
  6. type setIterObject struct {
  7. baseObject
  8. iter *orderedMapIter
  9. kind iterationKind
  10. }
  11. func (o *setIterObject) next() Value {
  12. if o.iter == nil {
  13. return o.val.runtime.createIterResultObject(_undefined, true)
  14. }
  15. entry := o.iter.next()
  16. if entry == nil {
  17. o.iter = nil
  18. return o.val.runtime.createIterResultObject(_undefined, true)
  19. }
  20. var result Value
  21. switch o.kind {
  22. case iterationKindValue:
  23. result = entry.key
  24. default:
  25. result = o.val.runtime.newArrayValues([]Value{entry.key, entry.key})
  26. }
  27. return o.val.runtime.createIterResultObject(result, false)
  28. }
  29. func (so *setObject) init() {
  30. so.baseObject.init()
  31. so.m = newOrderedMap(so.val.runtime.getHash())
  32. }
  33. func (r *Runtime) setProto_add(call FunctionCall) Value {
  34. thisObj := r.toObject(call.This)
  35. so, ok := thisObj.self.(*setObject)
  36. if !ok {
  37. panic(r.NewTypeError("Method Set.prototype.add called on incompatible receiver %s", thisObj.String()))
  38. }
  39. so.m.set(call.Argument(0), nil)
  40. return call.This
  41. }
  42. func (r *Runtime) setProto_clear(call FunctionCall) Value {
  43. thisObj := r.toObject(call.This)
  44. so, ok := thisObj.self.(*setObject)
  45. if !ok {
  46. panic(r.NewTypeError("Method Set.prototype.clear called on incompatible receiver %s", thisObj.String()))
  47. }
  48. so.m.clear()
  49. return _undefined
  50. }
  51. func (r *Runtime) setProto_delete(call FunctionCall) Value {
  52. thisObj := r.toObject(call.This)
  53. so, ok := thisObj.self.(*setObject)
  54. if !ok {
  55. panic(r.NewTypeError("Method Set.prototype.delete called on incompatible receiver %s", thisObj.String()))
  56. }
  57. return r.toBoolean(so.m.remove(call.Argument(0)))
  58. }
  59. func (r *Runtime) setProto_entries(call FunctionCall) Value {
  60. return r.createSetIterator(call.This, iterationKindKeyValue)
  61. }
  62. func (r *Runtime) setProto_forEach(call FunctionCall) Value {
  63. thisObj := r.toObject(call.This)
  64. so, ok := thisObj.self.(*setObject)
  65. if !ok {
  66. panic(r.NewTypeError("Method Set.prototype.forEach called on incompatible receiver %s", thisObj.String()))
  67. }
  68. callbackFn, ok := r.toObject(call.Argument(0)).self.assertCallable()
  69. if !ok {
  70. panic(r.NewTypeError("object is not a function %s"))
  71. }
  72. t := call.Argument(1)
  73. iter := so.m.newIter()
  74. for {
  75. entry := iter.next()
  76. if entry == nil {
  77. break
  78. }
  79. callbackFn(FunctionCall{This: t, Arguments: []Value{entry.key, entry.key, thisObj}})
  80. }
  81. return _undefined
  82. }
  83. func (r *Runtime) setProto_has(call FunctionCall) Value {
  84. thisObj := r.toObject(call.This)
  85. so, ok := thisObj.self.(*setObject)
  86. if !ok {
  87. panic(r.NewTypeError("Method Set.prototype.has called on incompatible receiver %s", thisObj.String()))
  88. }
  89. return r.toBoolean(so.m.has(call.Argument(0)))
  90. }
  91. func (r *Runtime) setProto_getSize(call FunctionCall) Value {
  92. thisObj := r.toObject(call.This)
  93. so, ok := thisObj.self.(*setObject)
  94. if !ok {
  95. panic(r.NewTypeError("Method get Set.prototype.size called on incompatible receiver %s", thisObj.String()))
  96. }
  97. return intToValue(int64(so.m.size))
  98. }
  99. func (r *Runtime) setProto_values(call FunctionCall) Value {
  100. return r.createSetIterator(call.This, iterationKindValue)
  101. }
  102. func (r *Runtime) builtin_newSet(args []Value, newTarget *Object) *Object {
  103. if newTarget == nil {
  104. panic(r.needNew("Set"))
  105. }
  106. proto := r.getPrototypeFromCtor(newTarget, r.global.Set, r.global.SetPrototype)
  107. o := &Object{runtime: r}
  108. so := &setObject{}
  109. so.class = classSet
  110. so.val = o
  111. so.extensible = true
  112. o.self = so
  113. so.prototype = proto
  114. so.init()
  115. if len(args) > 0 {
  116. if arg := args[0]; arg != nil && arg != _undefined && arg != _null {
  117. adder := so.getStr("add", nil)
  118. iter := r.getIterator(arg, nil)
  119. if adder == r.global.setAdder {
  120. iter.iterate(func(item Value) {
  121. so.m.set(item, nil)
  122. })
  123. } else {
  124. adderFn := toMethod(adder)
  125. if adderFn == nil {
  126. panic(r.NewTypeError("Set.add in missing"))
  127. }
  128. iter.iterate(func(item Value) {
  129. adderFn(FunctionCall{This: o, Arguments: []Value{item}})
  130. })
  131. }
  132. }
  133. }
  134. return o
  135. }
  136. func (r *Runtime) createSetIterator(setValue Value, kind iterationKind) Value {
  137. obj := r.toObject(setValue)
  138. setObj, ok := obj.self.(*setObject)
  139. if !ok {
  140. panic(r.NewTypeError("Object is not a Set"))
  141. }
  142. o := &Object{runtime: r}
  143. si := &setIterObject{
  144. iter: setObj.m.newIter(),
  145. kind: kind,
  146. }
  147. si.class = classSetIterator
  148. si.val = o
  149. si.extensible = true
  150. o.self = si
  151. si.prototype = r.global.SetIteratorPrototype
  152. si.init()
  153. return o
  154. }
  155. func (r *Runtime) setIterProto_next(call FunctionCall) Value {
  156. thisObj := r.toObject(call.This)
  157. if iter, ok := thisObj.self.(*setIterObject); ok {
  158. return iter.next()
  159. }
  160. panic(r.NewTypeError("Method Set Iterator.prototype.next called on incompatible receiver %s", thisObj.String()))
  161. }
  162. func (r *Runtime) createSetProto(val *Object) objectImpl {
  163. o := newBaseObjectObj(val, r.global.ObjectPrototype, classObject)
  164. o._putProp("constructor", r.global.Set, true, false, true)
  165. r.global.setAdder = r.newNativeFunc(r.setProto_add, nil, "add", nil, 1)
  166. o._putProp("add", r.global.setAdder, true, false, true)
  167. o._putProp("clear", r.newNativeFunc(r.setProto_clear, nil, "clear", nil, 0), true, false, true)
  168. o._putProp("delete", r.newNativeFunc(r.setProto_delete, nil, "delete", nil, 1), true, false, true)
  169. o._putProp("forEach", r.newNativeFunc(r.setProto_forEach, nil, "forEach", nil, 1), true, false, true)
  170. o._putProp("has", r.newNativeFunc(r.setProto_has, nil, "has", nil, 1), true, false, true)
  171. o.setOwnStr("size", &valueProperty{
  172. getterFunc: r.newNativeFunc(r.setProto_getSize, nil, "get size", nil, 0),
  173. accessor: true,
  174. writable: true,
  175. configurable: true,
  176. }, true)
  177. valuesFunc := r.newNativeFunc(r.setProto_values, nil, "values", nil, 0)
  178. o._putProp("values", valuesFunc, true, false, true)
  179. o._putProp("keys", valuesFunc, true, false, true)
  180. o._putProp("entries", r.newNativeFunc(r.setProto_entries, nil, "entries", nil, 0), true, false, true)
  181. o._putSym(SymIterator, valueProp(valuesFunc, true, false, true))
  182. o._putSym(SymToStringTag, valueProp(asciiString(classSet), false, false, true))
  183. return o
  184. }
  185. func (r *Runtime) createSet(val *Object) objectImpl {
  186. o := r.newNativeConstructOnly(val, r.builtin_newSet, r.global.SetPrototype, "Set", 0)
  187. r.putSpeciesReturnThis(o)
  188. return o
  189. }
  190. func (r *Runtime) createSetIterProto(val *Object) objectImpl {
  191. o := newBaseObjectObj(val, r.global.IteratorPrototype, classObject)
  192. o._putProp("next", r.newNativeFunc(r.setIterProto_next, nil, "next", nil, 0), true, false, true)
  193. o._putSym(SymToStringTag, valueProp(asciiString(classSetIterator), false, false, true))
  194. return o
  195. }
  196. func (r *Runtime) initSet() {
  197. r.global.SetIteratorPrototype = r.newLazyObject(r.createSetIterProto)
  198. r.global.SetPrototype = r.newLazyObject(r.createSetProto)
  199. r.global.Set = r.newLazyObject(r.createSet)
  200. r.addToGlobal("Set", r.global.Set)
  201. }