builtin_regexp.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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 isRegexp(patternVal) { // this may have side effects so need to call it anyway
  281. if obj, ok := patternVal.(*Object); ok {
  282. if rx, ok := obj.self.(*regexpObject); ok {
  283. if flagsVal == nil || flagsVal == _undefined {
  284. return rx.clone()
  285. } else {
  286. return r._newRegExp(rx.source, flagsVal.toString().String(), proto)
  287. }
  288. } else {
  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 := toLength(rxObj.self.getStr("lastIndex", nil))
  620. rxObj.self.setOwnStr("lastIndex", valueInt(advanceStringIndex64(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 advanceStringIndex64(s valueString, pos int64, unicode bool) int64 {
  793. next := pos + 1
  794. if !unicode {
  795. return next
  796. }
  797. l := int64(s.length())
  798. if next >= l {
  799. return next
  800. }
  801. if !isUTF16FirstSurrogate(s.charAt(int(pos))) {
  802. return next
  803. }
  804. if !isUTF16SecondSurrogate(s.charAt(int(next))) {
  805. return next
  806. }
  807. return next + 1
  808. }
  809. func (r *Runtime) regexpproto_stdSplitter(call FunctionCall) Value {
  810. rxObj := r.toObject(call.This)
  811. s := call.Argument(0).toString()
  812. limitValue := call.Argument(1)
  813. var splitter *Object
  814. search := r.checkStdRegexp(rxObj)
  815. c := r.speciesConstructorObj(rxObj, r.global.RegExp)
  816. if search == nil || c != r.global.RegExp {
  817. flags := nilSafe(rxObj.self.getStr("flags", nil)).toString()
  818. flagsStr := flags.String()
  819. // Add 'y' flag if missing
  820. if !strings.Contains(flagsStr, "y") {
  821. flags = flags.concat(asciiString("y"))
  822. }
  823. splitter = r.toConstructor(c)([]Value{rxObj, flags}, nil)
  824. search = r.checkStdRegexp(splitter)
  825. if search == nil {
  826. return r.regexpproto_stdSplitterGeneric(splitter, s, limitValue, strings.Contains(flagsStr, "u"))
  827. }
  828. }
  829. pattern := search.pattern // toUint32() may recompile the pattern, but we still need to use the original
  830. limit := -1
  831. if limitValue != _undefined {
  832. limit = int(toUint32(limitValue))
  833. }
  834. if limit == 0 {
  835. return r.newArrayValues(nil)
  836. }
  837. targetLength := s.length()
  838. var valueArray []Value
  839. lastIndex := 0
  840. found := 0
  841. result := pattern.findAllSubmatchIndex(s, 0, -1, false)
  842. if targetLength == 0 {
  843. if result == nil {
  844. valueArray = append(valueArray, s)
  845. }
  846. goto RETURN
  847. }
  848. for _, match := range result {
  849. if match[0] == match[1] {
  850. // FIXME Ugh, this is a hack
  851. if match[0] == 0 || match[0] == targetLength {
  852. continue
  853. }
  854. }
  855. if lastIndex != match[0] {
  856. valueArray = append(valueArray, s.substring(lastIndex, match[0]))
  857. found++
  858. } else if lastIndex == match[0] {
  859. if lastIndex != -1 {
  860. valueArray = append(valueArray, stringEmpty)
  861. found++
  862. }
  863. }
  864. lastIndex = match[1]
  865. if found == limit {
  866. goto RETURN
  867. }
  868. captureCount := len(match) / 2
  869. for index := 1; index < captureCount; index++ {
  870. offset := index * 2
  871. var value Value
  872. if match[offset] != -1 {
  873. value = s.substring(match[offset], match[offset+1])
  874. } else {
  875. value = _undefined
  876. }
  877. valueArray = append(valueArray, value)
  878. found++
  879. if found == limit {
  880. goto RETURN
  881. }
  882. }
  883. }
  884. if found != limit {
  885. if lastIndex != targetLength {
  886. valueArray = append(valueArray, s.substring(lastIndex, targetLength))
  887. } else {
  888. valueArray = append(valueArray, stringEmpty)
  889. }
  890. }
  891. RETURN:
  892. return r.newArrayValues(valueArray)
  893. }
  894. func (r *Runtime) regexpproto_stdReplacerGeneric(rxObj *Object, s, replaceStr valueString, rcall func(FunctionCall) Value) Value {
  895. var results []Value
  896. if nilSafe(rxObj.self.getStr("global", nil)).ToBoolean() {
  897. results = r.getGlobalRegexpMatches(rxObj, s)
  898. } else {
  899. execFn := toMethod(rxObj.self.getStr("exec", nil)) // must be non-nil
  900. result := r.regExpExec(execFn, rxObj, s)
  901. if result != _null {
  902. results = append(results, result)
  903. }
  904. }
  905. lengthS := s.length()
  906. nextSourcePosition := 0
  907. var resultBuf valueStringBuilder
  908. for _, result := range results {
  909. obj := r.toObject(result)
  910. nCaptures := max(toLength(obj.self.getStr("length", nil))-1, 0)
  911. matched := nilSafe(obj.self.getIdx(valueInt(0), nil)).toString()
  912. matchLength := matched.length()
  913. position := toIntStrict(max(min(nilSafe(obj.self.getStr("index", nil)).ToInteger(), int64(lengthS)), 0))
  914. var captures []Value
  915. if rcall != nil {
  916. captures = make([]Value, 0, nCaptures+3)
  917. } else {
  918. captures = make([]Value, 0, nCaptures+1)
  919. }
  920. captures = append(captures, matched)
  921. for n := int64(1); n <= nCaptures; n++ {
  922. capN := nilSafe(obj.self.getIdx(valueInt(n), nil))
  923. if capN != _undefined {
  924. capN = capN.ToString()
  925. }
  926. captures = append(captures, capN)
  927. }
  928. var replacement valueString
  929. if rcall != nil {
  930. captures = append(captures, intToValue(int64(position)), s)
  931. replacement = rcall(FunctionCall{
  932. This: _undefined,
  933. Arguments: captures,
  934. }).toString()
  935. if position >= nextSourcePosition {
  936. resultBuf.WriteString(s.substring(nextSourcePosition, position))
  937. resultBuf.WriteString(replacement)
  938. nextSourcePosition = position + matchLength
  939. }
  940. } else {
  941. if position >= nextSourcePosition {
  942. resultBuf.WriteString(s.substring(nextSourcePosition, position))
  943. writeSubstitution(s, position, len(captures), func(idx int) valueString {
  944. capture := captures[idx]
  945. if capture != _undefined {
  946. return capture.toString()
  947. }
  948. return stringEmpty
  949. }, replaceStr, &resultBuf)
  950. nextSourcePosition = position + matchLength
  951. }
  952. }
  953. }
  954. if nextSourcePosition < lengthS {
  955. resultBuf.WriteString(s.substring(nextSourcePosition, lengthS))
  956. }
  957. return resultBuf.String()
  958. }
  959. func writeSubstitution(s valueString, position int, numCaptures int, getCapture func(int) valueString, replaceStr valueString, buf *valueStringBuilder) {
  960. l := s.length()
  961. rl := replaceStr.length()
  962. matched := getCapture(0)
  963. tailPos := position + matched.length()
  964. for i := 0; i < rl; i++ {
  965. c := replaceStr.charAt(i)
  966. if c == '$' && i < rl-1 {
  967. ch := replaceStr.charAt(i + 1)
  968. switch ch {
  969. case '$':
  970. buf.WriteRune('$')
  971. case '`':
  972. buf.WriteString(s.substring(0, position))
  973. case '\'':
  974. if tailPos < l {
  975. buf.WriteString(s.substring(tailPos, l))
  976. }
  977. case '&':
  978. buf.WriteString(matched)
  979. default:
  980. matchNumber := 0
  981. j := i + 1
  982. for j < rl {
  983. ch := replaceStr.charAt(j)
  984. if ch >= '0' && ch <= '9' {
  985. m := matchNumber*10 + int(ch-'0')
  986. if m >= numCaptures {
  987. break
  988. }
  989. matchNumber = m
  990. j++
  991. } else {
  992. break
  993. }
  994. }
  995. if matchNumber > 0 {
  996. buf.WriteString(getCapture(matchNumber))
  997. i = j - 1
  998. continue
  999. } else {
  1000. buf.WriteRune('$')
  1001. buf.WriteRune(ch)
  1002. }
  1003. }
  1004. i++
  1005. } else {
  1006. buf.WriteRune(c)
  1007. }
  1008. }
  1009. }
  1010. func (r *Runtime) regexpproto_stdReplacer(call FunctionCall) Value {
  1011. rxObj := r.toObject(call.This)
  1012. s := call.Argument(0).toString()
  1013. replaceStr, rcall := getReplaceValue(call.Argument(1))
  1014. rx := r.checkStdRegexp(rxObj)
  1015. if rx == nil {
  1016. return r.regexpproto_stdReplacerGeneric(rxObj, s, replaceStr, rcall)
  1017. }
  1018. var index int64
  1019. find := 1
  1020. if rx.pattern.global {
  1021. find = -1
  1022. rx.setOwnStr("lastIndex", intToValue(0), true)
  1023. } else {
  1024. index = rx.getLastIndex()
  1025. }
  1026. found := rx.pattern.findAllSubmatchIndex(s, toIntStrict(index), find, rx.pattern.sticky)
  1027. if len(found) > 0 {
  1028. if !rx.updateLastIndex(index, found[0], found[len(found)-1]) {
  1029. found = nil
  1030. }
  1031. } else {
  1032. rx.updateLastIndex(index, nil, nil)
  1033. }
  1034. return stringReplace(s, found, replaceStr, rcall)
  1035. }
  1036. func (r *Runtime) initRegExp() {
  1037. o := r.newGuardedObject(r.global.ObjectPrototype, classObject)
  1038. r.global.RegExpPrototype = o.val
  1039. r.global.stdRegexpProto = o
  1040. o._putProp("compile", r.newNativeFunc(r.regexpproto_compile, nil, "compile", nil, 2), true, false, true)
  1041. o._putProp("exec", r.newNativeFunc(r.regexpproto_exec, nil, "exec", nil, 1), true, false, true)
  1042. o._putProp("test", r.newNativeFunc(r.regexpproto_test, nil, "test", nil, 1), true, false, true)
  1043. o._putProp("toString", r.newNativeFunc(r.regexpproto_toString, nil, "toString", nil, 0), true, false, true)
  1044. o.setOwnStr("source", &valueProperty{
  1045. configurable: true,
  1046. getterFunc: r.newNativeFunc(r.regexpproto_getSource, nil, "get source", nil, 0),
  1047. accessor: true,
  1048. }, false)
  1049. o.setOwnStr("global", &valueProperty{
  1050. configurable: true,
  1051. getterFunc: r.newNativeFunc(r.regexpproto_getGlobal, nil, "get global", nil, 0),
  1052. accessor: true,
  1053. }, false)
  1054. o.setOwnStr("multiline", &valueProperty{
  1055. configurable: true,
  1056. getterFunc: r.newNativeFunc(r.regexpproto_getMultiline, nil, "get multiline", nil, 0),
  1057. accessor: true,
  1058. }, false)
  1059. o.setOwnStr("ignoreCase", &valueProperty{
  1060. configurable: true,
  1061. getterFunc: r.newNativeFunc(r.regexpproto_getIgnoreCase, nil, "get ignoreCase", nil, 0),
  1062. accessor: true,
  1063. }, false)
  1064. o.setOwnStr("unicode", &valueProperty{
  1065. configurable: true,
  1066. getterFunc: r.newNativeFunc(r.regexpproto_getUnicode, nil, "get unicode", nil, 0),
  1067. accessor: true,
  1068. }, false)
  1069. o.setOwnStr("sticky", &valueProperty{
  1070. configurable: true,
  1071. getterFunc: r.newNativeFunc(r.regexpproto_getSticky, nil, "get sticky", nil, 0),
  1072. accessor: true,
  1073. }, false)
  1074. o.setOwnStr("flags", &valueProperty{
  1075. configurable: true,
  1076. getterFunc: r.newNativeFunc(r.regexpproto_getFlags, nil, "get flags", nil, 0),
  1077. accessor: true,
  1078. }, false)
  1079. o._putSym(SymMatch, valueProp(r.newNativeFunc(r.regexpproto_stdMatcher, nil, "[Symbol.match]", nil, 1), true, false, true))
  1080. o._putSym(SymSearch, valueProp(r.newNativeFunc(r.regexpproto_stdSearch, nil, "[Symbol.search]", nil, 1), true, false, true))
  1081. o._putSym(SymSplit, valueProp(r.newNativeFunc(r.regexpproto_stdSplitter, nil, "[Symbol.split]", nil, 2), true, false, true))
  1082. o._putSym(SymReplace, valueProp(r.newNativeFunc(r.regexpproto_stdReplacer, nil, "[Symbol.replace]", nil, 2), true, false, true))
  1083. o.guard("exec", "global", "multiline", "ignoreCase", "unicode", "sticky")
  1084. r.global.RegExp = r.newNativeFunc(r.builtin_RegExp, r.builtin_newRegExp, "RegExp", r.global.RegExpPrototype, 2)
  1085. rx := r.global.RegExp.self
  1086. rx._putSym(SymSpecies, &valueProperty{
  1087. getterFunc: r.newNativeFunc(r.returnThis, nil, "get [Symbol.species]", nil, 0),
  1088. accessor: true,
  1089. configurable: true,
  1090. })
  1091. r.addToGlobal("RegExp", r.global.RegExp)
  1092. }