2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Code in this file is based on files:
23 * from Mozilla project, released under LGPL 2.1 or later.
25 * The Original Code is Mozilla Communicator client code, released
28 * The Initial Developer of the Original Code is
29 * Netscape Communications Corporation.
30 * Portions created by the Initial Developer are Copyright (C) 1998
31 * the Initial Developer. All Rights Reserved.
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
42 #define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
43 #define JSREG_GLOB 0x02 /* global exec, creates array of matches */
44 #define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
45 #define JSREG_STICKY 0x08 /* only match starting at lastIndex */
47 typedef BYTE JSPackedBool
;
48 typedef BYTE jsbytecode
;
51 * This struct holds a bitmap representation of a class from a regexp.
52 * There's a list of these referenced by the classList field in the JSRegExp
53 * struct below. The initial state has startIndex set to the offset in the
54 * original regexp source of the beginning of the class contents. The first
55 * use of the class converts the source representation into a bitmap.
58 typedef struct RECharSet
{
59 JSPackedBool converted
;
72 WORD flags
; /* flags, see jsapi.h's JSREG_* defines */
73 size_t parenCount
; /* number of parenthesized submatches */
74 size_t classCount
; /* count [...] bitmaps */
75 RECharSet
*classList
; /* list of [...] bitmaps */
76 BSTR source
; /* locked source string, sans // */
77 jsbytecode program
[1]; /* regular expression bytecode */
87 static const WCHAR sourceW
[] = {'s','o','u','r','c','e',0};
88 static const WCHAR globalW
[] = {'g','l','o','b','a','l',0};
89 static const WCHAR ignoreCaseW
[] = {'i','g','n','o','r','e','C','a','s','e',0};
90 static const WCHAR multilineW
[] = {'m','u','l','t','i','l','i','n','e',0};
91 static const WCHAR lastIndexW
[] = {'l','a','s','t','I','n','d','e','x',0};
92 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
93 static const WCHAR toLocaleStringW
[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
94 static const WCHAR hasOwnPropertyW
[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
95 static const WCHAR propertyIsEnumerableW
[] =
96 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
97 static const WCHAR isPrototypeOfW
[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
98 static const WCHAR execW
[] = {'e','x','e','c',0};
99 static const WCHAR testW
[] = {'t','e','s','t',0};
101 static const WCHAR emptyW
[] = {0};
103 /* FIXME: Better error handling */
104 #define ReportRegExpError(a,b,c)
105 #define ReportRegExpErrorHelper(a,b,c,d)
106 #define JS_ReportErrorNumber(a,b,c,d)
107 #define JS_ReportErrorFlagsAndNumber(a,b,c,d,e,f)
108 #define js_ReportOutOfScriptQuota(a)
109 #define JS_ReportOutOfMemory(a)
110 #define JS_COUNT_OPERATION(a,b)
112 #define JSMSG_MIN_TOO_BIG 47
113 #define JSMSG_MAX_TOO_BIG 48
114 #define JSMSG_OUT_OF_ORDER 49
115 #define JSMSG_OUT_OF_MEMORY 137
117 #define LINE_SEPARATOR 0x2028
118 #define PARA_SEPARATOR 0x2029
120 #define RE_IS_LETTER(c) (((c >= 'A') && (c <= 'Z')) || \
121 ((c >= 'a') && (c <= 'z')) )
122 #define RE_IS_LINE_TERM(c) ((c == '\n') || (c == '\r') || \
123 (c == LINE_SEPARATOR) || (c == PARA_SEPARATOR))
125 #define JS_ISWORD(c) ((c) < 128 && (isalnum(c) || (c) == '_'))
127 #define JS7_ISDEC(c) ((((unsigned)(c)) - '0') <= 9)
128 #define JS7_UNDEC(c) ((c) - '0')
180 REOP_LIMIT
/* META: no operator >= to this */
183 #define REOP_IS_SIMPLE(op) ((op) <= REOP_NCLASS)
185 static const char *reop_names
[] = {
238 typedef struct RECapture
{
239 ptrdiff_t index
; /* start of contents, -1 for empty */
240 size_t length
; /* length of capture */
243 typedef struct REMatchState
{
245 RECapture parens
[1]; /* first of 're->parenCount' captures,
246 allocated at end of this struct */
249 typedef struct REProgState
{
250 jsbytecode
*continue_pc
; /* current continuation data */
251 jsbytecode continue_op
;
252 ptrdiff_t index
; /* progress in text */
253 size_t parenSoFar
; /* highest indexed paren started */
256 UINT min
; /* current quantifier limits */
260 size_t top
; /* backtrack stack state */
266 typedef struct REBackTrackData
{
267 size_t sz
; /* size of previous stack entry */
268 jsbytecode
*backtrack_pc
; /* where to backtrack to */
269 jsbytecode backtrack_op
;
270 const WCHAR
*cp
; /* index in text of match at backtrack */
271 size_t parenIndex
; /* start index of saved paren contents */
272 size_t parenCount
; /* # of saved paren contents */
273 size_t saveStateStackTop
; /* number of parent states */
274 /* saved parent states follow */
275 /* saved paren contents follow */
278 #define INITIAL_STATESTACK 100
279 #define INITIAL_BACKTRACK 8000
281 typedef struct REGlobalData
{
283 JSRegExp
*regexp
; /* the RE in execution */
284 BOOL ok
; /* runtime error (out_of_memory only?) */
285 size_t start
; /* offset to start at */
286 ptrdiff_t skipped
; /* chars skipped anchoring this r.e. */
287 const WCHAR
*cpbegin
; /* text base address */
288 const WCHAR
*cpend
; /* text limit address */
290 REProgState
*stateStack
; /* stack of state of current parents */
291 size_t stateStackTop
;
292 size_t stateStackLimit
;
294 REBackTrackData
*backTrackStack
;/* stack of matched-so-far positions */
295 REBackTrackData
*backTrackSP
;
296 size_t backTrackStackSize
;
297 size_t cursz
; /* size of current stack entry */
298 size_t backTrackCount
; /* how many times we've backtracked */
299 size_t backTrackLimit
; /* upper limit on backtrack states */
301 jsheap_t
*pool
; /* It's faster to use one malloc'd pool
302 than to malloc/free the three items
303 that are allocated from this pool */
306 typedef struct RENode RENode
;
308 REOp op
; /* r.e. op bytecode */
309 RENode
*next
; /* next in concatenation order */
310 void *kid
; /* first operand */
312 void *kid2
; /* second operand */
313 INT num
; /* could be a number */
314 size_t parenIndex
; /* or a parenthesis index */
315 struct { /* or a quantifier range */
320 struct { /* or a character class */
322 size_t kidlen
; /* length of string at kid, in jschars */
323 size_t index
; /* index into class list */
324 WORD bmsize
; /* bitmap size, based on max char code */
327 struct { /* or a literal sequence */
328 WCHAR chr
; /* of one character */
329 size_t length
; /* or many (via the kid) */
332 RENode
*kid2
; /* second operand from ALT */
333 WCHAR ch1
; /* match char for ALTPREREQ */
334 WCHAR ch2
; /* ditto, or class index for ALTPREREQ2 */
339 #define CLASS_CACHE_SIZE 4
341 typedef struct CompilerState
{
342 script_ctx_t
*context
;
343 const WCHAR
*cpbegin
;
347 size_t classCount
; /* number of [] encountered */
348 size_t treeDepth
; /* maximum depth of parse tree */
349 size_t progLength
; /* estimated bytecode length */
351 size_t classBitmapsMem
; /* memory to hold all class bitmaps */
353 const WCHAR
*start
; /* small cache of class strings */
354 size_t length
; /* since they're often the same */
356 } classCache
[CLASS_CACHE_SIZE
];
360 typedef struct EmitStateStackEntry
{
361 jsbytecode
*altHead
; /* start of REOP_ALT* opcode */
362 jsbytecode
*nextAltFixup
; /* fixup pointer to next-alt offset */
363 jsbytecode
*nextTermFixup
; /* fixup ptr. to REOP_JUMP offset */
364 jsbytecode
*endTermFixup
; /* fixup ptr. to REOPT_ALTPREREQ* offset */
365 RENode
*continueNode
; /* original REOP_ALT* node being stacked */
366 jsbytecode continueOp
; /* REOP_JUMP or REOP_ENDALT continuation */
367 JSPackedBool jumpToJumpFlag
; /* true if we've patched jump-to-jump to
368 avoid 16-bit unsigned offset overflow */
369 } EmitStateStackEntry
;
372 * Immediate operand sizes and getter/setters. Unlike the ones in jsopcode.h,
373 * the getters and setters take the pc of the offset, not of the opcode before
377 #define GET_ARG(pc) ((WORD)(((pc)[0] << 8) | (pc)[1]))
378 #define SET_ARG(pc, arg) ((pc)[0] = (jsbytecode) ((arg) >> 8), \
379 (pc)[1] = (jsbytecode) (arg))
381 #define OFFSET_LEN ARG_LEN
382 #define OFFSET_MAX ((1 << (ARG_LEN * 8)) - 1)
383 #define GET_OFFSET(pc) GET_ARG(pc)
385 static BOOL
ParseRegExp(CompilerState
*);
388 * Maximum supported tree depth is maximum size of EmitStateStackEntry stack.
389 * For sanity, we limit it to 2^24 bytes.
391 #define TREE_DEPTH_MAX ((1 << 24) / sizeof(EmitStateStackEntry))
394 * The maximum memory that can be allocated for class bitmaps.
395 * For sanity, we limit it to 2^24 bytes.
397 #define CLASS_BITMAPS_MEM_LIMIT (1 << 24)
400 * Functions to get size and write/read bytecode that represent small indexes
402 * Each byte in the code represent 7-bit chunk of the index. 8th bit when set
403 * indicates that the following byte brings more bits to the index. Otherwise
404 * this is the last byte in the index bytecode representing highest index bits.
407 GetCompactIndexWidth(size_t index
)
411 for (width
= 1; (index
>>= 7) != 0; ++width
) { }
415 static inline jsbytecode
*
416 WriteCompactIndex(jsbytecode
*pc
, size_t index
)
420 while ((next
= index
>> 7) != 0) {
421 *pc
++ = (jsbytecode
)(index
| 0x80);
424 *pc
++ = (jsbytecode
)index
;
428 static inline jsbytecode
*
429 ReadCompactIndex(jsbytecode
*pc
, size_t *result
)
434 if ((nextByte
& 0x80) == 0) {
436 * Short-circuit the most common case when compact index <= 127.
441 *result
= 0x7F & nextByte
;
444 *result
|= (nextByte
& 0x7F) << shift
;
446 } while ((nextByte
& 0x80) != 0);
451 /* Construct and initialize an RENode, returning NULL for out-of-memory */
453 NewRENode(CompilerState
*state
, REOp op
)
457 ren
= jsheap_alloc(&state
->context
->tmp_heap
, sizeof(*ren
));
459 /* js_ReportOutOfScriptQuota(cx); */
469 * Validates and converts hex ascii value.
472 isASCIIHexDigit(WCHAR c
, UINT
*digit
)
483 if (cv
>= 'a' && cv
<= 'f') {
484 *digit
= cv
- 'a' + 10;
496 #define JUMP_OFFSET_HI(off) ((jsbytecode)((off) >> 8))
497 #define JUMP_OFFSET_LO(off) ((jsbytecode)(off))
500 SetForwardJumpOffset(jsbytecode
*jump
, jsbytecode
*target
)
502 ptrdiff_t offset
= target
- jump
;
504 /* Check that target really points forward. */
506 if ((size_t)offset
> OFFSET_MAX
)
509 jump
[0] = JUMP_OFFSET_HI(offset
);
510 jump
[1] = JUMP_OFFSET_LO(offset
);
515 * Generate bytecode for the tree rooted at t using an explicit stack instead
519 EmitREBytecode(CompilerState
*state
, JSRegExp
*re
, size_t treeDepth
,
520 jsbytecode
*pc
, RENode
*t
)
522 EmitStateStackEntry
*emitStateSP
, *emitStateStack
;
526 if (treeDepth
== 0) {
527 emitStateStack
= NULL
;
529 emitStateStack
= heap_alloc(sizeof(EmitStateStackEntry
) * treeDepth
);
533 emitStateSP
= emitStateStack
;
535 assert(op
< REOP_LIMIT
);
544 case REOP_ALTPREREQ2
:
547 emitStateSP
->altHead
= pc
- 1;
548 emitStateSP
->endTermFixup
= pc
;
550 SET_ARG(pc
, t
->u
.altprereq
.ch1
);
552 SET_ARG(pc
, t
->u
.altprereq
.ch2
);
555 emitStateSP
->nextAltFixup
= pc
; /* offset to next alternate */
558 emitStateSP
->continueNode
= t
;
559 emitStateSP
->continueOp
= REOP_JUMP
;
560 emitStateSP
->jumpToJumpFlag
= FALSE
;
562 assert((size_t)(emitStateSP
- emitStateStack
) <= treeDepth
);
565 assert(op
< REOP_LIMIT
);
569 emitStateSP
->nextTermFixup
= pc
; /* offset to following term */
571 if (!SetForwardJumpOffset(emitStateSP
->nextAltFixup
, pc
))
573 emitStateSP
->continueOp
= REOP_ENDALT
;
575 assert((size_t)(emitStateSP
- emitStateStack
) <= treeDepth
);
578 assert(op
< REOP_LIMIT
);
583 * If we already patched emitStateSP->nextTermFixup to jump to
584 * a nearer jump, to avoid 16-bit immediate offset overflow, we
587 if (emitStateSP
->jumpToJumpFlag
)
591 * Fix up the REOP_JUMP offset to go to the op after REOP_ENDALT.
592 * REOP_ENDALT is executed only on successful match of the last
593 * alternate in a group.
595 if (!SetForwardJumpOffset(emitStateSP
->nextTermFixup
, pc
))
597 if (t
->op
!= REOP_ALT
) {
598 if (!SetForwardJumpOffset(emitStateSP
->endTermFixup
, pc
))
603 * If the program is bigger than the REOP_JUMP offset range, then
604 * we must check for alternates before this one that are part of
605 * the same group, and fix up their jump offsets to target jumps
606 * close enough to fit in a 16-bit unsigned offset immediate.
608 if ((size_t)(pc
- re
->program
) > OFFSET_MAX
&&
609 emitStateSP
> emitStateStack
) {
610 EmitStateStackEntry
*esp
, *esp2
;
611 jsbytecode
*alt
, *jump
;
612 ptrdiff_t span
, header
;
616 for (esp
= esp2
- 1; esp
>= emitStateStack
; --esp
) {
617 if (esp
->continueOp
== REOP_ENDALT
&&
618 !esp
->jumpToJumpFlag
&&
619 esp
->nextTermFixup
+ OFFSET_LEN
== alt
&&
620 (size_t)(pc
- ((esp
->continueNode
->op
!= REOP_ALT
)
622 : esp
->nextTermFixup
)) > OFFSET_MAX
) {
624 jump
= esp
->nextTermFixup
;
627 * The span must be 1 less than the distance from
628 * jump offset to jump offset, so we actually jump
629 * to a REOP_JUMP bytecode, not to its offset!
632 assert(jump
< esp2
->nextTermFixup
);
633 span
= esp2
->nextTermFixup
- jump
- 1;
634 if ((size_t)span
<= OFFSET_MAX
)
639 } while (esp2
->continueOp
!= REOP_ENDALT
);
642 jump
[0] = JUMP_OFFSET_HI(span
);
643 jump
[1] = JUMP_OFFSET_LO(span
);
645 if (esp
->continueNode
->op
!= REOP_ALT
) {
647 * We must patch the offset at esp->endTermFixup
648 * as well, for the REOP_ALTPREREQ{,2} opcodes.
649 * If we're unlucky and endTermFixup is more than
650 * OFFSET_MAX bytes from its target, we cheat by
651 * jumping 6 bytes to the jump whose offset is at
652 * esp->nextTermFixup, which has the same target.
654 jump
= esp
->endTermFixup
;
655 header
= esp
->nextTermFixup
- jump
;
657 if ((size_t)span
> OFFSET_MAX
)
660 jump
[0] = JUMP_OFFSET_HI(span
);
661 jump
[1] = JUMP_OFFSET_LO(span
);
664 esp
->jumpToJumpFlag
= TRUE
;
672 emitStateSP
->altHead
= pc
- 1;
673 emitStateSP
->nextAltFixup
= pc
; /* offset to next alternate */
675 emitStateSP
->continueNode
= t
;
676 emitStateSP
->continueOp
= REOP_JUMP
;
677 emitStateSP
->jumpToJumpFlag
= FALSE
;
679 assert((size_t)(emitStateSP
- emitStateStack
) <= treeDepth
);
682 assert(op
< REOP_LIMIT
);
687 * Coalesce FLATs if possible and if it would not increase bytecode
688 * beyond preallocated limit. The latter happens only when bytecode
689 * size for coalesced string with offset p and length 2 exceeds 6
690 * bytes preallocated for 2 single char nodes, i.e. when
691 * 1 + GetCompactIndexWidth(p) + GetCompactIndexWidth(2) > 6 or
692 * GetCompactIndexWidth(p) > 4.
693 * Since when GetCompactIndexWidth(p) <= 4 coalescing of 3 or more
694 * nodes strictly decreases bytecode size, the check has to be
695 * done only for the first coalescing.
698 GetCompactIndexWidth((WCHAR
*)t
->kid
- state
->cpbegin
) <= 4)
701 t
->next
->op
== REOP_FLAT
&&
702 (WCHAR
*)t
->kid
+ t
->u
.flat
.length
==
704 t
->u
.flat
.length
+= t
->next
->u
.flat
.length
;
705 t
->next
= t
->next
->next
;
708 if (t
->kid
&& t
->u
.flat
.length
> 1) {
709 pc
[-1] = (state
->flags
& JSREG_FOLD
) ? REOP_FLATi
: REOP_FLAT
;
710 pc
= WriteCompactIndex(pc
, (WCHAR
*)t
->kid
- state
->cpbegin
);
711 pc
= WriteCompactIndex(pc
, t
->u
.flat
.length
);
712 } else if (t
->u
.flat
.chr
< 256) {
713 pc
[-1] = (state
->flags
& JSREG_FOLD
) ? REOP_FLAT1i
: REOP_FLAT1
;
714 *pc
++ = (jsbytecode
) t
->u
.flat
.chr
;
716 pc
[-1] = (state
->flags
& JSREG_FOLD
)
719 SET_ARG(pc
, t
->u
.flat
.chr
);
726 pc
= WriteCompactIndex(pc
, t
->u
.parenIndex
);
727 emitStateSP
->continueNode
= t
;
728 emitStateSP
->continueOp
= REOP_RPAREN
;
730 assert((size_t)(emitStateSP
- emitStateStack
) <= treeDepth
);
736 pc
= WriteCompactIndex(pc
, t
->u
.parenIndex
);
740 pc
= WriteCompactIndex(pc
, t
->u
.parenIndex
);
745 emitStateSP
->nextTermFixup
= pc
;
747 emitStateSP
->continueNode
= t
;
748 emitStateSP
->continueOp
= REOP_ASSERTTEST
;
750 assert((size_t)(emitStateSP
- emitStateStack
) <= treeDepth
);
755 case REOP_ASSERTTEST
:
756 case REOP_ASSERTNOTTEST
:
757 if (!SetForwardJumpOffset(emitStateSP
->nextTermFixup
, pc
))
761 case REOP_ASSERT_NOT
:
763 emitStateSP
->nextTermFixup
= pc
;
765 emitStateSP
->continueNode
= t
;
766 emitStateSP
->continueOp
= REOP_ASSERTNOTTEST
;
768 assert((size_t)(emitStateSP
- emitStateStack
) <= treeDepth
);
775 if (t
->u
.range
.min
== 0 && t
->u
.range
.max
== (UINT
)-1) {
776 pc
[-1] = (t
->u
.range
.greedy
) ? REOP_STAR
: REOP_MINIMALSTAR
;
777 } else if (t
->u
.range
.min
== 0 && t
->u
.range
.max
== 1) {
778 pc
[-1] = (t
->u
.range
.greedy
) ? REOP_OPT
: REOP_MINIMALOPT
;
779 } else if (t
->u
.range
.min
== 1 && t
->u
.range
.max
== (UINT
) -1) {
780 pc
[-1] = (t
->u
.range
.greedy
) ? REOP_PLUS
: REOP_MINIMALPLUS
;
782 if (!t
->u
.range
.greedy
)
783 pc
[-1] = REOP_MINIMALQUANT
;
784 pc
= WriteCompactIndex(pc
, t
->u
.range
.min
);
786 * Write max + 1 to avoid using size_t(max) + 1 bytes
787 * for (UINT)-1 sentinel.
789 pc
= WriteCompactIndex(pc
, t
->u
.range
.max
+ 1);
791 emitStateSP
->nextTermFixup
= pc
;
793 emitStateSP
->continueNode
= t
;
794 emitStateSP
->continueOp
= REOP_ENDCHILD
;
796 assert((size_t)(emitStateSP
- emitStateStack
) <= treeDepth
);
802 if (!SetForwardJumpOffset(emitStateSP
->nextTermFixup
, pc
))
807 if (!t
->u
.ucclass
.sense
)
808 pc
[-1] = REOP_NCLASS
;
809 pc
= WriteCompactIndex(pc
, t
->u
.ucclass
.index
);
810 charSet
= &re
->classList
[t
->u
.ucclass
.index
];
811 charSet
->converted
= FALSE
;
812 charSet
->length
= t
->u
.ucclass
.bmsize
;
813 charSet
->u
.src
.startIndex
= t
->u
.ucclass
.startIndex
;
814 charSet
->u
.src
.length
= t
->u
.ucclass
.kidlen
;
815 charSet
->sense
= t
->u
.ucclass
.sense
;
826 if (emitStateSP
== emitStateStack
)
829 t
= emitStateSP
->continueNode
;
830 op
= (REOp
) emitStateSP
->continueOp
;
835 heap_free(emitStateStack
);
839 ReportRegExpError(state
, JSREPORT_ERROR
, JSMSG_REGEXP_TOO_COMPLEX
);
845 * Process the op against the two top operands, reducing them to a single
846 * operand in the penultimate slot. Update progLength and treeDepth.
849 ProcessOp(CompilerState
*state
, REOpData
*opData
, RENode
**operandStack
,
854 switch (opData
->op
) {
856 result
= NewRENode(state
, REOP_ALT
);
859 result
->kid
= operandStack
[operandSP
- 2];
860 result
->u
.kid2
= operandStack
[operandSP
- 1];
861 operandStack
[operandSP
- 2] = result
;
863 if (state
->treeDepth
== TREE_DEPTH_MAX
) {
864 ReportRegExpError(state
, JSREPORT_ERROR
, JSMSG_REGEXP_TOO_COMPLEX
);
870 * Look at both alternates to see if there's a FLAT or a CLASS at
871 * the start of each. If so, use a prerequisite match.
873 if (((RENode
*) result
->kid
)->op
== REOP_FLAT
&&
874 ((RENode
*) result
->u
.kid2
)->op
== REOP_FLAT
&&
875 (state
->flags
& JSREG_FOLD
) == 0) {
876 result
->op
= REOP_ALTPREREQ
;
877 result
->u
.altprereq
.ch1
= ((RENode
*) result
->kid
)->u
.flat
.chr
;
878 result
->u
.altprereq
.ch2
= ((RENode
*) result
->u
.kid2
)->u
.flat
.chr
;
879 /* ALTPREREQ, <end>, uch1, uch2, <next>, ...,
880 JUMP, <end> ... ENDALT */
881 state
->progLength
+= 13;
884 if (((RENode
*) result
->kid
)->op
== REOP_CLASS
&&
885 ((RENode
*) result
->kid
)->u
.ucclass
.index
< 256 &&
886 ((RENode
*) result
->u
.kid2
)->op
== REOP_FLAT
&&
887 (state
->flags
& JSREG_FOLD
) == 0) {
888 result
->op
= REOP_ALTPREREQ2
;
889 result
->u
.altprereq
.ch1
= ((RENode
*) result
->u
.kid2
)->u
.flat
.chr
;
890 result
->u
.altprereq
.ch2
= ((RENode
*) result
->kid
)->u
.ucclass
.index
;
891 /* ALTPREREQ2, <end>, uch1, uch2, <next>, ...,
892 JUMP, <end> ... ENDALT */
893 state
->progLength
+= 13;
896 if (((RENode
*) result
->kid
)->op
== REOP_FLAT
&&
897 ((RENode
*) result
->u
.kid2
)->op
== REOP_CLASS
&&
898 ((RENode
*) result
->u
.kid2
)->u
.ucclass
.index
< 256 &&
899 (state
->flags
& JSREG_FOLD
) == 0) {
900 result
->op
= REOP_ALTPREREQ2
;
901 result
->u
.altprereq
.ch1
= ((RENode
*) result
->kid
)->u
.flat
.chr
;
902 result
->u
.altprereq
.ch2
=
903 ((RENode
*) result
->u
.kid2
)->u
.ucclass
.index
;
904 /* ALTPREREQ2, <end>, uch1, uch2, <next>, ...,
905 JUMP, <end> ... ENDALT */
906 state
->progLength
+= 13;
909 /* ALT, <next>, ..., JUMP, <end> ... ENDALT */
910 state
->progLength
+= 7;
915 result
= operandStack
[operandSP
- 2];
917 result
= result
->next
;
918 result
->next
= operandStack
[operandSP
- 1];
922 case REOP_ASSERT_NOT
:
925 /* These should have been processed by a close paren. */
926 ReportRegExpErrorHelper(state
, JSREPORT_ERROR
, JSMSG_MISSING_PAREN
,
936 * Hack two bits in CompilerState.flags, for use within FindParenCount to flag
937 * its being on the stack, and to propagate errors to its callers.
939 #define JSREG_FIND_PAREN_COUNT 0x8000
940 #define JSREG_FIND_PAREN_ERROR 0x4000
943 * Magic return value from FindParenCount and GetDecimalValue, to indicate
944 * overflow beyond GetDecimalValue's max parameter, or a computed maximum if
945 * its findMax parameter is non-null.
947 #define OVERFLOW_VALUE ((UINT)-1)
950 FindParenCount(CompilerState
*state
)
955 if (state
->flags
& JSREG_FIND_PAREN_COUNT
)
956 return OVERFLOW_VALUE
;
959 * Copy state into temp, flag it so we never report an invalid backref,
960 * and reset its members to parse the entire regexp. This is obviously
961 * suboptimal, but GetDecimalValue calls us only if a backref appears to
962 * refer to a forward parenthetical, which is rare.
965 temp
.flags
|= JSREG_FIND_PAREN_COUNT
;
966 temp
.cp
= temp
.cpbegin
;
971 temp
.classBitmapsMem
= 0;
972 for (i
= 0; i
< CLASS_CACHE_SIZE
; i
++)
973 temp
.classCache
[i
].start
= NULL
;
975 if (!ParseRegExp(&temp
)) {
976 state
->flags
|= JSREG_FIND_PAREN_ERROR
;
977 return OVERFLOW_VALUE
;
979 return temp
.parenCount
;
983 * Extract and return a decimal value at state->cp. The initial character c
984 * has already been read. Return OVERFLOW_VALUE if the result exceeds max.
985 * Callers who pass a non-null findMax should test JSREG_FIND_PAREN_ERROR in
986 * state->flags to discover whether an error occurred under findMax.
989 GetDecimalValue(WCHAR c
, UINT max
, UINT (*findMax
)(CompilerState
*state
),
990 CompilerState
*state
)
992 UINT value
= JS7_UNDEC(c
);
993 BOOL overflow
= (value
> max
&& (!findMax
|| value
> findMax(state
)));
995 /* The following restriction allows simpler overflow checks. */
996 assert(max
<= ((UINT
)-1 - 9) / 10);
997 while (state
->cp
< state
->cpend
) {
1001 value
= 10 * value
+ JS7_UNDEC(c
);
1002 if (!overflow
&& value
> max
&& (!findMax
|| value
> findMax(state
)))
1006 return overflow
? OVERFLOW_VALUE
: value
;
1010 * Calculate the total size of the bitmap required for a class expression.
1013 CalculateBitmapSize(CompilerState
*state
, RENode
*target
, const WCHAR
*src
,
1017 BOOL inRange
= FALSE
;
1018 WCHAR c
, rangeStart
= 0;
1019 UINT n
, digit
, nDigits
, i
;
1021 target
->u
.ucclass
.bmsize
= 0;
1022 target
->u
.ucclass
.sense
= TRUE
;
1029 target
->u
.ucclass
.sense
= FALSE
;
1032 while (src
!= end
) {
1033 BOOL canStartRange
= TRUE
;
1060 if (src
< end
&& RE_IS_LETTER(*src
)) {
1061 localMax
= (UINT
) (*src
++) & 0x1F;
1074 for (i
= 0; (i
< nDigits
) && (src
< end
); i
++) {
1076 if (!isASCIIHexDigit(c
, &digit
)) {
1078 * Back off to accepting the original
1085 n
= (n
<< 4) | digit
;
1090 canStartRange
= FALSE
;
1092 JS_ReportErrorNumber(state
->context
,
1093 js_GetErrorMessage
, NULL
,
1094 JSMSG_BAD_CLASS_RANGE
);
1104 canStartRange
= FALSE
;
1106 JS_ReportErrorNumber(state
->context
,
1107 js_GetErrorMessage
, NULL
,
1108 JSMSG_BAD_CLASS_RANGE
);
1114 * If this is the start of a range, ensure that it's less than
1128 * This is a non-ECMA extension - decimal escapes (in this
1129 * case, octal!) are supposed to be an error inside class
1130 * ranges, but supported here for backwards compatibility.
1135 if ('0' <= c
&& c
<= '7') {
1137 n
= 8 * n
+ JS7_UNDEC(c
);
1139 if ('0' <= c
&& c
<= '7') {
1141 i
= 8 * n
+ JS7_UNDEC(c
);
1162 /* Throw a SyntaxError here, per ECMA-262, 15.10.2.15. */
1163 if (rangeStart
> localMax
) {
1164 JS_ReportErrorNumber(state
->context
,
1165 js_GetErrorMessage
, NULL
,
1166 JSMSG_BAD_CLASS_RANGE
);
1171 if (canStartRange
&& src
< end
- 1) {
1175 rangeStart
= (WCHAR
)localMax
;
1179 if (state
->flags
& JSREG_FOLD
)
1180 rangeStart
= localMax
; /* one run of the uc/dc loop below */
1183 if (state
->flags
& JSREG_FOLD
) {
1184 WCHAR maxch
= localMax
;
1186 for (i
= rangeStart
; i
<= localMax
; i
++) {
1202 target
->u
.ucclass
.bmsize
= max
;
1207 ParseMinMaxQuantifier(CompilerState
*state
, BOOL ignoreValues
)
1211 const WCHAR
*errp
= state
->cp
++;
1216 min
= GetDecimalValue(c
, 0xFFFF, NULL
, state
);
1219 if (!ignoreValues
&& min
== OVERFLOW_VALUE
)
1220 return JSMSG_MIN_TOO_BIG
;
1226 max
= GetDecimalValue(c
, 0xFFFF, NULL
, state
);
1228 if (!ignoreValues
&& max
== OVERFLOW_VALUE
)
1229 return JSMSG_MAX_TOO_BIG
;
1230 if (!ignoreValues
&& min
> max
)
1231 return JSMSG_OUT_OF_ORDER
;
1239 state
->result
= NewRENode(state
, REOP_QUANT
);
1241 return JSMSG_OUT_OF_MEMORY
;
1242 state
->result
->u
.range
.min
= min
;
1243 state
->result
->u
.range
.max
= max
;
1245 * QUANT, <min>, <max>, <next> ... <ENDCHILD>
1246 * where <max> is written as compact(max+1) to make
1247 * (UINT)-1 sentinel to occupy 1 byte, not width_of(max)+1.
1249 state
->progLength
+= (1 + GetCompactIndexWidth(min
)
1250 + GetCompactIndexWidth(max
+ 1)
1261 ParseQuantifier(CompilerState
*state
)
1264 term
= state
->result
;
1265 if (state
->cp
< state
->cpend
) {
1266 switch (*state
->cp
) {
1268 state
->result
= NewRENode(state
, REOP_QUANT
);
1271 state
->result
->u
.range
.min
= 1;
1272 state
->result
->u
.range
.max
= (UINT
)-1;
1273 /* <PLUS>, <next> ... <ENDCHILD> */
1274 state
->progLength
+= 4;
1277 state
->result
= NewRENode(state
, REOP_QUANT
);
1280 state
->result
->u
.range
.min
= 0;
1281 state
->result
->u
.range
.max
= (UINT
)-1;
1282 /* <STAR>, <next> ... <ENDCHILD> */
1283 state
->progLength
+= 4;
1286 state
->result
= NewRENode(state
, REOP_QUANT
);
1289 state
->result
->u
.range
.min
= 0;
1290 state
->result
->u
.range
.max
= 1;
1291 /* <OPT>, <next> ... <ENDCHILD> */
1292 state
->progLength
+= 4;
1294 case '{': /* balance '}' */
1298 err
= ParseMinMaxQuantifier(state
, FALSE
);
1304 ReportRegExpErrorHelper(state
, JSREPORT_ERROR
, err
, errp
);
1313 if (state
->treeDepth
== TREE_DEPTH_MAX
) {
1314 ReportRegExpError(state
, JSREPORT_ERROR
, JSMSG_REGEXP_TOO_COMPLEX
);
1320 state
->result
->kid
= term
;
1321 if (state
->cp
< state
->cpend
&& *state
->cp
== '?') {
1323 state
->result
->u
.range
.greedy
= FALSE
;
1325 state
->result
->u
.range
.greedy
= TRUE
;
1331 * item: assertion An item is either an assertion or
1332 * quantatom a quantified atom.
1334 * assertion: '^' Assertions match beginning of string
1335 * (or line if the class static property
1336 * RegExp.multiline is true).
1337 * '$' End of string (or line if the class
1338 * static property RegExp.multiline is
1340 * '\b' Word boundary (between \w and \W).
1341 * '\B' Word non-boundary.
1343 * quantatom: atom An unquantified atom.
1344 * quantatom '{' n ',' m '}'
1345 * Atom must occur between n and m times.
1346 * quantatom '{' n ',' '}' Atom must occur at least n times.
1347 * quantatom '{' n '}' Atom must occur exactly n times.
1348 * quantatom '*' Zero or more times (same as {0,}).
1349 * quantatom '+' One or more times (same as {1,}).
1350 * quantatom '?' Zero or one time (same as {0,1}).
1352 * any of which can be optionally followed by '?' for ungreedy
1354 * atom: '(' regexp ')' A parenthesized regexp (what matched
1355 * can be addressed using a backreference,
1357 * '.' Matches any char except '\n'.
1358 * '[' classlist ']' A character class.
1359 * '[' '^' classlist ']' A negated character class.
1361 * '\n' Newline (Line Feed).
1362 * '\r' Carriage Return.
1363 * '\t' Horizontal Tab.
1364 * '\v' Vertical Tab.
1365 * '\d' A digit (same as [0-9]).
1367 * '\w' A word character, [0-9a-z_A-Z].
1368 * '\W' A non-word character.
1369 * '\s' A whitespace character, [ \b\f\n\r\t\v].
1370 * '\S' A non-whitespace character.
1371 * '\' n A backreference to the nth (n decimal
1372 * and positive) parenthesized expression.
1373 * '\' octal An octal escape sequence (octal must be
1374 * two or three digits long, unless it is
1375 * 0 for the null character).
1376 * '\x' hex A hex escape (hex must be two digits).
1377 * '\u' unicode A unicode escape (must be four digits).
1378 * '\c' ctrl A control character, ctrl is a letter.
1379 * '\' literalatomchar Any character except one of the above
1380 * that follow '\' in an atom.
1381 * otheratomchar Any character not first among the other
1382 * atom right-hand sides.
1385 ParseTerm(CompilerState
*state
)
1387 WCHAR c
= *state
->cp
++;
1389 UINT num
, tmp
, n
, i
;
1390 const WCHAR
*termStart
;
1393 /* assertions and atoms */
1395 state
->result
= NewRENode(state
, REOP_BOL
);
1398 state
->progLength
++;
1401 state
->result
= NewRENode(state
, REOP_EOL
);
1404 state
->progLength
++;
1407 if (state
->cp
>= state
->cpend
) {
1408 /* a trailing '\' is an error */
1409 ReportRegExpError(state
, JSREPORT_ERROR
, JSMSG_TRAILING_SLASH
);
1414 /* assertion escapes */
1416 state
->result
= NewRENode(state
, REOP_WBDRY
);
1419 state
->progLength
++;
1422 state
->result
= NewRENode(state
, REOP_WNONBDRY
);
1425 state
->progLength
++;
1427 /* Decimal escape */
1429 /* Give a strict warning. See also the note below. */
1430 WARN("non-octal digit in an escape sequence that doesn't match a back-reference\n");
1433 while (state
->cp
< state
->cpend
) {
1435 if (c
< '0' || '7' < c
)
1438 tmp
= 8 * num
+ (UINT
)JS7_UNDEC(c
);
1445 state
->result
= NewRENode(state
, REOP_FLAT
);
1448 state
->result
->u
.flat
.chr
= c
;
1449 state
->result
->u
.flat
.length
= 1;
1450 state
->progLength
+= 3;
1461 termStart
= state
->cp
- 1;
1462 num
= GetDecimalValue(c
, state
->parenCount
, FindParenCount
, state
);
1463 if (state
->flags
& JSREG_FIND_PAREN_ERROR
)
1465 if (num
== OVERFLOW_VALUE
) {
1466 /* Give a strict mode warning. */
1467 WARN("back-reference exceeds number of capturing parentheses\n");
1470 * Note: ECMA 262, 15.10.2.9 says that we should throw a syntax
1471 * error here. However, for compatibility with IE, we treat the
1472 * whole backref as flat if the first character in it is not a
1473 * valid octal character, and as an octal escape otherwise.
1475 state
->cp
= termStart
;
1477 /* Treat this as flat. termStart - 1 is the \. */
1482 /* Treat this as an octal escape. */
1485 assert(1 <= num
&& num
<= 0x10000);
1486 state
->result
= NewRENode(state
, REOP_BACKREF
);
1489 state
->result
->u
.parenIndex
= num
- 1;
1491 += 1 + GetCompactIndexWidth(state
->result
->u
.parenIndex
);
1493 /* Control escape */
1509 /* Control letter */
1511 if (state
->cp
< state
->cpend
&& RE_IS_LETTER(*state
->cp
)) {
1512 c
= (WCHAR
) (*state
->cp
++ & 0x1F);
1514 /* back off to accepting the original '\' as a literal */
1519 /* HexEscapeSequence */
1523 /* UnicodeEscapeSequence */
1528 for (i
= 0; i
< nDigits
&& state
->cp
< state
->cpend
; i
++) {
1531 if (!isASCIIHexDigit(c
, &digit
)) {
1533 * Back off to accepting the original 'u' or 'x' as a
1540 n
= (n
<< 4) | digit
;
1544 /* Character class escapes */
1546 state
->result
= NewRENode(state
, REOP_DIGIT
);
1550 state
->progLength
++;
1553 state
->result
= NewRENode(state
, REOP_NONDIGIT
);
1556 state
->result
= NewRENode(state
, REOP_SPACE
);
1559 state
->result
= NewRENode(state
, REOP_NONSPACE
);
1562 state
->result
= NewRENode(state
, REOP_ALNUM
);
1565 state
->result
= NewRENode(state
, REOP_NONALNUM
);
1567 /* IdentityEscape */
1569 state
->result
= NewRENode(state
, REOP_FLAT
);
1572 state
->result
->u
.flat
.chr
= c
;
1573 state
->result
->u
.flat
.length
= 1;
1574 state
->result
->kid
= (void *) (state
->cp
- 1);
1575 state
->progLength
+= 3;
1580 state
->result
= NewRENode(state
, REOP_CLASS
);
1583 termStart
= state
->cp
;
1584 state
->result
->u
.ucclass
.startIndex
= termStart
- state
->cpbegin
;
1586 if (state
->cp
== state
->cpend
) {
1587 ReportRegExpErrorHelper(state
, JSREPORT_ERROR
,
1588 JSMSG_UNTERM_CLASS
, termStart
);
1592 if (*state
->cp
== '\\') {
1594 if (state
->cp
!= state
->cpend
)
1598 if (*state
->cp
== ']') {
1599 state
->result
->u
.ucclass
.kidlen
= state
->cp
- termStart
;
1604 for (i
= 0; i
< CLASS_CACHE_SIZE
; i
++) {
1605 if (!state
->classCache
[i
].start
) {
1606 state
->classCache
[i
].start
= termStart
;
1607 state
->classCache
[i
].length
= state
->result
->u
.ucclass
.kidlen
;
1608 state
->classCache
[i
].index
= state
->classCount
;
1611 if (state
->classCache
[i
].length
==
1612 state
->result
->u
.ucclass
.kidlen
) {
1613 for (n
= 0; ; n
++) {
1614 if (n
== state
->classCache
[i
].length
) {
1615 state
->result
->u
.ucclass
.index
1616 = state
->classCache
[i
].index
;
1619 if (state
->classCache
[i
].start
[n
] != termStart
[n
])
1624 state
->result
->u
.ucclass
.index
= state
->classCount
++;
1628 * Call CalculateBitmapSize now as we want any errors it finds
1629 * to be reported during the parse phase, not at execution.
1631 if (!CalculateBitmapSize(state
, state
->result
, termStart
, state
->cp
++))
1634 * Update classBitmapsMem with number of bytes to hold bmsize bits,
1635 * which is (bitsCount + 7) / 8 or (highest_bit + 1 + 7) / 8
1636 * or highest_bit / 8 + 1 where highest_bit is u.ucclass.bmsize.
1638 n
= (state
->result
->u
.ucclass
.bmsize
>> 3) + 1;
1639 if (n
> CLASS_BITMAPS_MEM_LIMIT
- state
->classBitmapsMem
) {
1640 ReportRegExpError(state
, JSREPORT_ERROR
, JSMSG_REGEXP_TOO_COMPLEX
);
1643 state
->classBitmapsMem
+= n
;
1644 /* CLASS, <index> */
1646 += 1 + GetCompactIndexWidth(state
->result
->u
.ucclass
.index
);
1650 state
->result
= NewRENode(state
, REOP_DOT
);
1655 const WCHAR
*errp
= state
->cp
--;
1658 err
= ParseMinMaxQuantifier(state
, TRUE
);
1669 ReportRegExpErrorHelper(state
, JSREPORT_ERROR
,
1670 JSMSG_BAD_QUANTIFIER
, state
->cp
- 1);
1674 state
->result
= NewRENode(state
, REOP_FLAT
);
1677 state
->result
->u
.flat
.chr
= c
;
1678 state
->result
->u
.flat
.length
= 1;
1679 state
->result
->kid
= (void *) (state
->cp
- 1);
1680 state
->progLength
+= 3;
1683 return ParseQuantifier(state
);
1687 * Top-down regular expression grammar, based closely on Perl4.
1689 * regexp: altern A regular expression is one or more
1690 * altern '|' regexp alternatives separated by vertical bar.
1692 #define INITIAL_STACK_SIZE 128
1695 ParseRegExp(CompilerState
*state
)
1699 REOpData
*operatorStack
;
1700 RENode
**operandStack
;
1703 BOOL result
= FALSE
;
1705 INT operatorSP
= 0, operatorStackSize
= INITIAL_STACK_SIZE
;
1706 INT operandSP
= 0, operandStackSize
= INITIAL_STACK_SIZE
;
1708 /* Watch out for empty regexp */
1709 if (state
->cp
== state
->cpend
) {
1710 state
->result
= NewRENode(state
, REOP_EMPTY
);
1711 return (state
->result
!= NULL
);
1714 operatorStack
= heap_alloc(sizeof(REOpData
) * operatorStackSize
);
1718 operandStack
= heap_alloc(sizeof(RENode
*) * operandStackSize
);
1723 parenIndex
= state
->parenCount
;
1724 if (state
->cp
== state
->cpend
) {
1726 * If we are at the end of the regexp and we're short one or more
1727 * operands, the regexp must have the form /x|/ or some such, with
1728 * left parentheses making us short more than one operand.
1730 if (operatorSP
>= operandSP
) {
1731 operand
= NewRENode(state
, REOP_EMPTY
);
1737 switch (*state
->cp
) {
1740 if (state
->cp
+ 1 < state
->cpend
&&
1741 *state
->cp
== '?' &&
1742 (state
->cp
[1] == '=' ||
1743 state
->cp
[1] == '!' ||
1744 state
->cp
[1] == ':')) {
1745 switch (state
->cp
[1]) {
1748 /* ASSERT, <next>, ... ASSERTTEST */
1749 state
->progLength
+= 4;
1752 op
= REOP_ASSERT_NOT
;
1753 /* ASSERTNOT, <next>, ... ASSERTNOTTEST */
1754 state
->progLength
+= 4;
1757 op
= REOP_LPARENNON
;
1763 /* LPAREN, <index>, ... RPAREN, <index> */
1765 += 2 * (1 + GetCompactIndexWidth(parenIndex
));
1766 state
->parenCount
++;
1767 if (state
->parenCount
== 65535) {
1768 ReportRegExpError(state
, JSREPORT_ERROR
,
1769 JSMSG_TOO_MANY_PARENS
);
1777 * If there's no stacked open parenthesis, throw syntax error.
1779 for (i
= operatorSP
- 1; ; i
--) {
1781 ReportRegExpError(state
, JSREPORT_ERROR
,
1782 JSMSG_UNMATCHED_RIGHT_PAREN
);
1785 if (operatorStack
[i
].op
== REOP_ASSERT
||
1786 operatorStack
[i
].op
== REOP_ASSERT_NOT
||
1787 operatorStack
[i
].op
== REOP_LPARENNON
||
1788 operatorStack
[i
].op
== REOP_LPAREN
) {
1795 /* Expected an operand before these, so make an empty one */
1796 operand
= NewRENode(state
, REOP_EMPTY
);
1802 if (!ParseTerm(state
))
1804 operand
= state
->result
;
1806 if (operandSP
== operandStackSize
) {
1808 operandStackSize
+= operandStackSize
;
1809 tmp
= heap_realloc(operandStack
, sizeof(RENode
*) * operandStackSize
);
1814 operandStack
[operandSP
++] = operand
;
1819 /* At the end; process remaining operators. */
1821 if (state
->cp
== state
->cpend
) {
1822 while (operatorSP
) {
1824 if (!ProcessOp(state
, &operatorStack
[operatorSP
],
1825 operandStack
, operandSP
))
1829 assert(operandSP
== 1);
1830 state
->result
= operandStack
[0];
1835 switch (*state
->cp
) {
1837 /* Process any stacked 'concat' operators */
1839 while (operatorSP
&&
1840 operatorStack
[operatorSP
- 1].op
== REOP_CONCAT
) {
1842 if (!ProcessOp(state
, &operatorStack
[operatorSP
],
1843 operandStack
, operandSP
)) {
1853 * If there's no stacked open parenthesis, throw syntax error.
1855 for (i
= operatorSP
- 1; ; i
--) {
1857 ReportRegExpError(state
, JSREPORT_ERROR
,
1858 JSMSG_UNMATCHED_RIGHT_PAREN
);
1861 if (operatorStack
[i
].op
== REOP_ASSERT
||
1862 operatorStack
[i
].op
== REOP_ASSERT_NOT
||
1863 operatorStack
[i
].op
== REOP_LPARENNON
||
1864 operatorStack
[i
].op
== REOP_LPAREN
) {
1870 /* Process everything on the stack until the open parenthesis. */
1874 switch (operatorStack
[operatorSP
].op
) {
1876 case REOP_ASSERT_NOT
:
1878 operand
= NewRENode(state
, operatorStack
[operatorSP
].op
);
1881 operand
->u
.parenIndex
=
1882 operatorStack
[operatorSP
].parenIndex
;
1884 operand
->kid
= operandStack
[operandSP
- 1];
1885 operandStack
[operandSP
- 1] = operand
;
1886 if (state
->treeDepth
== TREE_DEPTH_MAX
) {
1887 ReportRegExpError(state
, JSREPORT_ERROR
,
1888 JSMSG_REGEXP_TOO_COMPLEX
);
1894 case REOP_LPARENNON
:
1895 state
->result
= operandStack
[operandSP
- 1];
1896 if (!ParseQuantifier(state
))
1898 operandStack
[operandSP
- 1] = state
->result
;
1899 goto restartOperator
;
1901 if (!ProcessOp(state
, &operatorStack
[operatorSP
],
1902 operandStack
, operandSP
))
1912 const WCHAR
*errp
= state
->cp
;
1914 if (ParseMinMaxQuantifier(state
, TRUE
) < 0) {
1916 * This didn't even scan correctly as a quantifier, so we should
1930 ReportRegExpErrorHelper(state
, JSREPORT_ERROR
, JSMSG_BAD_QUANTIFIER
,
1936 /* Anything else is the start of the next term. */
1939 if (operatorSP
== operatorStackSize
) {
1941 operatorStackSize
+= operatorStackSize
;
1942 tmp
= heap_realloc(operatorStack
, sizeof(REOpData
) * operatorStackSize
);
1945 operatorStack
= tmp
;
1947 operatorStack
[operatorSP
].op
= op
;
1948 operatorStack
[operatorSP
].errPos
= state
->cp
;
1949 operatorStack
[operatorSP
++].parenIndex
= parenIndex
;
1954 heap_free(operatorStack
);
1955 heap_free(operandStack
);
1960 * Save the current state of the match - the position in the input
1961 * text as well as the position in the bytecode. The state of any
1962 * parent expressions is also saved (preceding state).
1963 * Contents of parenCount parentheses from parenIndex are also saved.
1965 static REBackTrackData
*
1966 PushBackTrackState(REGlobalData
*gData
, REOp op
,
1967 jsbytecode
*target
, REMatchState
*x
, const WCHAR
*cp
,
1968 size_t parenIndex
, size_t parenCount
)
1971 REBackTrackData
*result
=
1972 (REBackTrackData
*) ((char *)gData
->backTrackSP
+ gData
->cursz
);
1974 size_t sz
= sizeof(REBackTrackData
) +
1975 gData
->stateStackTop
* sizeof(REProgState
) +
1976 parenCount
* sizeof(RECapture
);
1978 ptrdiff_t btsize
= gData
->backTrackStackSize
;
1979 ptrdiff_t btincr
= ((char *)result
+ sz
) -
1980 ((char *)gData
->backTrackStack
+ btsize
);
1982 TRACE("\tBT_Push: %lu,%lu\n", (unsigned long) parenIndex
, (unsigned long) parenCount
);
1984 JS_COUNT_OPERATION(gData
->cx
, JSOW_JUMP
* (1 + parenCount
));
1986 ptrdiff_t offset
= (char *)result
- (char *)gData
->backTrackStack
;
1988 JS_COUNT_OPERATION(gData
->cx
, JSOW_ALLOCATION
);
1989 btincr
= ((btincr
+btsize
-1)/btsize
)*btsize
;
1990 gData
->backTrackStack
= jsheap_grow(gData
->pool
, gData
->backTrackStack
, btsize
, btincr
);
1991 if (!gData
->backTrackStack
) {
1992 js_ReportOutOfScriptQuota(gData
->cx
);
1996 gData
->backTrackStackSize
= btsize
+ btincr
;
1997 result
= (REBackTrackData
*) ((char *)gData
->backTrackStack
+ offset
);
1999 gData
->backTrackSP
= result
;
2000 result
->sz
= gData
->cursz
;
2003 result
->backtrack_op
= op
;
2004 result
->backtrack_pc
= target
;
2006 result
->parenCount
= parenCount
;
2007 result
->parenIndex
= parenIndex
;
2009 result
->saveStateStackTop
= gData
->stateStackTop
;
2010 assert(gData
->stateStackTop
);
2011 memcpy(result
+ 1, gData
->stateStack
,
2012 sizeof(REProgState
) * result
->saveStateStackTop
);
2014 if (parenCount
!= 0) {
2015 memcpy((char *)(result
+ 1) +
2016 sizeof(REProgState
) * result
->saveStateStackTop
,
2017 &x
->parens
[parenIndex
],
2018 sizeof(RECapture
) * parenCount
);
2019 for (i
= 0; i
!= parenCount
; i
++)
2020 x
->parens
[parenIndex
+ i
].index
= -1;
2026 static inline REMatchState
*
2027 FlatNIMatcher(REGlobalData
*gData
, REMatchState
*x
, WCHAR
*matchChars
,
2031 assert(gData
->cpend
>= x
->cp
);
2032 if (length
> (size_t)(gData
->cpend
- x
->cp
))
2034 for (i
= 0; i
!= length
; i
++) {
2035 if (toupperW(matchChars
[i
]) != toupperW(x
->cp
[i
]))
2043 * 1. Evaluate DecimalEscape to obtain an EscapeValue E.
2044 * 2. If E is not a character then go to step 6.
2045 * 3. Let ch be E's character.
2046 * 4. Let A be a one-element RECharSet containing the character ch.
2047 * 5. Call CharacterSetMatcher(A, false) and return its Matcher result.
2048 * 6. E must be an integer. Let n be that integer.
2049 * 7. If n=0 or n>NCapturingParens then throw a SyntaxError exception.
2050 * 8. Return an internal Matcher closure that takes two arguments, a State x
2051 * and a Continuation c, and performs the following:
2052 * 1. Let cap be x's captures internal array.
2053 * 2. Let s be cap[n].
2054 * 3. If s is undefined, then call c(x) and return its result.
2055 * 4. Let e be x's endIndex.
2056 * 5. Let len be s's length.
2057 * 6. Let f be e+len.
2058 * 7. If f>InputLength, return failure.
2059 * 8. If there exists an integer i between 0 (inclusive) and len (exclusive)
2060 * such that Canonicalize(s[i]) is not the same character as
2061 * Canonicalize(Input [e+i]), then return failure.
2062 * 9. Let y be the State (f, cap).
2063 * 10. Call c(y) and return its result.
2065 static REMatchState
*
2066 BackrefMatcher(REGlobalData
*gData
, REMatchState
*x
, size_t parenIndex
)
2069 const WCHAR
*parenContent
;
2070 RECapture
*cap
= &x
->parens
[parenIndex
];
2072 if (cap
->index
== -1)
2076 if (x
->cp
+ len
> gData
->cpend
)
2079 parenContent
= &gData
->cpbegin
[cap
->index
];
2080 if (gData
->regexp
->flags
& JSREG_FOLD
) {
2081 for (i
= 0; i
< len
; i
++) {
2082 if (toupperW(parenContent
[i
]) != toupperW(x
->cp
[i
]))
2086 for (i
= 0; i
< len
; i
++) {
2087 if (parenContent
[i
] != x
->cp
[i
])
2095 /* Add a single character to the RECharSet */
2097 AddCharacterToCharSet(RECharSet
*cs
, WCHAR c
)
2099 UINT byteIndex
= (UINT
)(c
>> 3);
2100 assert(c
<= cs
->length
);
2101 cs
->u
.bits
[byteIndex
] |= 1 << (c
& 0x7);
2105 /* Add a character range, c1 to c2 (inclusive) to the RECharSet */
2107 AddCharacterRangeToCharSet(RECharSet
*cs
, UINT c1
, UINT c2
)
2111 UINT byteIndex1
= c1
>> 3;
2112 UINT byteIndex2
= c2
>> 3;
2114 assert(c2
<= cs
->length
&& c1
<= c2
);
2119 if (byteIndex1
== byteIndex2
) {
2120 cs
->u
.bits
[byteIndex1
] |= ((BYTE
)0xFF >> (7 - (c2
- c1
))) << c1
;
2122 cs
->u
.bits
[byteIndex1
] |= 0xFF << c1
;
2123 for (i
= byteIndex1
+ 1; i
< byteIndex2
; i
++)
2124 cs
->u
.bits
[i
] = 0xFF;
2125 cs
->u
.bits
[byteIndex2
] |= (BYTE
)0xFF >> (7 - c2
);
2129 /* Compile the source of the class into a RECharSet */
2131 ProcessCharSet(REGlobalData
*gData
, RECharSet
*charSet
)
2133 const WCHAR
*src
, *end
;
2134 BOOL inRange
= FALSE
;
2135 WCHAR rangeStart
= 0;
2140 assert(!charSet
->converted
);
2142 * Assert that startIndex and length points to chars inside [] inside
2145 assert(1 <= charSet
->u
.src
.startIndex
);
2146 assert(charSet
->u
.src
.startIndex
2147 < SysStringLen(gData
->regexp
->source
));
2148 assert(charSet
->u
.src
.length
<= SysStringLen(gData
->regexp
->source
)
2149 - 1 - charSet
->u
.src
.startIndex
);
2151 charSet
->converted
= TRUE
;
2152 src
= gData
->regexp
->source
+ charSet
->u
.src
.startIndex
;
2154 end
= src
+ charSet
->u
.src
.length
;
2156 assert(src
[-1] == '[' && end
[0] == ']');
2158 byteLength
= (charSet
->length
>> 3) + 1;
2159 charSet
->u
.bits
= heap_alloc(byteLength
);
2160 if (!charSet
->u
.bits
) {
2161 JS_ReportOutOfMemory(gData
->cx
);
2165 memset(charSet
->u
.bits
, 0, byteLength
);
2171 assert(charSet
->sense
== FALSE
);
2174 assert(charSet
->sense
== TRUE
);
2177 while (src
!= end
) {
2202 if (src
< end
&& JS_ISWORD(*src
)) {
2203 thisCh
= (WCHAR
)(*src
++ & 0x1F);
2216 for (i
= 0; (i
< nDigits
) && (src
< end
); i
++) {
2219 if (!isASCIIHexDigit(c
, &digit
)) {
2221 * Back off to accepting the original '\'
2228 n
= (n
<< 4) | digit
;
2241 * This is a non-ECMA extension - decimal escapes (in this
2242 * case, octal!) are supposed to be an error inside class
2243 * ranges, but supported here for backwards compatibility.
2247 if ('0' <= c
&& c
<= '7') {
2249 n
= 8 * n
+ JS7_UNDEC(c
);
2251 if ('0' <= c
&& c
<= '7') {
2253 i
= 8 * n
+ JS7_UNDEC(c
);
2264 AddCharacterRangeToCharSet(charSet
, '0', '9');
2265 continue; /* don't need range processing */
2267 AddCharacterRangeToCharSet(charSet
, 0, '0' - 1);
2268 AddCharacterRangeToCharSet(charSet
,
2270 (WCHAR
)charSet
->length
);
2273 for (i
= (INT
)charSet
->length
; i
>= 0; i
--)
2275 AddCharacterToCharSet(charSet
, (WCHAR
)i
);
2278 for (i
= (INT
)charSet
->length
; i
>= 0; i
--)
2280 AddCharacterToCharSet(charSet
, (WCHAR
)i
);
2283 for (i
= (INT
)charSet
->length
; i
>= 0; i
--)
2285 AddCharacterToCharSet(charSet
, (WCHAR
)i
);
2288 for (i
= (INT
)charSet
->length
; i
>= 0; i
--)
2290 AddCharacterToCharSet(charSet
, (WCHAR
)i
);
2305 if (gData
->regexp
->flags
& JSREG_FOLD
) {
2308 assert(rangeStart
<= thisCh
);
2309 for (i
= rangeStart
; i
<= thisCh
; i
++) {
2312 AddCharacterToCharSet(charSet
, i
);
2316 AddCharacterToCharSet(charSet
, uch
);
2318 AddCharacterToCharSet(charSet
, dch
);
2321 AddCharacterRangeToCharSet(charSet
, rangeStart
, thisCh
);
2325 if (gData
->regexp
->flags
& JSREG_FOLD
) {
2326 AddCharacterToCharSet(charSet
, toupperW(thisCh
));
2327 AddCharacterToCharSet(charSet
, tolowerW(thisCh
));
2329 AddCharacterToCharSet(charSet
, thisCh
);
2331 if (src
< end
- 1) {
2335 rangeStart
= thisCh
;
2344 ReallocStateStack(REGlobalData
*gData
)
2346 size_t limit
= gData
->stateStackLimit
;
2347 size_t sz
= sizeof(REProgState
) * limit
;
2349 gData
->stateStack
= jsheap_grow(gData
->pool
, gData
->stateStack
, sz
, sz
);
2350 if (!gData
->stateStack
) {
2351 js_ReportOutOfScriptQuota(gData
->cx
);
2355 gData
->stateStackLimit
= limit
+ limit
;
2359 #define PUSH_STATE_STACK(data) \
2361 ++(data)->stateStackTop; \
2362 if ((data)->stateStackTop == (data)->stateStackLimit && \
2363 !ReallocStateStack((data))) { \
2369 * Apply the current op against the given input to see if it's going to match
2370 * or fail. Return false if we don't get a match, true if we do. If updatecp is
2371 * true, then update the current state's cp. Always update startpc to the next
2374 static inline REMatchState
*
2375 SimpleMatch(REGlobalData
*gData
, REMatchState
*x
, REOp op
,
2376 jsbytecode
**startpc
, BOOL updatecp
)
2378 REMatchState
*result
= NULL
;
2381 size_t offset
, length
, index
;
2382 jsbytecode
*pc
= *startpc
; /* pc has already been incremented past op */
2384 const WCHAR
*startcp
= x
->cp
;
2388 const char *opname
= reop_names
[op
];
2389 TRACE("\n%06d: %*s%s\n", pc
- gData
->regexp
->program
,
2390 (int)gData
->stateStackTop
* 2, "", opname
);
2397 if (x
->cp
!= gData
->cpbegin
) {
2398 if (/*!gData->cx->regExpStatics.multiline && FIXME !!! */
2399 !(gData
->regexp
->flags
& JSREG_MULTILINE
)) {
2402 if (!RE_IS_LINE_TERM(x
->cp
[-1]))
2408 if (x
->cp
!= gData
->cpend
) {
2409 if (/*!gData->cx->regExpStatics.multiline &&*/
2410 !(gData
->regexp
->flags
& JSREG_MULTILINE
)) {
2413 if (!RE_IS_LINE_TERM(*x
->cp
))
2419 if ((x
->cp
== gData
->cpbegin
|| !JS_ISWORD(x
->cp
[-1])) ^
2420 !(x
->cp
!= gData
->cpend
&& JS_ISWORD(*x
->cp
))) {
2425 if ((x
->cp
== gData
->cpbegin
|| !JS_ISWORD(x
->cp
[-1])) ^
2426 (x
->cp
!= gData
->cpend
&& JS_ISWORD(*x
->cp
))) {
2431 if (x
->cp
!= gData
->cpend
&& !RE_IS_LINE_TERM(*x
->cp
)) {
2437 if (x
->cp
!= gData
->cpend
&& JS7_ISDEC(*x
->cp
)) {
2443 if (x
->cp
!= gData
->cpend
&& !JS7_ISDEC(*x
->cp
)) {
2449 if (x
->cp
!= gData
->cpend
&& JS_ISWORD(*x
->cp
)) {
2455 if (x
->cp
!= gData
->cpend
&& !JS_ISWORD(*x
->cp
)) {
2461 if (x
->cp
!= gData
->cpend
&& isspaceW(*x
->cp
)) {
2467 if (x
->cp
!= gData
->cpend
&& !isspaceW(*x
->cp
)) {
2473 pc
= ReadCompactIndex(pc
, &parenIndex
);
2474 assert(parenIndex
< gData
->regexp
->parenCount
);
2475 result
= BackrefMatcher(gData
, x
, parenIndex
);
2478 pc
= ReadCompactIndex(pc
, &offset
);
2479 assert(offset
< SysStringLen(gData
->regexp
->source
));
2480 pc
= ReadCompactIndex(pc
, &length
);
2481 assert(1 <= length
);
2482 assert(length
<= SysStringLen(gData
->regexp
->source
) - offset
);
2483 if (length
<= (size_t)(gData
->cpend
- x
->cp
)) {
2484 source
= gData
->regexp
->source
+ offset
;
2485 TRACE("%s\n", debugstr_wn(source
, length
));
2486 for (index
= 0; index
!= length
; index
++) {
2487 if (source
[index
] != x
->cp
[index
])
2496 TRACE(" '%c' == '%c'\n", (char)matchCh
, (char)*x
->cp
);
2497 if (x
->cp
!= gData
->cpend
&& *x
->cp
== matchCh
) {
2503 pc
= ReadCompactIndex(pc
, &offset
);
2504 assert(offset
< SysStringLen(gData
->regexp
->source
));
2505 pc
= ReadCompactIndex(pc
, &length
);
2506 assert(1 <= length
);
2507 assert(length
<= SysStringLen(gData
->regexp
->source
) - offset
);
2508 source
= gData
->regexp
->source
;
2509 result
= FlatNIMatcher(gData
, x
, source
+ offset
, length
);
2513 if (x
->cp
!= gData
->cpend
&& toupperW(*x
->cp
) == toupperW(matchCh
)) {
2519 matchCh
= GET_ARG(pc
);
2520 TRACE(" '%c' == '%c'\n", (char)matchCh
, (char)*x
->cp
);
2522 if (x
->cp
!= gData
->cpend
&& *x
->cp
== matchCh
) {
2528 matchCh
= GET_ARG(pc
);
2530 if (x
->cp
!= gData
->cpend
&& toupperW(*x
->cp
) == toupperW(matchCh
)) {
2536 pc
= ReadCompactIndex(pc
, &index
);
2537 assert(index
< gData
->regexp
->classCount
);
2538 if (x
->cp
!= gData
->cpend
) {
2539 charSet
= &gData
->regexp
->classList
[index
];
2540 assert(charSet
->converted
);
2543 if (charSet
->length
!= 0 &&
2544 ch
<= charSet
->length
&&
2545 (charSet
->u
.bits
[index
] & (1 << (ch
& 0x7)))) {
2552 pc
= ReadCompactIndex(pc
, &index
);
2553 assert(index
< gData
->regexp
->classCount
);
2554 if (x
->cp
!= gData
->cpend
) {
2555 charSet
= &gData
->regexp
->classList
[index
];
2556 assert(charSet
->converted
);
2559 if (charSet
->length
== 0 ||
2560 ch
> charSet
->length
||
2561 !(charSet
->u
.bits
[index
] & (1 << (ch
& 0x7)))) {
2582 static inline REMatchState
*
2583 ExecuteREBytecode(REGlobalData
*gData
, REMatchState
*x
)
2585 REMatchState
*result
= NULL
;
2586 REBackTrackData
*backTrackData
;
2587 jsbytecode
*nextpc
, *testpc
;
2590 REProgState
*curState
;
2591 const WCHAR
*startcp
;
2592 size_t parenIndex
, k
;
2593 size_t parenSoFar
= 0;
2595 WCHAR matchCh1
, matchCh2
;
2599 jsbytecode
*pc
= gData
->regexp
->program
;
2600 REOp op
= (REOp
) *pc
++;
2603 * If the first node is a simple match, step the index into the string
2604 * until that match is made, or fail if it can't be found at all.
2606 if (REOP_IS_SIMPLE(op
) && !(gData
->regexp
->flags
& JSREG_STICKY
)) {
2608 while (x
->cp
<= gData
->cpend
) {
2609 nextpc
= pc
; /* reset back to start each time */
2610 result
= SimpleMatch(gData
, x
, op
, &nextpc
, TRUE
);
2614 pc
= nextpc
; /* accept skip to next opcode */
2616 assert(op
< REOP_LIMIT
);
2627 const char *opname
= reop_names
[op
];
2628 TRACE("\n%06d: %*s%s\n", pc
- gData
->regexp
->program
,
2629 (int)gData
->stateStackTop
* 2, "", opname
);
2631 if (REOP_IS_SIMPLE(op
)) {
2632 result
= SimpleMatch(gData
, x
, op
, &pc
, TRUE
);
2634 curState
= &gData
->stateStack
[gData
->stateStackTop
];
2638 case REOP_ALTPREREQ2
:
2639 nextpc
= pc
+ GET_OFFSET(pc
); /* start of next op */
2641 matchCh2
= GET_ARG(pc
);
2646 if (x
->cp
!= gData
->cpend
) {
2647 if (*x
->cp
== matchCh2
)
2650 charSet
= &gData
->regexp
->classList
[k
];
2651 if (!charSet
->converted
&& !ProcessCharSet(gData
, charSet
))
2655 if ((charSet
->length
== 0 ||
2656 matchCh1
> charSet
->length
||
2657 !(charSet
->u
.bits
[k
] & (1 << (matchCh1
& 0x7)))) ^
2665 case REOP_ALTPREREQ
:
2666 nextpc
= pc
+ GET_OFFSET(pc
); /* start of next op */
2668 matchCh1
= GET_ARG(pc
);
2670 matchCh2
= GET_ARG(pc
);
2672 if (x
->cp
== gData
->cpend
||
2673 (*x
->cp
!= matchCh1
&& *x
->cp
!= matchCh2
)) {
2677 /* else false thru... */
2681 nextpc
= pc
+ GET_OFFSET(pc
); /* start of next alternate */
2682 pc
+= ARG_LEN
; /* start of this alternate */
2683 curState
->parenSoFar
= parenSoFar
;
2684 PUSH_STATE_STACK(gData
);
2687 if (REOP_IS_SIMPLE(op
)) {
2688 if (!SimpleMatch(gData
, x
, op
, &pc
, TRUE
)) {
2689 op
= (REOp
) *nextpc
++;
2696 nextop
= (REOp
) *nextpc
++;
2697 if (!PushBackTrackState(gData
, nextop
, nextpc
, x
, startcp
, 0, 0))
2702 * Occurs at (successful) end of REOP_ALT,
2706 * If we have not gotten a result here, it is because of an
2707 * empty match. Do the same thing REOP_EMPTY would do.
2712 --gData
->stateStackTop
;
2713 pc
+= GET_OFFSET(pc
);
2718 * Occurs at last (successful) end of REOP_ALT,
2722 * If we have not gotten a result here, it is because of an
2723 * empty match. Do the same thing REOP_EMPTY would do.
2728 --gData
->stateStackTop
;
2733 pc
= ReadCompactIndex(pc
, &parenIndex
);
2734 TRACE("[ %lu ]\n", (unsigned long) parenIndex
);
2735 assert(parenIndex
< gData
->regexp
->parenCount
);
2736 if (parenIndex
+ 1 > parenSoFar
)
2737 parenSoFar
= parenIndex
+ 1;
2738 x
->parens
[parenIndex
].index
= x
->cp
- gData
->cpbegin
;
2739 x
->parens
[parenIndex
].length
= 0;
2747 pc
= ReadCompactIndex(pc
, &parenIndex
);
2748 assert(parenIndex
< gData
->regexp
->parenCount
);
2749 cap
= &x
->parens
[parenIndex
];
2750 delta
= x
->cp
- (gData
->cpbegin
+ cap
->index
);
2751 cap
->length
= (delta
< 0) ? 0 : (size_t) delta
;
2759 nextpc
= pc
+ GET_OFFSET(pc
); /* start of term after ASSERT */
2760 pc
+= ARG_LEN
; /* start of ASSERT child */
2763 if (REOP_IS_SIMPLE(op
) &&
2764 !SimpleMatch(gData
, x
, op
, &testpc
, FALSE
)) {
2768 curState
->u
.assertion
.top
=
2769 (char *)gData
->backTrackSP
- (char *)gData
->backTrackStack
;
2770 curState
->u
.assertion
.sz
= gData
->cursz
;
2771 curState
->index
= x
->cp
- gData
->cpbegin
;
2772 curState
->parenSoFar
= parenSoFar
;
2773 PUSH_STATE_STACK(gData
);
2774 if (!PushBackTrackState(gData
, REOP_ASSERTTEST
,
2775 nextpc
, x
, x
->cp
, 0, 0)) {
2780 case REOP_ASSERT_NOT
:
2781 nextpc
= pc
+ GET_OFFSET(pc
);
2785 if (REOP_IS_SIMPLE(op
) /* Note - fail to fail! */ &&
2786 SimpleMatch(gData
, x
, op
, &testpc
, FALSE
) &&
2787 *testpc
== REOP_ASSERTNOTTEST
) {
2791 curState
->u
.assertion
.top
2792 = (char *)gData
->backTrackSP
-
2793 (char *)gData
->backTrackStack
;
2794 curState
->u
.assertion
.sz
= gData
->cursz
;
2795 curState
->index
= x
->cp
- gData
->cpbegin
;
2796 curState
->parenSoFar
= parenSoFar
;
2797 PUSH_STATE_STACK(gData
);
2798 if (!PushBackTrackState(gData
, REOP_ASSERTNOTTEST
,
2799 nextpc
, x
, x
->cp
, 0, 0)) {
2804 case REOP_ASSERTTEST
:
2805 --gData
->stateStackTop
;
2807 x
->cp
= gData
->cpbegin
+ curState
->index
;
2808 gData
->backTrackSP
=
2809 (REBackTrackData
*) ((char *)gData
->backTrackStack
+
2810 curState
->u
.assertion
.top
);
2811 gData
->cursz
= curState
->u
.assertion
.sz
;
2816 case REOP_ASSERTNOTTEST
:
2817 --gData
->stateStackTop
;
2819 x
->cp
= gData
->cpbegin
+ curState
->index
;
2820 gData
->backTrackSP
=
2821 (REBackTrackData
*) ((char *)gData
->backTrackStack
+
2822 curState
->u
.assertion
.top
);
2823 gData
->cursz
= curState
->u
.assertion
.sz
;
2824 result
= (!result
) ? x
: NULL
;
2827 curState
->u
.quantifier
.min
= 0;
2828 curState
->u
.quantifier
.max
= (UINT
)-1;
2831 curState
->u
.quantifier
.min
= 1;
2832 curState
->u
.quantifier
.max
= (UINT
)-1;
2835 curState
->u
.quantifier
.min
= 0;
2836 curState
->u
.quantifier
.max
= 1;
2839 pc
= ReadCompactIndex(pc
, &k
);
2840 curState
->u
.quantifier
.min
= k
;
2841 pc
= ReadCompactIndex(pc
, &k
);
2842 /* max is k - 1 to use one byte for (UINT)-1 sentinel. */
2843 curState
->u
.quantifier
.max
= k
- 1;
2844 assert(curState
->u
.quantifier
.min
<= curState
->u
.quantifier
.max
);
2846 if (curState
->u
.quantifier
.max
== 0) {
2847 pc
= pc
+ GET_OFFSET(pc
);
2852 /* Step over <next> */
2853 nextpc
= pc
+ ARG_LEN
;
2854 op
= (REOp
) *nextpc
++;
2856 if (REOP_IS_SIMPLE(op
)) {
2857 if (!SimpleMatch(gData
, x
, op
, &nextpc
, TRUE
)) {
2858 if (curState
->u
.quantifier
.min
== 0)
2862 pc
= pc
+ GET_OFFSET(pc
);
2865 op
= (REOp
) *nextpc
++;
2868 curState
->index
= startcp
- gData
->cpbegin
;
2869 curState
->continue_op
= REOP_REPEAT
;
2870 curState
->continue_pc
= pc
;
2871 curState
->parenSoFar
= parenSoFar
;
2872 PUSH_STATE_STACK(gData
);
2873 if (curState
->u
.quantifier
.min
== 0 &&
2874 !PushBackTrackState(gData
, REOP_REPEAT
, pc
, x
, startcp
,
2881 case REOP_ENDCHILD
: /* marks the end of a quantifier child */
2882 pc
= curState
[-1].continue_pc
;
2883 op
= (REOp
) curState
[-1].continue_op
;
2892 --gData
->stateStackTop
;
2894 /* Failed, see if we have enough children. */
2895 if (curState
->u
.quantifier
.min
== 0)
2899 if (curState
->u
.quantifier
.min
== 0 &&
2900 x
->cp
== gData
->cpbegin
+ curState
->index
) {
2901 /* matched an empty string, that'll get us nowhere */
2905 if (curState
->u
.quantifier
.min
!= 0)
2906 curState
->u
.quantifier
.min
--;
2907 if (curState
->u
.quantifier
.max
!= (UINT
) -1)
2908 curState
->u
.quantifier
.max
--;
2909 if (curState
->u
.quantifier
.max
== 0)
2911 nextpc
= pc
+ ARG_LEN
;
2912 nextop
= (REOp
) *nextpc
;
2914 if (REOP_IS_SIMPLE(nextop
)) {
2916 if (!SimpleMatch(gData
, x
, nextop
, &nextpc
, TRUE
)) {
2917 if (curState
->u
.quantifier
.min
== 0)
2924 curState
->index
= startcp
- gData
->cpbegin
;
2925 PUSH_STATE_STACK(gData
);
2926 if (curState
->u
.quantifier
.min
== 0 &&
2927 !PushBackTrackState(gData
, REOP_REPEAT
,
2929 curState
->parenSoFar
,
2931 curState
->parenSoFar
)) {
2934 } while (*nextpc
== REOP_ENDCHILD
);
2937 parenSoFar
= curState
->parenSoFar
;
2942 pc
+= GET_OFFSET(pc
);
2945 case REOP_MINIMALSTAR
:
2946 curState
->u
.quantifier
.min
= 0;
2947 curState
->u
.quantifier
.max
= (UINT
)-1;
2948 goto minimalquantcommon
;
2949 case REOP_MINIMALPLUS
:
2950 curState
->u
.quantifier
.min
= 1;
2951 curState
->u
.quantifier
.max
= (UINT
)-1;
2952 goto minimalquantcommon
;
2953 case REOP_MINIMALOPT
:
2954 curState
->u
.quantifier
.min
= 0;
2955 curState
->u
.quantifier
.max
= 1;
2956 goto minimalquantcommon
;
2957 case REOP_MINIMALQUANT
:
2958 pc
= ReadCompactIndex(pc
, &k
);
2959 curState
->u
.quantifier
.min
= k
;
2960 pc
= ReadCompactIndex(pc
, &k
);
2961 /* See REOP_QUANT comments about k - 1. */
2962 curState
->u
.quantifier
.max
= k
- 1;
2963 assert(curState
->u
.quantifier
.min
2964 <= curState
->u
.quantifier
.max
);
2966 curState
->index
= x
->cp
- gData
->cpbegin
;
2967 curState
->parenSoFar
= parenSoFar
;
2968 PUSH_STATE_STACK(gData
);
2969 if (curState
->u
.quantifier
.min
!= 0) {
2970 curState
->continue_op
= REOP_MINIMALREPEAT
;
2971 curState
->continue_pc
= pc
;
2972 /* step over <next> */
2976 if (!PushBackTrackState(gData
, REOP_MINIMALREPEAT
,
2977 pc
, x
, x
->cp
, 0, 0)) {
2980 --gData
->stateStackTop
;
2981 pc
= pc
+ GET_OFFSET(pc
);
2986 case REOP_MINIMALREPEAT
:
2987 --gData
->stateStackTop
;
2990 TRACE("{%d,%d}\n", curState
->u
.quantifier
.min
, curState
->u
.quantifier
.max
);
2991 #define PREPARE_REPEAT() \
2993 curState->index = x->cp - gData->cpbegin; \
2994 curState->continue_op = REOP_MINIMALREPEAT; \
2995 curState->continue_pc = pc; \
2997 for (k = curState->parenSoFar; k < parenSoFar; k++) \
2998 x->parens[k].index = -1; \
2999 PUSH_STATE_STACK(gData); \
3000 op = (REOp) *pc++; \
3001 assert(op < REOP_LIMIT); \
3007 * Non-greedy failure - try to consume another child.
3009 if (curState
->u
.quantifier
.max
== (UINT
) -1 ||
3010 curState
->u
.quantifier
.max
> 0) {
3014 /* Don't need to adjust pc since we're going to pop. */
3017 if (curState
->u
.quantifier
.min
== 0 &&
3018 x
->cp
== gData
->cpbegin
+ curState
->index
) {
3019 /* Matched an empty string, that'll get us nowhere. */
3023 if (curState
->u
.quantifier
.min
!= 0)
3024 curState
->u
.quantifier
.min
--;
3025 if (curState
->u
.quantifier
.max
!= (UINT
) -1)
3026 curState
->u
.quantifier
.max
--;
3027 if (curState
->u
.quantifier
.min
!= 0) {
3031 curState
->index
= x
->cp
- gData
->cpbegin
;
3032 curState
->parenSoFar
= parenSoFar
;
3033 PUSH_STATE_STACK(gData
);
3034 if (!PushBackTrackState(gData
, REOP_MINIMALREPEAT
,
3036 curState
->parenSoFar
,
3037 parenSoFar
- curState
->parenSoFar
)) {
3040 --gData
->stateStackTop
;
3041 pc
= pc
+ GET_OFFSET(pc
);
3043 assert(op
< REOP_LIMIT
);
3053 * If the match failed and there's a backtrack option, take it.
3054 * Otherwise this is a complete and utter failure.
3057 if (gData
->cursz
== 0)
3060 /* Potentially detect explosive regex here. */
3061 gData
->backTrackCount
++;
3062 if (gData
->backTrackLimit
&&
3063 gData
->backTrackCount
>= gData
->backTrackLimit
) {
3064 JS_ReportErrorNumber(gData
->cx
, js_GetErrorMessage
, NULL
,
3065 JSMSG_REGEXP_TOO_COMPLEX
);
3070 backTrackData
= gData
->backTrackSP
;
3071 gData
->cursz
= backTrackData
->sz
;
3072 gData
->backTrackSP
=
3073 (REBackTrackData
*) ((char *)backTrackData
- backTrackData
->sz
);
3074 x
->cp
= backTrackData
->cp
;
3075 pc
= backTrackData
->backtrack_pc
;
3076 op
= (REOp
) backTrackData
->backtrack_op
;
3077 assert(op
< REOP_LIMIT
);
3078 gData
->stateStackTop
= backTrackData
->saveStateStackTop
;
3079 assert(gData
->stateStackTop
);
3081 memcpy(gData
->stateStack
, backTrackData
+ 1,
3082 sizeof(REProgState
) * backTrackData
->saveStateStackTop
);
3083 curState
= &gData
->stateStack
[gData
->stateStackTop
- 1];
3085 if (backTrackData
->parenCount
) {
3086 memcpy(&x
->parens
[backTrackData
->parenIndex
],
3087 (char *)(backTrackData
+ 1) +
3088 sizeof(REProgState
) * backTrackData
->saveStateStackTop
,
3089 sizeof(RECapture
) * backTrackData
->parenCount
);
3090 parenSoFar
= backTrackData
->parenIndex
+ backTrackData
->parenCount
;
3092 for (k
= curState
->parenSoFar
; k
< parenSoFar
; k
++)
3093 x
->parens
[k
].index
= -1;
3094 parenSoFar
= curState
->parenSoFar
;
3097 TRACE("\tBT_Pop: %ld,%ld\n",
3098 (unsigned long) backTrackData
->parenIndex
,
3099 (unsigned long) backTrackData
->parenCount
);
3105 * Continue with the expression.
3108 assert(op
< REOP_LIMIT
);
3120 static REMatchState
*MatchRegExp(REGlobalData
*gData
, REMatchState
*x
)
3122 REMatchState
*result
;
3123 const WCHAR
*cp
= x
->cp
;
3128 * Have to include the position beyond the last character
3129 * in order to detect end-of-input/line condition.
3131 for (cp2
= cp
; cp2
<= gData
->cpend
; cp2
++) {
3132 gData
->skipped
= cp2
- cp
;
3134 for (j
= 0; j
< gData
->regexp
->parenCount
; j
++)
3135 x
->parens
[j
].index
= -1;
3136 result
= ExecuteREBytecode(gData
, x
);
3137 if (!gData
->ok
|| result
|| (gData
->regexp
->flags
& JSREG_STICKY
))
3139 gData
->backTrackSP
= gData
->backTrackStack
;
3141 gData
->stateStackTop
= 0;
3142 cp2
= cp
+ gData
->skipped
;
3147 #define MIN_BACKTRACK_LIMIT 400000
3149 static REMatchState
*InitMatch(script_ctx_t
*cx
, REGlobalData
*gData
, JSRegExp
*re
, size_t length
)
3151 REMatchState
*result
;
3154 gData
->backTrackStackSize
= INITIAL_BACKTRACK
;
3155 gData
->backTrackStack
= jsheap_alloc(gData
->pool
, INITIAL_BACKTRACK
);
3156 if (!gData
->backTrackStack
)
3159 gData
->backTrackSP
= gData
->backTrackStack
;
3161 gData
->backTrackCount
= 0;
3162 gData
->backTrackLimit
= 0;
3164 gData
->stateStackLimit
= INITIAL_STATESTACK
;
3165 gData
->stateStack
= jsheap_alloc(gData
->pool
, sizeof(REProgState
) * INITIAL_STATESTACK
);
3166 if (!gData
->stateStack
)
3169 gData
->stateStackTop
= 0;
3174 result
= jsheap_alloc(gData
->pool
, offsetof(REMatchState
, parens
) + re
->parenCount
* sizeof(RECapture
));
3178 for (i
= 0; i
< re
->classCount
; i
++) {
3179 if (!re
->classList
[i
].converted
&&
3180 !ProcessCharSet(gData
, &re
->classList
[i
])) {
3188 js_ReportOutOfScriptQuota(cx
);
3194 js_DestroyRegExp(JSRegExp
*re
)
3196 if (re
->classList
) {
3198 for (i
= 0; i
< re
->classCount
; i
++) {
3199 if (re
->classList
[i
].converted
)
3200 heap_free(re
->classList
[i
].u
.bits
);
3201 re
->classList
[i
].u
.bits
= NULL
;
3203 heap_free(re
->classList
);
3209 js_NewRegExp(script_ctx_t
*cx
, BSTR str
, UINT flags
, BOOL flat
)
3213 CompilerState state
;
3220 mark
= jsheap_mark(&cx
->tmp_heap
);
3221 len
= SysStringLen(str
);
3227 state
.cpbegin
= state
.cp
;
3228 state
.cpend
= state
.cp
+ len
;
3229 state
.flags
= flags
;
3230 state
.parenCount
= 0;
3231 state
.classCount
= 0;
3232 state
.progLength
= 0;
3233 state
.treeDepth
= 0;
3234 state
.classBitmapsMem
= 0;
3235 for (i
= 0; i
< CLASS_CACHE_SIZE
; i
++)
3236 state
.classCache
[i
].start
= NULL
;
3238 if (len
!= 0 && flat
) {
3239 state
.result
= NewRENode(&state
, REOP_FLAT
);
3242 state
.result
->u
.flat
.chr
= *state
.cpbegin
;
3243 state
.result
->u
.flat
.length
= len
;
3244 state
.result
->kid
= (void *) state
.cpbegin
;
3245 /* Flat bytecode: REOP_FLAT compact(string_offset) compact(len). */
3246 state
.progLength
+= 1 + GetCompactIndexWidth(0)
3247 + GetCompactIndexWidth(len
);
3249 if (!ParseRegExp(&state
))
3252 resize
= offsetof(JSRegExp
, program
) + state
.progLength
+ 1;
3253 re
= heap_alloc(resize
);
3257 assert(state
.classBitmapsMem
<= CLASS_BITMAPS_MEM_LIMIT
);
3258 re
->classCount
= state
.classCount
;
3259 if (re
->classCount
) {
3260 re
->classList
= heap_alloc(re
->classCount
* sizeof(RECharSet
));
3261 if (!re
->classList
) {
3262 js_DestroyRegExp(re
);
3266 for (i
= 0; i
< re
->classCount
; i
++)
3267 re
->classList
[i
].converted
= FALSE
;
3269 re
->classList
= NULL
;
3271 endPC
= EmitREBytecode(&state
, re
, state
.treeDepth
, re
->program
, state
.result
);
3273 js_DestroyRegExp(re
);
3277 *endPC
++ = REOP_END
;
3279 * Check whether size was overestimated and shrink using realloc.
3280 * This is safe since no pointers to newly parsed regexp or its parts
3281 * besides re exist here.
3283 if ((size_t)(endPC
- re
->program
) != state
.progLength
+ 1) {
3285 assert((size_t)(endPC
- re
->program
) < state
.progLength
+ 1);
3286 resize
= offsetof(JSRegExp
, program
) + (endPC
- re
->program
);
3287 tmp
= heap_realloc(re
, resize
);
3293 re
->parenCount
= state
.parenCount
;
3301 static HRESULT
do_regexp_match_next(RegExpInstance
*regexp
, const WCHAR
*str
, DWORD len
,
3302 const WCHAR
**cp
, match_result_t
**parens
, DWORD
*parens_size
, DWORD
*parens_cnt
, match_result_t
*ret
)
3304 REMatchState
*x
, *result
;
3308 gData
.cpbegin
= *cp
;
3309 gData
.cpend
= str
+ len
;
3310 gData
.start
= *cp
-str
;
3312 gData
.pool
= ®exp
->dispex
.ctx
->tmp_heap
;
3314 x
= InitMatch(NULL
, &gData
, regexp
->jsregexp
, gData
.cpend
- gData
.cpbegin
);
3316 WARN("InitMatch failed\n");
3321 result
= MatchRegExp(&gData
, x
);
3323 WARN("MatchRegExp failed\n");
3333 if(regexp
->jsregexp
->parenCount
> *parens_size
) {
3334 match_result_t
*new_parens
;
3337 new_parens
= heap_realloc(*parens
, sizeof(match_result_t
)*regexp
->jsregexp
->parenCount
);
3339 new_parens
= heap_alloc(sizeof(match_result_t
)*regexp
->jsregexp
->parenCount
);
3341 return E_OUTOFMEMORY
;
3343 *parens
= new_parens
;
3346 *parens_cnt
= regexp
->jsregexp
->parenCount
;
3348 for(i
=0; i
< regexp
->jsregexp
->parenCount
; i
++) {
3349 (*parens
)[i
].str
= *cp
+ result
->parens
[i
].index
;
3350 (*parens
)[i
].len
= result
->parens
[i
].length
;
3354 matchlen
= (result
->cp
-*cp
) - gData
.skipped
;
3356 ret
->str
= result
->cp
-matchlen
;
3357 ret
->len
= matchlen
;
3362 HRESULT
regexp_match_next(DispatchEx
*dispex
, BOOL gcheck
, const WCHAR
*str
, DWORD len
,
3363 const WCHAR
**cp
, match_result_t
**parens
, DWORD
*parens_size
, DWORD
*parens_cnt
, match_result_t
*ret
)
3365 RegExpInstance
*regexp
= (RegExpInstance
*)dispex
;
3369 if(gcheck
&& !(regexp
->jsregexp
->flags
& JSREG_GLOB
))
3372 mark
= jsheap_mark(®exp
->dispex
.ctx
->tmp_heap
);
3374 hres
= do_regexp_match_next(regexp
, str
, len
, cp
, parens
, parens_size
, parens_cnt
, ret
);
3380 HRESULT
regexp_match(DispatchEx
*dispex
, const WCHAR
*str
, DWORD len
, BOOL gflag
, match_result_t
**match_result
,
3383 RegExpInstance
*This
= (RegExpInstance
*)dispex
;
3384 match_result_t
*ret
= NULL
, cres
;
3385 const WCHAR
*cp
= str
;
3386 DWORD i
=0, ret_size
= 0;
3390 mark
= jsheap_mark(&This
->dispex
.ctx
->tmp_heap
);
3393 hres
= do_regexp_match_next(This
, str
, len
, &cp
, NULL
, NULL
, NULL
, &cres
);
3394 if(hres
== S_FALSE
) {
3404 ret
= heap_realloc(ret
, (ret_size
<<= 1) * sizeof(match_result_t
));
3406 ret
= heap_alloc((ret_size
=4) * sizeof(match_result_t
));
3408 hres
= E_OUTOFMEMORY
;
3415 if(!gflag
&& !(This
->jsregexp
->flags
& JSREG_GLOB
)) {
3427 *match_result
= ret
;
3432 static HRESULT
RegExp_source(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3433 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3439 static HRESULT
RegExp_global(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3440 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3446 static HRESULT
RegExp_ignoreCase(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3447 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3453 static HRESULT
RegExp_multiline(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3454 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3460 static HRESULT
RegExp_lastIndex(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3461 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3467 static HRESULT
RegExp_toString(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3468 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3474 static HRESULT
RegExp_toLocaleString(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3475 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3481 static HRESULT
RegExp_hasOwnProperty(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3482 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3488 static HRESULT
RegExp_propertyIsEnumerable(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3489 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3495 static HRESULT
RegExp_isPrototypeOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3496 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3502 static HRESULT
RegExp_exec(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3503 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3509 static HRESULT
RegExp_test(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3510 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3516 static HRESULT
RegExp_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3517 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3523 static void RegExp_destructor(DispatchEx
*dispex
)
3525 RegExpInstance
*This
= (RegExpInstance
*)dispex
;
3528 js_DestroyRegExp(This
->jsregexp
);
3529 SysFreeString(This
->str
);
3533 static const builtin_prop_t RegExp_props
[] = {
3534 {execW
, RegExp_exec
, PROPF_METHOD
},
3535 {globalW
, RegExp_global
, 0},
3536 {hasOwnPropertyW
, RegExp_hasOwnProperty
, PROPF_METHOD
},
3537 {ignoreCaseW
, RegExp_ignoreCase
, 0},
3538 {isPrototypeOfW
, RegExp_isPrototypeOf
, PROPF_METHOD
},
3539 {lastIndexW
, RegExp_lastIndex
, 0},
3540 {multilineW
, RegExp_multiline
, 0},
3541 {propertyIsEnumerableW
, RegExp_propertyIsEnumerable
, PROPF_METHOD
},
3542 {sourceW
, RegExp_source
, 0},
3543 {testW
, RegExp_test
, PROPF_METHOD
},
3544 {toLocaleStringW
, RegExp_toLocaleString
, PROPF_METHOD
},
3545 {toStringW
, RegExp_toString
, PROPF_METHOD
}
3548 static const builtin_info_t RegExp_info
= {
3550 {NULL
, RegExp_value
, 0},
3551 sizeof(RegExp_props
)/sizeof(*RegExp_props
),
3557 static HRESULT
alloc_regexp(script_ctx_t
*ctx
, BOOL use_constr
, RegExpInstance
**ret
)
3559 RegExpInstance
*regexp
;
3562 regexp
= heap_alloc_zero(sizeof(RegExpInstance
));
3564 return E_OUTOFMEMORY
;
3567 hres
= init_dispex_from_constr(®exp
->dispex
, ctx
, &RegExp_info
, ctx
->regexp_constr
);
3569 hres
= init_dispex(®exp
->dispex
, ctx
, &RegExp_info
, NULL
);
3580 static HRESULT
create_regexp(script_ctx_t
*ctx
, const WCHAR
*exp
, int len
, DWORD flags
, DispatchEx
**ret
)
3582 RegExpInstance
*regexp
;
3585 TRACE("%s %x\n", debugstr_w(exp
), flags
);
3587 hres
= alloc_regexp(ctx
, TRUE
, ®exp
);
3592 regexp
->str
= SysAllocString(exp
);
3594 regexp
->str
= SysAllocStringLen(exp
, len
);
3596 jsdisp_release(®exp
->dispex
);
3597 return E_OUTOFMEMORY
;
3600 regexp
->jsregexp
= js_NewRegExp(ctx
, regexp
->str
, flags
, FALSE
);
3601 if(!regexp
->jsregexp
) {
3602 WARN("js_NewRegExp failed\n");
3603 jsdisp_release(®exp
->dispex
);
3607 *ret
= ®exp
->dispex
;
3611 static HRESULT
regexp_constructor(script_ctx_t
*ctx
, DISPPARAMS
*dp
, VARIANT
*retv
)
3613 const WCHAR
*opt
= emptyW
, *src
;
3623 arg
= get_arg(dp
,0);
3624 if(V_VT(arg
) == VT_DISPATCH
) {
3627 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg
));
3629 if(is_class(obj
, JSCLASS_REGEXP
)) {
3630 RegExpInstance
*regexp
= (RegExpInstance
*)obj
;
3632 hres
= create_regexp(ctx
, regexp
->str
, -1, regexp
->jsregexp
->flags
, &ret
);
3633 jsdisp_release(obj
);
3637 V_VT(retv
) = VT_DISPATCH
;
3638 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(ret
);
3642 jsdisp_release(obj
);
3646 if(V_VT(arg
) != VT_BSTR
) {
3647 FIXME("vt arg0 = %d\n", V_VT(arg
));
3653 if(arg_cnt(dp
) >= 2) {
3654 arg
= get_arg(dp
,1);
3655 if(V_VT(arg
) != VT_BSTR
) {
3656 FIXME("unimplemented for vt %d\n", V_VT(arg
));
3663 hres
= create_regexp_str(ctx
, src
, -1, opt
, strlenW(opt
), &ret
);
3667 V_VT(retv
) = VT_DISPATCH
;
3668 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(ret
);
3672 static HRESULT
RegExpConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
3673 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
3678 case DISPATCH_CONSTRUCT
:
3679 return regexp_constructor(dispex
->ctx
, dp
, retv
);
3681 FIXME("unimplemented flags: %x\n", flags
);
3688 HRESULT
create_regexp_constr(script_ctx_t
*ctx
, DispatchEx
**ret
)
3690 RegExpInstance
*regexp
;
3693 hres
= alloc_regexp(ctx
, FALSE
, ®exp
);
3697 hres
= create_builtin_function(ctx
, RegExpConstr_value
, NULL
, PROPF_CONSTR
, ®exp
->dispex
, ret
);
3699 jsdisp_release(®exp
->dispex
);
3703 HRESULT
create_regexp_str(script_ctx_t
*ctx
, const WCHAR
*exp
, DWORD exp_len
, const WCHAR
*opt
,
3704 DWORD opt_len
, DispatchEx
**ret
)
3710 for (p
= opt
; p
< opt
+opt_len
; p
++) {
3713 flags
|= JSREG_GLOB
;
3716 flags
|= JSREG_FOLD
;
3719 flags
|= JSREG_MULTILINE
;
3722 flags
|= JSREG_STICKY
;
3725 WARN("wrong flag %c\n", *p
);
3731 return create_regexp(ctx
, exp
, exp_len
, flags
, ret
);