object.go 40 KB

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