object.go 40 KB

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