object.go 36 KB

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