builtin_regexp.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. package goja
  2. import (
  3. "fmt"
  4. "github.com/dop251/goja/parser"
  5. "regexp"
  6. "strings"
  7. "unicode/utf16"
  8. "unicode/utf8"
  9. )
  10. func (r *Runtime) newRegexpObject(proto *Object) *regexpObject {
  11. v := &Object{runtime: r}
  12. o := &regexpObject{}
  13. o.class = classRegExp
  14. o.val = v
  15. o.extensible = true
  16. v.self = o
  17. o.prototype = proto
  18. o.init()
  19. return o
  20. }
  21. func (r *Runtime) newRegExpp(pattern *regexpPattern, patternStr valueString, proto *Object) *Object {
  22. o := r.newRegexpObject(proto)
  23. o.pattern = pattern
  24. o.source = patternStr
  25. return o.val
  26. }
  27. func decodeHex(s string) (int, bool) {
  28. var hex int
  29. for i := 0; i < len(s); i++ {
  30. var n byte
  31. chr := s[i]
  32. switch {
  33. case '0' <= chr && chr <= '9':
  34. n = chr - '0'
  35. case 'a' <= chr && chr <= 'f':
  36. n = chr - 'a' + 10
  37. case 'A' <= chr && chr <= 'F':
  38. n = chr - 'A' + 10
  39. default:
  40. return 0, false
  41. }
  42. hex = hex*16 + int(n)
  43. }
  44. return hex, true
  45. }
  46. func writeHex4(b *strings.Builder, i int) {
  47. b.WriteByte(hex[i>>12])
  48. b.WriteByte(hex[(i>>8)&0xF])
  49. b.WriteByte(hex[(i>>4)&0xF])
  50. b.WriteByte(hex[i&0xF])
  51. }
  52. // Convert any valid surrogate pairs in the form of \uXXXX\uXXXX to unicode characters
  53. func convertRegexpToUnicode(patternStr string) string {
  54. var sb strings.Builder
  55. pos := 0
  56. for i := 0; i < len(patternStr)-11; {
  57. r, size := utf8.DecodeRuneInString(patternStr[i:])
  58. if r == '\\' {
  59. i++
  60. if patternStr[i] == 'u' && patternStr[i+5] == '\\' && patternStr[i+6] == 'u' {
  61. if first, ok := decodeHex(patternStr[i+1 : i+5]); ok {
  62. if isUTF16FirstSurrogate(rune(first)) {
  63. if second, ok := decodeHex(patternStr[i+7 : i+11]); ok {
  64. if isUTF16SecondSurrogate(rune(second)) {
  65. r = utf16.DecodeRune(rune(first), rune(second))
  66. sb.WriteString(patternStr[pos : i-1])
  67. sb.WriteRune(r)
  68. i += 11
  69. pos = i
  70. continue
  71. }
  72. }
  73. }
  74. }
  75. }
  76. i++
  77. } else {
  78. i += size
  79. }
  80. }
  81. if pos > 0 {
  82. sb.WriteString(patternStr[pos:])
  83. return sb.String()
  84. }
  85. return patternStr
  86. }
  87. // Convert any extended unicode characters to UTF-16 in the form of \uXXXX\uXXXX
  88. func convertRegexpToUtf16(patternStr string) string {
  89. var sb strings.Builder
  90. pos := 0
  91. var prevRune rune
  92. for i := 0; i < len(patternStr); {
  93. r, size := utf8.DecodeRuneInString(patternStr[i:])
  94. if r > 0xFFFF {
  95. sb.WriteString(patternStr[pos:i])
  96. if prevRune == '\\' {
  97. sb.WriteRune('\\')
  98. }
  99. first, second := utf16.EncodeRune(r)
  100. sb.WriteString(`\u`)
  101. writeHex4(&sb, int(first))
  102. sb.WriteString(`\u`)
  103. writeHex4(&sb, int(second))
  104. pos = i + size
  105. }
  106. i += size
  107. prevRune = r
  108. }
  109. if pos > 0 {
  110. sb.WriteString(patternStr[pos:])
  111. return sb.String()
  112. }
  113. return patternStr
  114. }
  115. // convert any broken UTF-16 surrogate pairs to \uXXXX
  116. func escapeInvalidUtf16(s valueString) string {
  117. if ascii, ok := s.(asciiString); ok {
  118. return ascii.String()
  119. }
  120. var sb strings.Builder
  121. rd := &lenientUtf16Decoder{utf16Reader: s.utf16Reader(0)}
  122. pos := 0
  123. utf8Size := 0
  124. var utf8Buf [utf8.UTFMax]byte
  125. for {
  126. c, size, err := rd.ReadRune()
  127. if err != nil {
  128. break
  129. }
  130. if utf16.IsSurrogate(c) {
  131. if sb.Len() == 0 {
  132. sb.Grow(utf8Size + 7)
  133. hrd := s.reader(0)
  134. var c rune
  135. for p := 0; p < pos; {
  136. var size int
  137. var err error
  138. c, size, err = hrd.ReadRune()
  139. if err != nil {
  140. // will not happen
  141. panic(fmt.Errorf("error while reading string head %q, pos: %d: %w", s.String(), pos, err))
  142. }
  143. sb.WriteRune(c)
  144. p += size
  145. }
  146. if c == '\\' {
  147. sb.WriteRune(c)
  148. }
  149. }
  150. sb.WriteString(`\u`)
  151. writeHex4(&sb, int(c))
  152. } else {
  153. if sb.Len() > 0 {
  154. sb.WriteRune(c)
  155. } else {
  156. utf8Size += utf8.EncodeRune(utf8Buf[:], c)
  157. pos += size
  158. }
  159. }
  160. }
  161. if sb.Len() > 0 {
  162. return sb.String()
  163. }
  164. return s.String()
  165. }
  166. func compileRegexpFromValueString(patternStr valueString, flags string) (*regexpPattern, error) {
  167. return compileRegexp(escapeInvalidUtf16(patternStr), flags)
  168. }
  169. func compileRegexp(patternStr, flags string) (p *regexpPattern, err error) {
  170. var global, ignoreCase, multiline, sticky, unicode bool
  171. var wrapper *regexpWrapper
  172. var wrapper2 *regexp2Wrapper
  173. if flags != "" {
  174. invalidFlags := func() {
  175. err = fmt.Errorf("Invalid flags supplied to RegExp constructor '%s'", flags)
  176. }
  177. for _, chr := range flags {
  178. switch chr {
  179. case 'g':
  180. if global {
  181. invalidFlags()
  182. return
  183. }
  184. global = true
  185. case 'm':
  186. if multiline {
  187. invalidFlags()
  188. return
  189. }
  190. multiline = true
  191. case 'i':
  192. if ignoreCase {
  193. invalidFlags()
  194. return
  195. }
  196. ignoreCase = true
  197. case 'y':
  198. if sticky {
  199. invalidFlags()
  200. return
  201. }
  202. sticky = true
  203. case 'u':
  204. if unicode {
  205. invalidFlags()
  206. }
  207. unicode = true
  208. default:
  209. invalidFlags()
  210. return
  211. }
  212. }
  213. }
  214. if unicode {
  215. patternStr = convertRegexpToUnicode(patternStr)
  216. } else {
  217. patternStr = convertRegexpToUtf16(patternStr)
  218. }
  219. re2Str, err1 := parser.TransformRegExp(patternStr)
  220. if err1 == nil {
  221. re2flags := ""
  222. if multiline {
  223. re2flags += "m"
  224. }
  225. if ignoreCase {
  226. re2flags += "i"
  227. }
  228. if len(re2flags) > 0 {
  229. re2Str = fmt.Sprintf("(?%s:%s)", re2flags, re2Str)
  230. }
  231. pattern, err1 := regexp.Compile(re2Str)
  232. if err1 != nil {
  233. err = fmt.Errorf("Invalid regular expression (re2): %s (%v)", re2Str, err1)
  234. return
  235. }
  236. wrapper = (*regexpWrapper)(pattern)
  237. } else {
  238. if _, incompat := err1.(parser.RegexpErrorIncompatible); !incompat {
  239. err = err1
  240. return
  241. }
  242. wrapper2, err = compileRegexp2(patternStr, multiline, ignoreCase)
  243. if err != nil {
  244. err = fmt.Errorf("Invalid regular expression (regexp2): %s (%v)", patternStr, err)
  245. return
  246. }
  247. }
  248. p = &regexpPattern{
  249. src: patternStr,
  250. regexpWrapper: wrapper,
  251. regexp2Wrapper: wrapper2,
  252. global: global,
  253. ignoreCase: ignoreCase,
  254. multiline: multiline,
  255. sticky: sticky,
  256. unicode: unicode,
  257. }
  258. return
  259. }
  260. func (r *Runtime) _newRegExp(patternStr valueString, flags string, proto *Object) *Object {
  261. pattern, err := compileRegexpFromValueString(patternStr, flags)
  262. if err != nil {
  263. panic(r.newSyntaxError(err.Error(), -1))
  264. }
  265. return r.newRegExpp(pattern, patternStr, proto)
  266. }
  267. func (r *Runtime) builtin_newRegExp(args []Value, proto *Object) *Object {
  268. var patternVal, flagsVal Value
  269. if len(args) > 0 {
  270. patternVal = args[0]
  271. }
  272. if len(args) > 1 {
  273. flagsVal = args[1]
  274. }
  275. return r.newRegExp(patternVal, flagsVal, proto)
  276. }
  277. func (r *Runtime) newRegExp(patternVal, flagsVal Value, proto *Object) *Object {
  278. var pattern valueString
  279. var flags string
  280. if obj, ok := patternVal.(*Object); ok {
  281. if rx, ok := obj.self.(*regexpObject); ok {
  282. if flagsVal == nil || flagsVal == _undefined {
  283. return rx.clone()
  284. } else {
  285. return r._newRegExp(rx.source, flagsVal.toString().String(), proto)
  286. }
  287. } else {
  288. if isRegexp(patternVal) {
  289. pattern = nilSafe(obj.self.getStr("source", nil)).toString()
  290. if flagsVal == nil || flagsVal == _undefined {
  291. flags = nilSafe(obj.self.getStr("flags", nil)).toString().String()
  292. } else {
  293. flags = flagsVal.toString().String()
  294. }
  295. goto exit
  296. }
  297. }
  298. }
  299. if patternVal != nil && patternVal != _undefined {
  300. pattern = patternVal.toString()
  301. }
  302. if flagsVal != nil && flagsVal != _undefined {
  303. flags = flagsVal.toString().String()
  304. }
  305. if pattern == nil {
  306. pattern = stringEmpty
  307. }
  308. exit:
  309. return r._newRegExp(pattern, flags, proto)
  310. }
  311. func (r *Runtime) builtin_RegExp(call FunctionCall) Value {
  312. pattern := call.Argument(0)
  313. patternIsRegExp := isRegexp(pattern)
  314. flags := call.Argument(1)
  315. if patternIsRegExp && flags == _undefined {
  316. if obj, ok := call.Argument(0).(*Object); ok {
  317. patternConstructor := obj.self.getStr("constructor", nil)
  318. if patternConstructor == r.global.RegExp {
  319. return pattern
  320. }
  321. }
  322. }
  323. return r.newRegExp(pattern, flags, r.global.RegExpPrototype)
  324. }
  325. func (r *Runtime) regexpproto_compile(call FunctionCall) Value {
  326. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  327. var (
  328. pattern *regexpPattern
  329. source valueString
  330. flags string
  331. err error
  332. )
  333. patternVal := call.Argument(0)
  334. flagsVal := call.Argument(1)
  335. if o, ok := patternVal.(*Object); ok {
  336. if p, ok := o.self.(*regexpObject); ok {
  337. if flagsVal != _undefined {
  338. panic(r.NewTypeError("Cannot supply flags when constructing one RegExp from another"))
  339. }
  340. this.pattern = p.pattern
  341. this.source = p.source
  342. goto exit
  343. }
  344. }
  345. if patternVal != _undefined {
  346. source = patternVal.toString()
  347. } else {
  348. source = stringEmpty
  349. }
  350. if flagsVal != _undefined {
  351. flags = flagsVal.toString().String()
  352. }
  353. pattern, err = compileRegexpFromValueString(source, flags)
  354. if err != nil {
  355. panic(r.newSyntaxError(err.Error(), -1))
  356. }
  357. this.pattern = pattern
  358. this.source = source
  359. exit:
  360. this.setOwnStr("lastIndex", intToValue(0), true)
  361. return call.This
  362. }
  363. panic(r.NewTypeError("Method RegExp.prototype.compile called on incompatible receiver %s", call.This.toString()))
  364. }
  365. func (r *Runtime) regexpproto_exec(call FunctionCall) Value {
  366. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  367. return this.exec(call.Argument(0).toString())
  368. } else {
  369. r.typeErrorResult(true, "Method RegExp.prototype.exec called on incompatible receiver %s", call.This.toString())
  370. return nil
  371. }
  372. }
  373. func (r *Runtime) regexpproto_test(call FunctionCall) Value {
  374. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  375. if this.test(call.Argument(0).toString()) {
  376. return valueTrue
  377. } else {
  378. return valueFalse
  379. }
  380. } else {
  381. r.typeErrorResult(true, "Method RegExp.prototype.test called on incompatible receiver %s", call.This.toString())
  382. return nil
  383. }
  384. }
  385. func (r *Runtime) regexpproto_toString(call FunctionCall) Value {
  386. obj := r.toObject(call.This)
  387. if this := r.checkStdRegexp(obj); this != nil {
  388. var sb valueStringBuilder
  389. sb.WriteRune('/')
  390. if !this.writeEscapedSource(&sb) {
  391. sb.WriteString(this.source)
  392. }
  393. sb.WriteRune('/')
  394. if this.pattern.global {
  395. sb.WriteRune('g')
  396. }
  397. if this.pattern.ignoreCase {
  398. sb.WriteRune('i')
  399. }
  400. if this.pattern.multiline {
  401. sb.WriteRune('m')
  402. }
  403. if this.pattern.unicode {
  404. sb.WriteRune('u')
  405. }
  406. if this.pattern.sticky {
  407. sb.WriteRune('y')
  408. }
  409. return sb.String()
  410. }
  411. pattern := nilSafe(obj.self.getStr("source", nil)).toString()
  412. flags := nilSafe(obj.self.getStr("flags", nil)).toString()
  413. var sb valueStringBuilder
  414. sb.WriteRune('/')
  415. sb.WriteString(pattern)
  416. sb.WriteRune('/')
  417. sb.WriteString(flags)
  418. return sb.String()
  419. }
  420. func (r *regexpObject) writeEscapedSource(sb *valueStringBuilder) bool {
  421. if r.source.length() == 0 {
  422. sb.WriteString(asciiString("(?:)"))
  423. return true
  424. }
  425. pos := 0
  426. lastPos := 0
  427. rd := &lenientUtf16Decoder{utf16Reader: r.source.utf16Reader(0)}
  428. L:
  429. for {
  430. c, size, err := rd.ReadRune()
  431. if err != nil {
  432. break
  433. }
  434. switch c {
  435. case '\\':
  436. pos++
  437. _, size, err = rd.ReadRune()
  438. if err != nil {
  439. break L
  440. }
  441. case '/', '\u000a', '\u000d', '\u2028', '\u2029':
  442. sb.WriteSubstring(r.source, lastPos, pos)
  443. sb.WriteRune('\\')
  444. switch c {
  445. case '\u000a':
  446. sb.WriteRune('n')
  447. case '\u000d':
  448. sb.WriteRune('r')
  449. default:
  450. sb.WriteRune('u')
  451. sb.WriteRune(rune(hex[c>>12]))
  452. sb.WriteRune(rune(hex[(c>>8)&0xF]))
  453. sb.WriteRune(rune(hex[(c>>4)&0xF]))
  454. sb.WriteRune(rune(hex[c&0xF]))
  455. }
  456. lastPos = pos + size
  457. }
  458. pos += size
  459. }
  460. if lastPos > 0 {
  461. sb.WriteSubstring(r.source, lastPos, r.source.length())
  462. return true
  463. }
  464. return false
  465. }
  466. func (r *Runtime) regexpproto_getSource(call FunctionCall) Value {
  467. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  468. var sb valueStringBuilder
  469. if this.writeEscapedSource(&sb) {
  470. return sb.String()
  471. }
  472. return this.source
  473. } else {
  474. r.typeErrorResult(true, "Method RegExp.prototype.source getter called on incompatible receiver")
  475. return nil
  476. }
  477. }
  478. func (r *Runtime) regexpproto_getGlobal(call FunctionCall) Value {
  479. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  480. if this.pattern.global {
  481. return valueTrue
  482. } else {
  483. return valueFalse
  484. }
  485. } else {
  486. r.typeErrorResult(true, "Method RegExp.prototype.global getter called on incompatible receiver %s", call.This.toString())
  487. return nil
  488. }
  489. }
  490. func (r *Runtime) regexpproto_getMultiline(call FunctionCall) Value {
  491. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  492. if this.pattern.multiline {
  493. return valueTrue
  494. } else {
  495. return valueFalse
  496. }
  497. } else {
  498. r.typeErrorResult(true, "Method RegExp.prototype.multiline getter called on incompatible receiver %s", call.This.toString())
  499. return nil
  500. }
  501. }
  502. func (r *Runtime) regexpproto_getIgnoreCase(call FunctionCall) Value {
  503. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  504. if this.pattern.ignoreCase {
  505. return valueTrue
  506. } else {
  507. return valueFalse
  508. }
  509. } else {
  510. r.typeErrorResult(true, "Method RegExp.prototype.ignoreCase getter called on incompatible receiver %s", call.This.toString())
  511. return nil
  512. }
  513. }
  514. func (r *Runtime) regexpproto_getUnicode(call FunctionCall) Value {
  515. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  516. if this.pattern.unicode {
  517. return valueTrue
  518. } else {
  519. return valueFalse
  520. }
  521. } else {
  522. r.typeErrorResult(true, "Method RegExp.prototype.unicode getter called on incompatible receiver %s", call.This.toString())
  523. return nil
  524. }
  525. }
  526. func (r *Runtime) regexpproto_getSticky(call FunctionCall) Value {
  527. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  528. if this.pattern.sticky {
  529. return valueTrue
  530. } else {
  531. return valueFalse
  532. }
  533. } else {
  534. r.typeErrorResult(true, "Method RegExp.prototype.sticky getter called on incompatible receiver %s", call.This.toString())
  535. return nil
  536. }
  537. }
  538. func (r *Runtime) regexpproto_getFlags(call FunctionCall) Value {
  539. var global, ignoreCase, multiline, sticky, unicode bool
  540. thisObj := r.toObject(call.This)
  541. size := 0
  542. if v := thisObj.self.getStr("global", nil); v != nil {
  543. global = v.ToBoolean()
  544. if global {
  545. size++
  546. }
  547. }
  548. if v := thisObj.self.getStr("ignoreCase", nil); v != nil {
  549. ignoreCase = v.ToBoolean()
  550. if ignoreCase {
  551. size++
  552. }
  553. }
  554. if v := thisObj.self.getStr("multiline", nil); v != nil {
  555. multiline = v.ToBoolean()
  556. if multiline {
  557. size++
  558. }
  559. }
  560. if v := thisObj.self.getStr("sticky", nil); v != nil {
  561. sticky = v.ToBoolean()
  562. if sticky {
  563. size++
  564. }
  565. }
  566. if v := thisObj.self.getStr("unicode", nil); v != nil {
  567. unicode = v.ToBoolean()
  568. if unicode {
  569. size++
  570. }
  571. }
  572. var sb strings.Builder
  573. sb.Grow(size)
  574. if global {
  575. sb.WriteByte('g')
  576. }
  577. if ignoreCase {
  578. sb.WriteByte('i')
  579. }
  580. if multiline {
  581. sb.WriteByte('m')
  582. }
  583. if unicode {
  584. sb.WriteByte('u')
  585. }
  586. if sticky {
  587. sb.WriteByte('y')
  588. }
  589. return asciiString(sb.String())
  590. }
  591. func (r *Runtime) regExpExec(execFn func(FunctionCall) Value, rxObj *Object, arg Value) Value {
  592. res := execFn(FunctionCall{
  593. This: rxObj,
  594. Arguments: []Value{arg},
  595. })
  596. if res != _null {
  597. if _, ok := res.(*Object); !ok {
  598. panic(r.NewTypeError("RegExp exec method returned something other than an Object or null"))
  599. }
  600. }
  601. return res
  602. }
  603. func (r *Runtime) getGlobalRegexpMatches(rxObj *Object, s valueString) []Value {
  604. fullUnicode := nilSafe(rxObj.self.getStr("unicode", nil)).ToBoolean()
  605. rxObj.self.setOwnStr("lastIndex", intToValue(0), true)
  606. execFn, ok := r.toObject(rxObj.self.getStr("exec", nil)).self.assertCallable()
  607. if !ok {
  608. panic(r.NewTypeError("exec is not a function"))
  609. }
  610. var a []Value
  611. for {
  612. res := r.regExpExec(execFn, rxObj, s)
  613. if res == _null {
  614. break
  615. }
  616. a = append(a, res)
  617. matchStr := nilSafe(r.toObject(res).self.getIdx(valueInt(0), nil)).toString()
  618. if matchStr.length() == 0 {
  619. thisIndex := toIntStrict(nilSafe(rxObj.self.getStr("lastIndex", nil)).ToInteger())
  620. rxObj.self.setOwnStr("lastIndex", valueInt(advanceStringIndex(s, thisIndex, fullUnicode)), true)
  621. }
  622. }
  623. return a
  624. }
  625. func (r *Runtime) regexpproto_stdMatcherGeneric(rxObj *Object, s valueString) Value {
  626. rx := rxObj.self
  627. global := rx.getStr("global", nil)
  628. if global != nil && global.ToBoolean() {
  629. a := r.getGlobalRegexpMatches(rxObj, s)
  630. if len(a) == 0 {
  631. return _null
  632. }
  633. ar := make([]Value, 0, len(a))
  634. for _, result := range a {
  635. obj := r.toObject(result)
  636. matchStr := nilSafe(obj.self.getIdx(valueInt(0), nil)).ToString()
  637. ar = append(ar, matchStr)
  638. }
  639. return r.newArrayValues(ar)
  640. }
  641. execFn, ok := r.toObject(rx.getStr("exec", nil)).self.assertCallable()
  642. if !ok {
  643. panic(r.NewTypeError("exec is not a function"))
  644. }
  645. return r.regExpExec(execFn, rxObj, s)
  646. }
  647. func (r *Runtime) checkStdRegexp(rxObj *Object) *regexpObject {
  648. if deoptimiseRegexp {
  649. return nil
  650. }
  651. rx, ok := rxObj.self.(*regexpObject)
  652. if !ok {
  653. return nil
  654. }
  655. if !rx.standard || rx.prototype == nil || rx.prototype.self != r.global.stdRegexpProto {
  656. return nil
  657. }
  658. return rx
  659. }
  660. func (r *Runtime) regexpproto_stdMatcher(call FunctionCall) Value {
  661. thisObj := r.toObject(call.This)
  662. s := call.Argument(0).toString()
  663. rx := r.checkStdRegexp(thisObj)
  664. if rx == nil {
  665. return r.regexpproto_stdMatcherGeneric(thisObj, s)
  666. }
  667. if rx.pattern.global {
  668. res := rx.pattern.findAllSubmatchIndex(s, 0, -1, rx.pattern.sticky)
  669. if len(res) == 0 {
  670. rx.setOwnStr("lastIndex", intToValue(0), true)
  671. return _null
  672. }
  673. a := make([]Value, 0, len(res))
  674. for _, result := range res {
  675. a = append(a, s.substring(result[0], result[1]))
  676. }
  677. rx.setOwnStr("lastIndex", intToValue(int64(res[len(res)-1][1])), true)
  678. return r.newArrayValues(a)
  679. } else {
  680. return rx.exec(s)
  681. }
  682. }
  683. func (r *Runtime) regexpproto_stdSearchGeneric(rxObj *Object, arg valueString) Value {
  684. rx := rxObj.self
  685. previousLastIndex := nilSafe(rx.getStr("lastIndex", nil))
  686. zero := intToValue(0)
  687. if !previousLastIndex.SameAs(zero) {
  688. rx.setOwnStr("lastIndex", zero, true)
  689. }
  690. execFn, ok := r.toObject(rx.getStr("exec", nil)).self.assertCallable()
  691. if !ok {
  692. panic(r.NewTypeError("exec is not a function"))
  693. }
  694. result := r.regExpExec(execFn, rxObj, arg)
  695. currentLastIndex := nilSafe(rx.getStr("lastIndex", nil))
  696. if !currentLastIndex.SameAs(previousLastIndex) {
  697. rx.setOwnStr("lastIndex", previousLastIndex, true)
  698. }
  699. if result == _null {
  700. return intToValue(-1)
  701. }
  702. return r.toObject(result).self.getStr("index", nil)
  703. }
  704. func (r *Runtime) regexpproto_stdSearch(call FunctionCall) Value {
  705. thisObj := r.toObject(call.This)
  706. s := call.Argument(0).toString()
  707. rx := r.checkStdRegexp(thisObj)
  708. if rx == nil {
  709. return r.regexpproto_stdSearchGeneric(thisObj, s)
  710. }
  711. previousLastIndex := rx.getStr("lastIndex", nil)
  712. rx.setOwnStr("lastIndex", intToValue(0), true)
  713. match, result := rx.execRegexp(s)
  714. rx.setOwnStr("lastIndex", previousLastIndex, true)
  715. if !match {
  716. return intToValue(-1)
  717. }
  718. return intToValue(int64(result[0]))
  719. }
  720. func (r *Runtime) regexpproto_stdSplitterGeneric(splitter *Object, s valueString, limit Value, unicodeMatching bool) Value {
  721. var a []Value
  722. var lim int64
  723. if limit == nil || limit == _undefined {
  724. lim = maxInt - 1
  725. } else {
  726. lim = toLength(limit)
  727. }
  728. if lim == 0 {
  729. return r.newArrayValues(a)
  730. }
  731. size := s.length()
  732. p := 0
  733. execFn := toMethod(splitter.ToObject(r).self.getStr("exec", nil)) // must be non-nil
  734. if size == 0 {
  735. if r.regExpExec(execFn, splitter, s) == _null {
  736. a = append(a, s)
  737. }
  738. return r.newArrayValues(a)
  739. }
  740. q := p
  741. for q < size {
  742. splitter.self.setOwnStr("lastIndex", intToValue(int64(q)), true)
  743. z := r.regExpExec(execFn, splitter, s)
  744. if z == _null {
  745. q = advanceStringIndex(s, q, unicodeMatching)
  746. } else {
  747. z := r.toObject(z)
  748. e := toLength(splitter.self.getStr("lastIndex", nil))
  749. if e == int64(p) {
  750. q = advanceStringIndex(s, q, unicodeMatching)
  751. } else {
  752. a = append(a, s.substring(p, q))
  753. if int64(len(a)) == lim {
  754. return r.newArrayValues(a)
  755. }
  756. if e > int64(size) {
  757. p = size
  758. } else {
  759. p = int(e)
  760. }
  761. numberOfCaptures := max(toLength(z.self.getStr("length", nil))-1, 0)
  762. for i := int64(1); i <= numberOfCaptures; i++ {
  763. a = append(a, z.self.getIdx(valueInt(i), nil))
  764. if int64(len(a)) == lim {
  765. return r.newArrayValues(a)
  766. }
  767. }
  768. q = p
  769. }
  770. }
  771. }
  772. a = append(a, s.substring(p, size))
  773. return r.newArrayValues(a)
  774. }
  775. func advanceStringIndex(s valueString, pos int, unicode bool) int {
  776. next := pos + 1
  777. if !unicode {
  778. return next
  779. }
  780. l := s.length()
  781. if next >= l {
  782. return next
  783. }
  784. if !isUTF16FirstSurrogate(s.charAt(pos)) {
  785. return next
  786. }
  787. if !isUTF16SecondSurrogate(s.charAt(next)) {
  788. return next
  789. }
  790. return next + 1
  791. }
  792. func (r *Runtime) regexpproto_stdSplitter(call FunctionCall) Value {
  793. rxObj := r.toObject(call.This)
  794. s := call.Argument(0).toString()
  795. limitValue := call.Argument(1)
  796. var splitter *Object
  797. search := r.checkStdRegexp(rxObj)
  798. c := r.speciesConstructorObj(rxObj, r.global.RegExp)
  799. if search == nil || c != r.global.RegExp {
  800. flags := nilSafe(rxObj.self.getStr("flags", nil)).toString()
  801. flagsStr := flags.String()
  802. // Add 'y' flag if missing
  803. if !strings.Contains(flagsStr, "y") {
  804. flags = flags.concat(asciiString("y"))
  805. }
  806. splitter = r.toConstructor(c)([]Value{rxObj, flags}, nil)
  807. search = r.checkStdRegexp(splitter)
  808. if search == nil {
  809. return r.regexpproto_stdSplitterGeneric(splitter, s, limitValue, strings.Contains(flagsStr, "u"))
  810. }
  811. }
  812. limit := -1
  813. if limitValue != _undefined {
  814. limit = int(toUint32(limitValue))
  815. }
  816. if limit == 0 {
  817. return r.newArrayValues(nil)
  818. }
  819. targetLength := s.length()
  820. var valueArray []Value
  821. lastIndex := 0
  822. found := 0
  823. result := search.pattern.findAllSubmatchIndex(s, 0, -1, false)
  824. if targetLength == 0 {
  825. if result == nil {
  826. valueArray = append(valueArray, s)
  827. }
  828. goto RETURN
  829. }
  830. for _, match := range result {
  831. if match[0] == match[1] {
  832. // FIXME Ugh, this is a hack
  833. if match[0] == 0 || match[0] == targetLength {
  834. continue
  835. }
  836. }
  837. if lastIndex != match[0] {
  838. valueArray = append(valueArray, s.substring(lastIndex, match[0]))
  839. found++
  840. } else if lastIndex == match[0] {
  841. if lastIndex != -1 {
  842. valueArray = append(valueArray, stringEmpty)
  843. found++
  844. }
  845. }
  846. lastIndex = match[1]
  847. if found == limit {
  848. goto RETURN
  849. }
  850. captureCount := len(match) / 2
  851. for index := 1; index < captureCount; index++ {
  852. offset := index * 2
  853. var value Value
  854. if match[offset] != -1 {
  855. value = s.substring(match[offset], match[offset+1])
  856. } else {
  857. value = _undefined
  858. }
  859. valueArray = append(valueArray, value)
  860. found++
  861. if found == limit {
  862. goto RETURN
  863. }
  864. }
  865. }
  866. if found != limit {
  867. if lastIndex != targetLength {
  868. valueArray = append(valueArray, s.substring(lastIndex, targetLength))
  869. } else {
  870. valueArray = append(valueArray, stringEmpty)
  871. }
  872. }
  873. RETURN:
  874. return r.newArrayValues(valueArray)
  875. }
  876. func (r *Runtime) regexpproto_stdReplacerGeneric(rxObj *Object, s, replaceStr valueString, rcall func(FunctionCall) Value) Value {
  877. var results []Value
  878. if nilSafe(rxObj.self.getStr("global", nil)).ToBoolean() {
  879. results = r.getGlobalRegexpMatches(rxObj, s)
  880. } else {
  881. execFn := toMethod(rxObj.self.getStr("exec", nil)) // must be non-nil
  882. result := r.regExpExec(execFn, rxObj, s)
  883. if result != _null {
  884. results = append(results, result)
  885. }
  886. }
  887. lengthS := s.length()
  888. nextSourcePosition := 0
  889. var resultBuf valueStringBuilder
  890. for _, result := range results {
  891. obj := r.toObject(result)
  892. nCaptures := max(toLength(obj.self.getStr("length", nil))-1, 0)
  893. matched := nilSafe(obj.self.getIdx(valueInt(0), nil)).toString()
  894. matchLength := matched.length()
  895. position := toIntStrict(max(min(nilSafe(obj.self.getStr("index", nil)).ToInteger(), int64(lengthS)), 0))
  896. var captures []Value
  897. if rcall != nil {
  898. captures = make([]Value, 0, nCaptures+3)
  899. } else {
  900. captures = make([]Value, 0, nCaptures+1)
  901. }
  902. captures = append(captures, matched)
  903. for n := int64(1); n <= nCaptures; n++ {
  904. capN := nilSafe(obj.self.getIdx(valueInt(n), nil))
  905. if capN != _undefined {
  906. capN = capN.ToString()
  907. }
  908. captures = append(captures, capN)
  909. }
  910. var replacement valueString
  911. if rcall != nil {
  912. captures = append(captures, intToValue(int64(position)), s)
  913. replacement = rcall(FunctionCall{
  914. This: _undefined,
  915. Arguments: captures,
  916. }).toString()
  917. if position >= nextSourcePosition {
  918. resultBuf.WriteString(s.substring(nextSourcePosition, position))
  919. resultBuf.WriteString(replacement)
  920. nextSourcePosition = position + matchLength
  921. }
  922. } else {
  923. if position >= nextSourcePosition {
  924. resultBuf.WriteString(s.substring(nextSourcePosition, position))
  925. writeSubstitution(s, position, len(captures), func(idx int) valueString {
  926. capture := captures[idx]
  927. if capture != _undefined {
  928. return capture.toString()
  929. }
  930. return stringEmpty
  931. }, replaceStr, &resultBuf)
  932. nextSourcePosition = position + matchLength
  933. }
  934. }
  935. }
  936. if nextSourcePosition < lengthS {
  937. resultBuf.WriteString(s.substring(nextSourcePosition, lengthS))
  938. }
  939. return resultBuf.String()
  940. }
  941. func writeSubstitution(s valueString, position int, numCaptures int, getCapture func(int) valueString, replaceStr valueString, buf *valueStringBuilder) {
  942. l := s.length()
  943. rl := replaceStr.length()
  944. matched := getCapture(0)
  945. tailPos := position + matched.length()
  946. for i := 0; i < rl; i++ {
  947. c := replaceStr.charAt(i)
  948. if c == '$' && i < rl-1 {
  949. ch := replaceStr.charAt(i + 1)
  950. switch ch {
  951. case '$':
  952. buf.WriteRune('$')
  953. case '`':
  954. buf.WriteString(s.substring(0, position))
  955. case '\'':
  956. if tailPos < l {
  957. buf.WriteString(s.substring(tailPos, l))
  958. }
  959. case '&':
  960. buf.WriteString(matched)
  961. default:
  962. matchNumber := 0
  963. j := i + 1
  964. for j < rl {
  965. ch := replaceStr.charAt(j)
  966. if ch >= '0' && ch <= '9' {
  967. m := matchNumber*10 + int(ch-'0')
  968. if m >= numCaptures {
  969. break
  970. }
  971. matchNumber = m
  972. j++
  973. } else {
  974. break
  975. }
  976. }
  977. if matchNumber > 0 {
  978. buf.WriteString(getCapture(matchNumber))
  979. i = j - 1
  980. continue
  981. } else {
  982. buf.WriteRune('$')
  983. buf.WriteRune(ch)
  984. }
  985. }
  986. i++
  987. } else {
  988. buf.WriteRune(c)
  989. }
  990. }
  991. }
  992. func (r *Runtime) regexpproto_stdReplacer(call FunctionCall) Value {
  993. rxObj := r.toObject(call.This)
  994. s := call.Argument(0).toString()
  995. replaceStr, rcall := getReplaceValue(call.Argument(1))
  996. rx := r.checkStdRegexp(rxObj)
  997. if rx == nil {
  998. return r.regexpproto_stdReplacerGeneric(rxObj, s, replaceStr, rcall)
  999. }
  1000. var index int64
  1001. find := 1
  1002. if rx.pattern.global {
  1003. find = -1
  1004. rx.setOwnStr("lastIndex", intToValue(0), true)
  1005. } else {
  1006. index = rx.getLastIndex()
  1007. }
  1008. found := rx.pattern.findAllSubmatchIndex(s, toIntStrict(index), find, rx.pattern.sticky)
  1009. if len(found) > 0 {
  1010. if !rx.updateLastIndex(index, found[0], found[len(found)-1]) {
  1011. found = nil
  1012. }
  1013. } else {
  1014. rx.updateLastIndex(index, nil, nil)
  1015. }
  1016. return stringReplace(s, found, replaceStr, rcall)
  1017. }
  1018. func (r *Runtime) initRegExp() {
  1019. o := r.newGuardedObject(r.global.ObjectPrototype, classObject)
  1020. r.global.RegExpPrototype = o.val
  1021. r.global.stdRegexpProto = o
  1022. o._putProp("compile", r.newNativeFunc(r.regexpproto_compile, nil, "compile", nil, 2), true, false, true)
  1023. o._putProp("exec", r.newNativeFunc(r.regexpproto_exec, nil, "exec", nil, 1), true, false, true)
  1024. o._putProp("test", r.newNativeFunc(r.regexpproto_test, nil, "test", nil, 1), true, false, true)
  1025. o._putProp("toString", r.newNativeFunc(r.regexpproto_toString, nil, "toString", nil, 0), true, false, true)
  1026. o.setOwnStr("source", &valueProperty{
  1027. configurable: true,
  1028. getterFunc: r.newNativeFunc(r.regexpproto_getSource, nil, "get source", nil, 0),
  1029. accessor: true,
  1030. }, false)
  1031. o.setOwnStr("global", &valueProperty{
  1032. configurable: true,
  1033. getterFunc: r.newNativeFunc(r.regexpproto_getGlobal, nil, "get global", nil, 0),
  1034. accessor: true,
  1035. }, false)
  1036. o.setOwnStr("multiline", &valueProperty{
  1037. configurable: true,
  1038. getterFunc: r.newNativeFunc(r.regexpproto_getMultiline, nil, "get multiline", nil, 0),
  1039. accessor: true,
  1040. }, false)
  1041. o.setOwnStr("ignoreCase", &valueProperty{
  1042. configurable: true,
  1043. getterFunc: r.newNativeFunc(r.regexpproto_getIgnoreCase, nil, "get ignoreCase", nil, 0),
  1044. accessor: true,
  1045. }, false)
  1046. o.setOwnStr("unicode", &valueProperty{
  1047. configurable: true,
  1048. getterFunc: r.newNativeFunc(r.regexpproto_getUnicode, nil, "get unicode", nil, 0),
  1049. accessor: true,
  1050. }, false)
  1051. o.setOwnStr("sticky", &valueProperty{
  1052. configurable: true,
  1053. getterFunc: r.newNativeFunc(r.regexpproto_getSticky, nil, "get sticky", nil, 0),
  1054. accessor: true,
  1055. }, false)
  1056. o.setOwnStr("flags", &valueProperty{
  1057. configurable: true,
  1058. getterFunc: r.newNativeFunc(r.regexpproto_getFlags, nil, "get flags", nil, 0),
  1059. accessor: true,
  1060. }, false)
  1061. o._putSym(SymMatch, valueProp(r.newNativeFunc(r.regexpproto_stdMatcher, nil, "[Symbol.match]", nil, 1), true, false, true))
  1062. o._putSym(SymSearch, valueProp(r.newNativeFunc(r.regexpproto_stdSearch, nil, "[Symbol.search]", nil, 1), true, false, true))
  1063. o._putSym(SymSplit, valueProp(r.newNativeFunc(r.regexpproto_stdSplitter, nil, "[Symbol.split]", nil, 2), true, false, true))
  1064. o._putSym(SymReplace, valueProp(r.newNativeFunc(r.regexpproto_stdReplacer, nil, "[Symbol.replace]", nil, 2), true, false, true))
  1065. o.guard("exec", "global", "multiline", "ignoreCase", "unicode", "sticky")
  1066. r.global.RegExp = r.newNativeFunc(r.builtin_RegExp, r.builtin_newRegExp, "RegExp", r.global.RegExpPrototype, 2)
  1067. rx := r.global.RegExp.self
  1068. rx._putSym(SymSpecies, &valueProperty{
  1069. getterFunc: r.newNativeFunc(r.returnThis, nil, "get [Symbol.species]", nil, 0),
  1070. accessor: true,
  1071. configurable: true,
  1072. })
  1073. r.addToGlobal("RegExp", r.global.RegExp)
  1074. }