builtin_regexp.go 29 KB

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