object.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. package goja
  2. import (
  3. "fmt"
  4. "math"
  5. "reflect"
  6. "runtime"
  7. "sort"
  8. "github.com/dop251/goja/unistring"
  9. )
  10. const (
  11. classObject = "Object"
  12. classArray = "Array"
  13. classWeakSet = "WeakSet"
  14. classWeakMap = "WeakMap"
  15. classMap = "Map"
  16. classMath = "Math"
  17. classSet = "Set"
  18. classFunction = "Function"
  19. classNumber = "Number"
  20. classString = "String"
  21. classBoolean = "Boolean"
  22. classError = "Error"
  23. classRegExp = "RegExp"
  24. classDate = "Date"
  25. classJSON = "JSON"
  26. classArrayIterator = "Array Iterator"
  27. classMapIterator = "Map Iterator"
  28. classSetIterator = "Set Iterator"
  29. classStringIterator = "String Iterator"
  30. )
  31. var (
  32. hintDefault Value = asciiString("default")
  33. hintNumber Value = asciiString("number")
  34. hintString Value = asciiString("string")
  35. )
  36. type weakCollection interface {
  37. removeId(uint64)
  38. }
  39. type weakCollections struct {
  40. objId uint64
  41. colls []weakCollection
  42. }
  43. func (r *weakCollections) add(c weakCollection) {
  44. for _, ec := range r.colls {
  45. if ec == c {
  46. return
  47. }
  48. }
  49. r.colls = append(r.colls, c)
  50. }
  51. func (r *weakCollections) id() uint64 {
  52. return r.objId
  53. }
  54. func (r *weakCollections) remove(c weakCollection) {
  55. if cap(r.colls) > 16 && cap(r.colls)>>2 > len(r.colls) {
  56. // shrink
  57. colls := make([]weakCollection, 0, len(r.colls))
  58. for _, coll := range r.colls {
  59. if coll != c {
  60. colls = append(colls, coll)
  61. }
  62. }
  63. r.colls = colls
  64. } else {
  65. for i, coll := range r.colls {
  66. if coll == c {
  67. l := len(r.colls) - 1
  68. r.colls[i] = r.colls[l]
  69. r.colls[l] = nil
  70. r.colls = r.colls[:l]
  71. break
  72. }
  73. }
  74. }
  75. }
  76. func finalizeObjectWeakRefs(r *weakCollections) {
  77. id := r.id()
  78. for _, c := range r.colls {
  79. c.removeId(id)
  80. }
  81. r.colls = nil
  82. }
  83. type Object struct {
  84. id uint64
  85. runtime *Runtime
  86. self objectImpl
  87. // Contains references to all weak collections that contain this Object.
  88. // weakColls has a finalizer that removes the Object's id from all weak collections.
  89. // The id is the weakColls pointer value converted to uintptr.
  90. // Note, cannot set the finalizer on the *Object itself because it's a part of a
  91. // reference cycle.
  92. weakColls *weakCollections
  93. }
  94. type iterNextFunc func() (propIterItem, iterNextFunc)
  95. type PropertyDescriptor struct {
  96. jsDescriptor *Object
  97. Value Value
  98. Writable, Configurable, Enumerable Flag
  99. Getter, Setter Value
  100. }
  101. func (p *PropertyDescriptor) Empty() bool {
  102. var empty PropertyDescriptor
  103. return *p == empty
  104. }
  105. func (p *PropertyDescriptor) toValue(r *Runtime) Value {
  106. if p.jsDescriptor != nil {
  107. return p.jsDescriptor
  108. }
  109. o := r.NewObject()
  110. s := o.self
  111. if p.Value != nil {
  112. s._putProp("value", p.Value, true, true, true)
  113. }
  114. if p.Writable != FLAG_NOT_SET {
  115. s._putProp("writable", valueBool(p.Writable.Bool()), true, true, true)
  116. }
  117. if p.Enumerable != FLAG_NOT_SET {
  118. s._putProp("enumerable", valueBool(p.Enumerable.Bool()), true, true, true)
  119. }
  120. if p.Configurable != FLAG_NOT_SET {
  121. s._putProp("configurable", valueBool(p.Configurable.Bool()), true, true, true)
  122. }
  123. if p.Getter != nil {
  124. s._putProp("get", p.Getter, true, true, true)
  125. }
  126. if p.Setter != nil {
  127. s._putProp("set", p.Setter, true, true, true)
  128. }
  129. return o
  130. }
  131. func (p *PropertyDescriptor) complete() {
  132. if p.Getter == nil && p.Setter == nil {
  133. if p.Value == nil {
  134. p.Value = _undefined
  135. }
  136. if p.Writable == FLAG_NOT_SET {
  137. p.Writable = FLAG_FALSE
  138. }
  139. } else {
  140. if p.Getter == nil {
  141. p.Getter = _undefined
  142. }
  143. if p.Setter == nil {
  144. p.Setter = _undefined
  145. }
  146. }
  147. if p.Enumerable == FLAG_NOT_SET {
  148. p.Enumerable = FLAG_FALSE
  149. }
  150. if p.Configurable == FLAG_NOT_SET {
  151. p.Configurable = FLAG_FALSE
  152. }
  153. }
  154. type objectImpl interface {
  155. sortable
  156. className() string
  157. getStr(p unistring.String, receiver Value) Value
  158. getIdx(p valueInt, receiver Value) Value
  159. getSym(p *valueSymbol, receiver Value) Value
  160. getOwnPropStr(unistring.String) Value
  161. getOwnPropIdx(valueInt) Value
  162. getOwnPropSym(*valueSymbol) Value
  163. setOwnStr(p unistring.String, v Value, throw bool) bool
  164. setOwnIdx(p valueInt, v Value, throw bool) bool
  165. setOwnSym(p *valueSymbol, v Value, throw bool) bool
  166. setForeignStr(p unistring.String, v, receiver Value, throw bool) (res bool, handled bool)
  167. setForeignIdx(p valueInt, v, receiver Value, throw bool) (res bool, handled bool)
  168. setForeignSym(p *valueSymbol, v, receiver Value, throw bool) (res bool, handled bool)
  169. hasPropertyStr(unistring.String) bool
  170. hasPropertyIdx(idx valueInt) bool
  171. hasPropertySym(s *valueSymbol) bool
  172. hasOwnPropertyStr(unistring.String) bool
  173. hasOwnPropertyIdx(valueInt) bool
  174. hasOwnPropertySym(s *valueSymbol) bool
  175. defineOwnPropertyStr(name unistring.String, desc PropertyDescriptor, throw bool) bool
  176. defineOwnPropertyIdx(name valueInt, desc PropertyDescriptor, throw bool) bool
  177. defineOwnPropertySym(name *valueSymbol, desc PropertyDescriptor, throw bool) bool
  178. deleteStr(name unistring.String, throw bool) bool
  179. deleteIdx(idx valueInt, throw bool) bool
  180. deleteSym(s *valueSymbol, throw bool) bool
  181. toPrimitiveNumber() Value
  182. toPrimitiveString() Value
  183. toPrimitive() Value
  184. assertCallable() (call func(FunctionCall) Value, ok bool)
  185. assertConstructor() func(args []Value, newTarget *Object) *Object
  186. proto() *Object
  187. setProto(proto *Object, throw bool) bool
  188. hasInstance(v Value) bool
  189. isExtensible() bool
  190. preventExtensions(throw bool) bool
  191. enumerate() iterNextFunc
  192. enumerateUnfiltered() iterNextFunc
  193. export() interface{}
  194. exportType() reflect.Type
  195. equal(objectImpl) bool
  196. ownKeys(all bool, accum []Value) []Value
  197. ownSymbols(all bool, accum []Value) []Value
  198. ownPropertyKeys(all bool, accum []Value) []Value
  199. _putProp(name unistring.String, value Value, writable, enumerable, configurable bool) Value
  200. _putSym(s *valueSymbol, prop Value)
  201. }
  202. type baseObject struct {
  203. class string
  204. val *Object
  205. prototype *Object
  206. extensible bool
  207. values map[unistring.String]Value
  208. propNames []unistring.String
  209. lastSortedPropLen, idxPropCount int
  210. symValues *orderedMap
  211. }
  212. type guardedObject struct {
  213. baseObject
  214. guardedProps map[unistring.String]struct{}
  215. }
  216. type primitiveValueObject struct {
  217. baseObject
  218. pValue Value
  219. }
  220. func (o *primitiveValueObject) export() interface{} {
  221. return o.pValue.Export()
  222. }
  223. func (o *primitiveValueObject) exportType() reflect.Type {
  224. return o.pValue.ExportType()
  225. }
  226. type FunctionCall struct {
  227. This Value
  228. Arguments []Value
  229. }
  230. type ConstructorCall struct {
  231. This *Object
  232. Arguments []Value
  233. }
  234. func (f FunctionCall) Argument(idx int) Value {
  235. if idx < len(f.Arguments) {
  236. return f.Arguments[idx]
  237. }
  238. return _undefined
  239. }
  240. func (f ConstructorCall) Argument(idx int) Value {
  241. if idx < len(f.Arguments) {
  242. return f.Arguments[idx]
  243. }
  244. return _undefined
  245. }
  246. func (o *baseObject) init() {
  247. o.values = make(map[unistring.String]Value)
  248. }
  249. func (o *baseObject) className() string {
  250. return o.class
  251. }
  252. func (o *baseObject) hasPropertyStr(name unistring.String) bool {
  253. if o.val.self.hasOwnPropertyStr(name) {
  254. return true
  255. }
  256. if o.prototype != nil {
  257. return o.prototype.self.hasPropertyStr(name)
  258. }
  259. return false
  260. }
  261. func (o *baseObject) hasPropertyIdx(idx valueInt) bool {
  262. return o.val.self.hasPropertyStr(idx.string())
  263. }
  264. func (o *baseObject) hasPropertySym(s *valueSymbol) bool {
  265. if o.hasOwnPropertySym(s) {
  266. return true
  267. }
  268. if o.prototype != nil {
  269. return o.prototype.self.hasPropertySym(s)
  270. }
  271. return false
  272. }
  273. func (o *baseObject) getWithOwnProp(prop, p, receiver Value) Value {
  274. if prop == nil && o.prototype != nil {
  275. if receiver == nil {
  276. return o.prototype.get(p, o.val)
  277. }
  278. return o.prototype.get(p, receiver)
  279. }
  280. if prop, ok := prop.(*valueProperty); ok {
  281. if receiver == nil {
  282. return prop.get(o.val)
  283. }
  284. return prop.get(receiver)
  285. }
  286. return prop
  287. }
  288. func (o *baseObject) getStrWithOwnProp(prop Value, name unistring.String, receiver Value) Value {
  289. if prop == nil && o.prototype != nil {
  290. if receiver == nil {
  291. return o.prototype.self.getStr(name, o.val)
  292. }
  293. return o.prototype.self.getStr(name, receiver)
  294. }
  295. if prop, ok := prop.(*valueProperty); ok {
  296. if receiver == nil {
  297. return prop.get(o.val)
  298. }
  299. return prop.get(receiver)
  300. }
  301. return prop
  302. }
  303. func (o *baseObject) getIdx(idx valueInt, receiver Value) Value {
  304. return o.val.self.getStr(idx.string(), receiver)
  305. }
  306. func (o *baseObject) getSym(s *valueSymbol, receiver Value) Value {
  307. return o.getWithOwnProp(o.getOwnPropSym(s), s, receiver)
  308. }
  309. func (o *baseObject) getStr(name unistring.String, receiver Value) Value {
  310. prop := o.values[name]
  311. if prop == nil {
  312. if o.prototype != nil {
  313. if receiver == nil {
  314. return o.prototype.self.getStr(name, o.val)
  315. }
  316. return o.prototype.self.getStr(name, receiver)
  317. }
  318. }
  319. if prop, ok := prop.(*valueProperty); ok {
  320. if receiver == nil {
  321. return prop.get(o.val)
  322. }
  323. return prop.get(receiver)
  324. }
  325. return prop
  326. }
  327. func (o *baseObject) getOwnPropIdx(idx valueInt) Value {
  328. return o.val.self.getOwnPropStr(idx.string())
  329. }
  330. func (o *baseObject) getOwnPropSym(s *valueSymbol) Value {
  331. if o.symValues != nil {
  332. return o.symValues.get(s)
  333. }
  334. return nil
  335. }
  336. func (o *baseObject) getOwnPropStr(name unistring.String) Value {
  337. return o.values[name]
  338. }
  339. func (o *baseObject) checkDeleteProp(name unistring.String, prop *valueProperty, throw bool) bool {
  340. if !prop.configurable {
  341. o.val.runtime.typeErrorResult(throw, "Cannot delete property '%s' of %s", name, o.val.toString())
  342. return false
  343. }
  344. return true
  345. }
  346. func (o *baseObject) checkDelete(name unistring.String, val Value, throw bool) bool {
  347. if val, ok := val.(*valueProperty); ok {
  348. return o.checkDeleteProp(name, val, throw)
  349. }
  350. return true
  351. }
  352. func (o *baseObject) _delete(name unistring.String) {
  353. delete(o.values, name)
  354. for i, n := range o.propNames {
  355. if n == name {
  356. copy(o.propNames[i:], o.propNames[i+1:])
  357. o.propNames = o.propNames[:len(o.propNames)-1]
  358. if i < o.lastSortedPropLen {
  359. o.lastSortedPropLen--
  360. if i < o.idxPropCount {
  361. o.idxPropCount--
  362. }
  363. }
  364. break
  365. }
  366. }
  367. }
  368. func (o *baseObject) deleteIdx(idx valueInt, throw bool) bool {
  369. return o.val.self.deleteStr(idx.string(), throw)
  370. }
  371. func (o *baseObject) deleteSym(s *valueSymbol, throw bool) bool {
  372. if o.symValues != nil {
  373. if val := o.symValues.get(s); val != nil {
  374. if !o.checkDelete(s.desc.string(), val, throw) {
  375. return false
  376. }
  377. o.symValues.remove(s)
  378. }
  379. }
  380. return true
  381. }
  382. func (o *baseObject) deleteStr(name unistring.String, throw bool) bool {
  383. if val, exists := o.values[name]; exists {
  384. if !o.checkDelete(name, val, throw) {
  385. return false
  386. }
  387. o._delete(name)
  388. }
  389. return true
  390. }
  391. func (o *baseObject) setProto(proto *Object, throw bool) bool {
  392. current := o.prototype
  393. if current.SameAs(proto) {
  394. return true
  395. }
  396. if !o.extensible {
  397. o.val.runtime.typeErrorResult(throw, "%s is not extensible", o.val)
  398. return false
  399. }
  400. for p := proto; p != nil; {
  401. if p.SameAs(o.val) {
  402. o.val.runtime.typeErrorResult(throw, "Cyclic __proto__ value")
  403. return false
  404. }
  405. p = p.self.proto()
  406. }
  407. o.prototype = proto
  408. return true
  409. }
  410. func (o *baseObject) setOwnStr(name unistring.String, val Value, throw bool) bool {
  411. ownDesc := o.values[name]
  412. if ownDesc == nil {
  413. if proto := o.prototype; proto != nil {
  414. // we know it's foreign because prototype loops are not allowed
  415. if res, handled := proto.self.setForeignStr(name, val, o.val, throw); handled {
  416. return res
  417. }
  418. }
  419. // new property
  420. if !o.extensible {
  421. o.val.runtime.typeErrorResult(throw, "Cannot add property %s, object is not extensible", name)
  422. return false
  423. } else {
  424. o.values[name] = val
  425. o.propNames = append(o.propNames, name)
  426. }
  427. return true
  428. }
  429. if prop, ok := ownDesc.(*valueProperty); ok {
  430. if !prop.isWritable() {
  431. o.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%s'", name)
  432. return false
  433. } else {
  434. prop.set(o.val, val)
  435. }
  436. } else {
  437. o.values[name] = val
  438. }
  439. return true
  440. }
  441. func (o *baseObject) setOwnIdx(idx valueInt, val Value, throw bool) bool {
  442. return o.val.self.setOwnStr(idx.string(), val, throw)
  443. }
  444. func (o *baseObject) setOwnSym(name *valueSymbol, val Value, throw bool) bool {
  445. var ownDesc Value
  446. if o.symValues != nil {
  447. ownDesc = o.symValues.get(name)
  448. }
  449. if ownDesc == nil {
  450. if proto := o.prototype; proto != nil {
  451. // we know it's foreign because prototype loops are not allowed
  452. if res, handled := proto.self.setForeignSym(name, val, o.val, throw); handled {
  453. return res
  454. }
  455. }
  456. // new property
  457. if !o.extensible {
  458. o.val.runtime.typeErrorResult(throw, "Cannot add property %s, object is not extensible", name)
  459. return false
  460. } else {
  461. if o.symValues == nil {
  462. o.symValues = newOrderedMap(nil)
  463. }
  464. o.symValues.set(name, val)
  465. }
  466. return true
  467. }
  468. if prop, ok := ownDesc.(*valueProperty); ok {
  469. if !prop.isWritable() {
  470. o.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%s'", name)
  471. return false
  472. } else {
  473. prop.set(o.val, val)
  474. }
  475. } else {
  476. o.symValues.set(name, val)
  477. }
  478. return true
  479. }
  480. func (o *baseObject) _setForeignStr(name unistring.String, prop, val, receiver Value, throw bool) (bool, bool) {
  481. if prop != nil {
  482. if prop, ok := prop.(*valueProperty); ok {
  483. if !prop.isWritable() {
  484. o.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%s'", name)
  485. return false, true
  486. }
  487. if prop.setterFunc != nil {
  488. prop.set(receiver, val)
  489. return true, true
  490. }
  491. }
  492. } else {
  493. if proto := o.prototype; proto != nil {
  494. if receiver != proto {
  495. return proto.self.setForeignStr(name, val, receiver, throw)
  496. }
  497. return proto.self.setOwnStr(name, val, throw), true
  498. }
  499. }
  500. return false, false
  501. }
  502. func (o *baseObject) _setForeignIdx(idx valueInt, prop, val, receiver Value, throw bool) (bool, bool) {
  503. if prop != nil {
  504. if prop, ok := prop.(*valueProperty); ok {
  505. if !prop.isWritable() {
  506. o.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%d'", idx)
  507. return false, true
  508. }
  509. if prop.setterFunc != nil {
  510. prop.set(receiver, val)
  511. return true, true
  512. }
  513. }
  514. } else {
  515. if proto := o.prototype; proto != nil {
  516. if receiver != proto {
  517. return proto.self.setForeignIdx(idx, val, receiver, throw)
  518. }
  519. return proto.self.setOwnIdx(idx, val, throw), true
  520. }
  521. }
  522. return false, false
  523. }
  524. func (o *baseObject) setForeignStr(name unistring.String, val, receiver Value, throw bool) (bool, bool) {
  525. return o._setForeignStr(name, o.values[name], val, receiver, throw)
  526. }
  527. func (o *baseObject) setForeignIdx(name valueInt, val, receiver Value, throw bool) (bool, bool) {
  528. return o.val.self.setForeignStr(name.string(), val, receiver, throw)
  529. }
  530. func (o *baseObject) setForeignSym(name *valueSymbol, val, receiver Value, throw bool) (bool, bool) {
  531. var prop Value
  532. if o.symValues != nil {
  533. prop = o.symValues.get(name)
  534. }
  535. if prop != nil {
  536. if prop, ok := prop.(*valueProperty); ok {
  537. if !prop.isWritable() {
  538. o.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%s'", name)
  539. return false, true
  540. }
  541. if prop.setterFunc != nil {
  542. prop.set(receiver, val)
  543. return true, true
  544. }
  545. }
  546. } else {
  547. if proto := o.prototype; proto != nil {
  548. if receiver != o.val {
  549. return proto.self.setForeignSym(name, val, receiver, throw)
  550. }
  551. return proto.self.setOwnSym(name, val, throw), true
  552. }
  553. }
  554. return false, false
  555. }
  556. func (o *baseObject) hasOwnPropertySym(s *valueSymbol) bool {
  557. if o.symValues != nil {
  558. return o.symValues.has(s)
  559. }
  560. return false
  561. }
  562. func (o *baseObject) hasOwnPropertyStr(name unistring.String) bool {
  563. _, exists := o.values[name]
  564. return exists
  565. }
  566. func (o *baseObject) hasOwnPropertyIdx(idx valueInt) bool {
  567. return o.val.self.hasOwnPropertyStr(idx.string())
  568. }
  569. func (o *baseObject) _defineOwnProperty(name unistring.String, existingValue Value, descr PropertyDescriptor, throw bool) (val Value, ok bool) {
  570. getterObj, _ := descr.Getter.(*Object)
  571. setterObj, _ := descr.Setter.(*Object)
  572. var existing *valueProperty
  573. if existingValue == nil {
  574. if !o.extensible {
  575. o.val.runtime.typeErrorResult(throw, "Cannot define property %s, object is not extensible", name)
  576. return nil, false
  577. }
  578. existing = &valueProperty{}
  579. } else {
  580. if existing, ok = existingValue.(*valueProperty); !ok {
  581. existing = &valueProperty{
  582. writable: true,
  583. enumerable: true,
  584. configurable: true,
  585. value: existingValue,
  586. }
  587. }
  588. if !existing.configurable {
  589. if descr.Configurable == FLAG_TRUE {
  590. goto Reject
  591. }
  592. if descr.Enumerable != FLAG_NOT_SET && descr.Enumerable.Bool() != existing.enumerable {
  593. goto Reject
  594. }
  595. }
  596. if existing.accessor && descr.Value != nil || !existing.accessor && (getterObj != nil || setterObj != nil) {
  597. if !existing.configurable {
  598. goto Reject
  599. }
  600. } else if !existing.accessor {
  601. if !existing.configurable {
  602. if !existing.writable {
  603. if descr.Writable == FLAG_TRUE {
  604. goto Reject
  605. }
  606. if descr.Value != nil && !descr.Value.SameAs(existing.value) {
  607. goto Reject
  608. }
  609. }
  610. }
  611. } else {
  612. if !existing.configurable {
  613. if descr.Getter != nil && existing.getterFunc != getterObj || descr.Setter != nil && existing.setterFunc != setterObj {
  614. goto Reject
  615. }
  616. }
  617. }
  618. }
  619. if descr.Writable == FLAG_TRUE && descr.Enumerable == FLAG_TRUE && descr.Configurable == FLAG_TRUE && descr.Value != nil {
  620. return descr.Value, true
  621. }
  622. if descr.Writable != FLAG_NOT_SET {
  623. existing.writable = descr.Writable.Bool()
  624. }
  625. if descr.Enumerable != FLAG_NOT_SET {
  626. existing.enumerable = descr.Enumerable.Bool()
  627. }
  628. if descr.Configurable != FLAG_NOT_SET {
  629. existing.configurable = descr.Configurable.Bool()
  630. }
  631. if descr.Value != nil {
  632. existing.value = descr.Value
  633. existing.getterFunc = nil
  634. existing.setterFunc = nil
  635. }
  636. if descr.Value != nil || descr.Writable != FLAG_NOT_SET {
  637. existing.accessor = false
  638. }
  639. if descr.Getter != nil {
  640. existing.getterFunc = propGetter(o.val, descr.Getter, o.val.runtime)
  641. existing.value = nil
  642. existing.accessor = true
  643. }
  644. if descr.Setter != nil {
  645. existing.setterFunc = propSetter(o.val, descr.Setter, o.val.runtime)
  646. existing.value = nil
  647. existing.accessor = true
  648. }
  649. if !existing.accessor && existing.value == nil {
  650. existing.value = _undefined
  651. }
  652. return existing, true
  653. Reject:
  654. o.val.runtime.typeErrorResult(throw, "Cannot redefine property: %s", name)
  655. return nil, false
  656. }
  657. func (o *baseObject) defineOwnPropertyStr(name unistring.String, descr PropertyDescriptor, throw bool) bool {
  658. existingVal := o.values[name]
  659. if v, ok := o._defineOwnProperty(name, existingVal, descr, throw); ok {
  660. o.values[name] = v
  661. if existingVal == nil {
  662. o.propNames = append(o.propNames, name)
  663. }
  664. return true
  665. }
  666. return false
  667. }
  668. func (o *baseObject) defineOwnPropertyIdx(idx valueInt, desc PropertyDescriptor, throw bool) bool {
  669. return o.val.self.defineOwnPropertyStr(idx.string(), desc, throw)
  670. }
  671. func (o *baseObject) defineOwnPropertySym(s *valueSymbol, descr PropertyDescriptor, throw bool) bool {
  672. var existingVal Value
  673. if o.symValues != nil {
  674. existingVal = o.symValues.get(s)
  675. }
  676. if v, ok := o._defineOwnProperty(s.desc.string(), existingVal, descr, throw); ok {
  677. if o.symValues == nil {
  678. o.symValues = newOrderedMap(nil)
  679. }
  680. o.symValues.set(s, v)
  681. return true
  682. }
  683. return false
  684. }
  685. func (o *baseObject) _put(name unistring.String, v Value) {
  686. if _, exists := o.values[name]; !exists {
  687. o.propNames = append(o.propNames, name)
  688. }
  689. o.values[name] = v
  690. }
  691. func valueProp(value Value, writable, enumerable, configurable bool) Value {
  692. if writable && enumerable && configurable {
  693. return value
  694. }
  695. return &valueProperty{
  696. value: value,
  697. writable: writable,
  698. enumerable: enumerable,
  699. configurable: configurable,
  700. }
  701. }
  702. func (o *baseObject) _putProp(name unistring.String, value Value, writable, enumerable, configurable bool) Value {
  703. prop := valueProp(value, writable, enumerable, configurable)
  704. o._put(name, prop)
  705. return prop
  706. }
  707. func (o *baseObject) _putSym(s *valueSymbol, prop Value) {
  708. if o.symValues == nil {
  709. o.symValues = newOrderedMap(nil)
  710. }
  711. o.symValues.set(s, prop)
  712. }
  713. func (o *baseObject) tryPrimitive(methodName unistring.String) Value {
  714. if method, ok := o.val.self.getStr(methodName, nil).(*Object); ok {
  715. if call, ok := method.self.assertCallable(); ok {
  716. v := call(FunctionCall{
  717. This: o.val,
  718. })
  719. if _, fail := v.(*Object); !fail {
  720. return v
  721. }
  722. }
  723. }
  724. return nil
  725. }
  726. func (o *baseObject) toPrimitiveNumber() Value {
  727. if v := o.tryPrimitive("valueOf"); v != nil {
  728. return v
  729. }
  730. if v := o.tryPrimitive("toString"); v != nil {
  731. return v
  732. }
  733. o.val.runtime.typeErrorResult(true, "Could not convert %v to primitive", o)
  734. return nil
  735. }
  736. func (o *baseObject) toPrimitiveString() Value {
  737. if v := o.tryPrimitive("toString"); v != nil {
  738. return v
  739. }
  740. if v := o.tryPrimitive("valueOf"); v != nil {
  741. return v
  742. }
  743. o.val.runtime.typeErrorResult(true, "Could not convert %v to primitive", o)
  744. return nil
  745. }
  746. func (o *baseObject) toPrimitive() Value {
  747. if v := o.tryPrimitive("valueOf"); v != nil {
  748. return v
  749. }
  750. if v := o.tryPrimitive("toString"); v != nil {
  751. return v
  752. }
  753. o.val.runtime.typeErrorResult(true, "Could not convert %v to primitive", o)
  754. return nil
  755. }
  756. func (o *Object) tryExoticToPrimitive(hint Value) Value {
  757. exoticToPrimitive := toMethod(o.self.getSym(symToPrimitive, nil))
  758. if exoticToPrimitive != nil {
  759. ret := exoticToPrimitive(FunctionCall{
  760. This: o,
  761. Arguments: []Value{hint},
  762. })
  763. if _, fail := ret.(*Object); !fail {
  764. return ret
  765. }
  766. panic(o.runtime.NewTypeError("Cannot convert object to primitive value"))
  767. }
  768. return nil
  769. }
  770. func (o *Object) toPrimitiveNumber() Value {
  771. if v := o.tryExoticToPrimitive(hintNumber); v != nil {
  772. return v
  773. }
  774. return o.self.toPrimitiveNumber()
  775. }
  776. func (o *Object) toPrimitiveString() Value {
  777. if v := o.tryExoticToPrimitive(hintString); v != nil {
  778. return v
  779. }
  780. return o.self.toPrimitiveString()
  781. }
  782. func (o *Object) toPrimitive() Value {
  783. if v := o.tryExoticToPrimitive(hintDefault); v != nil {
  784. return v
  785. }
  786. return o.self.toPrimitive()
  787. }
  788. func (o *baseObject) assertCallable() (func(FunctionCall) Value, bool) {
  789. return nil, false
  790. }
  791. func (o *baseObject) assertConstructor() func(args []Value, newTarget *Object) *Object {
  792. return nil
  793. }
  794. func (o *baseObject) proto() *Object {
  795. return o.prototype
  796. }
  797. func (o *baseObject) isExtensible() bool {
  798. return o.extensible
  799. }
  800. func (o *baseObject) preventExtensions(bool) bool {
  801. o.extensible = false
  802. return true
  803. }
  804. func (o *baseObject) sortLen() int64 {
  805. return toLength(o.val.self.getStr("length", nil))
  806. }
  807. func (o *baseObject) sortGet(i int64) Value {
  808. return o.val.self.getIdx(valueInt(i), nil)
  809. }
  810. func (o *baseObject) swap(i, j int64) {
  811. ii := valueInt(i)
  812. jj := valueInt(j)
  813. x := o.val.self.getIdx(ii, nil)
  814. y := o.val.self.getIdx(jj, nil)
  815. o.val.self.setOwnIdx(ii, y, false)
  816. o.val.self.setOwnIdx(jj, x, false)
  817. }
  818. func (o *baseObject) export() interface{} {
  819. m := make(map[string]interface{})
  820. for _, itemName := range o.ownKeys(false, nil) {
  821. itemNameStr := itemName.String()
  822. v := o.val.self.getStr(itemName.string(), nil)
  823. if v != nil {
  824. m[itemNameStr] = v.Export()
  825. } else {
  826. m[itemNameStr] = nil
  827. }
  828. }
  829. return m
  830. }
  831. func (o *baseObject) exportType() reflect.Type {
  832. return reflectTypeMap
  833. }
  834. type enumerableFlag int
  835. const (
  836. _ENUM_UNKNOWN enumerableFlag = iota
  837. _ENUM_FALSE
  838. _ENUM_TRUE
  839. )
  840. type propIterItem struct {
  841. name unistring.String
  842. value Value // set only when enumerable == _ENUM_UNKNOWN
  843. enumerable enumerableFlag
  844. }
  845. type objectPropIter struct {
  846. o *baseObject
  847. propNames []unistring.String
  848. idx int
  849. }
  850. type propFilterIter struct {
  851. wrapped iterNextFunc
  852. all bool
  853. seen map[unistring.String]bool
  854. }
  855. func (i *propFilterIter) next() (propIterItem, iterNextFunc) {
  856. for {
  857. var item propIterItem
  858. item, i.wrapped = i.wrapped()
  859. if i.wrapped == nil {
  860. return propIterItem{}, nil
  861. }
  862. if !i.seen[item.name] {
  863. i.seen[item.name] = true
  864. if !i.all {
  865. if item.enumerable == _ENUM_FALSE {
  866. continue
  867. }
  868. if item.enumerable == _ENUM_UNKNOWN {
  869. if prop, ok := item.value.(*valueProperty); ok {
  870. if !prop.enumerable {
  871. continue
  872. }
  873. }
  874. }
  875. }
  876. return item, i.next
  877. }
  878. }
  879. }
  880. func (i *objectPropIter) next() (propIterItem, iterNextFunc) {
  881. for i.idx < len(i.propNames) {
  882. name := i.propNames[i.idx]
  883. i.idx++
  884. prop := i.o.values[name]
  885. if prop != nil {
  886. return propIterItem{name: name, value: prop}, i.next
  887. }
  888. }
  889. return propIterItem{}, nil
  890. }
  891. func (o *baseObject) enumerate() iterNextFunc {
  892. return (&propFilterIter{
  893. wrapped: o.val.self.enumerateUnfiltered(),
  894. seen: make(map[unistring.String]bool),
  895. }).next
  896. }
  897. func (o *baseObject) ownIter() iterNextFunc {
  898. if len(o.propNames) > o.lastSortedPropLen {
  899. o.fixPropOrder()
  900. }
  901. propNames := make([]unistring.String, len(o.propNames))
  902. copy(propNames, o.propNames)
  903. return (&objectPropIter{
  904. o: o,
  905. propNames: propNames,
  906. }).next
  907. }
  908. func (o *baseObject) recursiveIter(iter iterNextFunc) iterNextFunc {
  909. return (&recursiveIter{
  910. o: o,
  911. wrapped: iter,
  912. }).next
  913. }
  914. func (o *baseObject) enumerateUnfiltered() iterNextFunc {
  915. return o.recursiveIter(o.ownIter())
  916. }
  917. type recursiveIter struct {
  918. o *baseObject
  919. wrapped iterNextFunc
  920. }
  921. func (iter *recursiveIter) next() (propIterItem, iterNextFunc) {
  922. item, next := iter.wrapped()
  923. if next != nil {
  924. iter.wrapped = next
  925. return item, iter.next
  926. }
  927. if proto := iter.o.prototype; proto != nil {
  928. return proto.self.enumerateUnfiltered()()
  929. }
  930. return propIterItem{}, nil
  931. }
  932. func (o *baseObject) equal(objectImpl) bool {
  933. // Rely on parent reference comparison
  934. return false
  935. }
  936. // Reorder property names so that any integer properties are shifted to the beginning of the list
  937. // in ascending order. This is to conform to ES6 9.1.12.
  938. // Personally I think this requirement is strange. I can sort of understand where they are coming from,
  939. // this way arrays can be specified just as objects with a 'magic' length property. However, I think
  940. // it's safe to assume most devs don't use Objects to store integer properties. Therefore, performing
  941. // property type checks when adding (and potentially looking up) properties would be unreasonable.
  942. // Instead, we keep insertion order and only change it when (if) the properties get enumerated.
  943. func (o *baseObject) fixPropOrder() {
  944. names := o.propNames
  945. for i := o.lastSortedPropLen; i < len(names); i++ {
  946. name := names[i]
  947. if idx := strToIdx(name); idx != math.MaxUint32 {
  948. k := sort.Search(o.idxPropCount, func(j int) bool {
  949. return strToIdx(names[j]) >= idx
  950. })
  951. if k < i {
  952. copy(names[k+1:i+1], names[k:i])
  953. names[k] = name
  954. }
  955. o.idxPropCount++
  956. }
  957. }
  958. o.lastSortedPropLen = len(names)
  959. }
  960. func (o *baseObject) ownKeys(all bool, keys []Value) []Value {
  961. if len(o.propNames) > o.lastSortedPropLen {
  962. o.fixPropOrder()
  963. }
  964. if all {
  965. for _, k := range o.propNames {
  966. keys = append(keys, stringValueFromRaw(k))
  967. }
  968. } else {
  969. for _, k := range o.propNames {
  970. prop := o.values[k]
  971. if prop, ok := prop.(*valueProperty); ok && !prop.enumerable {
  972. continue
  973. }
  974. keys = append(keys, stringValueFromRaw(k))
  975. }
  976. }
  977. return keys
  978. }
  979. func (o *baseObject) ownSymbols(all bool, accum []Value) []Value {
  980. if o.symValues != nil {
  981. iter := o.symValues.newIter()
  982. if all {
  983. for {
  984. entry := iter.next()
  985. if entry == nil {
  986. break
  987. }
  988. accum = append(accum, entry.key)
  989. }
  990. } else {
  991. for {
  992. entry := iter.next()
  993. if entry == nil {
  994. break
  995. }
  996. if prop, ok := entry.value.(*valueProperty); ok {
  997. if !prop.enumerable {
  998. continue
  999. }
  1000. }
  1001. accum = append(accum, entry.key)
  1002. }
  1003. }
  1004. }
  1005. return accum
  1006. }
  1007. func (o *baseObject) ownPropertyKeys(all bool, accum []Value) []Value {
  1008. return o.ownSymbols(all, o.val.self.ownKeys(all, accum))
  1009. }
  1010. func (o *baseObject) hasInstance(Value) bool {
  1011. panic(o.val.runtime.NewTypeError("Expecting a function in instanceof check, but got %s", o.val.toString()))
  1012. }
  1013. func toMethod(v Value) func(FunctionCall) Value {
  1014. if v == nil || IsUndefined(v) || IsNull(v) {
  1015. return nil
  1016. }
  1017. if obj, ok := v.(*Object); ok {
  1018. if call, ok := obj.self.assertCallable(); ok {
  1019. return call
  1020. }
  1021. }
  1022. panic(typeError(fmt.Sprintf("%s is not a method", v.String())))
  1023. }
  1024. func instanceOfOperator(o Value, c *Object) bool {
  1025. if instOfHandler := toMethod(c.self.getSym(symHasInstance, c)); instOfHandler != nil {
  1026. return instOfHandler(FunctionCall{
  1027. This: c,
  1028. Arguments: []Value{o},
  1029. }).ToBoolean()
  1030. }
  1031. return c.self.hasInstance(o)
  1032. }
  1033. func (o *Object) get(p Value, receiver Value) Value {
  1034. switch p := p.(type) {
  1035. case valueInt:
  1036. return o.self.getIdx(p, receiver)
  1037. case *valueSymbol:
  1038. return o.self.getSym(p, receiver)
  1039. default:
  1040. return o.self.getStr(p.string(), receiver)
  1041. }
  1042. }
  1043. func (o *Object) getOwnProp(p Value) Value {
  1044. switch p := p.(type) {
  1045. case valueInt:
  1046. return o.self.getOwnPropIdx(p)
  1047. case *valueSymbol:
  1048. return o.self.getOwnPropSym(p)
  1049. default:
  1050. return o.self.getOwnPropStr(p.string())
  1051. }
  1052. }
  1053. func (o *Object) hasOwnProperty(p Value) bool {
  1054. switch p := p.(type) {
  1055. case valueInt:
  1056. return o.self.hasOwnPropertyIdx(p)
  1057. case *valueSymbol:
  1058. return o.self.hasOwnPropertySym(p)
  1059. default:
  1060. return o.self.hasOwnPropertyStr(p.string())
  1061. }
  1062. }
  1063. func (o *Object) hasProperty(p Value) bool {
  1064. switch p := p.(type) {
  1065. case valueInt:
  1066. return o.self.hasPropertyIdx(p)
  1067. case *valueSymbol:
  1068. return o.self.hasPropertySym(p)
  1069. default:
  1070. return o.self.hasPropertyStr(p.string())
  1071. }
  1072. }
  1073. func (o *Object) setStr(name unistring.String, val, receiver Value, throw bool) bool {
  1074. if receiver == o {
  1075. return o.self.setOwnStr(name, val, throw)
  1076. } else {
  1077. if res, ok := o.self.setForeignStr(name, val, receiver, throw); !ok {
  1078. if robj, ok := receiver.(*Object); ok {
  1079. if prop := robj.self.getOwnPropStr(name); prop != nil {
  1080. if desc, ok := prop.(*valueProperty); ok {
  1081. if desc.accessor {
  1082. o.runtime.typeErrorResult(throw, "Receiver property %s is an accessor", name)
  1083. return false
  1084. }
  1085. if !desc.writable {
  1086. o.runtime.typeErrorResult(throw, "Cannot assign to read only property '%s'", name)
  1087. return false
  1088. }
  1089. }
  1090. robj.self.defineOwnPropertyStr(name, PropertyDescriptor{Value: val}, throw)
  1091. } else {
  1092. robj.self.defineOwnPropertyStr(name, PropertyDescriptor{
  1093. Value: val,
  1094. Writable: FLAG_TRUE,
  1095. Configurable: FLAG_TRUE,
  1096. Enumerable: FLAG_TRUE,
  1097. }, throw)
  1098. }
  1099. } else {
  1100. o.runtime.typeErrorResult(throw, "Receiver is not an object: %v", receiver)
  1101. return false
  1102. }
  1103. } else {
  1104. return res
  1105. }
  1106. }
  1107. return true
  1108. }
  1109. func (o *Object) set(name Value, val, receiver Value, throw bool) bool {
  1110. switch name := name.(type) {
  1111. case valueInt:
  1112. return o.setIdx(name, val, receiver, throw)
  1113. case *valueSymbol:
  1114. return o.setSym(name, val, receiver, throw)
  1115. default:
  1116. return o.setStr(name.string(), val, receiver, throw)
  1117. }
  1118. }
  1119. func (o *Object) setOwn(name Value, val Value, throw bool) bool {
  1120. switch name := name.(type) {
  1121. case valueInt:
  1122. return o.self.setOwnIdx(name, val, throw)
  1123. case *valueSymbol:
  1124. return o.self.setOwnSym(name, val, throw)
  1125. default:
  1126. return o.self.setOwnStr(name.string(), val, throw)
  1127. }
  1128. }
  1129. func (o *Object) setIdx(name valueInt, val, receiver Value, throw bool) bool {
  1130. if receiver == o {
  1131. return o.self.setOwnIdx(name, val, throw)
  1132. } else {
  1133. if res, ok := o.self.setForeignIdx(name, val, receiver, throw); !ok {
  1134. if robj, ok := receiver.(*Object); ok {
  1135. if prop := robj.self.getOwnPropIdx(name); prop != nil {
  1136. if desc, ok := prop.(*valueProperty); ok {
  1137. if desc.accessor {
  1138. o.runtime.typeErrorResult(throw, "Receiver property %s is an accessor", name)
  1139. return false
  1140. }
  1141. if !desc.writable {
  1142. o.runtime.typeErrorResult(throw, "Cannot assign to read only property '%s'", name)
  1143. return false
  1144. }
  1145. }
  1146. robj.self.defineOwnPropertyIdx(name, PropertyDescriptor{Value: val}, throw)
  1147. } else {
  1148. robj.self.defineOwnPropertyIdx(name, PropertyDescriptor{
  1149. Value: val,
  1150. Writable: FLAG_TRUE,
  1151. Configurable: FLAG_TRUE,
  1152. Enumerable: FLAG_TRUE,
  1153. }, throw)
  1154. }
  1155. } else {
  1156. o.runtime.typeErrorResult(throw, "Receiver is not an object: %v", receiver)
  1157. return false
  1158. }
  1159. } else {
  1160. return res
  1161. }
  1162. }
  1163. return true
  1164. }
  1165. func (o *Object) setSym(name *valueSymbol, val, receiver Value, throw bool) bool {
  1166. if receiver == o {
  1167. return o.self.setOwnSym(name, val, throw)
  1168. } else {
  1169. if res, ok := o.self.setForeignSym(name, val, receiver, throw); !ok {
  1170. if robj, ok := receiver.(*Object); ok {
  1171. if prop := robj.self.getOwnPropSym(name); prop != nil {
  1172. if desc, ok := prop.(*valueProperty); ok {
  1173. if desc.accessor {
  1174. o.runtime.typeErrorResult(throw, "Receiver property %s is an accessor", name)
  1175. return false
  1176. }
  1177. if !desc.writable {
  1178. o.runtime.typeErrorResult(throw, "Cannot assign to read only property '%s'", name)
  1179. return false
  1180. }
  1181. }
  1182. robj.self.defineOwnPropertySym(name, PropertyDescriptor{Value: val}, throw)
  1183. } else {
  1184. robj.self.defineOwnPropertySym(name, PropertyDescriptor{
  1185. Value: val,
  1186. Writable: FLAG_TRUE,
  1187. Configurable: FLAG_TRUE,
  1188. Enumerable: FLAG_TRUE,
  1189. }, throw)
  1190. }
  1191. } else {
  1192. o.runtime.typeErrorResult(throw, "Receiver is not an object: %v", receiver)
  1193. return false
  1194. }
  1195. } else {
  1196. return res
  1197. }
  1198. }
  1199. return true
  1200. }
  1201. func (o *Object) delete(n Value, throw bool) bool {
  1202. switch n := n.(type) {
  1203. case valueInt:
  1204. return o.self.deleteIdx(n, throw)
  1205. case *valueSymbol:
  1206. return o.self.deleteSym(n, throw)
  1207. default:
  1208. return o.self.deleteStr(n.string(), throw)
  1209. }
  1210. }
  1211. func (o *Object) defineOwnProperty(n Value, desc PropertyDescriptor, throw bool) bool {
  1212. switch n := n.(type) {
  1213. case valueInt:
  1214. return o.self.defineOwnPropertyIdx(n, desc, throw)
  1215. case *valueSymbol:
  1216. return o.self.defineOwnPropertySym(n, desc, throw)
  1217. default:
  1218. return o.self.defineOwnPropertyStr(n.string(), desc, throw)
  1219. }
  1220. }
  1221. func (o *Object) getWeakCollRefs() *weakCollections {
  1222. if o.weakColls == nil {
  1223. o.weakColls = &weakCollections{
  1224. objId: o.getId(),
  1225. }
  1226. runtime.SetFinalizer(o.weakColls, finalizeObjectWeakRefs)
  1227. }
  1228. return o.weakColls
  1229. }
  1230. func (o *Object) getId() uint64 {
  1231. for o.id == 0 {
  1232. if o.runtime.hash == nil {
  1233. h := o.runtime.getHash()
  1234. o.runtime.idSeq = h.Sum64()
  1235. }
  1236. o.id = o.runtime.idSeq
  1237. o.runtime.idSeq++
  1238. }
  1239. return o.id
  1240. }
  1241. func (o *guardedObject) guard(props ...unistring.String) {
  1242. if o.guardedProps == nil {
  1243. o.guardedProps = make(map[unistring.String]struct{})
  1244. }
  1245. for _, p := range props {
  1246. o.guardedProps[p] = struct{}{}
  1247. }
  1248. }
  1249. func (o *guardedObject) check(p unistring.String) {
  1250. if _, exists := o.guardedProps[p]; exists {
  1251. o.val.self = &o.baseObject
  1252. }
  1253. }
  1254. func (o *guardedObject) setOwnStr(p unistring.String, v Value, throw bool) bool {
  1255. res := o.baseObject.setOwnStr(p, v, throw)
  1256. if res {
  1257. o.check(p)
  1258. }
  1259. return res
  1260. }
  1261. func (o *guardedObject) defineOwnPropertyStr(name unistring.String, desc PropertyDescriptor, throw bool) bool {
  1262. res := o.baseObject.defineOwnPropertyStr(name, desc, throw)
  1263. if res {
  1264. o.check(name)
  1265. }
  1266. return res
  1267. }
  1268. func (o *guardedObject) deleteStr(name unistring.String, throw bool) bool {
  1269. res := o.baseObject.deleteStr(name, throw)
  1270. if res {
  1271. o.check(name)
  1272. }
  1273. return res
  1274. }