object.go 32 KB

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