object.go 37 KB

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