builtin_regexp.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. wrapper2, err = compileRegexp2(patternStr, multiline, ignoreCase)
  239. if err != nil {
  240. err = fmt.Errorf("Invalid regular expression (regexp2): %s (%v)", patternStr, err1)
  241. }
  242. }
  243. p = &regexpPattern{
  244. src: patternStr,
  245. regexpWrapper: wrapper,
  246. regexp2Wrapper: wrapper2,
  247. global: global,
  248. ignoreCase: ignoreCase,
  249. multiline: multiline,
  250. sticky: sticky,
  251. unicode: unicode,
  252. }
  253. return
  254. }
  255. func (r *Runtime) _newRegExp(patternStr valueString, flags string, proto *Object) *Object {
  256. pattern, err := compileRegexpFromValueString(patternStr, flags)
  257. if err != nil {
  258. panic(r.newSyntaxError(err.Error(), -1))
  259. }
  260. return r.newRegExpp(pattern, patternStr, proto)
  261. }
  262. func (r *Runtime) builtin_newRegExp(args []Value, proto *Object) *Object {
  263. var patternVal, flagsVal Value
  264. if len(args) > 0 {
  265. patternVal = args[0]
  266. }
  267. if len(args) > 1 {
  268. flagsVal = args[1]
  269. }
  270. return r.newRegExp(patternVal, flagsVal, proto)
  271. }
  272. func (r *Runtime) newRegExp(patternVal, flagsVal Value, proto *Object) *Object {
  273. var pattern valueString
  274. var flags string
  275. if obj, ok := patternVal.(*Object); ok {
  276. if rx, ok := obj.self.(*regexpObject); ok {
  277. if flagsVal == nil || flagsVal == _undefined {
  278. return rx.clone()
  279. } else {
  280. return r._newRegExp(rx.source, flagsVal.toString().String(), proto)
  281. }
  282. } else {
  283. if isRegexp(patternVal) {
  284. pattern = nilSafe(obj.self.getStr("source", nil)).toString()
  285. if flagsVal == nil || flagsVal == _undefined {
  286. flags = nilSafe(obj.self.getStr("flags", nil)).toString().String()
  287. } else {
  288. flags = flagsVal.toString().String()
  289. }
  290. goto exit
  291. }
  292. }
  293. }
  294. if patternVal != nil && patternVal != _undefined {
  295. pattern = patternVal.toString()
  296. }
  297. if flagsVal != nil && flagsVal != _undefined {
  298. flags = flagsVal.toString().String()
  299. }
  300. if pattern == nil {
  301. pattern = stringEmpty
  302. }
  303. exit:
  304. return r._newRegExp(pattern, flags, proto)
  305. }
  306. func (r *Runtime) builtin_RegExp(call FunctionCall) Value {
  307. pattern := call.Argument(0)
  308. patternIsRegExp := isRegexp(pattern)
  309. flags := call.Argument(1)
  310. if patternIsRegExp && flags == _undefined {
  311. if obj, ok := call.Argument(0).(*Object); ok {
  312. patternConstructor := obj.self.getStr("constructor", nil)
  313. if patternConstructor == r.global.RegExp {
  314. return pattern
  315. }
  316. }
  317. }
  318. return r.newRegExp(pattern, flags, r.global.RegExpPrototype)
  319. }
  320. func (r *Runtime) regexpproto_compile(call FunctionCall) Value {
  321. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  322. var (
  323. pattern *regexpPattern
  324. source valueString
  325. flags string
  326. err error
  327. )
  328. patternVal := call.Argument(0)
  329. flagsVal := call.Argument(1)
  330. if o, ok := patternVal.(*Object); ok {
  331. if p, ok := o.self.(*regexpObject); ok {
  332. if flagsVal != _undefined {
  333. panic(r.NewTypeError("Cannot supply flags when constructing one RegExp from another"))
  334. }
  335. this.pattern = p.pattern
  336. this.source = p.source
  337. goto exit
  338. }
  339. }
  340. if patternVal != _undefined {
  341. source = patternVal.toString()
  342. } else {
  343. source = stringEmpty
  344. }
  345. if flagsVal != _undefined {
  346. flags = flagsVal.toString().String()
  347. }
  348. pattern, err = compileRegexpFromValueString(source, flags)
  349. if err != nil {
  350. panic(r.newSyntaxError(err.Error(), -1))
  351. }
  352. this.pattern = pattern
  353. this.source = source
  354. exit:
  355. this.setOwnStr("lastIndex", intToValue(0), true)
  356. return call.This
  357. }
  358. panic(r.NewTypeError("Method RegExp.prototype.compile called on incompatible receiver %s", call.This.toString()))
  359. }
  360. func (r *Runtime) regexpproto_exec(call FunctionCall) Value {
  361. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  362. return this.exec(call.Argument(0).toString())
  363. } else {
  364. r.typeErrorResult(true, "Method RegExp.prototype.exec called on incompatible receiver %s", call.This.toString())
  365. return nil
  366. }
  367. }
  368. func (r *Runtime) regexpproto_test(call FunctionCall) Value {
  369. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  370. if this.test(call.Argument(0).toString()) {
  371. return valueTrue
  372. } else {
  373. return valueFalse
  374. }
  375. } else {
  376. r.typeErrorResult(true, "Method RegExp.prototype.test called on incompatible receiver %s", call.This.toString())
  377. return nil
  378. }
  379. }
  380. func (r *Runtime) regexpproto_toString(call FunctionCall) Value {
  381. obj := r.toObject(call.This)
  382. if this := r.checkStdRegexp(obj); this != nil {
  383. var sb valueStringBuilder
  384. sb.WriteRune('/')
  385. if !this.writeEscapedSource(&sb) {
  386. sb.WriteString(this.source)
  387. }
  388. sb.WriteRune('/')
  389. if this.pattern.global {
  390. sb.WriteRune('g')
  391. }
  392. if this.pattern.ignoreCase {
  393. sb.WriteRune('i')
  394. }
  395. if this.pattern.multiline {
  396. sb.WriteRune('m')
  397. }
  398. if this.pattern.unicode {
  399. sb.WriteRune('u')
  400. }
  401. if this.pattern.sticky {
  402. sb.WriteRune('y')
  403. }
  404. return sb.String()
  405. }
  406. pattern := nilSafe(obj.self.getStr("source", nil)).toString()
  407. flags := nilSafe(obj.self.getStr("flags", nil)).toString()
  408. var sb valueStringBuilder
  409. sb.WriteRune('/')
  410. sb.WriteString(pattern)
  411. sb.WriteRune('/')
  412. sb.WriteString(flags)
  413. return sb.String()
  414. }
  415. func (r *regexpObject) writeEscapedSource(sb *valueStringBuilder) bool {
  416. if r.source.length() == 0 {
  417. sb.WriteString(asciiString("(?:)"))
  418. return true
  419. }
  420. pos := 0
  421. lastPos := 0
  422. rd := &lenientUtf16Decoder{utf16Reader: r.source.utf16Reader(0)}
  423. for {
  424. c, size, err := rd.ReadRune()
  425. if err != nil {
  426. break
  427. }
  428. switch c {
  429. case '/', '\u000a', '\u000d', '\u2028', '\u2029':
  430. sb.WriteSubstring(r.source, lastPos, pos)
  431. sb.WriteRune('\\')
  432. switch c {
  433. case '\u000a':
  434. sb.WriteRune('n')
  435. case '\u000d':
  436. sb.WriteRune('r')
  437. default:
  438. sb.WriteRune('u')
  439. sb.WriteRune(rune(hex[c>>12]))
  440. sb.WriteRune(rune(hex[(c>>8)&0xF]))
  441. sb.WriteRune(rune(hex[(c>>4)&0xF]))
  442. sb.WriteRune(rune(hex[c&0xF]))
  443. }
  444. lastPos = pos + size
  445. }
  446. pos += size
  447. }
  448. if lastPos > 0 {
  449. sb.WriteSubstring(r.source, lastPos, r.source.length())
  450. return true
  451. }
  452. return false
  453. }
  454. func (r *Runtime) regexpproto_getSource(call FunctionCall) Value {
  455. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  456. var sb valueStringBuilder
  457. if this.writeEscapedSource(&sb) {
  458. return sb.String()
  459. }
  460. return this.source
  461. } else {
  462. r.typeErrorResult(true, "Method RegExp.prototype.source getter called on incompatible receiver")
  463. return nil
  464. }
  465. }
  466. func (r *Runtime) regexpproto_getGlobal(call FunctionCall) Value {
  467. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  468. if this.pattern.global {
  469. return valueTrue
  470. } else {
  471. return valueFalse
  472. }
  473. } else {
  474. r.typeErrorResult(true, "Method RegExp.prototype.global getter called on incompatible receiver %s", call.This.toString())
  475. return nil
  476. }
  477. }
  478. func (r *Runtime) regexpproto_getMultiline(call FunctionCall) Value {
  479. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  480. if this.pattern.multiline {
  481. return valueTrue
  482. } else {
  483. return valueFalse
  484. }
  485. } else {
  486. r.typeErrorResult(true, "Method RegExp.prototype.multiline getter called on incompatible receiver %s", call.This.toString())
  487. return nil
  488. }
  489. }
  490. func (r *Runtime) regexpproto_getIgnoreCase(call FunctionCall) Value {
  491. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  492. if this.pattern.ignoreCase {
  493. return valueTrue
  494. } else {
  495. return valueFalse
  496. }
  497. } else {
  498. r.typeErrorResult(true, "Method RegExp.prototype.ignoreCase getter called on incompatible receiver %s", call.This.toString())
  499. return nil
  500. }
  501. }
  502. func (r *Runtime) regexpproto_getUnicode(call FunctionCall) Value {
  503. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  504. if this.pattern.unicode {
  505. return valueTrue
  506. } else {
  507. return valueFalse
  508. }
  509. } else {
  510. r.typeErrorResult(true, "Method RegExp.prototype.unicode getter called on incompatible receiver %s", call.This.toString())
  511. return nil
  512. }
  513. }
  514. func (r *Runtime) regexpproto_getSticky(call FunctionCall) Value {
  515. if this, ok := r.toObject(call.This).self.(*regexpObject); ok {
  516. if this.pattern.sticky {
  517. return valueTrue
  518. } else {
  519. return valueFalse
  520. }
  521. } else {
  522. r.typeErrorResult(true, "Method RegExp.prototype.sticky getter called on incompatible receiver %s", call.This.toString())
  523. return nil
  524. }
  525. }
  526. func (r *Runtime) regexpproto_getFlags(call FunctionCall) Value {
  527. var global, ignoreCase, multiline, sticky, unicode bool
  528. thisObj := r.toObject(call.This)
  529. size := 0
  530. if v := thisObj.self.getStr("global", nil); v != nil {
  531. global = v.ToBoolean()
  532. if global {
  533. size++
  534. }
  535. }
  536. if v := thisObj.self.getStr("ignoreCase", nil); v != nil {
  537. ignoreCase = v.ToBoolean()
  538. if ignoreCase {
  539. size++
  540. }
  541. }
  542. if v := thisObj.self.getStr("multiline", nil); v != nil {
  543. multiline = v.ToBoolean()
  544. if multiline {
  545. size++
  546. }
  547. }
  548. if v := thisObj.self.getStr("sticky", nil); v != nil {
  549. sticky = v.ToBoolean()
  550. if sticky {
  551. size++
  552. }
  553. }
  554. if v := thisObj.self.getStr("unicode", nil); v != nil {
  555. unicode = v.ToBoolean()
  556. if unicode {
  557. size++
  558. }
  559. }
  560. var sb strings.Builder
  561. sb.Grow(size)
  562. if global {
  563. sb.WriteByte('g')
  564. }
  565. if ignoreCase {
  566. sb.WriteByte('i')
  567. }
  568. if multiline {
  569. sb.WriteByte('m')
  570. }
  571. if unicode {
  572. sb.WriteByte('u')
  573. }
  574. if sticky {
  575. sb.WriteByte('y')
  576. }
  577. return asciiString(sb.String())
  578. }
  579. func (r *Runtime) regExpExec(execFn func(FunctionCall) Value, rxObj *Object, arg Value) Value {
  580. res := execFn(FunctionCall{
  581. This: rxObj,
  582. Arguments: []Value{arg},
  583. })
  584. if res != _null {
  585. if _, ok := res.(*Object); !ok {
  586. panic(r.NewTypeError("RegExp exec method returned something other than an Object or null"))
  587. }
  588. }
  589. return res
  590. }
  591. func (r *Runtime) getGlobalRegexpMatches(rxObj *Object, s valueString) []Value {
  592. fullUnicode := nilSafe(rxObj.self.getStr("unicode", nil)).ToBoolean()
  593. rxObj.self.setOwnStr("lastIndex", intToValue(0), true)
  594. execFn, ok := r.toObject(rxObj.self.getStr("exec", nil)).self.assertCallable()
  595. if !ok {
  596. panic(r.NewTypeError("exec is not a function"))
  597. }
  598. var a []Value
  599. for {
  600. res := r.regExpExec(execFn, rxObj, s)
  601. if res == _null {
  602. break
  603. }
  604. a = append(a, res)
  605. matchStr := nilSafe(r.toObject(res).self.getIdx(valueInt(0), nil)).toString()
  606. if matchStr.length() == 0 {
  607. thisIndex := toInt(nilSafe(rxObj.self.getStr("lastIndex", nil)).ToInteger())
  608. rxObj.self.setOwnStr("lastIndex", valueInt(advanceStringIndex(s, thisIndex, fullUnicode)), true)
  609. }
  610. }
  611. return a
  612. }
  613. func (r *Runtime) regexpproto_stdMatcherGeneric(rxObj *Object, s valueString) Value {
  614. rx := rxObj.self
  615. global := rx.getStr("global", nil)
  616. if global != nil && global.ToBoolean() {
  617. a := r.getGlobalRegexpMatches(rxObj, s)
  618. if len(a) == 0 {
  619. return _null
  620. }
  621. ar := make([]Value, 0, len(a))
  622. for _, result := range a {
  623. obj := r.toObject(result)
  624. matchStr := nilSafe(obj.self.getIdx(valueInt(0), nil)).ToString()
  625. ar = append(ar, matchStr)
  626. }
  627. return r.newArrayValues(ar)
  628. }
  629. execFn, ok := r.toObject(rx.getStr("exec", nil)).self.assertCallable()
  630. if !ok {
  631. panic(r.NewTypeError("exec is not a function"))
  632. }
  633. return r.regExpExec(execFn, rxObj, s)
  634. }
  635. func (r *Runtime) checkStdRegexp(rxObj *Object) *regexpObject {
  636. if deoptimiseRegexp {
  637. return nil
  638. }
  639. rx, ok := rxObj.self.(*regexpObject)
  640. if !ok {
  641. return nil
  642. }
  643. if !rx.standard || rx.prototype == nil || rx.prototype.self != r.global.stdRegexpProto {
  644. return nil
  645. }
  646. return rx
  647. }
  648. func (r *Runtime) regexpproto_stdMatcher(call FunctionCall) Value {
  649. thisObj := r.toObject(call.This)
  650. s := call.Argument(0).toString()
  651. rx := r.checkStdRegexp(thisObj)
  652. if rx == nil {
  653. return r.regexpproto_stdMatcherGeneric(thisObj, s)
  654. }
  655. if rx.pattern.global {
  656. rx.setOwnStr("lastIndex", intToValue(0), true)
  657. var a []Value
  658. var previousLastIndex int64
  659. for {
  660. match, result := rx.execRegexp(s)
  661. if !match {
  662. break
  663. }
  664. thisIndex := rx.getStr("lastIndex", nil).ToInteger()
  665. if thisIndex == previousLastIndex {
  666. previousLastIndex = int64(advanceStringIndex(s, toInt(previousLastIndex), rx.pattern.unicode))
  667. rx.setOwnStr("lastIndex", intToValue(previousLastIndex), true)
  668. } else {
  669. previousLastIndex = thisIndex
  670. }
  671. a = append(a, s.substring(result[0], result[1]))
  672. }
  673. if len(a) == 0 {
  674. return _null
  675. }
  676. return r.newArrayValues(a)
  677. } else {
  678. return rx.exec(s)
  679. }
  680. }
  681. func (r *Runtime) regexpproto_stdSearchGeneric(rxObj *Object, arg valueString) Value {
  682. rx := rxObj.self
  683. previousLastIndex := nilSafe(rx.getStr("lastIndex", nil))
  684. zero := intToValue(0)
  685. if !previousLastIndex.SameAs(zero) {
  686. rx.setOwnStr("lastIndex", zero, true)
  687. }
  688. execFn, ok := r.toObject(rx.getStr("exec", nil)).self.assertCallable()
  689. if !ok {
  690. panic(r.NewTypeError("exec is not a function"))
  691. }
  692. result := r.regExpExec(execFn, rxObj, arg)
  693. currentLastIndex := nilSafe(rx.getStr("lastIndex", nil))
  694. if !currentLastIndex.SameAs(previousLastIndex) {
  695. rx.setOwnStr("lastIndex", previousLastIndex, true)
  696. }
  697. if result == _null {
  698. return intToValue(-1)
  699. }
  700. return r.toObject(result).self.getStr("index", nil)
  701. }
  702. func (r *Runtime) regexpproto_stdSearch(call FunctionCall) Value {
  703. thisObj := r.toObject(call.This)
  704. s := call.Argument(0).toString()
  705. rx := r.checkStdRegexp(thisObj)
  706. if rx == nil {
  707. return r.regexpproto_stdSearchGeneric(thisObj, s)
  708. }
  709. previousLastIndex := rx.getStr("lastIndex", nil)
  710. rx.setOwnStr("lastIndex", intToValue(0), true)
  711. match, result := rx.execRegexp(s)
  712. rx.setOwnStr("lastIndex", previousLastIndex, true)
  713. if !match {
  714. return intToValue(-1)
  715. }
  716. return intToValue(int64(result[0]))
  717. }
  718. func (r *Runtime) regexpproto_stdSplitterGeneric(splitter *Object, s valueString, limit Value, unicodeMatching bool) Value {
  719. var a []Value
  720. var lim int64
  721. if limit == nil || limit == _undefined {
  722. lim = maxInt - 1
  723. } else {
  724. lim = toLength(limit)
  725. }
  726. if lim == 0 {
  727. return r.newArrayValues(a)
  728. }
  729. size := s.length()
  730. p := 0
  731. execFn := toMethod(splitter.ToObject(r).self.getStr("exec", nil)) // must be non-nil
  732. if size == 0 {
  733. if r.regExpExec(execFn, splitter, s) == _null {
  734. a = append(a, s)
  735. }
  736. return r.newArrayValues(a)
  737. }
  738. q := p
  739. for q < size {
  740. splitter.self.setOwnStr("lastIndex", intToValue(int64(q)), true)
  741. z := r.regExpExec(execFn, splitter, s)
  742. if z == _null {
  743. q = advanceStringIndex(s, q, unicodeMatching)
  744. } else {
  745. z := r.toObject(z)
  746. e := toLength(splitter.self.getStr("lastIndex", nil))
  747. if e == int64(p) {
  748. q = advanceStringIndex(s, q, unicodeMatching)
  749. } else {
  750. a = append(a, s.substring(p, q))
  751. if int64(len(a)) == lim {
  752. return r.newArrayValues(a)
  753. }
  754. if e > int64(size) {
  755. p = size
  756. } else {
  757. p = int(e)
  758. }
  759. numberOfCaptures := max(toLength(z.self.getStr("length", nil))-1, 0)
  760. for i := int64(1); i <= numberOfCaptures; i++ {
  761. a = append(a, z.self.getIdx(valueInt(i), nil))
  762. if int64(len(a)) == lim {
  763. return r.newArrayValues(a)
  764. }
  765. }
  766. q = p
  767. }
  768. }
  769. }
  770. a = append(a, s.substring(p, size))
  771. return r.newArrayValues(a)
  772. }
  773. func advanceStringIndex(s valueString, pos int, unicode bool) int {
  774. next := pos + 1
  775. if !unicode {
  776. return next
  777. }
  778. l := s.length()
  779. if next >= l {
  780. return next
  781. }
  782. if !isUTF16FirstSurrogate(s.charAt(pos)) {
  783. return next
  784. }
  785. if !isUTF16SecondSurrogate(s.charAt(next)) {
  786. return next
  787. }
  788. return next + 1
  789. }
  790. func (r *Runtime) regexpproto_stdSplitter(call FunctionCall) Value {
  791. rxObj := r.toObject(call.This)
  792. c := r.speciesConstructor(rxObj, r.global.RegExp)
  793. flags := nilSafe(rxObj.self.getStr("flags", nil)).toString()
  794. flagsStr := flags.String()
  795. // Add 'y' flag if missing
  796. if !strings.Contains(flagsStr, "y") {
  797. flags = newStringValue(flagsStr + "y")
  798. }
  799. splitter := c([]Value{rxObj, flags}, nil)
  800. s := call.Argument(0).toString()
  801. limitValue := call.Argument(1)
  802. search := r.checkStdRegexp(splitter)
  803. if search == nil {
  804. return r.regexpproto_stdSplitterGeneric(splitter, s, limitValue, strings.Contains(flagsStr, "u"))
  805. }
  806. limit := -1
  807. if limitValue != _undefined {
  808. limit = int(toUint32(limitValue))
  809. }
  810. if limit == 0 {
  811. return r.newArrayValues(nil)
  812. }
  813. targetLength := s.length()
  814. var valueArray []Value
  815. lastIndex := 0
  816. found := 0
  817. result := search.pattern.findAllSubmatchIndex(s, 0, -1, false)
  818. if targetLength == 0 {
  819. if result == nil {
  820. valueArray = append(valueArray, s)
  821. }
  822. goto RETURN
  823. }
  824. for _, match := range result {
  825. if match[0] == match[1] {
  826. // FIXME Ugh, this is a hack
  827. if match[0] == 0 || match[0] == targetLength {
  828. continue
  829. }
  830. }
  831. if lastIndex != match[0] {
  832. valueArray = append(valueArray, s.substring(lastIndex, match[0]))
  833. found++
  834. } else if lastIndex == match[0] {
  835. if lastIndex != -1 {
  836. valueArray = append(valueArray, stringEmpty)
  837. found++
  838. }
  839. }
  840. lastIndex = match[1]
  841. if found == limit {
  842. goto RETURN
  843. }
  844. captureCount := len(match) / 2
  845. for index := 1; index < captureCount; index++ {
  846. offset := index * 2
  847. var value Value
  848. if match[offset] != -1 {
  849. value = s.substring(match[offset], match[offset+1])
  850. } else {
  851. value = _undefined
  852. }
  853. valueArray = append(valueArray, value)
  854. found++
  855. if found == limit {
  856. goto RETURN
  857. }
  858. }
  859. }
  860. if found != limit {
  861. if lastIndex != targetLength {
  862. valueArray = append(valueArray, s.substring(lastIndex, targetLength))
  863. } else {
  864. valueArray = append(valueArray, stringEmpty)
  865. }
  866. }
  867. RETURN:
  868. return r.newArrayValues(valueArray)
  869. }
  870. func (r *Runtime) regexpproto_stdReplacerGeneric(rxObj *Object, s, replaceStr valueString, rcall func(FunctionCall) Value) Value {
  871. var results []Value
  872. if nilSafe(rxObj.self.getStr("global", nil)).ToBoolean() {
  873. results = r.getGlobalRegexpMatches(rxObj, s)
  874. } else {
  875. execFn := toMethod(rxObj.self.getStr("exec", nil)) // must be non-nil
  876. result := r.regExpExec(execFn, rxObj, s)
  877. if result != _null {
  878. results = append(results, result)
  879. }
  880. }
  881. lengthS := s.length()
  882. nextSourcePosition := 0
  883. var resultBuf valueStringBuilder
  884. for _, result := range results {
  885. obj := r.toObject(result)
  886. nCaptures := max(toLength(obj.self.getStr("length", nil))-1, 0)
  887. matched := nilSafe(obj.self.getIdx(valueInt(0), nil)).toString()
  888. matchLength := matched.length()
  889. position := toInt(max(min(nilSafe(obj.self.getStr("index", nil)).ToInteger(), int64(lengthS)), 0))
  890. var captures []Value
  891. if rcall != nil {
  892. captures = make([]Value, 0, nCaptures+3)
  893. } else {
  894. captures = make([]Value, 0, nCaptures+1)
  895. }
  896. captures = append(captures, matched)
  897. for n := int64(1); n <= nCaptures; n++ {
  898. capN := nilSafe(obj.self.getIdx(valueInt(n), nil))
  899. if capN != _undefined {
  900. capN = capN.ToString()
  901. }
  902. captures = append(captures, capN)
  903. }
  904. var replacement valueString
  905. if rcall != nil {
  906. captures = append(captures, intToValue(int64(position)), s)
  907. replacement = rcall(FunctionCall{
  908. This: _undefined,
  909. Arguments: captures,
  910. }).toString()
  911. if position >= nextSourcePosition {
  912. resultBuf.WriteString(s.substring(nextSourcePosition, position))
  913. resultBuf.WriteString(replacement)
  914. nextSourcePosition = position + matchLength
  915. }
  916. } else {
  917. if position >= nextSourcePosition {
  918. resultBuf.WriteString(s.substring(nextSourcePosition, position))
  919. writeSubstitution(s, position, len(captures), func(idx int) valueString {
  920. capture := captures[idx]
  921. if capture != _undefined {
  922. return capture.toString()
  923. }
  924. return stringEmpty
  925. }, replaceStr, &resultBuf)
  926. nextSourcePosition = position + matchLength
  927. }
  928. }
  929. }
  930. if nextSourcePosition < lengthS {
  931. resultBuf.WriteString(s.substring(nextSourcePosition, lengthS))
  932. }
  933. return resultBuf.String()
  934. }
  935. func writeSubstitution(s valueString, position int, numCaptures int, getCapture func(int) valueString, replaceStr valueString, buf *valueStringBuilder) {
  936. l := s.length()
  937. rl := replaceStr.length()
  938. matched := getCapture(0)
  939. tailPos := position + matched.length()
  940. for i := 0; i < rl; i++ {
  941. c := replaceStr.charAt(i)
  942. if c == '$' && i < rl-1 {
  943. ch := replaceStr.charAt(i + 1)
  944. switch ch {
  945. case '$':
  946. buf.WriteRune('$')
  947. case '`':
  948. buf.WriteString(s.substring(0, position))
  949. case '\'':
  950. if tailPos < l {
  951. buf.WriteString(s.substring(tailPos, l))
  952. }
  953. case '&':
  954. buf.WriteString(matched)
  955. default:
  956. matchNumber := 0
  957. j := i + 1
  958. for j < rl {
  959. ch := replaceStr.charAt(j)
  960. if ch >= '0' && ch <= '9' {
  961. m := matchNumber*10 + int(ch-'0')
  962. if m >= numCaptures {
  963. break
  964. }
  965. matchNumber = m
  966. j++
  967. } else {
  968. break
  969. }
  970. }
  971. if matchNumber > 0 {
  972. buf.WriteString(getCapture(matchNumber))
  973. i = j - 1
  974. continue
  975. } else {
  976. buf.WriteRune('$')
  977. buf.WriteRune(ch)
  978. }
  979. }
  980. i++
  981. } else {
  982. buf.WriteRune(c)
  983. }
  984. }
  985. }
  986. func (r *Runtime) regexpproto_stdReplacer(call FunctionCall) Value {
  987. rxObj := r.toObject(call.This)
  988. s := call.Argument(0).toString()
  989. replaceStr, rcall := getReplaceValue(call.Argument(1))
  990. rx := r.checkStdRegexp(rxObj)
  991. if rx == nil {
  992. return r.regexpproto_stdReplacerGeneric(rxObj, s, replaceStr, rcall)
  993. }
  994. var index int64
  995. find := 1
  996. if rx.pattern.global {
  997. find = -1
  998. rx.setOwnStr("lastIndex", intToValue(0), true)
  999. } else {
  1000. index = rx.getLastIndex()
  1001. }
  1002. found := rx.pattern.findAllSubmatchIndex(s, toInt(index), find, rx.pattern.sticky)
  1003. if len(found) > 0 {
  1004. if !rx.updateLastIndex(index, found[0], found[len(found)-1]) {
  1005. found = nil
  1006. }
  1007. } else {
  1008. rx.updateLastIndex(index, nil, nil)
  1009. }
  1010. return stringReplace(s, found, replaceStr, rcall)
  1011. }
  1012. func (r *Runtime) initRegExp() {
  1013. o := r.newGuardedObject(r.global.ObjectPrototype, classObject)
  1014. r.global.RegExpPrototype = o.val
  1015. r.global.stdRegexpProto = o
  1016. o._putProp("compile", r.newNativeFunc(r.regexpproto_compile, nil, "compile", nil, 2), true, false, true)
  1017. o._putProp("exec", r.newNativeFunc(r.regexpproto_exec, nil, "exec", nil, 1), true, false, true)
  1018. o._putProp("test", r.newNativeFunc(r.regexpproto_test, nil, "test", nil, 1), true, false, true)
  1019. o._putProp("toString", r.newNativeFunc(r.regexpproto_toString, nil, "toString", nil, 0), true, false, true)
  1020. o.setOwnStr("source", &valueProperty{
  1021. configurable: true,
  1022. getterFunc: r.newNativeFunc(r.regexpproto_getSource, nil, "get source", nil, 0),
  1023. accessor: true,
  1024. }, false)
  1025. o.setOwnStr("global", &valueProperty{
  1026. configurable: true,
  1027. getterFunc: r.newNativeFunc(r.regexpproto_getGlobal, nil, "get global", nil, 0),
  1028. accessor: true,
  1029. }, false)
  1030. o.setOwnStr("multiline", &valueProperty{
  1031. configurable: true,
  1032. getterFunc: r.newNativeFunc(r.regexpproto_getMultiline, nil, "get multiline", nil, 0),
  1033. accessor: true,
  1034. }, false)
  1035. o.setOwnStr("ignoreCase", &valueProperty{
  1036. configurable: true,
  1037. getterFunc: r.newNativeFunc(r.regexpproto_getIgnoreCase, nil, "get ignoreCase", nil, 0),
  1038. accessor: true,
  1039. }, false)
  1040. o.setOwnStr("unicode", &valueProperty{
  1041. configurable: true,
  1042. getterFunc: r.newNativeFunc(r.regexpproto_getUnicode, nil, "get unicode", nil, 0),
  1043. accessor: true,
  1044. }, false)
  1045. o.setOwnStr("sticky", &valueProperty{
  1046. configurable: true,
  1047. getterFunc: r.newNativeFunc(r.regexpproto_getSticky, nil, "get sticky", nil, 0),
  1048. accessor: true,
  1049. }, false)
  1050. o.setOwnStr("flags", &valueProperty{
  1051. configurable: true,
  1052. getterFunc: r.newNativeFunc(r.regexpproto_getFlags, nil, "get flags", nil, 0),
  1053. accessor: true,
  1054. }, false)
  1055. o._putSym(symMatch, valueProp(r.newNativeFunc(r.regexpproto_stdMatcher, nil, "[Symbol.match]", nil, 1), true, false, true))
  1056. o._putSym(symSearch, valueProp(r.newNativeFunc(r.regexpproto_stdSearch, nil, "[Symbol.search]", nil, 1), true, false, true))
  1057. o._putSym(symSplit, valueProp(r.newNativeFunc(r.regexpproto_stdSplitter, nil, "[Symbol.split]", nil, 2), true, false, true))
  1058. o._putSym(symReplace, valueProp(r.newNativeFunc(r.regexpproto_stdReplacer, nil, "[Symbol.replace]", nil, 2), true, false, true))
  1059. o.guard("exec", "global", "multiline", "ignoreCase", "unicode", "sticky")
  1060. r.global.RegExp = r.newNativeFunc(r.builtin_RegExp, r.builtin_newRegExp, "RegExp", r.global.RegExpPrototype, 2)
  1061. rx := r.global.RegExp.self
  1062. rx._putSym(symSpecies, &valueProperty{
  1063. getterFunc: r.newNativeFunc(r.returnThis, nil, "get [Symbol.species]", nil, 0),
  1064. accessor: true,
  1065. configurable: true,
  1066. })
  1067. r.addToGlobal("RegExp", r.global.RegExp)
  1068. }