builtin_date.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. package goja
  2. import (
  3. "fmt"
  4. "math"
  5. "sync"
  6. "time"
  7. )
  8. func (r *Runtime) makeDate(args []Value, utc bool) (t time.Time, valid bool) {
  9. switch {
  10. case len(args) >= 2:
  11. t = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local)
  12. t, valid = _dateSetYear(t, FunctionCall{Arguments: args}, 0, utc)
  13. case len(args) == 0:
  14. t = r.now()
  15. valid = true
  16. default: // one argument
  17. if o, ok := args[0].(*Object); ok {
  18. if d, ok := o.self.(*dateObject); ok {
  19. t = d.time()
  20. valid = true
  21. }
  22. }
  23. if !valid {
  24. pv := toPrimitive(args[0])
  25. if val, ok := pv.(String); ok {
  26. return dateParse(val.String())
  27. }
  28. pv = pv.ToNumber()
  29. var n int64
  30. if i, ok := pv.(valueInt); ok {
  31. n = int64(i)
  32. } else if f, ok := pv.(valueFloat); ok {
  33. f := float64(f)
  34. if math.IsNaN(f) || math.IsInf(f, 0) {
  35. return
  36. }
  37. if math.Abs(f) > maxTime {
  38. return
  39. }
  40. n = int64(f)
  41. } else {
  42. n = pv.ToInteger()
  43. }
  44. t = timeFromMsec(n)
  45. valid = true
  46. }
  47. }
  48. if valid {
  49. msec := t.Unix()*1000 + int64(t.Nanosecond()/1e6)
  50. if msec < 0 {
  51. msec = -msec
  52. }
  53. if msec > maxTime {
  54. valid = false
  55. }
  56. }
  57. return
  58. }
  59. func (r *Runtime) newDateTime(args []Value, proto *Object) *Object {
  60. t, isSet := r.makeDate(args, false)
  61. return r.newDateObject(t, isSet, proto)
  62. }
  63. func (r *Runtime) builtin_newDate(args []Value, proto *Object) *Object {
  64. return r.newDateTime(args, proto)
  65. }
  66. func (r *Runtime) builtin_date(FunctionCall) Value {
  67. return asciiString(dateFormat(r.now()))
  68. }
  69. func (r *Runtime) date_parse(call FunctionCall) Value {
  70. t, set := dateParse(call.Argument(0).toString().String())
  71. if set {
  72. return intToValue(timeToMsec(t))
  73. }
  74. return _NaN
  75. }
  76. func (r *Runtime) date_UTC(call FunctionCall) Value {
  77. var args []Value
  78. if len(call.Arguments) < 2 {
  79. args = []Value{call.Argument(0), _positiveZero}
  80. } else {
  81. args = call.Arguments
  82. }
  83. t, valid := r.makeDate(args, true)
  84. if !valid {
  85. return _NaN
  86. }
  87. return intToValue(timeToMsec(t))
  88. }
  89. func (r *Runtime) date_now(FunctionCall) Value {
  90. return intToValue(timeToMsec(r.now()))
  91. }
  92. func (r *Runtime) dateproto_toString(call FunctionCall) Value {
  93. obj := r.toObject(call.This)
  94. if d, ok := obj.self.(*dateObject); ok {
  95. if d.isSet() {
  96. return asciiString(d.time().Format(dateTimeLayout))
  97. } else {
  98. return stringInvalidDate
  99. }
  100. }
  101. panic(r.NewTypeError("Method Date.prototype.toString is called on incompatible receiver"))
  102. }
  103. func (r *Runtime) dateproto_toUTCString(call FunctionCall) Value {
  104. obj := r.toObject(call.This)
  105. if d, ok := obj.self.(*dateObject); ok {
  106. if d.isSet() {
  107. return asciiString(d.timeUTC().Format(utcDateTimeLayout))
  108. } else {
  109. return stringInvalidDate
  110. }
  111. }
  112. panic(r.NewTypeError("Method Date.prototype.toUTCString is called on incompatible receiver"))
  113. }
  114. func (r *Runtime) dateproto_toISOString(call FunctionCall) Value {
  115. obj := r.toObject(call.This)
  116. if d, ok := obj.self.(*dateObject); ok {
  117. if d.isSet() {
  118. utc := d.timeUTC()
  119. year := utc.Year()
  120. if year >= -9999 && year <= 9999 {
  121. return asciiString(utc.Format(isoDateTimeLayout))
  122. }
  123. // extended year
  124. return asciiString(fmt.Sprintf("%+06d-", year) + utc.Format(isoDateTimeLayout[5:]))
  125. } else {
  126. panic(r.newError(r.getRangeError(), "Invalid time value"))
  127. }
  128. }
  129. panic(r.NewTypeError("Method Date.prototype.toISOString is called on incompatible receiver"))
  130. }
  131. func (r *Runtime) dateproto_toJSON(call FunctionCall) Value {
  132. obj := call.This.ToObject(r)
  133. tv := obj.toPrimitiveNumber()
  134. if f, ok := tv.(valueFloat); ok {
  135. f := float64(f)
  136. if math.IsNaN(f) || math.IsInf(f, 0) {
  137. return _null
  138. }
  139. }
  140. if toISO, ok := obj.self.getStr("toISOString", nil).(*Object); ok {
  141. if toISO, ok := toISO.self.assertCallable(); ok {
  142. return toISO(FunctionCall{
  143. This: obj,
  144. })
  145. }
  146. }
  147. panic(r.NewTypeError("toISOString is not a function"))
  148. }
  149. func (r *Runtime) dateproto_toPrimitive(call FunctionCall) Value {
  150. o := r.toObject(call.This)
  151. arg := call.Argument(0)
  152. if asciiString("string").StrictEquals(arg) || asciiString("default").StrictEquals(arg) {
  153. return o.ordinaryToPrimitiveString()
  154. }
  155. if asciiString("number").StrictEquals(arg) {
  156. return o.ordinaryToPrimitiveNumber()
  157. }
  158. panic(r.NewTypeError("Invalid hint: %s", arg))
  159. }
  160. func (r *Runtime) dateproto_toDateString(call FunctionCall) Value {
  161. obj := r.toObject(call.This)
  162. if d, ok := obj.self.(*dateObject); ok {
  163. if d.isSet() {
  164. return asciiString(d.time().Format(dateLayout))
  165. } else {
  166. return stringInvalidDate
  167. }
  168. }
  169. panic(r.NewTypeError("Method Date.prototype.toDateString is called on incompatible receiver"))
  170. }
  171. func (r *Runtime) dateproto_toTimeString(call FunctionCall) Value {
  172. obj := r.toObject(call.This)
  173. if d, ok := obj.self.(*dateObject); ok {
  174. if d.isSet() {
  175. return asciiString(d.time().Format(timeLayout))
  176. } else {
  177. return stringInvalidDate
  178. }
  179. }
  180. panic(r.NewTypeError("Method Date.prototype.toTimeString is called on incompatible receiver"))
  181. }
  182. func (r *Runtime) dateproto_toLocaleString(call FunctionCall) Value {
  183. obj := r.toObject(call.This)
  184. if d, ok := obj.self.(*dateObject); ok {
  185. if d.isSet() {
  186. return asciiString(d.time().Format(datetimeLayout_en_GB))
  187. } else {
  188. return stringInvalidDate
  189. }
  190. }
  191. panic(r.NewTypeError("Method Date.prototype.toLocaleString is called on incompatible receiver"))
  192. }
  193. func (r *Runtime) dateproto_toLocaleDateString(call FunctionCall) Value {
  194. obj := r.toObject(call.This)
  195. if d, ok := obj.self.(*dateObject); ok {
  196. if d.isSet() {
  197. return asciiString(d.time().Format(dateLayout_en_GB))
  198. } else {
  199. return stringInvalidDate
  200. }
  201. }
  202. panic(r.NewTypeError("Method Date.prototype.toLocaleDateString is called on incompatible receiver"))
  203. }
  204. func (r *Runtime) dateproto_toLocaleTimeString(call FunctionCall) Value {
  205. obj := r.toObject(call.This)
  206. if d, ok := obj.self.(*dateObject); ok {
  207. if d.isSet() {
  208. return asciiString(d.time().Format(timeLayout_en_GB))
  209. } else {
  210. return stringInvalidDate
  211. }
  212. }
  213. panic(r.NewTypeError("Method Date.prototype.toLocaleTimeString is called on incompatible receiver"))
  214. }
  215. func (r *Runtime) dateproto_valueOf(call FunctionCall) Value {
  216. obj := r.toObject(call.This)
  217. if d, ok := obj.self.(*dateObject); ok {
  218. if d.isSet() {
  219. return intToValue(d.msec)
  220. } else {
  221. return _NaN
  222. }
  223. }
  224. panic(r.NewTypeError("Method Date.prototype.valueOf is called on incompatible receiver"))
  225. }
  226. func (r *Runtime) dateproto_getTime(call FunctionCall) Value {
  227. obj := r.toObject(call.This)
  228. if d, ok := obj.self.(*dateObject); ok {
  229. if d.isSet() {
  230. return intToValue(d.msec)
  231. } else {
  232. return _NaN
  233. }
  234. }
  235. panic(r.NewTypeError("Method Date.prototype.getTime is called on incompatible receiver"))
  236. }
  237. func (r *Runtime) dateproto_getFullYear(call FunctionCall) Value {
  238. obj := r.toObject(call.This)
  239. if d, ok := obj.self.(*dateObject); ok {
  240. if d.isSet() {
  241. return intToValue(int64(d.time().Year()))
  242. } else {
  243. return _NaN
  244. }
  245. }
  246. panic(r.NewTypeError("Method Date.prototype.getFullYear is called on incompatible receiver"))
  247. }
  248. func (r *Runtime) dateproto_getUTCFullYear(call FunctionCall) Value {
  249. obj := r.toObject(call.This)
  250. if d, ok := obj.self.(*dateObject); ok {
  251. if d.isSet() {
  252. return intToValue(int64(d.timeUTC().Year()))
  253. } else {
  254. return _NaN
  255. }
  256. }
  257. panic(r.NewTypeError("Method Date.prototype.getUTCFullYear is called on incompatible receiver"))
  258. }
  259. func (r *Runtime) dateproto_getMonth(call FunctionCall) Value {
  260. obj := r.toObject(call.This)
  261. if d, ok := obj.self.(*dateObject); ok {
  262. if d.isSet() {
  263. return intToValue(int64(d.time().Month()) - 1)
  264. } else {
  265. return _NaN
  266. }
  267. }
  268. panic(r.NewTypeError("Method Date.prototype.getMonth is called on incompatible receiver"))
  269. }
  270. func (r *Runtime) dateproto_getUTCMonth(call FunctionCall) Value {
  271. obj := r.toObject(call.This)
  272. if d, ok := obj.self.(*dateObject); ok {
  273. if d.isSet() {
  274. return intToValue(int64(d.timeUTC().Month()) - 1)
  275. } else {
  276. return _NaN
  277. }
  278. }
  279. panic(r.NewTypeError("Method Date.prototype.getUTCMonth is called on incompatible receiver"))
  280. }
  281. func (r *Runtime) dateproto_getHours(call FunctionCall) Value {
  282. obj := r.toObject(call.This)
  283. if d, ok := obj.self.(*dateObject); ok {
  284. if d.isSet() {
  285. return intToValue(int64(d.time().Hour()))
  286. } else {
  287. return _NaN
  288. }
  289. }
  290. panic(r.NewTypeError("Method Date.prototype.getHours is called on incompatible receiver"))
  291. }
  292. func (r *Runtime) dateproto_getUTCHours(call FunctionCall) Value {
  293. obj := r.toObject(call.This)
  294. if d, ok := obj.self.(*dateObject); ok {
  295. if d.isSet() {
  296. return intToValue(int64(d.timeUTC().Hour()))
  297. } else {
  298. return _NaN
  299. }
  300. }
  301. panic(r.NewTypeError("Method Date.prototype.getUTCHours is called on incompatible receiver"))
  302. }
  303. func (r *Runtime) dateproto_getDate(call FunctionCall) Value {
  304. obj := r.toObject(call.This)
  305. if d, ok := obj.self.(*dateObject); ok {
  306. if d.isSet() {
  307. return intToValue(int64(d.time().Day()))
  308. } else {
  309. return _NaN
  310. }
  311. }
  312. panic(r.NewTypeError("Method Date.prototype.getDate is called on incompatible receiver"))
  313. }
  314. func (r *Runtime) dateproto_getUTCDate(call FunctionCall) Value {
  315. obj := r.toObject(call.This)
  316. if d, ok := obj.self.(*dateObject); ok {
  317. if d.isSet() {
  318. return intToValue(int64(d.timeUTC().Day()))
  319. } else {
  320. return _NaN
  321. }
  322. }
  323. panic(r.NewTypeError("Method Date.prototype.getUTCDate is called on incompatible receiver"))
  324. }
  325. func (r *Runtime) dateproto_getDay(call FunctionCall) Value {
  326. obj := r.toObject(call.This)
  327. if d, ok := obj.self.(*dateObject); ok {
  328. if d.isSet() {
  329. return intToValue(int64(d.time().Weekday()))
  330. } else {
  331. return _NaN
  332. }
  333. }
  334. panic(r.NewTypeError("Method Date.prototype.getDay is called on incompatible receiver"))
  335. }
  336. func (r *Runtime) dateproto_getUTCDay(call FunctionCall) Value {
  337. obj := r.toObject(call.This)
  338. if d, ok := obj.self.(*dateObject); ok {
  339. if d.isSet() {
  340. return intToValue(int64(d.timeUTC().Weekday()))
  341. } else {
  342. return _NaN
  343. }
  344. }
  345. panic(r.NewTypeError("Method Date.prototype.getUTCDay is called on incompatible receiver"))
  346. }
  347. func (r *Runtime) dateproto_getMinutes(call FunctionCall) Value {
  348. obj := r.toObject(call.This)
  349. if d, ok := obj.self.(*dateObject); ok {
  350. if d.isSet() {
  351. return intToValue(int64(d.time().Minute()))
  352. } else {
  353. return _NaN
  354. }
  355. }
  356. panic(r.NewTypeError("Method Date.prototype.getMinutes is called on incompatible receiver"))
  357. }
  358. func (r *Runtime) dateproto_getUTCMinutes(call FunctionCall) Value {
  359. obj := r.toObject(call.This)
  360. if d, ok := obj.self.(*dateObject); ok {
  361. if d.isSet() {
  362. return intToValue(int64(d.timeUTC().Minute()))
  363. } else {
  364. return _NaN
  365. }
  366. }
  367. panic(r.NewTypeError("Method Date.prototype.getUTCMinutes is called on incompatible receiver"))
  368. }
  369. func (r *Runtime) dateproto_getSeconds(call FunctionCall) Value {
  370. obj := r.toObject(call.This)
  371. if d, ok := obj.self.(*dateObject); ok {
  372. if d.isSet() {
  373. return intToValue(int64(d.time().Second()))
  374. } else {
  375. return _NaN
  376. }
  377. }
  378. panic(r.NewTypeError("Method Date.prototype.getSeconds is called on incompatible receiver"))
  379. }
  380. func (r *Runtime) dateproto_getUTCSeconds(call FunctionCall) Value {
  381. obj := r.toObject(call.This)
  382. if d, ok := obj.self.(*dateObject); ok {
  383. if d.isSet() {
  384. return intToValue(int64(d.timeUTC().Second()))
  385. } else {
  386. return _NaN
  387. }
  388. }
  389. panic(r.NewTypeError("Method Date.prototype.getUTCSeconds is called on incompatible receiver"))
  390. }
  391. func (r *Runtime) dateproto_getMilliseconds(call FunctionCall) Value {
  392. obj := r.toObject(call.This)
  393. if d, ok := obj.self.(*dateObject); ok {
  394. if d.isSet() {
  395. return intToValue(int64(d.time().Nanosecond() / 1e6))
  396. } else {
  397. return _NaN
  398. }
  399. }
  400. panic(r.NewTypeError("Method Date.prototype.getMilliseconds is called on incompatible receiver"))
  401. }
  402. func (r *Runtime) dateproto_getUTCMilliseconds(call FunctionCall) Value {
  403. obj := r.toObject(call.This)
  404. if d, ok := obj.self.(*dateObject); ok {
  405. if d.isSet() {
  406. return intToValue(int64(d.timeUTC().Nanosecond() / 1e6))
  407. } else {
  408. return _NaN
  409. }
  410. }
  411. panic(r.NewTypeError("Method Date.prototype.getUTCMilliseconds is called on incompatible receiver"))
  412. }
  413. func (r *Runtime) dateproto_getTimezoneOffset(call FunctionCall) Value {
  414. obj := r.toObject(call.This)
  415. if d, ok := obj.self.(*dateObject); ok {
  416. if d.isSet() {
  417. _, offset := d.time().Zone()
  418. return floatToValue(float64(-offset) / 60)
  419. } else {
  420. return _NaN
  421. }
  422. }
  423. panic(r.NewTypeError("Method Date.prototype.getTimezoneOffset is called on incompatible receiver"))
  424. }
  425. func (r *Runtime) dateproto_setTime(call FunctionCall) Value {
  426. obj := r.toObject(call.This)
  427. if d, ok := obj.self.(*dateObject); ok {
  428. n := call.Argument(0).ToNumber()
  429. if IsNaN(n) {
  430. d.unset()
  431. return _NaN
  432. }
  433. return d.setTimeMs(n.ToInteger())
  434. }
  435. panic(r.NewTypeError("Method Date.prototype.setTime is called on incompatible receiver"))
  436. }
  437. // _norm returns nhi, nlo such that
  438. //
  439. // hi * base + lo == nhi * base + nlo
  440. // 0 <= nlo < base
  441. func _norm(hi, lo, base int64) (nhi, nlo int64, ok bool) {
  442. if lo < 0 {
  443. if hi == math.MinInt64 && lo <= -base {
  444. // underflow
  445. ok = false
  446. return
  447. }
  448. n := (-lo-1)/base + 1
  449. hi -= n
  450. lo += n * base
  451. }
  452. if lo >= base {
  453. if hi == math.MaxInt64 {
  454. // overflow
  455. ok = false
  456. return
  457. }
  458. n := lo / base
  459. hi += n
  460. lo -= n * base
  461. }
  462. return hi, lo, true
  463. }
  464. func mkTime(year, m, day, hour, min, sec, nsec int64, loc *time.Location) (t time.Time, ok bool) {
  465. year, m, ok = _norm(year, m, 12)
  466. if !ok {
  467. return
  468. }
  469. // Normalise nsec, sec, min, hour, overflowing into day.
  470. sec, nsec, ok = _norm(sec, nsec, 1e9)
  471. if !ok {
  472. return
  473. }
  474. min, sec, ok = _norm(min, sec, 60)
  475. if !ok {
  476. return
  477. }
  478. hour, min, ok = _norm(hour, min, 60)
  479. if !ok {
  480. return
  481. }
  482. day, hour, ok = _norm(day, hour, 24)
  483. if !ok {
  484. return
  485. }
  486. if year > math.MaxInt32 || year < math.MinInt32 ||
  487. day > math.MaxInt32 || day < math.MinInt32 ||
  488. m >= math.MaxInt32 || m < math.MinInt32-1 {
  489. return time.Time{}, false
  490. }
  491. month := time.Month(m) + 1
  492. return time.Date(int(year), month, int(day), int(hour), int(min), int(sec), int(nsec), loc), true
  493. }
  494. func _intArg(call FunctionCall, argNum int) (int64, bool) {
  495. n := call.Argument(argNum).ToNumber()
  496. if IsNaN(n) {
  497. return 0, false
  498. }
  499. return n.ToInteger(), true
  500. }
  501. func _dateSetYear(t time.Time, call FunctionCall, argNum int, utc bool) (time.Time, bool) {
  502. var year int64
  503. if argNum == 0 || argNum > 0 && argNum < len(call.Arguments) {
  504. var ok bool
  505. year, ok = _intArg(call, argNum)
  506. if !ok {
  507. return time.Time{}, false
  508. }
  509. if year >= 0 && year <= 99 {
  510. year += 1900
  511. }
  512. } else {
  513. year = int64(t.Year())
  514. }
  515. return _dateSetMonth(year, t, call, argNum+1, utc)
  516. }
  517. func _dateSetFullYear(t time.Time, call FunctionCall, argNum int, utc bool) (time.Time, bool) {
  518. var year int64
  519. if argNum == 0 || argNum > 0 && argNum < len(call.Arguments) {
  520. var ok bool
  521. year, ok = _intArg(call, argNum)
  522. if !ok {
  523. return time.Time{}, false
  524. }
  525. } else {
  526. year = int64(t.Year())
  527. }
  528. return _dateSetMonth(year, t, call, argNum+1, utc)
  529. }
  530. func _dateSetMonth(year int64, t time.Time, call FunctionCall, argNum int, utc bool) (time.Time, bool) {
  531. var mon int64
  532. if argNum == 0 || argNum > 0 && argNum < len(call.Arguments) {
  533. var ok bool
  534. mon, ok = _intArg(call, argNum)
  535. if !ok {
  536. return time.Time{}, false
  537. }
  538. } else {
  539. mon = int64(t.Month()) - 1
  540. }
  541. return _dateSetDay(year, mon, t, call, argNum+1, utc)
  542. }
  543. func _dateSetDay(year, mon int64, t time.Time, call FunctionCall, argNum int, utc bool) (time.Time, bool) {
  544. var day int64
  545. if argNum == 0 || argNum > 0 && argNum < len(call.Arguments) {
  546. var ok bool
  547. day, ok = _intArg(call, argNum)
  548. if !ok {
  549. return time.Time{}, false
  550. }
  551. } else {
  552. day = int64(t.Day())
  553. }
  554. return _dateSetHours(year, mon, day, t, call, argNum+1, utc)
  555. }
  556. func _dateSetHours(year, mon, day int64, t time.Time, call FunctionCall, argNum int, utc bool) (time.Time, bool) {
  557. var hours int64
  558. if argNum == 0 || argNum > 0 && argNum < len(call.Arguments) {
  559. var ok bool
  560. hours, ok = _intArg(call, argNum)
  561. if !ok {
  562. return time.Time{}, false
  563. }
  564. } else {
  565. hours = int64(t.Hour())
  566. }
  567. return _dateSetMinutes(year, mon, day, hours, t, call, argNum+1, utc)
  568. }
  569. func _dateSetMinutes(year, mon, day, hours int64, t time.Time, call FunctionCall, argNum int, utc bool) (time.Time, bool) {
  570. var min int64
  571. if argNum == 0 || argNum > 0 && argNum < len(call.Arguments) {
  572. var ok bool
  573. min, ok = _intArg(call, argNum)
  574. if !ok {
  575. return time.Time{}, false
  576. }
  577. } else {
  578. min = int64(t.Minute())
  579. }
  580. return _dateSetSeconds(year, mon, day, hours, min, t, call, argNum+1, utc)
  581. }
  582. func _dateSetSeconds(year, mon, day, hours, min int64, t time.Time, call FunctionCall, argNum int, utc bool) (time.Time, bool) {
  583. var sec int64
  584. if argNum == 0 || argNum > 0 && argNum < len(call.Arguments) {
  585. var ok bool
  586. sec, ok = _intArg(call, argNum)
  587. if !ok {
  588. return time.Time{}, false
  589. }
  590. } else {
  591. sec = int64(t.Second())
  592. }
  593. return _dateSetMilliseconds(year, mon, day, hours, min, sec, t, call, argNum+1, utc)
  594. }
  595. func _dateSetMilliseconds(year, mon, day, hours, min, sec int64, t time.Time, call FunctionCall, argNum int, utc bool) (time.Time, bool) {
  596. var msec int64
  597. if argNum == 0 || argNum > 0 && argNum < len(call.Arguments) {
  598. var ok bool
  599. msec, ok = _intArg(call, argNum)
  600. if !ok {
  601. return time.Time{}, false
  602. }
  603. } else {
  604. msec = int64(t.Nanosecond() / 1e6)
  605. }
  606. var ok bool
  607. sec, msec, ok = _norm(sec, msec, 1e3)
  608. if !ok {
  609. return time.Time{}, false
  610. }
  611. var loc *time.Location
  612. if utc {
  613. loc = time.UTC
  614. } else {
  615. loc = time.Local
  616. }
  617. r, ok := mkTime(year, mon, day, hours, min, sec, msec*1e6, loc)
  618. if !ok {
  619. return time.Time{}, false
  620. }
  621. if utc {
  622. return r.In(time.Local), true
  623. }
  624. return r, true
  625. }
  626. func (r *Runtime) dateproto_setMilliseconds(call FunctionCall) Value {
  627. obj := r.toObject(call.This)
  628. if d, ok := obj.self.(*dateObject); ok {
  629. n := call.Argument(0).ToNumber()
  630. if IsNaN(n) {
  631. d.unset()
  632. return _NaN
  633. }
  634. msec := n.ToInteger()
  635. sec := d.msec / 1e3
  636. var ok bool
  637. sec, msec, ok = _norm(sec, msec, 1e3)
  638. if !ok {
  639. d.unset()
  640. return _NaN
  641. }
  642. if d.isSet() {
  643. return d.setTimeMs(sec*1e3 + msec)
  644. } else {
  645. return _NaN
  646. }
  647. }
  648. panic(r.NewTypeError("Method Date.prototype.setMilliseconds is called on incompatible receiver"))
  649. }
  650. func (r *Runtime) dateproto_setUTCMilliseconds(call FunctionCall) Value {
  651. obj := r.toObject(call.This)
  652. if d, ok := obj.self.(*dateObject); ok {
  653. n := call.Argument(0).ToNumber()
  654. if IsNaN(n) {
  655. d.unset()
  656. return _NaN
  657. }
  658. msec := n.ToInteger()
  659. sec := d.msec / 1e3
  660. var ok bool
  661. sec, msec, ok = _norm(sec, msec, 1e3)
  662. if !ok {
  663. d.unset()
  664. return _NaN
  665. }
  666. if d.isSet() {
  667. return d.setTimeMs(sec*1e3 + msec)
  668. } else {
  669. return _NaN
  670. }
  671. }
  672. panic(r.NewTypeError("Method Date.prototype.setUTCMilliseconds is called on incompatible receiver"))
  673. }
  674. func (r *Runtime) dateproto_setSeconds(call FunctionCall) Value {
  675. obj := r.toObject(call.This)
  676. if d, ok := obj.self.(*dateObject); ok {
  677. t, ok := _dateSetFullYear(d.time(), call, -5, false)
  678. if !ok {
  679. d.unset()
  680. return _NaN
  681. }
  682. if d.isSet() {
  683. return d.setTimeMs(timeToMsec(t))
  684. } else {
  685. return _NaN
  686. }
  687. }
  688. panic(r.NewTypeError("Method Date.prototype.setSeconds is called on incompatible receiver"))
  689. }
  690. func (r *Runtime) dateproto_setUTCSeconds(call FunctionCall) Value {
  691. obj := r.toObject(call.This)
  692. if d, ok := obj.self.(*dateObject); ok {
  693. t, ok := _dateSetFullYear(d.timeUTC(), call, -5, true)
  694. if !ok {
  695. d.unset()
  696. return _NaN
  697. }
  698. if d.isSet() {
  699. return d.setTimeMs(timeToMsec(t))
  700. } else {
  701. return _NaN
  702. }
  703. }
  704. panic(r.NewTypeError("Method Date.prototype.setUTCSeconds is called on incompatible receiver"))
  705. }
  706. func (r *Runtime) dateproto_setMinutes(call FunctionCall) Value {
  707. obj := r.toObject(call.This)
  708. if d, ok := obj.self.(*dateObject); ok {
  709. t, ok := _dateSetFullYear(d.time(), call, -4, false)
  710. if !ok {
  711. d.unset()
  712. return _NaN
  713. }
  714. if d.isSet() {
  715. return d.setTimeMs(timeToMsec(t))
  716. } else {
  717. return _NaN
  718. }
  719. }
  720. panic(r.NewTypeError("Method Date.prototype.setMinutes is called on incompatible receiver"))
  721. }
  722. func (r *Runtime) dateproto_setUTCMinutes(call FunctionCall) Value {
  723. obj := r.toObject(call.This)
  724. if d, ok := obj.self.(*dateObject); ok {
  725. t, ok := _dateSetFullYear(d.timeUTC(), call, -4, true)
  726. if !ok {
  727. d.unset()
  728. return _NaN
  729. }
  730. if d.isSet() {
  731. return d.setTimeMs(timeToMsec(t))
  732. } else {
  733. return _NaN
  734. }
  735. }
  736. panic(r.NewTypeError("Method Date.prototype.setUTCMinutes is called on incompatible receiver"))
  737. }
  738. func (r *Runtime) dateproto_setHours(call FunctionCall) Value {
  739. obj := r.toObject(call.This)
  740. if d, ok := obj.self.(*dateObject); ok {
  741. t, ok := _dateSetFullYear(d.time(), call, -3, false)
  742. if !ok {
  743. d.unset()
  744. return _NaN
  745. }
  746. if d.isSet() {
  747. return d.setTimeMs(timeToMsec(t))
  748. } else {
  749. return _NaN
  750. }
  751. }
  752. panic(r.NewTypeError("Method Date.prototype.setHours is called on incompatible receiver"))
  753. }
  754. func (r *Runtime) dateproto_setUTCHours(call FunctionCall) Value {
  755. obj := r.toObject(call.This)
  756. if d, ok := obj.self.(*dateObject); ok {
  757. t, ok := _dateSetFullYear(d.timeUTC(), call, -3, true)
  758. if !ok {
  759. d.unset()
  760. return _NaN
  761. }
  762. if d.isSet() {
  763. return d.setTimeMs(timeToMsec(t))
  764. } else {
  765. return _NaN
  766. }
  767. }
  768. panic(r.NewTypeError("Method Date.prototype.setUTCHours is called on incompatible receiver"))
  769. }
  770. func (r *Runtime) dateproto_setDate(call FunctionCall) Value {
  771. obj := r.toObject(call.This)
  772. if d, ok := obj.self.(*dateObject); ok {
  773. t, ok := _dateSetFullYear(d.time(), limitCallArgs(call, 1), -2, false)
  774. if !ok {
  775. d.unset()
  776. return _NaN
  777. }
  778. if d.isSet() {
  779. return d.setTimeMs(timeToMsec(t))
  780. } else {
  781. return _NaN
  782. }
  783. }
  784. panic(r.NewTypeError("Method Date.prototype.setDate is called on incompatible receiver"))
  785. }
  786. func (r *Runtime) dateproto_setUTCDate(call FunctionCall) Value {
  787. obj := r.toObject(call.This)
  788. if d, ok := obj.self.(*dateObject); ok {
  789. t, ok := _dateSetFullYear(d.timeUTC(), limitCallArgs(call, 1), -2, true)
  790. if !ok {
  791. d.unset()
  792. return _NaN
  793. }
  794. if d.isSet() {
  795. return d.setTimeMs(timeToMsec(t))
  796. } else {
  797. return _NaN
  798. }
  799. }
  800. panic(r.NewTypeError("Method Date.prototype.setUTCDate is called on incompatible receiver"))
  801. }
  802. func (r *Runtime) dateproto_setMonth(call FunctionCall) Value {
  803. obj := r.toObject(call.This)
  804. if d, ok := obj.self.(*dateObject); ok {
  805. t, ok := _dateSetFullYear(d.time(), limitCallArgs(call, 2), -1, false)
  806. if !ok {
  807. d.unset()
  808. return _NaN
  809. }
  810. if d.isSet() {
  811. return d.setTimeMs(timeToMsec(t))
  812. } else {
  813. return _NaN
  814. }
  815. }
  816. panic(r.NewTypeError("Method Date.prototype.setMonth is called on incompatible receiver"))
  817. }
  818. func (r *Runtime) dateproto_setUTCMonth(call FunctionCall) Value {
  819. obj := r.toObject(call.This)
  820. if d, ok := obj.self.(*dateObject); ok {
  821. t, ok := _dateSetFullYear(d.timeUTC(), limitCallArgs(call, 2), -1, true)
  822. if !ok {
  823. d.unset()
  824. return _NaN
  825. }
  826. if d.isSet() {
  827. return d.setTimeMs(timeToMsec(t))
  828. } else {
  829. return _NaN
  830. }
  831. }
  832. panic(r.NewTypeError("Method Date.prototype.setUTCMonth is called on incompatible receiver"))
  833. }
  834. func (r *Runtime) dateproto_setFullYear(call FunctionCall) Value {
  835. obj := r.toObject(call.This)
  836. if d, ok := obj.self.(*dateObject); ok {
  837. var t time.Time
  838. if d.isSet() {
  839. t = d.time()
  840. } else {
  841. t = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local)
  842. }
  843. t, ok := _dateSetFullYear(t, limitCallArgs(call, 3), 0, false)
  844. if !ok {
  845. d.unset()
  846. return _NaN
  847. }
  848. return d.setTimeMs(timeToMsec(t))
  849. }
  850. panic(r.NewTypeError("Method Date.prototype.setFullYear is called on incompatible receiver"))
  851. }
  852. func (r *Runtime) dateproto_setUTCFullYear(call FunctionCall) Value {
  853. obj := r.toObject(call.This)
  854. if d, ok := obj.self.(*dateObject); ok {
  855. var t time.Time
  856. if d.isSet() {
  857. t = d.timeUTC()
  858. } else {
  859. t = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC)
  860. }
  861. t, ok := _dateSetFullYear(t, limitCallArgs(call, 3), 0, true)
  862. if !ok {
  863. d.unset()
  864. return _NaN
  865. }
  866. return d.setTimeMs(timeToMsec(t))
  867. }
  868. panic(r.NewTypeError("Method Date.prototype.setUTCFullYear is called on incompatible receiver"))
  869. }
  870. var dateTemplate *objectTemplate
  871. var dateTemplateOnce sync.Once
  872. func getDateTemplate() *objectTemplate {
  873. dateTemplateOnce.Do(func() {
  874. dateTemplate = createDateTemplate()
  875. })
  876. return dateTemplate
  877. }
  878. func createDateTemplate() *objectTemplate {
  879. t := newObjectTemplate()
  880. t.protoFactory = func(r *Runtime) *Object {
  881. return r.getFunctionPrototype()
  882. }
  883. t.putStr("name", func(r *Runtime) Value { return valueProp(asciiString("Date"), false, false, true) })
  884. t.putStr("length", func(r *Runtime) Value { return valueProp(intToValue(7), false, false, true) })
  885. t.putStr("prototype", func(r *Runtime) Value { return valueProp(r.getDatePrototype(), false, false, false) })
  886. t.putStr("parse", func(r *Runtime) Value { return r.methodProp(r.date_parse, "parse", 1) })
  887. t.putStr("UTC", func(r *Runtime) Value { return r.methodProp(r.date_UTC, "UTC", 7) })
  888. t.putStr("now", func(r *Runtime) Value { return r.methodProp(r.date_now, "now", 0) })
  889. return t
  890. }
  891. func (r *Runtime) getDate() *Object {
  892. ret := r.global.Date
  893. if ret == nil {
  894. ret = &Object{runtime: r}
  895. r.global.Date = ret
  896. r.newTemplatedFuncObject(getDateTemplate(), ret, r.builtin_date,
  897. r.wrapNativeConstruct(r.builtin_newDate, ret, r.getDatePrototype()))
  898. }
  899. return ret
  900. }
  901. func createDateProtoTemplate() *objectTemplate {
  902. t := newObjectTemplate()
  903. t.protoFactory = func(r *Runtime) *Object {
  904. return r.global.ObjectPrototype
  905. }
  906. t.putStr("constructor", func(r *Runtime) Value { return valueProp(r.getDate(), true, false, true) })
  907. t.putStr("toString", func(r *Runtime) Value { return r.methodProp(r.dateproto_toString, "toString", 0) })
  908. t.putStr("toDateString", func(r *Runtime) Value { return r.methodProp(r.dateproto_toDateString, "toDateString", 0) })
  909. t.putStr("toTimeString", func(r *Runtime) Value { return r.methodProp(r.dateproto_toTimeString, "toTimeString", 0) })
  910. t.putStr("toLocaleString", func(r *Runtime) Value { return r.methodProp(r.dateproto_toLocaleString, "toLocaleString", 0) })
  911. t.putStr("toLocaleDateString", func(r *Runtime) Value { return r.methodProp(r.dateproto_toLocaleDateString, "toLocaleDateString", 0) })
  912. t.putStr("toLocaleTimeString", func(r *Runtime) Value { return r.methodProp(r.dateproto_toLocaleTimeString, "toLocaleTimeString", 0) })
  913. t.putStr("valueOf", func(r *Runtime) Value { return r.methodProp(r.dateproto_valueOf, "valueOf", 0) })
  914. t.putStr("getTime", func(r *Runtime) Value { return r.methodProp(r.dateproto_getTime, "getTime", 0) })
  915. t.putStr("getFullYear", func(r *Runtime) Value { return r.methodProp(r.dateproto_getFullYear, "getFullYear", 0) })
  916. t.putStr("getUTCFullYear", func(r *Runtime) Value { return r.methodProp(r.dateproto_getUTCFullYear, "getUTCFullYear", 0) })
  917. t.putStr("getMonth", func(r *Runtime) Value { return r.methodProp(r.dateproto_getMonth, "getMonth", 0) })
  918. t.putStr("getUTCMonth", func(r *Runtime) Value { return r.methodProp(r.dateproto_getUTCMonth, "getUTCMonth", 0) })
  919. t.putStr("getDate", func(r *Runtime) Value { return r.methodProp(r.dateproto_getDate, "getDate", 0) })
  920. t.putStr("getUTCDate", func(r *Runtime) Value { return r.methodProp(r.dateproto_getUTCDate, "getUTCDate", 0) })
  921. t.putStr("getDay", func(r *Runtime) Value { return r.methodProp(r.dateproto_getDay, "getDay", 0) })
  922. t.putStr("getUTCDay", func(r *Runtime) Value { return r.methodProp(r.dateproto_getUTCDay, "getUTCDay", 0) })
  923. t.putStr("getHours", func(r *Runtime) Value { return r.methodProp(r.dateproto_getHours, "getHours", 0) })
  924. t.putStr("getUTCHours", func(r *Runtime) Value { return r.methodProp(r.dateproto_getUTCHours, "getUTCHours", 0) })
  925. t.putStr("getMinutes", func(r *Runtime) Value { return r.methodProp(r.dateproto_getMinutes, "getMinutes", 0) })
  926. t.putStr("getUTCMinutes", func(r *Runtime) Value { return r.methodProp(r.dateproto_getUTCMinutes, "getUTCMinutes", 0) })
  927. t.putStr("getSeconds", func(r *Runtime) Value { return r.methodProp(r.dateproto_getSeconds, "getSeconds", 0) })
  928. t.putStr("getUTCSeconds", func(r *Runtime) Value { return r.methodProp(r.dateproto_getUTCSeconds, "getUTCSeconds", 0) })
  929. t.putStr("getMilliseconds", func(r *Runtime) Value { return r.methodProp(r.dateproto_getMilliseconds, "getMilliseconds", 0) })
  930. t.putStr("getUTCMilliseconds", func(r *Runtime) Value { return r.methodProp(r.dateproto_getUTCMilliseconds, "getUTCMilliseconds", 0) })
  931. t.putStr("getTimezoneOffset", func(r *Runtime) Value { return r.methodProp(r.dateproto_getTimezoneOffset, "getTimezoneOffset", 0) })
  932. t.putStr("setTime", func(r *Runtime) Value { return r.methodProp(r.dateproto_setTime, "setTime", 1) })
  933. t.putStr("setMilliseconds", func(r *Runtime) Value { return r.methodProp(r.dateproto_setMilliseconds, "setMilliseconds", 1) })
  934. t.putStr("setUTCMilliseconds", func(r *Runtime) Value { return r.methodProp(r.dateproto_setUTCMilliseconds, "setUTCMilliseconds", 1) })
  935. t.putStr("setSeconds", func(r *Runtime) Value { return r.methodProp(r.dateproto_setSeconds, "setSeconds", 2) })
  936. t.putStr("setUTCSeconds", func(r *Runtime) Value { return r.methodProp(r.dateproto_setUTCSeconds, "setUTCSeconds", 2) })
  937. t.putStr("setMinutes", func(r *Runtime) Value { return r.methodProp(r.dateproto_setMinutes, "setMinutes", 3) })
  938. t.putStr("setUTCMinutes", func(r *Runtime) Value { return r.methodProp(r.dateproto_setUTCMinutes, "setUTCMinutes", 3) })
  939. t.putStr("setHours", func(r *Runtime) Value { return r.methodProp(r.dateproto_setHours, "setHours", 4) })
  940. t.putStr("setUTCHours", func(r *Runtime) Value { return r.methodProp(r.dateproto_setUTCHours, "setUTCHours", 4) })
  941. t.putStr("setDate", func(r *Runtime) Value { return r.methodProp(r.dateproto_setDate, "setDate", 1) })
  942. t.putStr("setUTCDate", func(r *Runtime) Value { return r.methodProp(r.dateproto_setUTCDate, "setUTCDate", 1) })
  943. t.putStr("setMonth", func(r *Runtime) Value { return r.methodProp(r.dateproto_setMonth, "setMonth", 2) })
  944. t.putStr("setUTCMonth", func(r *Runtime) Value { return r.methodProp(r.dateproto_setUTCMonth, "setUTCMonth", 2) })
  945. t.putStr("setFullYear", func(r *Runtime) Value { return r.methodProp(r.dateproto_setFullYear, "setFullYear", 3) })
  946. t.putStr("setUTCFullYear", func(r *Runtime) Value { return r.methodProp(r.dateproto_setUTCFullYear, "setUTCFullYear", 3) })
  947. t.putStr("toUTCString", func(r *Runtime) Value { return r.methodProp(r.dateproto_toUTCString, "toUTCString", 0) })
  948. t.putStr("toISOString", func(r *Runtime) Value { return r.methodProp(r.dateproto_toISOString, "toISOString", 0) })
  949. t.putStr("toJSON", func(r *Runtime) Value { return r.methodProp(r.dateproto_toJSON, "toJSON", 1) })
  950. t.putSym(SymToPrimitive, func(r *Runtime) Value {
  951. return valueProp(r.newNativeFunc(r.dateproto_toPrimitive, "[Symbol.toPrimitive]", 1), false, false, true)
  952. })
  953. return t
  954. }
  955. var dateProtoTemplate *objectTemplate
  956. var dateProtoTemplateOnce sync.Once
  957. func getDateProtoTemplate() *objectTemplate {
  958. dateProtoTemplateOnce.Do(func() {
  959. dateProtoTemplate = createDateProtoTemplate()
  960. })
  961. return dateProtoTemplate
  962. }
  963. func (r *Runtime) getDatePrototype() *Object {
  964. ret := r.global.DatePrototype
  965. if ret == nil {
  966. ret = &Object{runtime: r}
  967. r.global.DatePrototype = ret
  968. r.newTemplatedObject(getDateProtoTemplate(), ret)
  969. }
  970. return ret
  971. }