object.go 36 KB

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