2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2007-03-22 10:30:00 -07:00
|
|
|
* vim: set sw=4 ts=8 et tw=78:
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS regular expressions, after Perl.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-03-22 20:35:42 -07:00
|
|
|
#include <stdarg.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jstypes.h"
|
2009-03-18 11:38:16 -07:00
|
|
|
#include "jsstdint.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsarena.h" /* Added by JSIFY */
|
|
|
|
#include "jsutil.h" /* Added by JSIFY */
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsarray.h"
|
|
|
|
#include "jsatom.h"
|
2008-10-13 17:39:36 -07:00
|
|
|
#include "jsbuiltins.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jscntxt.h"
|
2008-09-05 10:19:17 -07:00
|
|
|
#include "jsversion.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsfun.h"
|
|
|
|
#include "jsgc.h"
|
|
|
|
#include "jsinterp.h"
|
|
|
|
#include "jslock.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsopcode.h"
|
|
|
|
#include "jsregexp.h"
|
|
|
|
#include "jsscan.h"
|
2008-03-04 15:40:10 -08:00
|
|
|
#include "jsscope.h"
|
2009-02-03 16:25:12 -08:00
|
|
|
#include "jsstaticcheck.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsstr.h"
|
2009-07-16 17:17:35 -07:00
|
|
|
#include "jsvector.h"
|
|
|
|
|
2008-11-04 16:03:57 -08:00
|
|
|
#ifdef JS_TRACER
|
2008-11-04 14:51:51 -08:00
|
|
|
#include "jstracer.h"
|
|
|
|
using namespace avmplus;
|
|
|
|
using namespace nanojit;
|
2008-11-04 16:03:57 -08:00
|
|
|
#endif
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2010-01-22 14:49:18 -08:00
|
|
|
using namespace js;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
typedef enum REOp {
|
2007-03-22 20:35:42 -07:00
|
|
|
#define REOP_DEF(opcode, name) opcode,
|
|
|
|
#include "jsreops.tbl"
|
|
|
|
#undef REOP_DEF
|
2007-10-05 06:58:51 -07:00
|
|
|
REOP_LIMIT /* META: no operator >= to this */
|
2007-03-22 10:30:00 -07:00
|
|
|
} REOp;
|
|
|
|
|
2007-07-05 13:37:47 -07:00
|
|
|
#define REOP_IS_SIMPLE(op) ((op) <= REOP_NCLASS)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-30 23:31:05 -07:00
|
|
|
#ifdef REGEXP_DEBUG
|
2007-03-22 20:35:42 -07:00
|
|
|
const char *reop_names[] = {
|
|
|
|
#define REOP_DEF(opcode, name) name,
|
|
|
|
#include "jsreops.tbl"
|
|
|
|
#undef REOP_DEF
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
2008-02-13 14:33:57 -08:00
|
|
|
static int
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef REGEXP_DEBUG
|
2008-02-13 14:33:57 -08:00
|
|
|
static int
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
retval = vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-02-13 14:33:57 -08:00
|
|
|
static void
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug_chars(const jschar *chrs, size_t length)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
printf(" \"");
|
|
|
|
while (*chrs && i++ < length) {
|
|
|
|
putchar((char)*chrs++);
|
|
|
|
}
|
|
|
|
printf("\"");
|
|
|
|
}
|
|
|
|
#else /* !REGEXP_DEBUG */
|
|
|
|
/* This should be optimized to a no-op by our tier-1 compilers. */
|
2008-02-13 14:33:57 -08:00
|
|
|
static int
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-13 14:33:57 -08:00
|
|
|
static void
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug_chars(const jschar *chrs, size_t length)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /* !REGEXP_DEBUG */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
struct RENode {
|
|
|
|
REOp op; /* r.e. op bytecode */
|
|
|
|
RENode *next; /* next in concatenation order */
|
|
|
|
void *kid; /* first operand */
|
|
|
|
union {
|
|
|
|
void *kid2; /* second operand */
|
|
|
|
jsint num; /* could be a number */
|
|
|
|
size_t parenIndex; /* or a parenthesis index */
|
|
|
|
struct { /* or a quantifier range */
|
|
|
|
uintN min;
|
|
|
|
uintN max;
|
|
|
|
JSPackedBool greedy;
|
|
|
|
} range;
|
|
|
|
struct { /* or a character class */
|
|
|
|
size_t startIndex;
|
|
|
|
size_t kidlen; /* length of string at kid, in jschars */
|
|
|
|
size_t index; /* index into class list */
|
|
|
|
uint16 bmsize; /* bitmap size, based on max char code */
|
|
|
|
JSPackedBool sense;
|
|
|
|
} ucclass;
|
|
|
|
struct { /* or a literal sequence */
|
|
|
|
jschar chr; /* of one character */
|
|
|
|
size_t length; /* or many (via the kid) */
|
|
|
|
} flat;
|
|
|
|
struct {
|
|
|
|
RENode *kid2; /* second operand from ALT */
|
|
|
|
jschar ch1; /* match char for ALTPREREQ */
|
|
|
|
jschar ch2; /* ditto, or class index for ALTPREREQ2 */
|
|
|
|
} altprereq;
|
|
|
|
} u;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define RE_IS_LETTER(c) (((c >= 'A') && (c <= 'Z')) || \
|
|
|
|
((c >= 'a') && (c <= 'z')) )
|
|
|
|
#define RE_IS_LINE_TERM(c) ((c == '\n') || (c == '\r') || \
|
|
|
|
(c == LINE_SEPARATOR) || (c == PARA_SEPARATOR))
|
|
|
|
|
|
|
|
#define CLASS_CACHE_SIZE 4
|
|
|
|
|
|
|
|
typedef struct CompilerState {
|
|
|
|
JSContext *context;
|
|
|
|
JSTokenStream *tokenStream; /* For reporting errors */
|
|
|
|
const jschar *cpbegin;
|
|
|
|
const jschar *cpend;
|
|
|
|
const jschar *cp;
|
|
|
|
size_t parenCount;
|
|
|
|
size_t classCount; /* number of [] encountered */
|
|
|
|
size_t treeDepth; /* maximum depth of parse tree */
|
|
|
|
size_t progLength; /* estimated bytecode length */
|
|
|
|
RENode *result;
|
|
|
|
size_t classBitmapsMem; /* memory to hold all class bitmaps */
|
|
|
|
struct {
|
|
|
|
const jschar *start; /* small cache of class strings */
|
|
|
|
size_t length; /* since they're often the same */
|
|
|
|
size_t index;
|
|
|
|
} classCache[CLASS_CACHE_SIZE];
|
|
|
|
uint16 flags;
|
|
|
|
} CompilerState;
|
|
|
|
|
|
|
|
typedef struct EmitStateStackEntry {
|
|
|
|
jsbytecode *altHead; /* start of REOP_ALT* opcode */
|
|
|
|
jsbytecode *nextAltFixup; /* fixup pointer to next-alt offset */
|
|
|
|
jsbytecode *nextTermFixup; /* fixup ptr. to REOP_JUMP offset */
|
|
|
|
jsbytecode *endTermFixup; /* fixup ptr. to REOPT_ALTPREREQ* offset */
|
|
|
|
RENode *continueNode; /* original REOP_ALT* node being stacked */
|
|
|
|
jsbytecode continueOp; /* REOP_JUMP or REOP_ENDALT continuation */
|
|
|
|
JSPackedBool jumpToJumpFlag; /* true if we've patched jump-to-jump to
|
|
|
|
avoid 16-bit unsigned offset overflow */
|
|
|
|
} EmitStateStackEntry;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Immediate operand sizes and getter/setters. Unlike the ones in jsopcode.h,
|
|
|
|
* the getters and setters take the pc of the offset, not of the opcode before
|
|
|
|
* the offset.
|
|
|
|
*/
|
|
|
|
#define ARG_LEN 2
|
|
|
|
#define GET_ARG(pc) ((uint16)(((pc)[0] << 8) | (pc)[1]))
|
|
|
|
#define SET_ARG(pc, arg) ((pc)[0] = (jsbytecode) ((arg) >> 8), \
|
|
|
|
(pc)[1] = (jsbytecode) (arg))
|
|
|
|
|
|
|
|
#define OFFSET_LEN ARG_LEN
|
|
|
|
#define OFFSET_MAX (JS_BIT(ARG_LEN * 8) - 1)
|
|
|
|
#define GET_OFFSET(pc) GET_ARG(pc)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Maximum supported tree depth is maximum size of EmitStateStackEntry stack.
|
|
|
|
* For sanity, we limit it to 2^24 bytes.
|
|
|
|
*/
|
|
|
|
#define TREE_DEPTH_MAX (JS_BIT(24) / sizeof(EmitStateStackEntry))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The maximum memory that can be allocated for class bitmaps.
|
|
|
|
* For sanity, we limit it to 2^24 bytes.
|
|
|
|
*/
|
|
|
|
#define CLASS_BITMAPS_MEM_LIMIT JS_BIT(24)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions to get size and write/read bytecode that represent small indexes
|
|
|
|
* compactly.
|
|
|
|
* Each byte in the code represent 7-bit chunk of the index. 8th bit when set
|
|
|
|
* indicates that the following byte brings more bits to the index. Otherwise
|
|
|
|
* this is the last byte in the index bytecode representing highest index bits.
|
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
GetCompactIndexWidth(size_t index)
|
|
|
|
{
|
|
|
|
size_t width;
|
|
|
|
|
|
|
|
for (width = 1; (index >>= 7) != 0; ++width) { }
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2008-07-29 10:53:03 -07:00
|
|
|
static JS_ALWAYS_INLINE jsbytecode *
|
2008-02-14 15:56:53 -08:00
|
|
|
WriteCompactIndex(jsbytecode *pc, size_t index)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
size_t next;
|
|
|
|
|
|
|
|
while ((next = index >> 7) != 0) {
|
|
|
|
*pc++ = (jsbytecode)(index | 0x80);
|
|
|
|
index = next;
|
|
|
|
}
|
|
|
|
*pc++ = (jsbytecode)index;
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
2008-07-29 10:53:03 -07:00
|
|
|
static JS_ALWAYS_INLINE jsbytecode *
|
2008-02-14 15:56:53 -08:00
|
|
|
ReadCompactIndex(jsbytecode *pc, size_t *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
size_t nextByte;
|
|
|
|
|
|
|
|
nextByte = *pc++;
|
|
|
|
if ((nextByte & 0x80) == 0) {
|
|
|
|
/*
|
|
|
|
* Short-circuit the most common case when compact index <= 127.
|
|
|
|
*/
|
|
|
|
*result = nextByte;
|
|
|
|
} else {
|
|
|
|
size_t shift = 7;
|
|
|
|
*result = 0x7F & nextByte;
|
|
|
|
do {
|
|
|
|
nextByte = *pc++;
|
|
|
|
*result |= (nextByte & 0x7F) << shift;
|
|
|
|
shift += 7;
|
|
|
|
} while ((nextByte & 0x80) != 0);
|
|
|
|
}
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct RECapture {
|
|
|
|
ptrdiff_t index; /* start of contents, -1 for empty */
|
|
|
|
size_t length; /* length of capture */
|
|
|
|
} RECapture;
|
|
|
|
|
|
|
|
typedef struct REMatchState {
|
|
|
|
const jschar *cp;
|
|
|
|
RECapture parens[1]; /* first of 're->parenCount' captures,
|
|
|
|
allocated at end of this struct */
|
|
|
|
} REMatchState;
|
|
|
|
|
|
|
|
struct REBackTrackData;
|
|
|
|
|
|
|
|
typedef struct REProgState {
|
|
|
|
jsbytecode *continue_pc; /* current continuation data */
|
|
|
|
jsbytecode continue_op;
|
|
|
|
ptrdiff_t index; /* progress in text */
|
|
|
|
size_t parenSoFar; /* highest indexed paren started */
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uintN min; /* current quantifier limits */
|
|
|
|
uintN max;
|
|
|
|
} quantifier;
|
|
|
|
struct {
|
|
|
|
size_t top; /* backtrack stack state */
|
|
|
|
size_t sz;
|
|
|
|
} assertion;
|
|
|
|
} u;
|
|
|
|
} REProgState;
|
|
|
|
|
|
|
|
typedef struct REBackTrackData {
|
|
|
|
size_t sz; /* size of previous stack entry */
|
|
|
|
jsbytecode *backtrack_pc; /* where to backtrack to */
|
|
|
|
jsbytecode backtrack_op;
|
|
|
|
const jschar *cp; /* index in text of match at backtrack */
|
|
|
|
size_t parenIndex; /* start index of saved paren contents */
|
|
|
|
size_t parenCount; /* # of saved paren contents */
|
|
|
|
size_t saveStateStackTop; /* number of parent states */
|
|
|
|
/* saved parent states follow */
|
|
|
|
/* saved paren contents follow */
|
|
|
|
} REBackTrackData;
|
|
|
|
|
|
|
|
#define INITIAL_STATESTACK 100
|
|
|
|
#define INITIAL_BACKTRACK 8000
|
|
|
|
|
|
|
|
typedef struct REGlobalData {
|
|
|
|
JSContext *cx;
|
|
|
|
JSRegExp *regexp; /* the RE in execution */
|
|
|
|
JSBool ok; /* runtime error (out_of_memory only?) */
|
|
|
|
size_t start; /* offset to start at */
|
|
|
|
ptrdiff_t skipped; /* chars skipped anchoring this r.e. */
|
|
|
|
const jschar *cpbegin; /* text base address */
|
|
|
|
const jschar *cpend; /* text limit address */
|
|
|
|
|
|
|
|
REProgState *stateStack; /* stack of state of current parents */
|
|
|
|
size_t stateStackTop;
|
|
|
|
size_t stateStackLimit;
|
|
|
|
|
|
|
|
REBackTrackData *backTrackStack;/* stack of matched-so-far positions */
|
|
|
|
REBackTrackData *backTrackSP;
|
|
|
|
size_t backTrackStackSize;
|
|
|
|
size_t cursz; /* size of current stack entry */
|
|
|
|
size_t backTrackCount; /* how many times we've backtracked */
|
|
|
|
size_t backTrackLimit; /* upper limit on backtrack states */
|
|
|
|
} REGlobalData;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 1. If IgnoreCase is false, return ch.
|
|
|
|
* 2. Let u be ch converted to upper case as if by calling
|
|
|
|
* String.prototype.toUpperCase on the one-character string ch.
|
|
|
|
* 3. If u does not consist of a single character, return ch.
|
|
|
|
* 4. Let cu be u's character.
|
|
|
|
* 5. If ch's code point value is greater than or equal to decimal 128 and cu's
|
|
|
|
* code point value is less than decimal 128, then return ch.
|
|
|
|
* 6. Return cu.
|
|
|
|
*/
|
2008-07-29 10:53:03 -07:00
|
|
|
static JS_ALWAYS_INLINE uintN
|
2008-02-14 15:56:53 -08:00
|
|
|
upcase(uintN ch)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-16 00:31:15 -08:00
|
|
|
uintN cu;
|
|
|
|
|
|
|
|
JS_ASSERT((uintN) (jschar) ch == ch);
|
|
|
|
if (ch < 128) {
|
|
|
|
if (ch - (uintN) 'a' <= (uintN) ('z' - 'a'))
|
|
|
|
ch -= (uintN) ('a' - 'A');
|
2007-03-22 10:30:00 -07:00
|
|
|
return ch;
|
2008-01-16 00:31:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
cu = JS_TOUPPER(ch);
|
|
|
|
return (cu < 128) ? ch : cu;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-12-23 18:48:53 -08:00
|
|
|
/*
|
|
|
|
* Return the 'canonical' inverse upcase of |ch|. That is the character
|
|
|
|
* |lch| such that |upcase(lch) == ch| and (|lch| is the lower-case form
|
|
|
|
* of |ch| or is |ch|).
|
|
|
|
*/
|
|
|
|
static inline jschar inverse_upcase(jschar ch)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-12-23 18:56:50 -08:00
|
|
|
jschar lch = JS_TOLOWER(ch);
|
|
|
|
return (upcase(lch) == ch) ? lch : ch;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Construct and initialize an RENode, returning NULL for out-of-memory */
|
|
|
|
static RENode *
|
|
|
|
NewRENode(CompilerState *state, REOp op)
|
|
|
|
{
|
|
|
|
JSContext *cx;
|
|
|
|
RENode *ren;
|
|
|
|
|
|
|
|
cx = state->context;
|
|
|
|
JS_ARENA_ALLOCATE_CAST(ren, RENode *, &cx->tempPool, sizeof *ren);
|
|
|
|
if (!ren) {
|
2007-11-22 14:21:18 -08:00
|
|
|
js_ReportOutOfScriptQuota(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ren->op = op;
|
|
|
|
ren->next = NULL;
|
|
|
|
ren->kid = NULL;
|
|
|
|
return ren;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validates and converts hex ascii value.
|
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
isASCIIHexDigit(jschar c, uintN *digit)
|
|
|
|
{
|
|
|
|
uintN cv = c;
|
|
|
|
|
|
|
|
if (cv < '0')
|
|
|
|
return JS_FALSE;
|
|
|
|
if (cv <= '9') {
|
|
|
|
*digit = cv - '0';
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
cv |= 0x20;
|
|
|
|
if (cv >= 'a' && cv <= 'f') {
|
|
|
|
*digit = cv - 'a' + 10;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
REOp op;
|
|
|
|
const jschar *errPos;
|
|
|
|
size_t parenIndex;
|
|
|
|
} REOpData;
|
|
|
|
|
2007-11-13 14:18:17 -08:00
|
|
|
static JSBool
|
|
|
|
ReportRegExpErrorHelper(CompilerState *state, uintN flags, uintN errorNumber,
|
|
|
|
const jschar *arg)
|
|
|
|
{
|
|
|
|
if (state->tokenStream) {
|
|
|
|
return js_ReportCompileErrorNumber(state->context, state->tokenStream,
|
|
|
|
NULL, JSREPORT_UC | flags,
|
|
|
|
errorNumber, arg);
|
|
|
|
}
|
|
|
|
return JS_ReportErrorFlagsAndNumberUC(state->context, flags,
|
|
|
|
js_GetErrorMessage, NULL,
|
|
|
|
errorNumber, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
ReportRegExpError(CompilerState *state, uintN flags, uintN errorNumber)
|
|
|
|
{
|
|
|
|
return ReportRegExpErrorHelper(state, flags, errorNumber, NULL);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Process the op against the two top operands, reducing them to a single
|
|
|
|
* operand in the penultimate slot. Update progLength and treeDepth.
|
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
ProcessOp(CompilerState *state, REOpData *opData, RENode **operandStack,
|
|
|
|
intN operandSP)
|
|
|
|
{
|
|
|
|
RENode *result;
|
|
|
|
|
|
|
|
switch (opData->op) {
|
|
|
|
case REOP_ALT:
|
|
|
|
result = NewRENode(state, REOP_ALT);
|
|
|
|
if (!result)
|
|
|
|
return JS_FALSE;
|
|
|
|
result->kid = operandStack[operandSP - 2];
|
|
|
|
result->u.kid2 = operandStack[operandSP - 1];
|
|
|
|
operandStack[operandSP - 2] = result;
|
|
|
|
|
|
|
|
if (state->treeDepth == TREE_DEPTH_MAX) {
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpError(state, JSREPORT_ERROR, JSMSG_REGEXP_TOO_COMPLEX);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
++state->treeDepth;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look at both alternates to see if there's a FLAT or a CLASS at
|
|
|
|
* the start of each. If so, use a prerequisite match.
|
|
|
|
*/
|
|
|
|
if (((RENode *) result->kid)->op == REOP_FLAT &&
|
|
|
|
((RENode *) result->u.kid2)->op == REOP_FLAT &&
|
|
|
|
(state->flags & JSREG_FOLD) == 0) {
|
|
|
|
result->op = REOP_ALTPREREQ;
|
|
|
|
result->u.altprereq.ch1 = ((RENode *) result->kid)->u.flat.chr;
|
|
|
|
result->u.altprereq.ch2 = ((RENode *) result->u.kid2)->u.flat.chr;
|
|
|
|
/* ALTPREREQ, <end>, uch1, uch2, <next>, ...,
|
|
|
|
JUMP, <end> ... ENDALT */
|
|
|
|
state->progLength += 13;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (((RENode *) result->kid)->op == REOP_CLASS &&
|
|
|
|
((RENode *) result->kid)->u.ucclass.index < 256 &&
|
|
|
|
((RENode *) result->u.kid2)->op == REOP_FLAT &&
|
|
|
|
(state->flags & JSREG_FOLD) == 0) {
|
|
|
|
result->op = REOP_ALTPREREQ2;
|
|
|
|
result->u.altprereq.ch1 = ((RENode *) result->u.kid2)->u.flat.chr;
|
|
|
|
result->u.altprereq.ch2 = ((RENode *) result->kid)->u.ucclass.index;
|
|
|
|
/* ALTPREREQ2, <end>, uch1, uch2, <next>, ...,
|
|
|
|
JUMP, <end> ... ENDALT */
|
|
|
|
state->progLength += 13;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (((RENode *) result->kid)->op == REOP_FLAT &&
|
|
|
|
((RENode *) result->u.kid2)->op == REOP_CLASS &&
|
|
|
|
((RENode *) result->u.kid2)->u.ucclass.index < 256 &&
|
|
|
|
(state->flags & JSREG_FOLD) == 0) {
|
|
|
|
result->op = REOP_ALTPREREQ2;
|
|
|
|
result->u.altprereq.ch1 = ((RENode *) result->kid)->u.flat.chr;
|
|
|
|
result->u.altprereq.ch2 =
|
|
|
|
((RENode *) result->u.kid2)->u.ucclass.index;
|
|
|
|
/* ALTPREREQ2, <end>, uch1, uch2, <next>, ...,
|
|
|
|
JUMP, <end> ... ENDALT */
|
|
|
|
state->progLength += 13;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* ALT, <next>, ..., JUMP, <end> ... ENDALT */
|
|
|
|
state->progLength += 7;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REOP_CONCAT:
|
|
|
|
result = operandStack[operandSP - 2];
|
|
|
|
while (result->next)
|
|
|
|
result = result->next;
|
|
|
|
result->next = operandStack[operandSP - 1];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REOP_ASSERT:
|
|
|
|
case REOP_ASSERT_NOT:
|
|
|
|
case REOP_LPARENNON:
|
|
|
|
case REOP_LPAREN:
|
|
|
|
/* These should have been processed by a close paren. */
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpErrorHelper(state, JSREPORT_ERROR, JSMSG_MISSING_PAREN,
|
|
|
|
opData->errPos);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parser forward declarations.
|
|
|
|
*/
|
|
|
|
static JSBool ParseTerm(CompilerState *state);
|
|
|
|
static JSBool ParseQuantifier(CompilerState *state);
|
|
|
|
static intN ParseMinMaxQuantifier(CompilerState *state, JSBool ignoreValues);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Top-down regular expression grammar, based closely on Perl4.
|
|
|
|
*
|
|
|
|
* regexp: altern A regular expression is one or more
|
|
|
|
* altern '|' regexp alternatives separated by vertical bar.
|
|
|
|
*/
|
|
|
|
#define INITIAL_STACK_SIZE 128
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
ParseRegExp(CompilerState *state)
|
|
|
|
{
|
|
|
|
size_t parenIndex;
|
|
|
|
RENode *operand;
|
|
|
|
REOpData *operatorStack;
|
|
|
|
RENode **operandStack;
|
|
|
|
REOp op;
|
|
|
|
intN i;
|
|
|
|
JSBool result = JS_FALSE;
|
|
|
|
|
|
|
|
intN operatorSP = 0, operatorStackSize = INITIAL_STACK_SIZE;
|
|
|
|
intN operandSP = 0, operandStackSize = INITIAL_STACK_SIZE;
|
|
|
|
|
|
|
|
/* Watch out for empty regexp */
|
|
|
|
if (state->cp == state->cpend) {
|
|
|
|
state->result = NewRENode(state, REOP_EMPTY);
|
|
|
|
return (state->result != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
operatorStack = (REOpData *)
|
2009-07-27 21:10:12 -07:00
|
|
|
state->context->malloc(sizeof(REOpData) * operatorStackSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!operatorStack)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
operandStack = (RENode **)
|
2009-07-27 21:10:12 -07:00
|
|
|
state->context->malloc(sizeof(RENode *) * operandStackSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!operandStack)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
parenIndex = state->parenCount;
|
|
|
|
if (state->cp == state->cpend) {
|
|
|
|
/*
|
|
|
|
* If we are at the end of the regexp and we're short one or more
|
|
|
|
* operands, the regexp must have the form /x|/ or some such, with
|
|
|
|
* left parentheses making us short more than one operand.
|
|
|
|
*/
|
|
|
|
if (operatorSP >= operandSP) {
|
|
|
|
operand = NewRENode(state, REOP_EMPTY);
|
|
|
|
if (!operand)
|
|
|
|
goto out;
|
|
|
|
goto pushOperand;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (*state->cp) {
|
|
|
|
case '(':
|
|
|
|
++state->cp;
|
|
|
|
if (state->cp + 1 < state->cpend &&
|
|
|
|
*state->cp == '?' &&
|
|
|
|
(state->cp[1] == '=' ||
|
|
|
|
state->cp[1] == '!' ||
|
|
|
|
state->cp[1] == ':')) {
|
|
|
|
switch (state->cp[1]) {
|
|
|
|
case '=':
|
|
|
|
op = REOP_ASSERT;
|
|
|
|
/* ASSERT, <next>, ... ASSERTTEST */
|
|
|
|
state->progLength += 4;
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
op = REOP_ASSERT_NOT;
|
|
|
|
/* ASSERTNOT, <next>, ... ASSERTNOTTEST */
|
|
|
|
state->progLength += 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
op = REOP_LPARENNON;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state->cp += 2;
|
|
|
|
} else {
|
|
|
|
op = REOP_LPAREN;
|
|
|
|
/* LPAREN, <index>, ... RPAREN, <index> */
|
|
|
|
state->progLength
|
|
|
|
+= 2 * (1 + GetCompactIndexWidth(parenIndex));
|
|
|
|
state->parenCount++;
|
|
|
|
if (state->parenCount == 65535) {
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpError(state, JSREPORT_ERROR,
|
|
|
|
JSMSG_TOO_MANY_PARENS);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto pushOperator;
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
/*
|
|
|
|
* If there's no stacked open parenthesis, throw syntax error.
|
|
|
|
*/
|
|
|
|
for (i = operatorSP - 1; ; i--) {
|
|
|
|
if (i < 0) {
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpError(state, JSREPORT_ERROR,
|
|
|
|
JSMSG_UNMATCHED_RIGHT_PAREN);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (operatorStack[i].op == REOP_ASSERT ||
|
|
|
|
operatorStack[i].op == REOP_ASSERT_NOT ||
|
|
|
|
operatorStack[i].op == REOP_LPARENNON ||
|
|
|
|
operatorStack[i].op == REOP_LPAREN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
case '|':
|
|
|
|
/* Expected an operand before these, so make an empty one */
|
|
|
|
operand = NewRENode(state, REOP_EMPTY);
|
|
|
|
if (!operand)
|
|
|
|
goto out;
|
|
|
|
goto pushOperand;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (!ParseTerm(state))
|
|
|
|
goto out;
|
|
|
|
operand = state->result;
|
|
|
|
pushOperand:
|
|
|
|
if (operandSP == operandStackSize) {
|
2007-05-30 23:31:05 -07:00
|
|
|
RENode **tmp;
|
2007-03-22 10:30:00 -07:00
|
|
|
operandStackSize += operandStackSize;
|
2007-05-30 23:31:05 -07:00
|
|
|
tmp = (RENode **)
|
2009-07-27 21:10:12 -07:00
|
|
|
state->context->realloc(operandStack,
|
|
|
|
sizeof(RENode *) * operandStackSize);
|
2007-05-30 23:31:05 -07:00
|
|
|
if (!tmp)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
2007-05-30 23:31:05 -07:00
|
|
|
operandStack = tmp;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
operandStack[operandSP++] = operand;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At the end; process remaining operators. */
|
|
|
|
restartOperator:
|
|
|
|
if (state->cp == state->cpend) {
|
|
|
|
while (operatorSP) {
|
|
|
|
--operatorSP;
|
|
|
|
if (!ProcessOp(state, &operatorStack[operatorSP],
|
|
|
|
operandStack, operandSP))
|
|
|
|
goto out;
|
|
|
|
--operandSP;
|
|
|
|
}
|
|
|
|
JS_ASSERT(operandSP == 1);
|
|
|
|
state->result = operandStack[0];
|
|
|
|
result = JS_TRUE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*state->cp) {
|
|
|
|
case '|':
|
|
|
|
/* Process any stacked 'concat' operators */
|
|
|
|
++state->cp;
|
|
|
|
while (operatorSP &&
|
|
|
|
operatorStack[operatorSP - 1].op == REOP_CONCAT) {
|
|
|
|
--operatorSP;
|
|
|
|
if (!ProcessOp(state, &operatorStack[operatorSP],
|
|
|
|
operandStack, operandSP)) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
--operandSP;
|
|
|
|
}
|
|
|
|
op = REOP_ALT;
|
|
|
|
goto pushOperator;
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
/*
|
|
|
|
* If there's no stacked open parenthesis, throw syntax error.
|
|
|
|
*/
|
|
|
|
for (i = operatorSP - 1; ; i--) {
|
|
|
|
if (i < 0) {
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpError(state, JSREPORT_ERROR,
|
|
|
|
JSMSG_UNMATCHED_RIGHT_PAREN);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (operatorStack[i].op == REOP_ASSERT ||
|
|
|
|
operatorStack[i].op == REOP_ASSERT_NOT ||
|
|
|
|
operatorStack[i].op == REOP_LPARENNON ||
|
|
|
|
operatorStack[i].op == REOP_LPAREN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++state->cp;
|
|
|
|
|
|
|
|
/* Process everything on the stack until the open parenthesis. */
|
|
|
|
for (;;) {
|
|
|
|
JS_ASSERT(operatorSP);
|
|
|
|
--operatorSP;
|
|
|
|
switch (operatorStack[operatorSP].op) {
|
|
|
|
case REOP_ASSERT:
|
|
|
|
case REOP_ASSERT_NOT:
|
|
|
|
case REOP_LPAREN:
|
|
|
|
operand = NewRENode(state, operatorStack[operatorSP].op);
|
|
|
|
if (!operand)
|
|
|
|
goto out;
|
|
|
|
operand->u.parenIndex =
|
|
|
|
operatorStack[operatorSP].parenIndex;
|
|
|
|
JS_ASSERT(operandSP);
|
|
|
|
operand->kid = operandStack[operandSP - 1];
|
|
|
|
operandStack[operandSP - 1] = operand;
|
|
|
|
if (state->treeDepth == TREE_DEPTH_MAX) {
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpError(state, JSREPORT_ERROR,
|
|
|
|
JSMSG_REGEXP_TOO_COMPLEX);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
++state->treeDepth;
|
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
case REOP_LPARENNON:
|
|
|
|
state->result = operandStack[operandSP - 1];
|
|
|
|
if (!ParseQuantifier(state))
|
|
|
|
goto out;
|
|
|
|
operandStack[operandSP - 1] = state->result;
|
|
|
|
goto restartOperator;
|
|
|
|
default:
|
|
|
|
if (!ProcessOp(state, &operatorStack[operatorSP],
|
|
|
|
operandStack, operandSP))
|
|
|
|
goto out;
|
|
|
|
--operandSP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
{
|
|
|
|
const jschar *errp = state->cp;
|
|
|
|
|
|
|
|
if (ParseMinMaxQuantifier(state, JS_TRUE) < 0) {
|
|
|
|
/*
|
|
|
|
* This didn't even scan correctly as a quantifier, so we should
|
|
|
|
* treat it as flat.
|
|
|
|
*/
|
|
|
|
op = REOP_CONCAT;
|
|
|
|
goto pushOperator;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->cp = errp;
|
|
|
|
/* FALL THROUGH */
|
|
|
|
}
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
case '*':
|
|
|
|
case '?':
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpErrorHelper(state, JSREPORT_ERROR, JSMSG_BAD_QUANTIFIER,
|
|
|
|
state->cp);
|
2007-03-22 10:30:00 -07:00
|
|
|
result = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Anything else is the start of the next term. */
|
|
|
|
op = REOP_CONCAT;
|
|
|
|
pushOperator:
|
|
|
|
if (operatorSP == operatorStackSize) {
|
2007-05-30 23:31:05 -07:00
|
|
|
REOpData *tmp;
|
2007-03-22 10:30:00 -07:00
|
|
|
operatorStackSize += operatorStackSize;
|
2007-05-30 23:31:05 -07:00
|
|
|
tmp = (REOpData *)
|
2009-07-27 21:10:12 -07:00
|
|
|
state->context->realloc(operatorStack,
|
|
|
|
sizeof(REOpData) * operatorStackSize);
|
2007-05-30 23:31:05 -07:00
|
|
|
if (!tmp)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
2007-05-30 23:31:05 -07:00
|
|
|
operatorStack = tmp;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
operatorStack[operatorSP].op = op;
|
|
|
|
operatorStack[operatorSP].errPos = state->cp;
|
|
|
|
operatorStack[operatorSP++].parenIndex = parenIndex;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (operatorStack)
|
2009-07-27 21:10:12 -07:00
|
|
|
state->context->free(operatorStack);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (operandStack)
|
2009-07-27 21:10:12 -07:00
|
|
|
state->context->free(operandStack);
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hack two bits in CompilerState.flags, for use within FindParenCount to flag
|
|
|
|
* its being on the stack, and to propagate errors to its callers.
|
|
|
|
*/
|
|
|
|
#define JSREG_FIND_PAREN_COUNT 0x8000
|
|
|
|
#define JSREG_FIND_PAREN_ERROR 0x4000
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Magic return value from FindParenCount and GetDecimalValue, to indicate
|
|
|
|
* overflow beyond GetDecimalValue's max parameter, or a computed maximum if
|
|
|
|
* its findMax parameter is non-null.
|
|
|
|
*/
|
|
|
|
#define OVERFLOW_VALUE ((uintN)-1)
|
|
|
|
|
|
|
|
static uintN
|
|
|
|
FindParenCount(CompilerState *state)
|
|
|
|
{
|
|
|
|
CompilerState temp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (state->flags & JSREG_FIND_PAREN_COUNT)
|
|
|
|
return OVERFLOW_VALUE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy state into temp, flag it so we never report an invalid backref,
|
|
|
|
* and reset its members to parse the entire regexp. This is obviously
|
|
|
|
* suboptimal, but GetDecimalValue calls us only if a backref appears to
|
|
|
|
* refer to a forward parenthetical, which is rare.
|
|
|
|
*/
|
|
|
|
temp = *state;
|
|
|
|
temp.flags |= JSREG_FIND_PAREN_COUNT;
|
|
|
|
temp.cp = temp.cpbegin;
|
|
|
|
temp.parenCount = 0;
|
|
|
|
temp.classCount = 0;
|
|
|
|
temp.progLength = 0;
|
|
|
|
temp.treeDepth = 0;
|
|
|
|
temp.classBitmapsMem = 0;
|
|
|
|
for (i = 0; i < CLASS_CACHE_SIZE; i++)
|
|
|
|
temp.classCache[i].start = NULL;
|
|
|
|
|
|
|
|
if (!ParseRegExp(&temp)) {
|
|
|
|
state->flags |= JSREG_FIND_PAREN_ERROR;
|
|
|
|
return OVERFLOW_VALUE;
|
|
|
|
}
|
|
|
|
return temp.parenCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract and return a decimal value at state->cp. The initial character c
|
|
|
|
* has already been read. Return OVERFLOW_VALUE if the result exceeds max.
|
|
|
|
* Callers who pass a non-null findMax should test JSREG_FIND_PAREN_ERROR in
|
|
|
|
* state->flags to discover whether an error occurred under findMax.
|
|
|
|
*/
|
|
|
|
static uintN
|
|
|
|
GetDecimalValue(jschar c, uintN max, uintN (*findMax)(CompilerState *state),
|
|
|
|
CompilerState *state)
|
|
|
|
{
|
|
|
|
uintN value = JS7_UNDEC(c);
|
|
|
|
JSBool overflow = (value > max && (!findMax || value > findMax(state)));
|
|
|
|
|
|
|
|
/* The following restriction allows simpler overflow checks. */
|
|
|
|
JS_ASSERT(max <= ((uintN)-1 - 9) / 10);
|
|
|
|
while (state->cp < state->cpend) {
|
|
|
|
c = *state->cp;
|
|
|
|
if (!JS7_ISDEC(c))
|
|
|
|
break;
|
|
|
|
value = 10 * value + JS7_UNDEC(c);
|
|
|
|
if (!overflow && value > max && (!findMax || value > findMax(state)))
|
|
|
|
overflow = JS_TRUE;
|
|
|
|
++state->cp;
|
|
|
|
}
|
|
|
|
return overflow ? OVERFLOW_VALUE : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the total size of the bitmap required for a class expression.
|
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
CalculateBitmapSize(CompilerState *state, RENode *target, const jschar *src,
|
|
|
|
const jschar *end)
|
|
|
|
{
|
|
|
|
uintN max = 0;
|
|
|
|
JSBool inRange = JS_FALSE;
|
|
|
|
jschar c, rangeStart = 0;
|
|
|
|
uintN n, digit, nDigits, i;
|
|
|
|
|
|
|
|
target->u.ucclass.bmsize = 0;
|
|
|
|
target->u.ucclass.sense = JS_TRUE;
|
|
|
|
|
|
|
|
if (src == end)
|
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
if (*src == '^') {
|
|
|
|
++src;
|
|
|
|
target->u.ucclass.sense = JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (src != end) {
|
2007-12-11 12:13:42 -08:00
|
|
|
JSBool canStartRange = JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
uintN localMax = 0;
|
2007-12-11 12:13:42 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (*src) {
|
|
|
|
case '\\':
|
|
|
|
++src;
|
|
|
|
c = *src++;
|
|
|
|
switch (c) {
|
|
|
|
case 'b':
|
|
|
|
localMax = 0x8;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
localMax = 0xC;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
localMax = 0xA;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
localMax = 0xD;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
localMax = 0x9;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
localMax = 0xB;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (src < end && RE_IS_LETTER(*src)) {
|
2008-01-16 00:31:15 -08:00
|
|
|
localMax = (uintN) (*src++) & 0x1F;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
--src;
|
|
|
|
localMax = '\\';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
nDigits = 2;
|
|
|
|
goto lexHex;
|
|
|
|
case 'u':
|
|
|
|
nDigits = 4;
|
|
|
|
lexHex:
|
|
|
|
n = 0;
|
|
|
|
for (i = 0; (i < nDigits) && (src < end); i++) {
|
|
|
|
c = *src++;
|
|
|
|
if (!isASCIIHexDigit(c, &digit)) {
|
|
|
|
/*
|
|
|
|
* Back off to accepting the original
|
|
|
|
*'\' as a literal.
|
|
|
|
*/
|
|
|
|
src -= i + 1;
|
|
|
|
n = '\\';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
n = (n << 4) | digit;
|
|
|
|
}
|
|
|
|
localMax = n;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2007-12-11 12:13:42 -08:00
|
|
|
canStartRange = JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (inRange) {
|
|
|
|
JS_ReportErrorNumber(state->context,
|
|
|
|
js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_CLASS_RANGE);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
localMax = '9';
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
case 'w':
|
|
|
|
case 'W':
|
2007-12-11 12:13:42 -08:00
|
|
|
canStartRange = JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (inRange) {
|
|
|
|
JS_ReportErrorNumber(state->context,
|
|
|
|
js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_CLASS_RANGE);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-05-23 14:13:13 -07:00
|
|
|
max = 65535;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is the start of a range, ensure that it's less than
|
|
|
|
* the end.
|
|
|
|
*/
|
|
|
|
localMax = 0;
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
/*
|
|
|
|
* This is a non-ECMA extension - decimal escapes (in this
|
|
|
|
* case, octal!) are supposed to be an error inside class
|
|
|
|
* ranges, but supported here for backwards compatibility.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
n = JS7_UNDEC(c);
|
|
|
|
c = *src;
|
|
|
|
if ('0' <= c && c <= '7') {
|
|
|
|
src++;
|
|
|
|
n = 8 * n + JS7_UNDEC(c);
|
|
|
|
c = *src;
|
|
|
|
if ('0' <= c && c <= '7') {
|
|
|
|
src++;
|
|
|
|
i = 8 * n + JS7_UNDEC(c);
|
|
|
|
if (i <= 0377)
|
|
|
|
n = i;
|
|
|
|
else
|
|
|
|
src--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
localMax = n;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
localMax = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
localMax = *src++;
|
|
|
|
break;
|
|
|
|
}
|
2008-02-11 12:23:28 -08:00
|
|
|
|
2008-04-22 09:26:37 -07:00
|
|
|
if (inRange) {
|
2008-03-19 23:59:34 -07:00
|
|
|
/* Throw a SyntaxError here, per ECMA-262, 15.10.2.15. */
|
2008-04-22 09:26:37 -07:00
|
|
|
if (rangeStart > localMax) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ReportErrorNumber(state->context,
|
|
|
|
js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_CLASS_RANGE);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
inRange = JS_FALSE;
|
|
|
|
} else {
|
2007-12-11 12:13:42 -08:00
|
|
|
if (canStartRange && src < end - 1) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (*src == '-') {
|
|
|
|
++src;
|
|
|
|
inRange = JS_TRUE;
|
|
|
|
rangeStart = (jschar)localMax;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2008-04-22 09:26:37 -07:00
|
|
|
if (state->flags & JSREG_FOLD)
|
|
|
|
rangeStart = localMax; /* one run of the uc/dc loop below */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->flags & JSREG_FOLD) {
|
|
|
|
jschar maxch = localMax;
|
|
|
|
|
|
|
|
for (i = rangeStart; i <= localMax; i++) {
|
|
|
|
jschar uch, dch;
|
2008-06-11 13:07:24 -07:00
|
|
|
|
2008-04-22 09:26:37 -07:00
|
|
|
uch = upcase(i);
|
2009-12-23 18:48:53 -08:00
|
|
|
dch = inverse_upcase(i);
|
2008-04-22 09:26:37 -07:00
|
|
|
maxch = JS_MAX(maxch, uch);
|
|
|
|
maxch = JS_MAX(maxch, dch);
|
|
|
|
}
|
|
|
|
localMax = maxch;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-04-22 09:26:37 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (localMax > max)
|
|
|
|
max = localMax;
|
|
|
|
}
|
|
|
|
target->u.ucclass.bmsize = max;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* item: assertion An item is either an assertion or
|
|
|
|
* quantatom a quantified atom.
|
|
|
|
*
|
|
|
|
* assertion: '^' Assertions match beginning of string
|
|
|
|
* (or line if the class static property
|
|
|
|
* RegExp.multiline is true).
|
|
|
|
* '$' End of string (or line if the class
|
|
|
|
* static property RegExp.multiline is
|
|
|
|
* true).
|
|
|
|
* '\b' Word boundary (between \w and \W).
|
|
|
|
* '\B' Word non-boundary.
|
|
|
|
*
|
|
|
|
* quantatom: atom An unquantified atom.
|
|
|
|
* quantatom '{' n ',' m '}'
|
|
|
|
* Atom must occur between n and m times.
|
|
|
|
* quantatom '{' n ',' '}' Atom must occur at least n times.
|
|
|
|
* quantatom '{' n '}' Atom must occur exactly n times.
|
|
|
|
* quantatom '*' Zero or more times (same as {0,}).
|
|
|
|
* quantatom '+' One or more times (same as {1,}).
|
|
|
|
* quantatom '?' Zero or one time (same as {0,1}).
|
|
|
|
*
|
|
|
|
* any of which can be optionally followed by '?' for ungreedy
|
|
|
|
*
|
|
|
|
* atom: '(' regexp ')' A parenthesized regexp (what matched
|
|
|
|
* can be addressed using a backreference,
|
|
|
|
* see '\' n below).
|
|
|
|
* '.' Matches any char except '\n'.
|
|
|
|
* '[' classlist ']' A character class.
|
|
|
|
* '[' '^' classlist ']' A negated character class.
|
|
|
|
* '\f' Form Feed.
|
|
|
|
* '\n' Newline (Line Feed).
|
|
|
|
* '\r' Carriage Return.
|
|
|
|
* '\t' Horizontal Tab.
|
|
|
|
* '\v' Vertical Tab.
|
|
|
|
* '\d' A digit (same as [0-9]).
|
|
|
|
* '\D' A non-digit.
|
|
|
|
* '\w' A word character, [0-9a-z_A-Z].
|
|
|
|
* '\W' A non-word character.
|
|
|
|
* '\s' A whitespace character, [ \b\f\n\r\t\v].
|
|
|
|
* '\S' A non-whitespace character.
|
|
|
|
* '\' n A backreference to the nth (n decimal
|
|
|
|
* and positive) parenthesized expression.
|
|
|
|
* '\' octal An octal escape sequence (octal must be
|
|
|
|
* two or three digits long, unless it is
|
|
|
|
* 0 for the null character).
|
|
|
|
* '\x' hex A hex escape (hex must be two digits).
|
|
|
|
* '\u' unicode A unicode escape (must be four digits).
|
|
|
|
* '\c' ctrl A control character, ctrl is a letter.
|
|
|
|
* '\' literalatomchar Any character except one of the above
|
|
|
|
* that follow '\' in an atom.
|
|
|
|
* otheratomchar Any character not first among the other
|
|
|
|
* atom right-hand sides.
|
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
ParseTerm(CompilerState *state)
|
|
|
|
{
|
|
|
|
jschar c = *state->cp++;
|
|
|
|
uintN nDigits;
|
|
|
|
uintN num, tmp, n, i;
|
|
|
|
const jschar *termStart;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
/* assertions and atoms */
|
|
|
|
case '^':
|
|
|
|
state->result = NewRENode(state, REOP_BOL);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->progLength++;
|
|
|
|
return JS_TRUE;
|
|
|
|
case '$':
|
|
|
|
state->result = NewRENode(state, REOP_EOL);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->progLength++;
|
|
|
|
return JS_TRUE;
|
|
|
|
case '\\':
|
|
|
|
if (state->cp >= state->cpend) {
|
|
|
|
/* a trailing '\' is an error */
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpError(state, JSREPORT_ERROR, JSMSG_TRAILING_SLASH);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
c = *state->cp++;
|
|
|
|
switch (c) {
|
|
|
|
/* assertion escapes */
|
|
|
|
case 'b' :
|
|
|
|
state->result = NewRENode(state, REOP_WBDRY);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->progLength++;
|
|
|
|
return JS_TRUE;
|
|
|
|
case 'B':
|
|
|
|
state->result = NewRENode(state, REOP_WNONBDRY);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->progLength++;
|
|
|
|
return JS_TRUE;
|
|
|
|
/* Decimal escape */
|
|
|
|
case '0':
|
|
|
|
/* Give a strict warning. See also the note below. */
|
2007-11-13 14:18:17 -08:00
|
|
|
if (!ReportRegExpError(state, JSREPORT_WARNING | JSREPORT_STRICT,
|
|
|
|
JSMSG_INVALID_BACKREF)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
doOctal:
|
|
|
|
num = 0;
|
|
|
|
while (state->cp < state->cpend) {
|
|
|
|
c = *state->cp;
|
|
|
|
if (c < '0' || '7' < c)
|
|
|
|
break;
|
|
|
|
state->cp++;
|
|
|
|
tmp = 8 * num + (uintN)JS7_UNDEC(c);
|
|
|
|
if (tmp > 0377)
|
|
|
|
break;
|
|
|
|
num = tmp;
|
|
|
|
}
|
|
|
|
c = (jschar)num;
|
|
|
|
doFlat:
|
|
|
|
state->result = NewRENode(state, REOP_FLAT);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->result->u.flat.chr = c;
|
|
|
|
state->result->u.flat.length = 1;
|
|
|
|
state->progLength += 3;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
termStart = state->cp - 1;
|
|
|
|
num = GetDecimalValue(c, state->parenCount, FindParenCount, state);
|
|
|
|
if (state->flags & JSREG_FIND_PAREN_ERROR)
|
|
|
|
return JS_FALSE;
|
|
|
|
if (num == OVERFLOW_VALUE) {
|
|
|
|
/* Give a strict mode warning. */
|
2007-11-13 14:18:17 -08:00
|
|
|
if (!ReportRegExpError(state,
|
|
|
|
JSREPORT_WARNING | JSREPORT_STRICT,
|
|
|
|
(c >= '8')
|
|
|
|
? JSMSG_INVALID_BACKREF
|
|
|
|
: JSMSG_BAD_BACKREF)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: ECMA 262, 15.10.2.9 says that we should throw a syntax
|
|
|
|
* error here. However, for compatibility with IE, we treat the
|
|
|
|
* whole backref as flat if the first character in it is not a
|
|
|
|
* valid octal character, and as an octal escape otherwise.
|
|
|
|
*/
|
|
|
|
state->cp = termStart;
|
|
|
|
if (c >= '8') {
|
|
|
|
/* Treat this as flat. termStart - 1 is the \. */
|
|
|
|
c = '\\';
|
|
|
|
goto asFlat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Treat this as an octal escape. */
|
|
|
|
goto doOctal;
|
|
|
|
}
|
2009-03-20 04:30:40 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* When FindParenCount calls the regex parser recursively (to find
|
|
|
|
* the number of backrefs) num can be arbitrary and the maximum
|
|
|
|
* supported number of backrefs does not bound it.
|
|
|
|
*/
|
|
|
|
JS_ASSERT_IF(!(state->flags & JSREG_FIND_PAREN_COUNT),
|
|
|
|
1 <= num && num <= 0x10000);
|
2007-03-22 10:30:00 -07:00
|
|
|
state->result = NewRENode(state, REOP_BACKREF);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->result->u.parenIndex = num - 1;
|
|
|
|
state->progLength
|
|
|
|
+= 1 + GetCompactIndexWidth(state->result->u.parenIndex);
|
|
|
|
break;
|
|
|
|
/* Control escape */
|
|
|
|
case 'f':
|
|
|
|
c = 0xC;
|
|
|
|
goto doFlat;
|
|
|
|
case 'n':
|
|
|
|
c = 0xA;
|
|
|
|
goto doFlat;
|
|
|
|
case 'r':
|
|
|
|
c = 0xD;
|
|
|
|
goto doFlat;
|
|
|
|
case 't':
|
|
|
|
c = 0x9;
|
|
|
|
goto doFlat;
|
|
|
|
case 'v':
|
|
|
|
c = 0xB;
|
|
|
|
goto doFlat;
|
|
|
|
/* Control letter */
|
|
|
|
case 'c':
|
|
|
|
if (state->cp < state->cpend && RE_IS_LETTER(*state->cp)) {
|
|
|
|
c = (jschar) (*state->cp++ & 0x1F);
|
|
|
|
} else {
|
|
|
|
/* back off to accepting the original '\' as a literal */
|
|
|
|
--state->cp;
|
|
|
|
c = '\\';
|
|
|
|
}
|
|
|
|
goto doFlat;
|
|
|
|
/* HexEscapeSequence */
|
|
|
|
case 'x':
|
|
|
|
nDigits = 2;
|
|
|
|
goto lexHex;
|
|
|
|
/* UnicodeEscapeSequence */
|
|
|
|
case 'u':
|
|
|
|
nDigits = 4;
|
|
|
|
lexHex:
|
|
|
|
n = 0;
|
|
|
|
for (i = 0; i < nDigits && state->cp < state->cpend; i++) {
|
|
|
|
uintN digit;
|
|
|
|
c = *state->cp++;
|
|
|
|
if (!isASCIIHexDigit(c, &digit)) {
|
|
|
|
/*
|
|
|
|
* Back off to accepting the original 'u' or 'x' as a
|
|
|
|
* literal.
|
|
|
|
*/
|
|
|
|
state->cp -= i + 2;
|
|
|
|
n = *state->cp++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
n = (n << 4) | digit;
|
|
|
|
}
|
|
|
|
c = (jschar) n;
|
|
|
|
goto doFlat;
|
|
|
|
/* Character class escapes */
|
|
|
|
case 'd':
|
|
|
|
state->result = NewRENode(state, REOP_DIGIT);
|
|
|
|
doSimple:
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->progLength++;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
state->result = NewRENode(state, REOP_NONDIGIT);
|
|
|
|
goto doSimple;
|
|
|
|
case 's':
|
|
|
|
state->result = NewRENode(state, REOP_SPACE);
|
|
|
|
goto doSimple;
|
|
|
|
case 'S':
|
|
|
|
state->result = NewRENode(state, REOP_NONSPACE);
|
|
|
|
goto doSimple;
|
|
|
|
case 'w':
|
|
|
|
state->result = NewRENode(state, REOP_ALNUM);
|
|
|
|
goto doSimple;
|
|
|
|
case 'W':
|
|
|
|
state->result = NewRENode(state, REOP_NONALNUM);
|
|
|
|
goto doSimple;
|
|
|
|
/* IdentityEscape */
|
|
|
|
default:
|
|
|
|
state->result = NewRENode(state, REOP_FLAT);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->result->u.flat.chr = c;
|
|
|
|
state->result->u.flat.length = 1;
|
|
|
|
state->result->kid = (void *) (state->cp - 1);
|
|
|
|
state->progLength += 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
state->result = NewRENode(state, REOP_CLASS);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
termStart = state->cp;
|
|
|
|
state->result->u.ucclass.startIndex = termStart - state->cpbegin;
|
|
|
|
for (;;) {
|
|
|
|
if (state->cp == state->cpend) {
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpErrorHelper(state, JSREPORT_ERROR,
|
|
|
|
JSMSG_UNTERM_CLASS, termStart);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
if (*state->cp == '\\') {
|
|
|
|
state->cp++;
|
|
|
|
if (state->cp != state->cpend)
|
|
|
|
state->cp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*state->cp == ']') {
|
|
|
|
state->result->u.ucclass.kidlen = state->cp - termStart;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state->cp++;
|
|
|
|
}
|
|
|
|
for (i = 0; i < CLASS_CACHE_SIZE; i++) {
|
|
|
|
if (!state->classCache[i].start) {
|
|
|
|
state->classCache[i].start = termStart;
|
|
|
|
state->classCache[i].length = state->result->u.ucclass.kidlen;
|
|
|
|
state->classCache[i].index = state->classCount;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (state->classCache[i].length ==
|
|
|
|
state->result->u.ucclass.kidlen) {
|
|
|
|
for (n = 0; ; n++) {
|
|
|
|
if (n == state->classCache[i].length) {
|
|
|
|
state->result->u.ucclass.index
|
|
|
|
= state->classCache[i].index;
|
|
|
|
goto claim;
|
|
|
|
}
|
|
|
|
if (state->classCache[i].start[n] != termStart[n])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state->result->u.ucclass.index = state->classCount++;
|
|
|
|
|
|
|
|
claim:
|
|
|
|
/*
|
|
|
|
* Call CalculateBitmapSize now as we want any errors it finds
|
|
|
|
* to be reported during the parse phase, not at execution.
|
|
|
|
*/
|
|
|
|
if (!CalculateBitmapSize(state, state->result, termStart, state->cp++))
|
|
|
|
return JS_FALSE;
|
|
|
|
/*
|
|
|
|
* Update classBitmapsMem with number of bytes to hold bmsize bits,
|
|
|
|
* which is (bitsCount + 7) / 8 or (highest_bit + 1 + 7) / 8
|
|
|
|
* or highest_bit / 8 + 1 where highest_bit is u.ucclass.bmsize.
|
|
|
|
*/
|
|
|
|
n = (state->result->u.ucclass.bmsize >> 3) + 1;
|
|
|
|
if (n > CLASS_BITMAPS_MEM_LIMIT - state->classBitmapsMem) {
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpError(state, JSREPORT_ERROR, JSMSG_REGEXP_TOO_COMPLEX);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
state->classBitmapsMem += n;
|
|
|
|
/* CLASS, <index> */
|
|
|
|
state->progLength
|
|
|
|
+= 1 + GetCompactIndexWidth(state->result->u.ucclass.index);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
state->result = NewRENode(state, REOP_DOT);
|
|
|
|
goto doSimple;
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
{
|
|
|
|
const jschar *errp = state->cp--;
|
|
|
|
intN err;
|
|
|
|
|
|
|
|
err = ParseMinMaxQuantifier(state, JS_TRUE);
|
|
|
|
state->cp = errp;
|
|
|
|
|
|
|
|
if (err < 0)
|
|
|
|
goto asFlat;
|
|
|
|
|
|
|
|
/* FALL THROUGH */
|
|
|
|
}
|
|
|
|
case '*':
|
|
|
|
case '+':
|
|
|
|
case '?':
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpErrorHelper(state, JSREPORT_ERROR,
|
|
|
|
JSMSG_BAD_QUANTIFIER, state->cp - 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
default:
|
|
|
|
asFlat:
|
|
|
|
state->result = NewRENode(state, REOP_FLAT);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->result->u.flat.chr = c;
|
|
|
|
state->result->u.flat.length = 1;
|
|
|
|
state->result->kid = (void *) (state->cp - 1);
|
|
|
|
state->progLength += 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ParseQuantifier(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
ParseQuantifier(CompilerState *state)
|
|
|
|
{
|
|
|
|
RENode *term;
|
|
|
|
term = state->result;
|
|
|
|
if (state->cp < state->cpend) {
|
|
|
|
switch (*state->cp) {
|
|
|
|
case '+':
|
|
|
|
state->result = NewRENode(state, REOP_QUANT);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->result->u.range.min = 1;
|
|
|
|
state->result->u.range.max = (uintN)-1;
|
|
|
|
/* <PLUS>, <next> ... <ENDCHILD> */
|
|
|
|
state->progLength += 4;
|
|
|
|
goto quantifier;
|
|
|
|
case '*':
|
|
|
|
state->result = NewRENode(state, REOP_QUANT);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->result->u.range.min = 0;
|
|
|
|
state->result->u.range.max = (uintN)-1;
|
|
|
|
/* <STAR>, <next> ... <ENDCHILD> */
|
|
|
|
state->progLength += 4;
|
|
|
|
goto quantifier;
|
|
|
|
case '?':
|
|
|
|
state->result = NewRENode(state, REOP_QUANT);
|
|
|
|
if (!state->result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state->result->u.range.min = 0;
|
|
|
|
state->result->u.range.max = 1;
|
|
|
|
/* <OPT>, <next> ... <ENDCHILD> */
|
|
|
|
state->progLength += 4;
|
|
|
|
goto quantifier;
|
|
|
|
case '{': /* balance '}' */
|
|
|
|
{
|
|
|
|
intN err;
|
|
|
|
const jschar *errp = state->cp;
|
|
|
|
|
|
|
|
err = ParseMinMaxQuantifier(state, JS_FALSE);
|
|
|
|
if (err == 0)
|
|
|
|
goto quantifier;
|
|
|
|
if (err == -1)
|
|
|
|
return JS_TRUE;
|
|
|
|
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpErrorHelper(state, JSREPORT_ERROR, err, errp);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
quantifier:
|
|
|
|
if (state->treeDepth == TREE_DEPTH_MAX) {
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpError(state, JSREPORT_ERROR, JSMSG_REGEXP_TOO_COMPLEX);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
++state->treeDepth;
|
|
|
|
++state->cp;
|
|
|
|
state->result->kid = term;
|
|
|
|
if (state->cp < state->cpend && *state->cp == '?') {
|
|
|
|
++state->cp;
|
|
|
|
state->result->u.range.greedy = JS_FALSE;
|
|
|
|
} else {
|
|
|
|
state->result->u.range.greedy = JS_TRUE;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static intN
|
|
|
|
ParseMinMaxQuantifier(CompilerState *state, JSBool ignoreValues)
|
|
|
|
{
|
|
|
|
uintN min, max;
|
|
|
|
jschar c;
|
|
|
|
const jschar *errp = state->cp++;
|
|
|
|
|
|
|
|
c = *state->cp;
|
|
|
|
if (JS7_ISDEC(c)) {
|
|
|
|
++state->cp;
|
|
|
|
min = GetDecimalValue(c, 0xFFFF, NULL, state);
|
|
|
|
c = *state->cp;
|
|
|
|
|
|
|
|
if (!ignoreValues && min == OVERFLOW_VALUE)
|
|
|
|
return JSMSG_MIN_TOO_BIG;
|
|
|
|
|
|
|
|
if (c == ',') {
|
|
|
|
c = *++state->cp;
|
|
|
|
if (JS7_ISDEC(c)) {
|
|
|
|
++state->cp;
|
|
|
|
max = GetDecimalValue(c, 0xFFFF, NULL, state);
|
|
|
|
c = *state->cp;
|
|
|
|
if (!ignoreValues && max == OVERFLOW_VALUE)
|
|
|
|
return JSMSG_MAX_TOO_BIG;
|
|
|
|
if (!ignoreValues && min > max)
|
|
|
|
return JSMSG_OUT_OF_ORDER;
|
|
|
|
} else {
|
|
|
|
max = (uintN)-1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
max = min;
|
|
|
|
}
|
|
|
|
if (c == '}') {
|
|
|
|
state->result = NewRENode(state, REOP_QUANT);
|
|
|
|
if (!state->result)
|
2007-05-30 23:31:05 -07:00
|
|
|
return JSMSG_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
state->result->u.range.min = min;
|
|
|
|
state->result->u.range.max = max;
|
|
|
|
/*
|
|
|
|
* QUANT, <min>, <max>, <next> ... <ENDCHILD>
|
|
|
|
* where <max> is written as compact(max+1) to make
|
|
|
|
* (uintN)-1 sentinel to occupy 1 byte, not width_of(max)+1.
|
|
|
|
*/
|
|
|
|
state->progLength += (1 + GetCompactIndexWidth(min)
|
|
|
|
+ GetCompactIndexWidth(max + 1)
|
|
|
|
+3);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state->cp = errp;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
SetForwardJumpOffset(jsbytecode *jump, jsbytecode *target)
|
|
|
|
{
|
|
|
|
ptrdiff_t offset = target - jump;
|
|
|
|
|
|
|
|
/* Check that target really points forward. */
|
|
|
|
JS_ASSERT(offset >= 2);
|
|
|
|
if ((size_t)offset > OFFSET_MAX)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
jump[0] = JUMP_OFFSET_HI(offset);
|
|
|
|
jump[1] = JUMP_OFFSET_LO(offset);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-11-04 14:51:51 -08:00
|
|
|
/* Copy the charset data from a character class node to the charset list
|
|
|
|
* in the regexp object. */
|
|
|
|
static JS_ALWAYS_INLINE RECharSet *
|
|
|
|
InitNodeCharSet(JSRegExp *re, RENode *node)
|
|
|
|
{
|
|
|
|
RECharSet *charSet = &re->classList[node->u.ucclass.index];
|
|
|
|
charSet->converted = JS_FALSE;
|
|
|
|
charSet->length = node->u.ucclass.bmsize;
|
|
|
|
charSet->u.src.startIndex = node->u.ucclass.startIndex;
|
|
|
|
charSet->u.src.length = node->u.ucclass.kidlen;
|
|
|
|
charSet->sense = node->u.ucclass.sense;
|
|
|
|
return charSet;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Generate bytecode for the tree rooted at t using an explicit stack instead
|
|
|
|
* of recursion.
|
|
|
|
*/
|
|
|
|
static jsbytecode *
|
|
|
|
EmitREBytecode(CompilerState *state, JSRegExp *re, size_t treeDepth,
|
|
|
|
jsbytecode *pc, RENode *t)
|
|
|
|
{
|
|
|
|
EmitStateStackEntry *emitStateSP, *emitStateStack;
|
|
|
|
REOp op;
|
|
|
|
|
|
|
|
if (treeDepth == 0) {
|
|
|
|
emitStateStack = NULL;
|
|
|
|
} else {
|
|
|
|
emitStateStack =
|
2009-07-27 21:10:12 -07:00
|
|
|
(EmitStateStackEntry *)
|
|
|
|
state->context->malloc(sizeof(EmitStateStackEntry) * treeDepth);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!emitStateStack)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
emitStateSP = emitStateStack;
|
|
|
|
op = t->op;
|
|
|
|
JS_ASSERT(op < REOP_LIMIT);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
*pc++ = op;
|
|
|
|
switch (op) {
|
|
|
|
case REOP_EMPTY:
|
|
|
|
--pc;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REOP_ALTPREREQ2:
|
|
|
|
case REOP_ALTPREREQ:
|
|
|
|
JS_ASSERT(emitStateSP);
|
|
|
|
emitStateSP->altHead = pc - 1;
|
|
|
|
emitStateSP->endTermFixup = pc;
|
|
|
|
pc += OFFSET_LEN;
|
|
|
|
SET_ARG(pc, t->u.altprereq.ch1);
|
|
|
|
pc += ARG_LEN;
|
|
|
|
SET_ARG(pc, t->u.altprereq.ch2);
|
|
|
|
pc += ARG_LEN;
|
|
|
|
|
|
|
|
emitStateSP->nextAltFixup = pc; /* offset to next alternate */
|
|
|
|
pc += OFFSET_LEN;
|
|
|
|
|
|
|
|
emitStateSP->continueNode = t;
|
|
|
|
emitStateSP->continueOp = REOP_JUMP;
|
|
|
|
emitStateSP->jumpToJumpFlag = JS_FALSE;
|
|
|
|
++emitStateSP;
|
|
|
|
JS_ASSERT((size_t)(emitStateSP - emitStateStack) <= treeDepth);
|
|
|
|
t = (RENode *) t->kid;
|
|
|
|
op = t->op;
|
|
|
|
JS_ASSERT(op < REOP_LIMIT);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_JUMP:
|
|
|
|
emitStateSP->nextTermFixup = pc; /* offset to following term */
|
|
|
|
pc += OFFSET_LEN;
|
|
|
|
if (!SetForwardJumpOffset(emitStateSP->nextAltFixup, pc))
|
|
|
|
goto jump_too_big;
|
|
|
|
emitStateSP->continueOp = REOP_ENDALT;
|
|
|
|
++emitStateSP;
|
|
|
|
JS_ASSERT((size_t)(emitStateSP - emitStateStack) <= treeDepth);
|
2007-07-05 13:37:47 -07:00
|
|
|
t = (RENode *) t->u.kid2;
|
2007-03-22 10:30:00 -07:00
|
|
|
op = t->op;
|
|
|
|
JS_ASSERT(op < REOP_LIMIT);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_ENDALT:
|
|
|
|
/*
|
|
|
|
* If we already patched emitStateSP->nextTermFixup to jump to
|
|
|
|
* a nearer jump, to avoid 16-bit immediate offset overflow, we
|
|
|
|
* are done here.
|
|
|
|
*/
|
|
|
|
if (emitStateSP->jumpToJumpFlag)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fix up the REOP_JUMP offset to go to the op after REOP_ENDALT.
|
|
|
|
* REOP_ENDALT is executed only on successful match of the last
|
|
|
|
* alternate in a group.
|
|
|
|
*/
|
|
|
|
if (!SetForwardJumpOffset(emitStateSP->nextTermFixup, pc))
|
|
|
|
goto jump_too_big;
|
|
|
|
if (t->op != REOP_ALT) {
|
|
|
|
if (!SetForwardJumpOffset(emitStateSP->endTermFixup, pc))
|
|
|
|
goto jump_too_big;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the program is bigger than the REOP_JUMP offset range, then
|
|
|
|
* we must check for alternates before this one that are part of
|
|
|
|
* the same group, and fix up their jump offsets to target jumps
|
|
|
|
* close enough to fit in a 16-bit unsigned offset immediate.
|
|
|
|
*/
|
2008-11-10 12:49:56 -08:00
|
|
|
if ((size_t)(pc - re->program) > OFFSET_MAX &&
|
2007-03-22 10:30:00 -07:00
|
|
|
emitStateSP > emitStateStack) {
|
|
|
|
EmitStateStackEntry *esp, *esp2;
|
|
|
|
jsbytecode *alt, *jump;
|
|
|
|
ptrdiff_t span, header;
|
|
|
|
|
|
|
|
esp2 = emitStateSP;
|
|
|
|
alt = esp2->altHead;
|
|
|
|
for (esp = esp2 - 1; esp >= emitStateStack; --esp) {
|
|
|
|
if (esp->continueOp == REOP_ENDALT &&
|
|
|
|
!esp->jumpToJumpFlag &&
|
|
|
|
esp->nextTermFixup + OFFSET_LEN == alt &&
|
|
|
|
(size_t)(pc - ((esp->continueNode->op != REOP_ALT)
|
|
|
|
? esp->endTermFixup
|
|
|
|
: esp->nextTermFixup)) > OFFSET_MAX) {
|
|
|
|
alt = esp->altHead;
|
|
|
|
jump = esp->nextTermFixup;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The span must be 1 less than the distance from
|
|
|
|
* jump offset to jump offset, so we actually jump
|
|
|
|
* to a REOP_JUMP bytecode, not to its offset!
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
JS_ASSERT(jump < esp2->nextTermFixup);
|
|
|
|
span = esp2->nextTermFixup - jump - 1;
|
|
|
|
if ((size_t)span <= OFFSET_MAX)
|
|
|
|
break;
|
|
|
|
do {
|
|
|
|
if (--esp2 == esp)
|
|
|
|
goto jump_too_big;
|
|
|
|
} while (esp2->continueOp != REOP_ENDALT);
|
|
|
|
}
|
|
|
|
|
|
|
|
jump[0] = JUMP_OFFSET_HI(span);
|
|
|
|
jump[1] = JUMP_OFFSET_LO(span);
|
|
|
|
|
|
|
|
if (esp->continueNode->op != REOP_ALT) {
|
|
|
|
/*
|
|
|
|
* We must patch the offset at esp->endTermFixup
|
|
|
|
* as well, for the REOP_ALTPREREQ{,2} opcodes.
|
|
|
|
* If we're unlucky and endTermFixup is more than
|
|
|
|
* OFFSET_MAX bytes from its target, we cheat by
|
|
|
|
* jumping 6 bytes to the jump whose offset is at
|
|
|
|
* esp->nextTermFixup, which has the same target.
|
|
|
|
*/
|
|
|
|
jump = esp->endTermFixup;
|
|
|
|
header = esp->nextTermFixup - jump;
|
|
|
|
span += header;
|
|
|
|
if ((size_t)span > OFFSET_MAX)
|
|
|
|
span = header;
|
|
|
|
|
|
|
|
jump[0] = JUMP_OFFSET_HI(span);
|
|
|
|
jump[1] = JUMP_OFFSET_LO(span);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp->jumpToJumpFlag = JS_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REOP_ALT:
|
|
|
|
JS_ASSERT(emitStateSP);
|
|
|
|
emitStateSP->altHead = pc - 1;
|
|
|
|
emitStateSP->nextAltFixup = pc; /* offset to next alternate */
|
|
|
|
pc += OFFSET_LEN;
|
|
|
|
emitStateSP->continueNode = t;
|
|
|
|
emitStateSP->continueOp = REOP_JUMP;
|
|
|
|
emitStateSP->jumpToJumpFlag = JS_FALSE;
|
|
|
|
++emitStateSP;
|
|
|
|
JS_ASSERT((size_t)(emitStateSP - emitStateStack) <= treeDepth);
|
2007-07-05 13:37:47 -07:00
|
|
|
t = (RENode *) t->kid;
|
2007-03-22 10:30:00 -07:00
|
|
|
op = t->op;
|
|
|
|
JS_ASSERT(op < REOP_LIMIT);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_FLAT:
|
|
|
|
/*
|
|
|
|
* Coalesce FLATs if possible and if it would not increase bytecode
|
|
|
|
* beyond preallocated limit. The latter happens only when bytecode
|
|
|
|
* size for coalesced string with offset p and length 2 exceeds 6
|
|
|
|
* bytes preallocated for 2 single char nodes, i.e. when
|
|
|
|
* 1 + GetCompactIndexWidth(p) + GetCompactIndexWidth(2) > 6 or
|
|
|
|
* GetCompactIndexWidth(p) > 4.
|
|
|
|
* Since when GetCompactIndexWidth(p) <= 4 coalescing of 3 or more
|
|
|
|
* nodes strictly decreases bytecode size, the check has to be
|
|
|
|
* done only for the first coalescing.
|
|
|
|
*/
|
|
|
|
if (t->kid &&
|
|
|
|
GetCompactIndexWidth((jschar *)t->kid - state->cpbegin) <= 4)
|
|
|
|
{
|
|
|
|
while (t->next &&
|
|
|
|
t->next->op == REOP_FLAT &&
|
|
|
|
(jschar*)t->kid + t->u.flat.length ==
|
|
|
|
(jschar*)t->next->kid) {
|
|
|
|
t->u.flat.length += t->next->u.flat.length;
|
|
|
|
t->next = t->next->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (t->kid && t->u.flat.length > 1) {
|
|
|
|
pc[-1] = (state->flags & JSREG_FOLD) ? REOP_FLATi : REOP_FLAT;
|
|
|
|
pc = WriteCompactIndex(pc, (jschar *)t->kid - state->cpbegin);
|
|
|
|
pc = WriteCompactIndex(pc, t->u.flat.length);
|
|
|
|
} else if (t->u.flat.chr < 256) {
|
|
|
|
pc[-1] = (state->flags & JSREG_FOLD) ? REOP_FLAT1i : REOP_FLAT1;
|
|
|
|
*pc++ = (jsbytecode) t->u.flat.chr;
|
|
|
|
} else {
|
|
|
|
pc[-1] = (state->flags & JSREG_FOLD)
|
|
|
|
? REOP_UCFLAT1i
|
|
|
|
: REOP_UCFLAT1;
|
|
|
|
SET_ARG(pc, t->u.flat.chr);
|
|
|
|
pc += ARG_LEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REOP_LPAREN:
|
|
|
|
JS_ASSERT(emitStateSP);
|
|
|
|
pc = WriteCompactIndex(pc, t->u.parenIndex);
|
|
|
|
emitStateSP->continueNode = t;
|
|
|
|
emitStateSP->continueOp = REOP_RPAREN;
|
|
|
|
++emitStateSP;
|
|
|
|
JS_ASSERT((size_t)(emitStateSP - emitStateStack) <= treeDepth);
|
|
|
|
t = (RENode *) t->kid;
|
|
|
|
op = t->op;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_RPAREN:
|
|
|
|
pc = WriteCompactIndex(pc, t->u.parenIndex);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REOP_BACKREF:
|
|
|
|
pc = WriteCompactIndex(pc, t->u.parenIndex);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REOP_ASSERT:
|
|
|
|
JS_ASSERT(emitStateSP);
|
|
|
|
emitStateSP->nextTermFixup = pc;
|
|
|
|
pc += OFFSET_LEN;
|
|
|
|
emitStateSP->continueNode = t;
|
|
|
|
emitStateSP->continueOp = REOP_ASSERTTEST;
|
|
|
|
++emitStateSP;
|
|
|
|
JS_ASSERT((size_t)(emitStateSP - emitStateStack) <= treeDepth);
|
|
|
|
t = (RENode *) t->kid;
|
|
|
|
op = t->op;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_ASSERTTEST:
|
|
|
|
case REOP_ASSERTNOTTEST:
|
|
|
|
if (!SetForwardJumpOffset(emitStateSP->nextTermFixup, pc))
|
|
|
|
goto jump_too_big;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REOP_ASSERT_NOT:
|
|
|
|
JS_ASSERT(emitStateSP);
|
|
|
|
emitStateSP->nextTermFixup = pc;
|
|
|
|
pc += OFFSET_LEN;
|
|
|
|
emitStateSP->continueNode = t;
|
|
|
|
emitStateSP->continueOp = REOP_ASSERTNOTTEST;
|
|
|
|
++emitStateSP;
|
|
|
|
JS_ASSERT((size_t)(emitStateSP - emitStateStack) <= treeDepth);
|
|
|
|
t = (RENode *) t->kid;
|
|
|
|
op = t->op;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_QUANT:
|
|
|
|
JS_ASSERT(emitStateSP);
|
|
|
|
if (t->u.range.min == 0 && t->u.range.max == (uintN)-1) {
|
|
|
|
pc[-1] = (t->u.range.greedy) ? REOP_STAR : REOP_MINIMALSTAR;
|
|
|
|
} else if (t->u.range.min == 0 && t->u.range.max == 1) {
|
|
|
|
pc[-1] = (t->u.range.greedy) ? REOP_OPT : REOP_MINIMALOPT;
|
|
|
|
} else if (t->u.range.min == 1 && t->u.range.max == (uintN) -1) {
|
|
|
|
pc[-1] = (t->u.range.greedy) ? REOP_PLUS : REOP_MINIMALPLUS;
|
|
|
|
} else {
|
|
|
|
if (!t->u.range.greedy)
|
|
|
|
pc[-1] = REOP_MINIMALQUANT;
|
|
|
|
pc = WriteCompactIndex(pc, t->u.range.min);
|
|
|
|
/*
|
|
|
|
* Write max + 1 to avoid using size_t(max) + 1 bytes
|
|
|
|
* for (uintN)-1 sentinel.
|
|
|
|
*/
|
|
|
|
pc = WriteCompactIndex(pc, t->u.range.max + 1);
|
|
|
|
}
|
|
|
|
emitStateSP->nextTermFixup = pc;
|
|
|
|
pc += OFFSET_LEN;
|
|
|
|
emitStateSP->continueNode = t;
|
|
|
|
emitStateSP->continueOp = REOP_ENDCHILD;
|
|
|
|
++emitStateSP;
|
|
|
|
JS_ASSERT((size_t)(emitStateSP - emitStateStack) <= treeDepth);
|
|
|
|
t = (RENode *) t->kid;
|
|
|
|
op = t->op;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_ENDCHILD:
|
|
|
|
if (!SetForwardJumpOffset(emitStateSP->nextTermFixup, pc))
|
|
|
|
goto jump_too_big;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REOP_CLASS:
|
|
|
|
if (!t->u.ucclass.sense)
|
|
|
|
pc[-1] = REOP_NCLASS;
|
|
|
|
pc = WriteCompactIndex(pc, t->u.ucclass.index);
|
2008-11-04 14:51:51 -08:00
|
|
|
InitNodeCharSet(re, t);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = t->next;
|
|
|
|
if (t) {
|
|
|
|
op = t->op;
|
|
|
|
} else {
|
|
|
|
if (emitStateSP == emitStateStack)
|
|
|
|
break;
|
|
|
|
--emitStateSP;
|
|
|
|
t = emitStateSP->continueNode;
|
2007-07-05 13:37:47 -07:00
|
|
|
op = (REOp) emitStateSP->continueOp;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (emitStateStack)
|
2009-07-27 21:10:12 -07:00
|
|
|
state->context->free(emitStateStack);
|
2007-03-22 10:30:00 -07:00
|
|
|
return pc;
|
|
|
|
|
|
|
|
jump_too_big:
|
2007-11-13 14:18:17 -08:00
|
|
|
ReportRegExpError(state, JSREPORT_ERROR, JSMSG_REGEXP_TOO_COMPLEX);
|
2007-03-22 10:30:00 -07:00
|
|
|
pc = NULL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2008-11-21 17:54:53 -08:00
|
|
|
static JSBool
|
|
|
|
CompileRegExpToAST(JSContext* cx, JSTokenStream* ts,
|
|
|
|
JSString* str, uintN flags, CompilerState& state)
|
|
|
|
{
|
|
|
|
uintN i;
|
|
|
|
size_t len;
|
|
|
|
|
2009-06-10 18:29:44 -07:00
|
|
|
len = str->length();
|
2008-11-21 17:54:53 -08:00
|
|
|
|
|
|
|
state.context = cx;
|
|
|
|
state.tokenStream = ts;
|
|
|
|
state.cp = js_UndependString(cx, str);
|
|
|
|
if (!state.cp)
|
|
|
|
return JS_FALSE;
|
|
|
|
state.cpbegin = state.cp;
|
|
|
|
state.cpend = state.cp + len;
|
|
|
|
state.flags = flags;
|
|
|
|
state.parenCount = 0;
|
|
|
|
state.classCount = 0;
|
|
|
|
state.progLength = 0;
|
|
|
|
state.treeDepth = 0;
|
|
|
|
state.classBitmapsMem = 0;
|
|
|
|
for (i = 0; i < CLASS_CACHE_SIZE; i++)
|
|
|
|
state.classCache[i].start = NULL;
|
|
|
|
|
|
|
|
if (len != 0 && (flags & JSREG_FLAT)) {
|
|
|
|
state.result = NewRENode(&state, REOP_FLAT);
|
|
|
|
if (!state.result)
|
|
|
|
return JS_FALSE;
|
|
|
|
state.result->u.flat.chr = *state.cpbegin;
|
|
|
|
state.result->u.flat.length = len;
|
|
|
|
state.result->kid = (void *) state.cpbegin;
|
|
|
|
/* Flat bytecode: REOP_FLAT compact(string_offset) compact(len). */
|
|
|
|
state.progLength += 1 + GetCompactIndexWidth(0)
|
|
|
|
+ GetCompactIndexWidth(len);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2009-07-27 18:40:12 -07:00
|
|
|
|
2008-11-21 17:54:53 -08:00
|
|
|
return ParseRegExp(&state);
|
|
|
|
}
|
|
|
|
|
2008-11-04 16:03:57 -08:00
|
|
|
#ifdef JS_TRACER
|
2009-09-01 18:46:19 -07:00
|
|
|
typedef js::Vector<LIns *, 4, js::ContextAllocPolicy> LInsList;
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2010-01-22 14:49:18 -08:00
|
|
|
namespace js {
|
|
|
|
|
2009-11-05 15:04:54 -08:00
|
|
|
struct REFragment : public nanojit::Fragment
|
|
|
|
{
|
|
|
|
REFragment(const void* _ip verbose_only(, uint32_t profFragID))
|
|
|
|
: nanojit::Fragment(ip verbose_only(, profFragID))
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2010-01-22 14:49:18 -08:00
|
|
|
} /* namespace js */
|
|
|
|
|
2009-08-18 15:32:54 -07:00
|
|
|
/* Return the cached fragment for the given regexp, or create one. */
|
2009-06-10 18:29:44 -07:00
|
|
|
static Fragment*
|
2009-08-18 15:32:54 -07:00
|
|
|
LookupNativeRegExp(JSContext* cx, uint16 re_flags,
|
2009-06-10 18:29:44 -07:00
|
|
|
const jschar* re_chars, size_t re_length)
|
2008-11-20 16:37:36 -08:00
|
|
|
{
|
2010-01-22 14:49:18 -08:00
|
|
|
TraceMonitor *tm = &JS_TRACE_MONITOR(cx);
|
2009-09-22 12:36:20 -07:00
|
|
|
VMAllocator &alloc = *tm->dataAlloc;
|
2009-08-18 15:32:54 -07:00
|
|
|
REHashMap &table = *tm->reFragments;
|
|
|
|
|
|
|
|
REHashKey k(re_length, re_flags, re_chars);
|
2009-11-05 15:04:54 -08:00
|
|
|
REFragment *frag = table.get(k);
|
2009-08-18 15:32:54 -07:00
|
|
|
|
|
|
|
if (!frag) {
|
2009-09-15 15:05:53 -07:00
|
|
|
verbose_only(
|
2010-01-22 14:49:18 -08:00
|
|
|
uint32_t profFragID = (LogController.lcbits & LC_FragProfile)
|
2009-09-15 15:05:53 -07:00
|
|
|
? (++(tm->lastFragID)) : 0;
|
|
|
|
)
|
2009-11-05 15:04:54 -08:00
|
|
|
frag = new (alloc) REFragment(0 verbose_only(, profFragID));
|
2009-08-18 15:32:54 -07:00
|
|
|
/*
|
|
|
|
* Copy the re_chars portion of the hash key into the Allocator, so
|
|
|
|
* its lifecycle is disconnected from the lifecycle of the
|
|
|
|
* underlying regexp.
|
|
|
|
*/
|
|
|
|
k.re_chars = (const jschar*) new (alloc) jschar[re_length];
|
|
|
|
memcpy((void*) k.re_chars, re_chars, re_length * sizeof(jschar));
|
|
|
|
table.put(k, frag);
|
2008-11-20 16:37:36 -08:00
|
|
|
}
|
2009-08-18 15:32:54 -07:00
|
|
|
return frag;
|
2008-11-20 16:37:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
ProcessCharSet(JSContext *cx, JSRegExp *re, RECharSet *charSet);
|
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
/* Utilities for the RegExpNativeCompiler */
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/*
|
|
|
|
* An efficient way to simultaneously statically guard that the sizeof(bool) is a
|
|
|
|
* small power of 2 and take its log2.
|
|
|
|
*/
|
|
|
|
template <int> struct StaticLog2 {};
|
|
|
|
template <> struct StaticLog2<1> { static const int result = 0; };
|
|
|
|
template <> struct StaticLog2<2> { static const int result = 1; };
|
|
|
|
template <> struct StaticLog2<4> { static const int result = 2; };
|
|
|
|
template <> struct StaticLog2<8> { static const int result = 3; };
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This table allows efficient testing for the ASCII portion of \s during a
|
2009-08-07 20:09:11 -07:00
|
|
|
* trace. ECMA-262 15.10.2.12 defines the following characters below 128 to be
|
|
|
|
* whitespace: 0x9 (0), 0xA (10), 0xB (11), 0xC (12), 0xD (13), 0x20 (32). The
|
2009-07-16 17:17:35 -07:00
|
|
|
* index must be <= 32.
|
|
|
|
*/
|
|
|
|
static const bool js_ws[] = {
|
|
|
|
/* 0 1 2 3 4 5 5 7 8 9 */
|
|
|
|
/* 0 */ false, false, false, false, false, false, false, false, false, true,
|
|
|
|
/* 1 */ true, true, true, true, false, false, false, false, false, false,
|
|
|
|
/* 2 */ false, false, false, false, false, false, false, false, false, false,
|
|
|
|
/* 3 */ false, false, true
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Sets of characters are described in terms of individuals and classes. */
|
|
|
|
class CharSet {
|
|
|
|
public:
|
|
|
|
CharSet() : charEnd(charBuf), classes(0) {}
|
|
|
|
|
2009-11-30 09:03:43 -08:00
|
|
|
static const uintN sBufSize = 8;
|
|
|
|
|
|
|
|
bool full() { return charEnd == charBuf + sBufSize; }
|
2009-07-16 17:17:35 -07:00
|
|
|
|
2009-07-21 15:19:40 -07:00
|
|
|
/* Add a single char to the set. */
|
|
|
|
bool addChar(jschar c)
|
|
|
|
{
|
|
|
|
if (full())
|
|
|
|
return false;
|
|
|
|
*charEnd++ = c;
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-16 17:17:35 -07:00
|
|
|
|
|
|
|
enum Class {
|
|
|
|
LineTerms = 1 << 0, /* Line Terminators (E262 7.3) */
|
|
|
|
OtherSpace = 1 << 1, /* \s (E262 15.10.2.12) - LineTerms */
|
|
|
|
Digit = 1 << 2, /* \d (E262 15.10.2.12) */
|
|
|
|
OtherAlnum = 1 << 3, /* \w (E262 15,10.2.12) - Digit */
|
|
|
|
Other = 1 << 4, /* all other characters */
|
|
|
|
All = LineTerms | OtherSpace | Digit | OtherAlnum | Other,
|
|
|
|
|
|
|
|
Space = LineTerms | OtherSpace,
|
|
|
|
AlNum = Digit | OtherAlnum,
|
|
|
|
Dot = All & ~LineTerms
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Add a set of chars to the set. */
|
|
|
|
void addClass(Class c) { classes |= c; }
|
|
|
|
|
|
|
|
/* Return whether two sets of chars are disjoint. */
|
|
|
|
bool disjoint(const CharSet &) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static bool disjoint(const jschar *beg, const jschar *end, uintN classes);
|
|
|
|
|
2009-11-30 09:03:43 -08:00
|
|
|
mutable jschar charBuf[sBufSize];
|
2009-07-16 17:17:35 -07:00
|
|
|
jschar *charEnd;
|
|
|
|
uintN classes;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Appease the type checker. */
|
|
|
|
static inline CharSet::Class
|
|
|
|
operator|(CharSet::Class c1, CharSet::Class c2) {
|
|
|
|
return (CharSet::Class)(((int)c1) | ((int)c2));
|
|
|
|
}
|
|
|
|
static inline CharSet::Class
|
|
|
|
operator~(CharSet::Class c) {
|
|
|
|
return (CharSet::Class)(~(int)c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return whether the characters in the range [beg, end) fall within any of the
|
|
|
|
* classes with a bit set in 'classes'.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
CharSet::disjoint(const jschar *beg, const jschar *end, uintN classes)
|
|
|
|
{
|
|
|
|
for (const jschar *p = beg; p != end; ++p) {
|
|
|
|
if (JS7_ISDEC(*p)) {
|
|
|
|
if (classes & Digit)
|
|
|
|
return false;
|
|
|
|
} else if (JS_ISWORD(*p)) {
|
|
|
|
if (classes & OtherAlnum)
|
|
|
|
return false;
|
|
|
|
} else if (RE_IS_LINE_TERM(*p)) {
|
|
|
|
if (classes & LineTerms)
|
|
|
|
return false;
|
|
|
|
} else if (JS_ISSPACE(*p)) {
|
|
|
|
if (classes & OtherSpace)
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (classes & Other)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-08-07 20:09:11 -07:00
|
|
|
* Predicate version of the STL's set_intersection. Assumes both ranges are
|
2009-07-16 17:17:35 -07:00
|
|
|
* sorted and thus runs in linear time.
|
|
|
|
*
|
|
|
|
* FIXME: This is a reusable algorithm, perhaps it should be put somewhere.
|
|
|
|
*/
|
|
|
|
template <class InputIterator1, class InputIterator2>
|
|
|
|
bool
|
|
|
|
set_disjoint(InputIterator1 p1, InputIterator1 end1,
|
|
|
|
InputIterator2 p2, InputIterator2 end2)
|
|
|
|
{
|
|
|
|
if (p1 == end1 || p2 == end2)
|
|
|
|
return true;
|
|
|
|
while (*p1 != *p2) {
|
|
|
|
if (*p1 < *p2) {
|
|
|
|
++p1;
|
|
|
|
if (p1 == end1)
|
|
|
|
return true;
|
|
|
|
} else if (*p2 < *p1) {
|
|
|
|
++p2;
|
|
|
|
if (p2 == end2)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-30 09:03:43 -08:00
|
|
|
static JSBool
|
|
|
|
CharCmp(void *arg, const void *a, const void *b, int *result)
|
|
|
|
{
|
|
|
|
jschar ca = *(jschar *)a, cb = *(jschar *)b;
|
|
|
|
*result = ca - cb;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
bool
|
|
|
|
CharSet::disjoint(const CharSet &other) const
|
|
|
|
{
|
|
|
|
/* Check overlap between classes. */
|
|
|
|
if (classes & other.classes)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check char-class overlap. Compare this->charBuf with other.classes and
|
|
|
|
* vice versa with a loop.
|
|
|
|
*/
|
|
|
|
if (!disjoint(this->charBuf, this->charEnd, other.classes) ||
|
|
|
|
!disjoint(other.charBuf, other.charEnd, this->classes))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Check char-char overlap. */
|
2009-11-30 09:03:43 -08:00
|
|
|
jschar tmp[CharSet::sBufSize];
|
|
|
|
js_MergeSort(charBuf, charEnd - charBuf, sizeof(jschar),
|
|
|
|
CharCmp, 0, tmp);
|
|
|
|
js_MergeSort(other.charBuf, other.charEnd - other.charBuf, sizeof(jschar),
|
|
|
|
CharCmp, 0, tmp);
|
2009-07-16 17:17:35 -07:00
|
|
|
return set_disjoint(charBuf, charEnd, other.charBuf, other.charEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-08-07 20:09:11 -07:00
|
|
|
* Return true if the given subexpression may match the empty string. The
|
|
|
|
* conservative answer is |true|. If |next| is true, then the subexpression is
|
|
|
|
* considered to be |node| followed by the rest of |node->next|. Otherwise, the
|
2009-07-16 17:17:35 -07:00
|
|
|
* subexpression is considered to be |node| by itself.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
mayMatchEmpty(RENode *node, bool next = true)
|
|
|
|
{
|
|
|
|
if (!node)
|
|
|
|
return true;
|
|
|
|
switch (node->op) {
|
|
|
|
case REOP_EMPTY: return true;
|
|
|
|
case REOP_FLAT: return false;
|
|
|
|
case REOP_CLASS: return false;
|
|
|
|
case REOP_ALNUM: return false;
|
|
|
|
case REOP_ALT: return (mayMatchEmpty((RENode *)node->kid) ||
|
|
|
|
mayMatchEmpty((RENode *)node->u.kid2)) &&
|
|
|
|
(!next || mayMatchEmpty(node->next));
|
|
|
|
case REOP_QUANT: return (node->u.range.min == 0 ||
|
|
|
|
mayMatchEmpty((RENode *)node->kid)) &&
|
|
|
|
(!next || mayMatchEmpty(node->next));
|
|
|
|
default: return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enumerate the set of characters that may be consumed next by the given
|
2009-08-07 20:09:11 -07:00
|
|
|
* subexpression in isolation. Return whether the enumeration was successful.
|
2009-07-16 17:17:35 -07:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
enumerateNextChars(JSContext *cx, RENode *node, CharSet &set)
|
|
|
|
{
|
|
|
|
JS_CHECK_RECURSION(cx, return JS_FALSE);
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
switch (node->op) {
|
|
|
|
/* Record as bitflags. */
|
|
|
|
case REOP_DOT: set.addClass(CharSet::Dot); return true;
|
|
|
|
case REOP_DIGIT: set.addClass(CharSet::Digit); return true;
|
|
|
|
case REOP_NONDIGIT: set.addClass(~CharSet::Digit); return true;
|
|
|
|
case REOP_ALNUM: set.addClass(CharSet::AlNum); return true;
|
|
|
|
case REOP_NONALNUM: set.addClass(~CharSet::AlNum); return true;
|
|
|
|
case REOP_SPACE: set.addClass(CharSet::Space); return true;
|
|
|
|
case REOP_NONSPACE: set.addClass(~CharSet::Space); return true;
|
|
|
|
|
|
|
|
/* Record as individual characters. */
|
|
|
|
case REOP_FLAT:
|
2009-07-21 15:19:40 -07:00
|
|
|
return set.addChar(node->u.flat.chr);
|
2009-07-16 17:17:35 -07:00
|
|
|
|
|
|
|
/* Control structures. */
|
|
|
|
case REOP_EMPTY:
|
|
|
|
return true;
|
|
|
|
case REOP_ALT:
|
|
|
|
case REOP_ALTPREREQ:
|
|
|
|
return enumerateNextChars(cx, (RENode *)node->kid, set) &&
|
|
|
|
enumerateNextChars(cx, (RENode *)node->u.kid2, set) &&
|
|
|
|
(!mayMatchEmpty(node, false) ||
|
|
|
|
enumerateNextChars(cx, (RENode *)node->next, set));
|
|
|
|
case REOP_QUANT:
|
|
|
|
return enumerateNextChars(cx, (RENode *)node->kid, set) &&
|
|
|
|
(!mayMatchEmpty(node, false) ||
|
|
|
|
enumerateNextChars(cx, (RENode *)node->next, set));
|
|
|
|
|
|
|
|
/* Arbitrary character classes and oddities. */
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 14:51:51 -08:00
|
|
|
class RegExpNativeCompiler {
|
|
|
|
private:
|
2009-09-23 15:12:58 -07:00
|
|
|
VMAllocator& tempAlloc;
|
2008-11-20 16:37:36 -08:00
|
|
|
JSContext* cx;
|
|
|
|
JSRegExp* re;
|
|
|
|
CompilerState* cs; /* RegExp to compile */
|
2008-11-05 11:38:18 -08:00
|
|
|
Fragment* fragment;
|
|
|
|
LirWriter* lir;
|
2009-09-09 15:53:26 -07:00
|
|
|
#ifdef DEBUG
|
2010-01-21 18:41:24 -08:00
|
|
|
LirWriter* validate_writer;
|
2009-09-09 15:53:26 -07:00
|
|
|
#endif
|
|
|
|
#ifdef NJ_VERBOSE
|
|
|
|
LirWriter* verbose_filter;
|
|
|
|
#endif
|
2008-11-20 16:37:36 -08:00
|
|
|
LirBufWriter* lirBufWriter; /* for skip */
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2008-11-05 11:38:18 -08:00
|
|
|
LIns* state;
|
2009-11-24 11:22:20 -08:00
|
|
|
LIns* start;
|
2008-11-05 11:38:18 -08:00
|
|
|
LIns* cpend;
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2009-12-07 21:06:41 -08:00
|
|
|
LirBuffer* const lirbuf;
|
|
|
|
|
2009-09-22 12:36:20 -07:00
|
|
|
bool outOfMemory() {
|
|
|
|
return tempAlloc.outOfMemory() || JS_TRACE_MONITOR(cx).dataAlloc->outOfMemory();
|
|
|
|
}
|
|
|
|
|
2009-02-27 11:34:32 -08:00
|
|
|
JSBool isCaseInsensitive() const { return (cs->flags & JSREG_FOLD) != 0; }
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
void targetCurrentPoint(LIns *ins)
|
2008-12-31 11:43:50 -08:00
|
|
|
{
|
2009-07-16 17:17:35 -07:00
|
|
|
ins->setTarget(lir->ins0(LIR_label));
|
2008-12-31 11:43:50 -08:00
|
|
|
}
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
void targetCurrentPoint(LInsList &fails)
|
2008-11-05 11:38:18 -08:00
|
|
|
{
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns *fail = lir->ins0(LIR_label);
|
2009-08-14 16:10:59 -07:00
|
|
|
for (size_t i = 0; i < fails.length(); ++i) {
|
2009-05-09 11:38:34 -07:00
|
|
|
fails[i]->setTarget(fail);
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
|
|
|
fails.clear();
|
|
|
|
}
|
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
/*
|
2008-11-12 17:55:56 -08:00
|
|
|
* These functions return the new position after their match operation,
|
|
|
|
* or NULL if there was an error.
|
|
|
|
*/
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns* compileEmpty(RENode* node, LIns* pos, LInsList& fails)
|
2008-11-05 11:38:18 -08:00
|
|
|
{
|
2008-11-12 17:55:56 -08:00
|
|
|
return pos;
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
|
|
|
|
2009-07-19 23:11:12 -07:00
|
|
|
#if defined(AVMPLUS_ARM) || defined(AVMPLUS_SPARC)
|
|
|
|
/* We can't do this on ARM or SPARC, since it relies on doing a 32-bit load from
|
2009-07-16 17:17:35 -07:00
|
|
|
* a pointer which is only 2-byte aligned.
|
|
|
|
*/
|
|
|
|
#undef USE_DOUBLE_CHAR_MATCH
|
|
|
|
#else
|
|
|
|
#define USE_DOUBLE_CHAR_MATCH
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LIns* compileFlatSingleChar(jschar ch, LIns* pos, LInsList& fails)
|
2008-11-05 11:38:18 -08:00
|
|
|
{
|
2009-09-09 15:53:26 -07:00
|
|
|
LIns* to_fail = lir->insBranch(LIR_jf, lir->ins2(LIR_plt, pos, cpend), 0);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(to_fail))
|
|
|
|
return NULL;
|
2009-07-13 12:41:30 -07:00
|
|
|
LIns* text_ch = lir->insLoad(LIR_ldcs, pos, 0);
|
|
|
|
|
|
|
|
// Extra characters that need to be compared against when doing folding.
|
|
|
|
struct extra {
|
|
|
|
jschar ch;
|
|
|
|
LIns *match;
|
|
|
|
};
|
|
|
|
extra extras[5];
|
|
|
|
int nextras = 0;
|
|
|
|
|
2008-11-04 14:51:51 -08:00
|
|
|
if (cs->flags & JSREG_FOLD) {
|
2009-07-13 12:41:30 -07:00
|
|
|
ch = JS_TOUPPER(ch);
|
2009-12-23 18:48:53 -08:00
|
|
|
jschar lch = inverse_upcase(ch);
|
2009-07-13 12:41:30 -07:00
|
|
|
|
|
|
|
if (ch != lch) {
|
|
|
|
if (L'A' <= ch && ch <= L'Z') {
|
|
|
|
// Fast conversion of text character to lower case by OR-ing with 32.
|
|
|
|
text_ch = lir->ins2(LIR_or, text_ch, lir->insImm(32));
|
|
|
|
// These ASCII letters have 2 lower-case forms. We put the ASCII one in
|
|
|
|
// |extras| so it is tested first, because we expect that to be the common
|
|
|
|
// case. Note that the code points of the non-ASCII forms both have the
|
|
|
|
// 32 bit set, so it is OK to compare against the OR-32-converted text char.
|
|
|
|
ch = lch;
|
|
|
|
if (ch == L'i') {
|
|
|
|
extras[nextras++].ch = ch;
|
|
|
|
ch = 0x131;
|
|
|
|
} else if (ch == L's') {
|
|
|
|
extras[nextras++].ch = ch;
|
|
|
|
ch = 0x17f;
|
|
|
|
}
|
|
|
|
goto gen;
|
|
|
|
} else if (0x01c4 <= ch && ch <= 0x1e60) {
|
|
|
|
// The following group of conditionals handles characters that have 1 or 2
|
|
|
|
// lower-case forms in addition to JS_TOLOWER(ch).
|
|
|
|
if (ch <= 0x1f1) { // DZ,LJ,NJ
|
|
|
|
if (ch == 0x01c4) {
|
|
|
|
extras[nextras++].ch = 0x01c5;
|
|
|
|
} else if (ch == 0x01c7) {
|
|
|
|
extras[nextras++].ch = 0x01c8;
|
|
|
|
} else if (ch == 0x01ca) {
|
|
|
|
extras[nextras++].ch = 0x01cb;
|
|
|
|
} else if (ch == 0x01f1) {
|
|
|
|
extras[nextras++].ch = 0x01f2;
|
|
|
|
}
|
|
|
|
} else if (ch < 0x0392) { // no extra lower-case forms in this range
|
|
|
|
} else if (ch <= 0x03a6) { // Greek
|
|
|
|
if (ch == 0x0392) {
|
|
|
|
extras[nextras++].ch = 0x03d0;
|
|
|
|
} else if (ch == 0x0395) {
|
|
|
|
extras[nextras++].ch = 0x03f5;
|
|
|
|
} else if (ch == 0x0398) {
|
|
|
|
extras[nextras++].ch = 0x03d1;
|
|
|
|
} else if (ch == 0x0399) {
|
|
|
|
extras[nextras++].ch = 0x0345;
|
|
|
|
extras[nextras++].ch = 0x1fbe;
|
|
|
|
} else if (ch == 0x039a) {
|
|
|
|
extras[nextras++].ch = 0x03f0;
|
|
|
|
} else if (ch == 0x039c) {
|
|
|
|
extras[nextras++].ch = 0xb5;
|
|
|
|
} else if (ch == 0x03a0) {
|
|
|
|
extras[nextras++].ch = 0x03d6;
|
|
|
|
} else if (ch == 0x03a1) {
|
|
|
|
extras[nextras++].ch = 0x03f1;
|
|
|
|
} else if (ch == 0x03a3) {
|
|
|
|
extras[nextras++].ch = 0x03c2;
|
|
|
|
} else if (ch == 0x03a6) {
|
|
|
|
extras[nextras++].ch = 0x03d5;
|
|
|
|
}
|
|
|
|
} else if (ch == 0x1e60) { // S with dot above
|
|
|
|
extras[nextras++].ch = 0x1e9b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extras[nextras++].ch = lch;
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-13 12:41:30 -07:00
|
|
|
gen:
|
|
|
|
for (int i = 0; i < nextras; ++i) {
|
|
|
|
LIns *test = lir->ins2(LIR_eq, text_ch, lir->insImm(extras[i].ch));
|
|
|
|
LIns *branch = lir->insBranch(LIR_jt, test, 0);
|
|
|
|
extras[i].match = branch;
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
2009-07-27 18:40:12 -07:00
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jf, lir->ins2(LIR_eq, text_ch, lir->insImm(ch)), 0)))
|
|
|
|
return NULL;
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
for (int i = 0; i < nextras; ++i)
|
|
|
|
targetCurrentPoint(extras[i].match);
|
2009-09-09 15:53:26 -07:00
|
|
|
return lir->ins2(LIR_piadd, pos, lir->insImmWord(2));
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
|
|
|
|
2009-07-27 18:40:12 -07:00
|
|
|
JS_INLINE bool hasCases(jschar ch)
|
2009-07-13 12:41:30 -07:00
|
|
|
{
|
|
|
|
return JS_TOLOWER(ch) != JS_TOUPPER(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIns* compileFlatDoubleChar(jschar ch1, jschar ch2, LIns* pos, LInsList& fails)
|
2009-02-06 13:41:21 -08:00
|
|
|
{
|
2009-02-15 18:10:03 -08:00
|
|
|
#ifdef IS_BIG_ENDIAN
|
|
|
|
uint32 word = (ch1 << 16) | ch2;
|
|
|
|
#else
|
2009-02-06 13:41:21 -08:00
|
|
|
uint32 word = (ch2 << 16) | ch1;
|
2009-02-15 18:10:03 -08:00
|
|
|
#endif
|
2009-02-13 11:11:16 -08:00
|
|
|
/*
|
2009-02-06 13:41:21 -08:00
|
|
|
* Fast case-insensitive test for ASCII letters: convert text
|
|
|
|
* char to lower case by bit-or-ing in 32 and compare.
|
|
|
|
*/
|
|
|
|
JSBool useFastCI = JS_FALSE;
|
|
|
|
union { jschar c[2]; uint32 i; } mask;
|
|
|
|
if (cs->flags & JSREG_FOLD) {
|
2009-07-13 12:41:30 -07:00
|
|
|
jschar uch1 = JS_TOUPPER(ch1);
|
|
|
|
jschar uch2 = JS_TOUPPER(ch2);
|
|
|
|
JSBool mask1 = (L'A' <= uch1 && uch1 <= L'Z' && uch1 != L'I' && uch1 != L'S');
|
|
|
|
JSBool mask2 = (L'A' <= uch2 && uch2 <= L'Z' && uch2 != L'I' && uch2 != L'S');
|
|
|
|
if ((!mask1 && hasCases(ch1)) || (!mask2 && hasCases(ch2))) {
|
2009-02-06 13:41:21 -08:00
|
|
|
pos = compileFlatSingleChar(ch1, pos, fails);
|
|
|
|
if (!pos) return NULL;
|
|
|
|
return compileFlatSingleChar(ch2, pos, fails);
|
|
|
|
}
|
2009-02-13 11:11:16 -08:00
|
|
|
|
|
|
|
mask.c[0] = mask1 ? 0x0020 : 0x0;
|
|
|
|
mask.c[1] = mask2 ? 0x0020 : 0x0;
|
2009-02-06 13:41:21 -08:00
|
|
|
|
|
|
|
if (mask.i) {
|
|
|
|
word |= mask.i;
|
|
|
|
useFastCI = JS_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 15:53:26 -07:00
|
|
|
LIns* to_fail = lir->insBranch(LIR_jf,
|
|
|
|
lir->ins2(LIR_plt,
|
|
|
|
pos,
|
|
|
|
lir->ins2(LIR_piadd,
|
|
|
|
cpend,
|
|
|
|
lir->insImmWord(-2))),
|
|
|
|
0);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(to_fail))
|
|
|
|
return NULL;
|
2009-07-06 16:26:12 -07:00
|
|
|
LIns* text_word = lir->insLoad(LIR_ld, pos, 0);
|
2009-02-13 11:11:16 -08:00
|
|
|
LIns* comp_word = useFastCI ?
|
2009-02-06 13:41:21 -08:00
|
|
|
lir->ins2(LIR_or, text_word, lir->insImm(mask.i)) :
|
|
|
|
text_word;
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jf, lir->ins2(LIR_eq, comp_word, lir->insImm(word)), 0)))
|
|
|
|
return NULL;
|
2009-02-06 13:41:21 -08:00
|
|
|
|
2009-09-09 15:53:26 -07:00
|
|
|
return lir->ins2(LIR_piadd, pos, lir->insImmWord(4));
|
2009-02-06 13:41:21 -08:00
|
|
|
}
|
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns* compileFlat(RENode *&node, LIns* pos, LInsList& fails)
|
|
|
|
{
|
|
|
|
#ifdef USE_DOUBLE_CHAR_MATCH
|
|
|
|
if (node->u.flat.length == 1) {
|
|
|
|
if (node->next && node->next->op == REOP_FLAT &&
|
|
|
|
node->next->u.flat.length == 1) {
|
|
|
|
pos = compileFlatDoubleChar(node->u.flat.chr,
|
|
|
|
node->next->u.flat.chr,
|
|
|
|
pos, fails);
|
|
|
|
node = node->next;
|
|
|
|
} else {
|
|
|
|
pos = compileFlatSingleChar(node->u.flat.chr, pos, fails);
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
} else {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < node->u.flat.length - 1; i += 2) {
|
2009-09-22 12:36:20 -07:00
|
|
|
if (outOfMemory())
|
2009-07-16 17:17:35 -07:00
|
|
|
return 0;
|
|
|
|
pos = compileFlatDoubleChar(((jschar*) node->kid)[i],
|
|
|
|
((jschar*) node->kid)[i+1],
|
|
|
|
pos, fails);
|
|
|
|
if (!pos)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
JS_ASSERT(pos != 0);
|
|
|
|
if (i == node->u.flat.length - 1)
|
|
|
|
pos = compileFlatSingleChar(((jschar*) node->kid)[i], pos, fails);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (node->u.flat.length == 1) {
|
|
|
|
return compileFlatSingleChar(node->u.flat.chr, pos, fails);
|
|
|
|
} else {
|
|
|
|
for (size_t i = 0; i < node->u.flat.length; i++) {
|
2009-09-22 12:36:20 -07:00
|
|
|
if (outOfMemory())
|
2009-07-16 17:17:35 -07:00
|
|
|
return 0;
|
|
|
|
pos = compileFlatSingleChar(((jschar*) node->kid)[i], pos, fails);
|
|
|
|
if (!pos)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
LIns* compileClass(RENode* node, LIns* pos, LInsList& fails)
|
2008-11-05 11:38:18 -08:00
|
|
|
{
|
2008-11-20 16:37:36 -08:00
|
|
|
if (!node->u.ucclass.sense)
|
2008-11-05 11:38:18 -08:00
|
|
|
return JS_FALSE;
|
2009-07-16 17:17:35 -07:00
|
|
|
/*
|
2008-11-20 16:37:36 -08:00
|
|
|
* If we share generated native code, we need to make a copy
|
2009-07-16 17:17:35 -07:00
|
|
|
* of the bitmap because the original regexp's copy is destroyed when
|
|
|
|
* that regexp is.
|
2008-11-20 16:37:36 -08:00
|
|
|
*/
|
|
|
|
RECharSet *charSet = &re->classList[node->u.ucclass.index];
|
|
|
|
size_t bitmapLen = (charSet->length >> 3) + 1;
|
2009-09-22 16:06:52 -07:00
|
|
|
/* Arbitrary size limit on bitmap. */
|
2008-11-20 16:37:36 -08:00
|
|
|
if (bitmapLen > 1024)
|
|
|
|
return NULL;
|
2009-09-22 16:06:52 -07:00
|
|
|
Allocator &alloc = *JS_TRACE_MONITOR(cx).dataAlloc;
|
2008-11-20 16:37:36 -08:00
|
|
|
/* The following line allocates charSet.u.bits if successful. */
|
|
|
|
if (!charSet->converted && !ProcessCharSet(cx, re, charSet))
|
|
|
|
return NULL;
|
2009-09-22 16:06:52 -07:00
|
|
|
void* bitmapData = alloc.alloc(bitmapLen);
|
2009-09-22 12:36:20 -07:00
|
|
|
if (outOfMemory())
|
2008-11-20 16:37:36 -08:00
|
|
|
return NULL;
|
|
|
|
memcpy(bitmapData, charSet->u.bits, bitmapLen);
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2009-09-09 15:53:26 -07:00
|
|
|
LIns* to_fail = lir->insBranch(LIR_jf, lir->ins2(LIR_plt, pos, cpend), 0);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(to_fail))
|
|
|
|
return NULL;
|
2009-07-06 16:26:12 -07:00
|
|
|
LIns* text_ch = lir->insLoad(LIR_ldcs, pos, 0);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jf,
|
|
|
|
lir->ins2(LIR_le, text_ch, lir->insImm(charSet->length)),
|
|
|
|
0))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-09 15:53:26 -07:00
|
|
|
LIns* byteIndex = lir->ins_i2p(lir->ins2(LIR_rsh, text_ch, lir->insImm(3)));
|
2008-11-20 16:37:36 -08:00
|
|
|
LIns* bitmap = lir->insImmPtr(bitmapData);
|
2008-11-05 11:38:18 -08:00
|
|
|
LIns* byte = lir->insLoad(LIR_ldcb, lir->ins2(LIR_piadd, bitmap, byteIndex), (int) 0);
|
|
|
|
LIns* bitMask = lir->ins2(LIR_lsh, lir->insImm(1),
|
2008-11-04 14:51:51 -08:00
|
|
|
lir->ins2(LIR_and, text_ch, lir->insImm(0x7)));
|
2008-11-05 11:38:18 -08:00
|
|
|
LIns* test = lir->ins2(LIR_eq, lir->ins2(LIR_and, byte, bitMask), lir->insImm(0));
|
2009-07-16 17:17:35 -07:00
|
|
|
|
2008-11-05 11:38:18 -08:00
|
|
|
LIns* to_next = lir->insBranch(LIR_jt, test, 0);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(to_next))
|
|
|
|
return NULL;
|
2009-09-09 15:53:26 -07:00
|
|
|
return lir->ins2(LIR_piadd, pos, lir->insImmWord(2));
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
/* Factor out common code to index js_alnum. */
|
|
|
|
LIns *compileTableRead(LIns *chr, const bool *tbl)
|
2008-11-05 11:38:18 -08:00
|
|
|
{
|
2009-07-16 17:17:35 -07:00
|
|
|
if (sizeof(bool) != 1) {
|
|
|
|
LIns *sizeLog2 = lir->insImm(StaticLog2<sizeof(bool)>::result);
|
|
|
|
chr = lir->ins2(LIR_lsh, chr, sizeLog2);
|
|
|
|
}
|
2009-09-09 15:53:26 -07:00
|
|
|
LIns *addr = lir->ins2(LIR_piadd, lir->insImmPtr(tbl), lir->ins_u2p(chr));
|
2009-07-16 17:17:35 -07:00
|
|
|
return lir->insLoad(LIR_ldcb, addr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile a builtin character class. */
|
|
|
|
LIns *compileBuiltinClass(RENode *node, LIns *pos, LInsList &fails)
|
|
|
|
{
|
|
|
|
/* All the builtins checked below consume one character. */
|
2009-09-09 15:53:26 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jf, lir->ins2(LIR_plt, pos, cpend), 0)))
|
2009-08-07 20:09:11 -07:00
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns *chr = lir->insLoad(LIR_ldcs, pos, 0);
|
|
|
|
|
|
|
|
switch (node->op) {
|
|
|
|
case REOP_DOT:
|
|
|
|
{
|
|
|
|
/* Accept any character except those in ECMA-262 15.10.2.8. */
|
|
|
|
LIns *eq1 = lir->ins2(LIR_eq, chr, lir->insImm('\n'));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jt, eq1, NULL)))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns *eq2 = lir->ins2(LIR_eq, chr, lir->insImm('\r'));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jt, eq2, NULL)))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns *eq3 = lir->ins2(LIR_eq, chr, lir->insImm(LINE_SEPARATOR));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jt, eq3, NULL)))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns *eq4 = lir->ins2(LIR_eq, chr, lir->insImm(PARA_SEPARATOR));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jt, eq4, NULL)))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REOP_DIGIT:
|
|
|
|
{
|
|
|
|
LIns *ge = lir->ins2(LIR_ge, chr, lir->insImm('0'));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jf, ge, NULL)))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns *le = lir->ins2(LIR_le, chr, lir->insImm('9'));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jf, le, NULL)))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REOP_NONDIGIT:
|
|
|
|
{
|
|
|
|
/* Use 'and' to give a predictable branch for success path. */
|
|
|
|
LIns *ge = lir->ins2(LIR_ge, chr, lir->insImm('0'));
|
|
|
|
LIns *le = lir->ins2(LIR_le, chr, lir->insImm('9'));
|
|
|
|
LIns *both = lir->ins2(LIR_and, ge, le);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jf, lir->ins_eq0(both), NULL)))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REOP_ALNUM:
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Compile the condition:
|
|
|
|
* ((uint)*cp) < 128 && js_alnum[(uint)*cp]
|
|
|
|
*/
|
|
|
|
LIns *rangeCnd = lir->ins2(LIR_ult, chr, lir->insImm(128));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jf, rangeCnd, NULL)))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns *tableVal = compileTableRead(chr, js_alnum);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jt, lir->ins_eq0(tableVal), NULL)))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REOP_NONALNUM:
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Compile the condition:
|
|
|
|
* ((uint)*cp) >= 128 || !js_alnum[(uint)*cp]
|
|
|
|
*/
|
|
|
|
LIns *rangeCnd = lir->ins2(LIR_uge, chr, lir->insImm(128));
|
|
|
|
LIns *rangeBr = lir->insBranch(LIR_jt, rangeCnd, NULL);
|
|
|
|
LIns *tableVal = compileTableRead(chr, js_alnum);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(lir->insBranch(LIR_jf, lir->ins_eq0(tableVal), NULL)))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns *success = lir->ins0(LIR_label);
|
|
|
|
rangeBr->setTarget(success);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REOP_SPACE:
|
|
|
|
case REOP_NONSPACE:
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* ECMA-262 7.2, 7.3, and 15.10.2.12 define a bunch of Unicode code
|
2009-08-07 20:09:11 -07:00
|
|
|
* points for whitespace. We optimize here for the common case of
|
2009-07-16 17:17:35 -07:00
|
|
|
* ASCII characters using a table lookup for the lower block that
|
2009-08-07 20:09:11 -07:00
|
|
|
* can actually contain spaces. For the rest, use a (more or less)
|
2009-07-16 17:17:35 -07:00
|
|
|
* binary search to minimize tests.
|
|
|
|
*
|
|
|
|
* [0000,0020]: 9, A, B, C, D, 20
|
|
|
|
* (0020,00A0): none
|
|
|
|
* [00A0,2000): A0, 1680, 180E
|
|
|
|
* [2000,200A]: all
|
|
|
|
* (200A, max): 2028, 2029, 202F, 205F, 3000
|
|
|
|
*/
|
|
|
|
/* Below 0x20? */
|
|
|
|
LIns *tableRangeCnd = lir->ins2(LIR_ule, chr, lir->insImm(0x20));
|
|
|
|
LIns *tableRangeBr = lir->insBranch(LIR_jt, tableRangeCnd, NULL);
|
|
|
|
/* Fall through means *chr > 0x20. */
|
|
|
|
|
|
|
|
/* Handle (0x20,0xA0). */
|
|
|
|
LIns *asciiCnd = lir->ins2(LIR_ult, chr, lir->insImm(0xA0));
|
|
|
|
LIns *asciiMissBr = lir->insBranch(LIR_jt, asciiCnd, NULL);
|
|
|
|
/* Fall through means *chr >= 0xA0. */
|
|
|
|
|
|
|
|
/* Partition around [0x2000,0x200A]. */
|
|
|
|
LIns *belowCnd = lir->ins2(LIR_ult, chr, lir->insImm(0x2000));
|
|
|
|
LIns *belowBr = lir->insBranch(LIR_jt, belowCnd, NULL);
|
|
|
|
LIns *aboveCnd = lir->ins2(LIR_ugt, chr, lir->insImm(0x200A));
|
|
|
|
LIns *aboveBr = lir->insBranch(LIR_jt, aboveCnd, NULL);
|
2010-01-21 21:20:42 -08:00
|
|
|
LIns *intervalMatchBr = lir->insBranch(LIR_j, NULL, NULL);
|
2009-07-16 17:17:35 -07:00
|
|
|
|
|
|
|
/* Handle [0xA0,0x2000). */
|
|
|
|
LIns *belowLbl = lir->ins0(LIR_label);
|
|
|
|
belowBr->setTarget(belowLbl);
|
|
|
|
LIns *eq1Cnd = lir->ins2(LIR_eq, chr, lir->insImm(0xA0));
|
|
|
|
LIns *eq1Br = lir->insBranch(LIR_jt, eq1Cnd, NULL);
|
|
|
|
LIns *eq2Cnd = lir->ins2(LIR_eq, chr, lir->insImm(0x1680));
|
|
|
|
LIns *eq2Br = lir->insBranch(LIR_jt, eq2Cnd, NULL);
|
|
|
|
LIns *eq3Cnd = lir->ins2(LIR_eq, chr, lir->insImm(0x180E));
|
|
|
|
LIns *eq3Br = lir->insBranch(LIR_jt, eq3Cnd, NULL);
|
2010-01-21 21:20:42 -08:00
|
|
|
LIns *belowMissBr = lir->insBranch(LIR_j, NULL, NULL);
|
2009-07-16 17:17:35 -07:00
|
|
|
|
|
|
|
/* Handle (0x200A, max). */
|
|
|
|
LIns *aboveLbl = lir->ins0(LIR_label);
|
|
|
|
aboveBr->setTarget(aboveLbl);
|
|
|
|
LIns *eq4Cnd = lir->ins2(LIR_eq, chr, lir->insImm(0x2028));
|
|
|
|
LIns *eq4Br = lir->insBranch(LIR_jt, eq4Cnd, NULL);
|
|
|
|
LIns *eq5Cnd = lir->ins2(LIR_eq, chr, lir->insImm(0x2029));
|
|
|
|
LIns *eq5Br = lir->insBranch(LIR_jt, eq5Cnd, NULL);
|
|
|
|
LIns *eq6Cnd = lir->ins2(LIR_eq, chr, lir->insImm(0x202F));
|
|
|
|
LIns *eq6Br = lir->insBranch(LIR_jt, eq6Cnd, NULL);
|
|
|
|
LIns *eq7Cnd = lir->ins2(LIR_eq, chr, lir->insImm(0x205F));
|
|
|
|
LIns *eq7Br = lir->insBranch(LIR_jt, eq7Cnd, NULL);
|
|
|
|
LIns *eq8Cnd = lir->ins2(LIR_eq, chr, lir->insImm(0x3000));
|
|
|
|
LIns *eq8Br = lir->insBranch(LIR_jt, eq8Cnd, NULL);
|
2010-01-21 21:20:42 -08:00
|
|
|
LIns *aboveMissBr = lir->insBranch(LIR_j, NULL, NULL);
|
2009-07-16 17:17:35 -07:00
|
|
|
|
|
|
|
/* Handle [0,0x20]. */
|
|
|
|
LIns *tableLbl = lir->ins0(LIR_label);
|
|
|
|
tableRangeBr->setTarget(tableLbl);
|
|
|
|
LIns *tableVal = compileTableRead(chr, js_ws);
|
|
|
|
LIns *tableCnd = lir->ins_eq0(tableVal);
|
|
|
|
LIns *tableMatchBr = lir->insBranch(LIR_jf, tableCnd, NULL);
|
|
|
|
|
|
|
|
/* Collect misses. */
|
|
|
|
LIns *missLbl = lir->ins0(LIR_label);
|
|
|
|
asciiMissBr->setTarget(missLbl);
|
|
|
|
belowMissBr->setTarget(missLbl);
|
|
|
|
aboveMissBr->setTarget(missLbl);
|
2010-01-21 21:20:42 -08:00
|
|
|
LIns *missBr = lir->insBranch(LIR_j, NULL, NULL);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (node->op == REOP_SPACE) {
|
|
|
|
if (!fails.append(missBr))
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-07-16 17:17:35 -07:00
|
|
|
|
|
|
|
/* Collect matches. */
|
|
|
|
LIns *matchLbl = lir->ins0(LIR_label);
|
|
|
|
intervalMatchBr->setTarget(matchLbl);
|
|
|
|
tableMatchBr->setTarget(matchLbl);
|
|
|
|
eq1Br->setTarget(matchLbl); eq2Br->setTarget(matchLbl);
|
|
|
|
eq3Br->setTarget(matchLbl); eq4Br->setTarget(matchLbl);
|
|
|
|
eq5Br->setTarget(matchLbl); eq6Br->setTarget(matchLbl);
|
|
|
|
eq7Br->setTarget(matchLbl); eq8Br->setTarget(matchLbl);
|
|
|
|
if (node->op == REOP_NONSPACE) {
|
2010-01-21 21:20:42 -08:00
|
|
|
LIns *matchBr = lir->insBranch(LIR_j, NULL, NULL);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!fails.append(matchBr))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
}
|
|
|
|
/* Fall through means match == success. */
|
|
|
|
|
|
|
|
/* Collect successes to fall through. */
|
|
|
|
LIns *success = lir->ins0(LIR_label);
|
|
|
|
if (node->op == REOP_NONSPACE)
|
|
|
|
missBr->setTarget(success);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2008-12-31 11:43:50 -08:00
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
}
|
|
|
|
|
2009-09-09 15:53:26 -07:00
|
|
|
return lir->ins2(LIR_piadd, pos, lir->insImmWord(2));
|
2009-07-16 17:17:35 -07:00
|
|
|
}
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns *compileAlt(RENode *node, LIns *pos, bool atEnd, LInsList &fails)
|
|
|
|
{
|
|
|
|
RENode *leftRe = (RENode *)node->kid, *rightRe = (RENode *)node->u.kid2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the RE continues after the alternative, we need to ensure that no
|
2009-08-07 20:09:11 -07:00
|
|
|
* backtracking is required. Recursive calls to compileNode will fail
|
2009-07-16 17:17:35 -07:00
|
|
|
* on capturing parens, so the only thing we have to check here is that,
|
|
|
|
* if the left subexpression matches, we can keep going without later
|
|
|
|
* deciding we need to try the right subexpression.
|
|
|
|
*/
|
|
|
|
if (!atEnd) {
|
|
|
|
/*
|
|
|
|
* If there is no character overlap between left and right, then
|
|
|
|
* there is only one possible path through the alternative.
|
|
|
|
*/
|
|
|
|
CharSet leftSet, rightSet;
|
|
|
|
if (!enumerateNextChars(cx, leftRe, leftSet) ||
|
|
|
|
!enumerateNextChars(cx, rightRe, rightSet) ||
|
|
|
|
!leftSet.disjoint(rightSet))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is an empty path through either subexpression, the above
|
|
|
|
* check is incomplete; we need to include |node->next| as well.
|
|
|
|
*/
|
|
|
|
bool epsLeft = mayMatchEmpty(leftRe),
|
|
|
|
epsRight = mayMatchEmpty(rightRe);
|
|
|
|
if (epsRight && epsLeft) {
|
|
|
|
return NULL;
|
|
|
|
} else if (epsLeft || epsRight) {
|
|
|
|
CharSet nextSet;
|
|
|
|
if (!enumerateNextChars(cx, node->next, nextSet) ||
|
|
|
|
(epsLeft && !nextSet.disjoint(rightSet)) ||
|
|
|
|
(epsRight && !nextSet.disjoint(leftSet))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try left branch. */
|
|
|
|
LInsList kidFails(cx);
|
|
|
|
LIns *branchEnd = compileNode(leftRe, pos, atEnd, kidFails);
|
|
|
|
if (!branchEnd)
|
2008-12-31 11:43:50 -08:00
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Since there are no phis, simulate by writing to and reading from
|
|
|
|
* memory (REGlobalData::stateStack, since it is unused).
|
|
|
|
*/
|
|
|
|
lir->insStorei(branchEnd, state,
|
|
|
|
offsetof(REGlobalData, stateStack));
|
2010-01-21 21:20:42 -08:00
|
|
|
LIns *leftSuccess = lir->insBranch(LIR_j, NULL, NULL);
|
2009-07-16 17:17:35 -07:00
|
|
|
|
|
|
|
/* Try right branch. */
|
|
|
|
targetCurrentPoint(kidFails);
|
|
|
|
if (!(branchEnd = compileNode(rightRe, pos, atEnd, fails)))
|
2008-12-31 11:43:50 -08:00
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
lir->insStorei(branchEnd, state,
|
|
|
|
offsetof(REGlobalData, stateStack));
|
|
|
|
|
|
|
|
/* Land success on the left branch. */
|
|
|
|
targetCurrentPoint(leftSuccess);
|
|
|
|
return addName(fragment->lirbuf,
|
|
|
|
lir->insLoad(LIR_ldp, state,
|
|
|
|
offsetof(REGlobalData, stateStack)),
|
|
|
|
"pos");
|
|
|
|
}
|
|
|
|
|
|
|
|
LIns *compileOpt(RENode *node, LIns *pos, bool atEnd, LInsList &fails)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Since there are no phis, simulate by writing to and reading from
|
|
|
|
* memory (REGlobalData::stateStack, since it is unused).
|
2008-11-05 11:38:18 -08:00
|
|
|
*/
|
2009-07-16 17:17:35 -07:00
|
|
|
lir->insStorei(pos, state, offsetof(REGlobalData, stateStack));
|
|
|
|
|
|
|
|
/* Try ? body. */
|
|
|
|
LInsList kidFails(cx);
|
|
|
|
if (!(pos = compileNode(node, pos, atEnd, kidFails)))
|
2008-12-31 11:43:50 -08:00
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
lir->insStorei(pos, state, offsetof(REGlobalData, stateStack));
|
|
|
|
|
|
|
|
/* Join success and failure and get new position. */
|
|
|
|
targetCurrentPoint(kidFails);
|
|
|
|
pos = addName(fragment->lirbuf,
|
|
|
|
lir->insLoad(LIR_ldp, state,
|
|
|
|
offsetof(REGlobalData, stateStack)),
|
|
|
|
"pos");
|
|
|
|
|
2008-11-12 17:55:56 -08:00
|
|
|
return pos;
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
LIns *compileQuant(RENode *node, LIns *pos, bool atEnd, LInsList &fails)
|
|
|
|
{
|
|
|
|
/* Only support greedy *, +, ?. */
|
|
|
|
if (!node->u.range.greedy ||
|
|
|
|
node->u.range.min > 1 ||
|
|
|
|
(node->u.range.max > 1 && node->u.range.max < (uintN)-1)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
RENode *bodyRe = (RENode *)node->kid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the RE continues after the alternative, we need to ensure that no
|
2009-08-07 20:09:11 -07:00
|
|
|
* backtracking is required. Recursive calls to compileNode will fail
|
2009-07-16 17:17:35 -07:00
|
|
|
* on capturing parens, so the only thing we have to check here is that,
|
|
|
|
* if the quantifier body matches, we can continue matching the body
|
|
|
|
* without later deciding we need to undo the body matches.
|
|
|
|
*/
|
|
|
|
if (!atEnd) {
|
|
|
|
/*
|
|
|
|
* If there is no character overlap between the body and
|
|
|
|
* |node->next|, then all possible body matches are used.
|
|
|
|
*/
|
|
|
|
CharSet bodySet, nextSet;
|
|
|
|
if (!enumerateNextChars(cx, bodyRe, bodySet) ||
|
|
|
|
!enumerateNextChars(cx, node->next, nextSet) ||
|
|
|
|
!bodySet.disjoint(nextSet)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fork off ? and {1,1}. */
|
|
|
|
if (node->u.range.max == 1) {
|
|
|
|
if (node->u.range.min == 1)
|
|
|
|
return compileNode(bodyRe, pos, atEnd, fails);
|
|
|
|
else
|
|
|
|
return compileOpt(bodyRe, pos, atEnd, fails);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For +, compile a copy of the body where failure is real failure. */
|
|
|
|
if (node->u.range.min == 1) {
|
|
|
|
if (!(pos = compileNode(bodyRe, pos, atEnd, fails)))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since there are no phis, simulate by writing to and reading from
|
|
|
|
* memory (REGlobalData::stateStack, since it is unused).
|
|
|
|
*/
|
|
|
|
lir->insStorei(pos, state, offsetof(REGlobalData, stateStack));
|
|
|
|
|
|
|
|
/* Begin iteration: load loop variables. */
|
|
|
|
LIns *loopTop = lir->ins0(LIR_label);
|
|
|
|
LIns *iterBegin = addName(fragment->lirbuf,
|
|
|
|
lir->insLoad(LIR_ldp, state,
|
|
|
|
offsetof(REGlobalData, stateStack)),
|
|
|
|
"pos");
|
|
|
|
|
|
|
|
/* Match quantifier body. */
|
|
|
|
LInsList kidFails(cx);
|
|
|
|
LIns *iterEnd = compileNode(bodyRe, iterBegin, atEnd, kidFails);
|
|
|
|
if (!iterEnd)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is an epsilon path through the body then, when it is taken,
|
|
|
|
* we need to abort the loop or else we will loop forever.
|
|
|
|
*/
|
|
|
|
if (mayMatchEmpty(bodyRe)) {
|
2009-09-09 15:53:26 -07:00
|
|
|
LIns *eqCnd = lir->ins2(LIR_peq, iterBegin, iterEnd);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!kidFails.append(lir->insBranch(LIR_jt, eqCnd, NULL)))
|
|
|
|
return NULL;
|
2009-07-16 17:17:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End iteration: store loop variables, increment, jump */
|
|
|
|
lir->insStorei(iterEnd, state, offsetof(REGlobalData, stateStack));
|
2010-01-21 21:20:42 -08:00
|
|
|
lir->insBranch(LIR_j, NULL, loopTop);
|
2009-07-16 17:17:35 -07:00
|
|
|
|
|
|
|
/*
|
2009-11-24 11:22:20 -08:00
|
|
|
* Using '+' as branch, the intended control flow is:
|
2009-07-16 17:17:35 -07:00
|
|
|
*
|
2009-11-24 11:22:20 -08:00
|
|
|
* ...
|
|
|
|
* A -> |
|
|
|
|
* |<---.
|
|
|
|
* B -> | |
|
|
|
|
* +--. |
|
|
|
|
* C -> | | |
|
|
|
|
* +--. |
|
|
|
|
* D -> | | |
|
|
|
|
* +--|-'
|
|
|
|
* X -> | |
|
|
|
|
* |<-'
|
|
|
|
* E -> |
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* We are currently at point X. Since the regalloc makes a single,
|
|
|
|
* linear, backwards sweep over the IR (going from E to A), point X
|
|
|
|
* must tell the regalloc what LIR insns are live at the end of D.
|
|
|
|
* Thus, we need to report *all* insns defined *before* the end of D
|
|
|
|
* that may be used *after* D. This means insns defined in A, B, C, or
|
|
|
|
* D and used in B, C, D, or E. Since insns in B, C, and D are
|
|
|
|
* conditionally executed, and we (currently) don't have real phi
|
|
|
|
* nodes, we need only consider insns defined in A and used in E.
|
2009-07-16 17:17:35 -07:00
|
|
|
*/
|
2010-01-27 14:43:36 -08:00
|
|
|
lir->ins1(LIR_plive, state);
|
|
|
|
lir->ins1(LIR_plive, cpend);
|
|
|
|
lir->ins1(LIR_plive, start);
|
2009-02-26 12:04:37 -08:00
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
/* After the loop: reload 'pos' from memory and continue. */
|
|
|
|
targetCurrentPoint(kidFails);
|
|
|
|
return iterBegin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-08-07 20:09:11 -07:00
|
|
|
* Compile the regular expression rooted at 'node'. Return 0 on failed
|
|
|
|
* compilation. Otherwise, generate code that falls through on success (the
|
2009-07-16 17:17:35 -07:00
|
|
|
* returned LIns* is the current 'pos') and jumps to the end on failure (by
|
|
|
|
* adding the guard LIns to 'fails').
|
|
|
|
*/
|
|
|
|
LIns *compileNode(RENode *node, LIns *pos, bool atEnd, LInsList &fails)
|
2008-11-05 11:38:18 -08:00
|
|
|
{
|
2009-07-16 17:17:35 -07:00
|
|
|
for (; pos && node; node = node->next) {
|
2009-09-22 12:36:20 -07:00
|
|
|
if (outOfMemory())
|
2009-07-16 17:17:35 -07:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
bool childNextIsEnd = atEnd && !node->next;
|
2008-11-04 20:07:37 -08:00
|
|
|
|
2008-11-12 17:55:56 -08:00
|
|
|
switch (node->op) {
|
2009-07-16 17:17:35 -07:00
|
|
|
case REOP_EMPTY:
|
2008-11-12 17:55:56 -08:00
|
|
|
pos = compileEmpty(node, pos, fails);
|
|
|
|
break;
|
2009-07-16 17:17:35 -07:00
|
|
|
case REOP_FLAT:
|
|
|
|
pos = compileFlat(node, pos, fails);
|
2008-11-12 17:55:56 -08:00
|
|
|
break;
|
2009-07-16 17:17:35 -07:00
|
|
|
case REOP_ALT:
|
|
|
|
case REOP_ALTPREREQ:
|
|
|
|
pos = compileAlt(node, pos, childNextIsEnd, fails);
|
|
|
|
break;
|
|
|
|
case REOP_QUANT:
|
|
|
|
pos = compileQuant(node, pos, childNextIsEnd, fails);
|
2008-11-12 17:55:56 -08:00
|
|
|
break;
|
2009-07-16 17:17:35 -07:00
|
|
|
case REOP_CLASS:
|
2008-11-12 17:55:56 -08:00
|
|
|
pos = compileClass(node, pos, fails);
|
|
|
|
break;
|
2009-07-16 17:17:35 -07:00
|
|
|
case REOP_DOT:
|
|
|
|
case REOP_DIGIT:
|
|
|
|
case REOP_NONDIGIT:
|
|
|
|
case REOP_ALNUM:
|
|
|
|
case REOP_NONALNUM:
|
|
|
|
case REOP_SPACE:
|
|
|
|
case REOP_NONSPACE:
|
|
|
|
pos = compileBuiltinClass(node, pos, fails);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
2008-11-12 17:55:56 -08:00
|
|
|
}
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
2009-07-16 17:17:35 -07:00
|
|
|
return pos;
|
|
|
|
}
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
/*
|
|
|
|
* This function kicks off recursive compileNode compilation, finishes the
|
|
|
|
* success path, and lets the failed-match path fall through.
|
|
|
|
*/
|
|
|
|
bool compileRootNode(RENode *root, LIns *pos, LIns *anchorFail)
|
|
|
|
{
|
|
|
|
/* Compile the regular expression body. */
|
|
|
|
LInsList fails(cx);
|
|
|
|
pos = compileNode(root, pos, true, fails);
|
|
|
|
if (!pos)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Fall-through from compileNode means success. */
|
|
|
|
lir->insStorei(pos, state, offsetof(REGlobalData, stateStack));
|
2009-09-03 14:43:30 -07:00
|
|
|
lir->ins0(LIR_regfence);
|
2009-07-16 17:17:35 -07:00
|
|
|
lir->ins1(LIR_ret, lir->insImm(1));
|
|
|
|
|
|
|
|
/* Stick return here so we don't have to jump over it every time. */
|
|
|
|
if (anchorFail) {
|
|
|
|
targetCurrentPoint(anchorFail);
|
2009-09-03 14:43:30 -07:00
|
|
|
lir->ins0(LIR_regfence);
|
2009-07-16 17:17:35 -07:00
|
|
|
lir->ins1(LIR_ret, lir->insImm(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Target failed matches. */
|
|
|
|
targetCurrentPoint(fails);
|
|
|
|
return true;
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
/* Compile a regular expressions that can only match on the first char. */
|
|
|
|
bool compileSticky(RENode *root, LIns *start)
|
2008-11-05 11:38:18 -08:00
|
|
|
{
|
2009-07-16 17:17:35 -07:00
|
|
|
if (!compileRootNode(root, start, NULL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Failed to match on first character, so fail whole match. */
|
2009-09-03 14:43:30 -07:00
|
|
|
lir->ins0(LIR_regfence);
|
2008-11-04 14:51:51 -08:00
|
|
|
lir->ins1(LIR_ret, lir->insImm(0));
|
2009-09-22 12:36:20 -07:00
|
|
|
return !outOfMemory();
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
/* Compile normal regular expressions that can match starting at any char. */
|
|
|
|
bool compileAnchoring(RENode *root, LIns *start)
|
2008-11-05 11:38:18 -08:00
|
|
|
{
|
2009-08-07 20:09:11 -07:00
|
|
|
/* Guard outer anchoring loop. Use <= to allow empty regexp match. */
|
2009-09-09 15:53:26 -07:00
|
|
|
LIns *anchorFail = lir->insBranch(LIR_jf, lir->ins2(LIR_ple, start, cpend), 0);
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
if (!compileRootNode(root, start, anchorFail))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Outer loop increment. */
|
2009-09-09 15:53:26 -07:00
|
|
|
lir->insStorei(lir->ins2(LIR_piadd, start, lir->insImmWord(2)), state,
|
2009-07-16 17:17:35 -07:00
|
|
|
offsetof(REGlobalData, skipped));
|
|
|
|
|
2009-09-22 12:36:20 -07:00
|
|
|
return !outOfMemory();
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline LIns*
|
2008-11-05 11:38:18 -08:00
|
|
|
addName(LirBuffer* lirbuf, LIns* ins, const char* name)
|
2008-11-04 14:51:51 -08:00
|
|
|
{
|
2008-12-11 10:55:04 -08:00
|
|
|
#ifdef NJ_VERBOSE
|
2009-06-24 20:32:00 -07:00
|
|
|
debug_only_stmt(lirbuf->names->addName(ins, name);)
|
2008-12-11 10:55:04 -08:00
|
|
|
#endif
|
2008-11-04 14:51:51 -08:00
|
|
|
return ins;
|
|
|
|
}
|
|
|
|
|
2008-11-21 17:54:53 -08:00
|
|
|
/*
|
|
|
|
* Insert the side exit and guard record for a compiled regexp. Most
|
|
|
|
* of the fields are not used. The important part is the regexp source
|
|
|
|
* and flags, which we use as the fragment lookup key.
|
|
|
|
*/
|
2009-08-27 18:46:45 -07:00
|
|
|
GuardRecord* insertGuard(LIns* loopLabel, const jschar* re_chars, size_t re_length)
|
2008-11-21 17:54:53 -08:00
|
|
|
{
|
2009-08-27 18:46:45 -07:00
|
|
|
if (loopLabel) {
|
|
|
|
lir->insBranch(LIR_j, NULL, loopLabel);
|
|
|
|
LirBuffer* lirbuf = fragment->lirbuf;
|
2010-01-27 14:43:36 -08:00
|
|
|
lir->ins1(LIR_plive, lirbuf->state);
|
|
|
|
lir->ins1(LIR_plive, lirbuf->param1);
|
2009-08-27 18:46:45 -07:00
|
|
|
}
|
|
|
|
|
2009-09-22 16:06:52 -07:00
|
|
|
Allocator &alloc = *JS_TRACE_MONITOR(cx).dataAlloc;
|
|
|
|
|
2009-09-28 14:26:18 -07:00
|
|
|
/* Must only create a VMSideExit; see StackFilter::getTops. */
|
2009-09-23 16:56:25 -07:00
|
|
|
size_t len = (sizeof(GuardRecord) +
|
|
|
|
sizeof(VMSideExit) +
|
|
|
|
(re_length-1) * sizeof(jschar));
|
|
|
|
GuardRecord* guard = (GuardRecord *) alloc.alloc(len);
|
|
|
|
VMSideExit* exit = (VMSideExit*)(guard+1);
|
2008-11-21 17:54:53 -08:00
|
|
|
guard->exit = exit;
|
|
|
|
guard->exit->target = fragment;
|
2009-09-22 16:06:52 -07:00
|
|
|
fragment->lastIns = lir->insGuard(LIR_x, NULL, guard);
|
2009-09-25 15:16:10 -07:00
|
|
|
// guard->profCount is calloc'd to zero
|
2009-09-15 15:05:53 -07:00
|
|
|
verbose_only(
|
|
|
|
guard->profGuardID = fragment->guardNumberer++;
|
|
|
|
guard->nextInFrag = fragment->guardsForFrag;
|
|
|
|
fragment->guardsForFrag = guard;
|
|
|
|
)
|
2008-11-21 17:54:53 -08:00
|
|
|
return guard;
|
|
|
|
}
|
|
|
|
|
2008-11-04 14:51:51 -08:00
|
|
|
public:
|
2009-09-22 12:36:20 -07:00
|
|
|
RegExpNativeCompiler(JSContext* cx, JSRegExp* re, CompilerState* cs, Fragment* fragment)
|
2009-09-23 15:12:58 -07:00
|
|
|
: tempAlloc(*JS_TRACE_MONITOR(cx).reTempAlloc), cx(cx),
|
2009-12-07 21:06:41 -08:00
|
|
|
re(re), cs(cs), fragment(fragment), lir(NULL), lirBufWriter(NULL),
|
|
|
|
lirbuf(new (tempAlloc) LirBuffer(tempAlloc))
|
|
|
|
{
|
|
|
|
fragment->lirbuf = lirbuf;
|
|
|
|
#ifdef DEBUG
|
2010-01-22 14:49:18 -08:00
|
|
|
LabelMap* labels = new (tempAlloc) LabelMap(tempAlloc, &LogController);
|
2009-12-07 21:06:41 -08:00
|
|
|
lirbuf->names = new (tempAlloc) LirNameMap(tempAlloc, labels);
|
|
|
|
#endif
|
|
|
|
}
|
2009-09-23 15:12:58 -07:00
|
|
|
|
|
|
|
~RegExpNativeCompiler() {
|
|
|
|
/* Purge the tempAlloc used during recording. */
|
|
|
|
tempAlloc.reset();
|
|
|
|
}
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2009-09-22 12:36:20 -07:00
|
|
|
JSBool compile()
|
2008-11-05 11:38:18 -08:00
|
|
|
{
|
2008-11-21 17:54:53 -08:00
|
|
|
GuardRecord* guard = NULL;
|
2009-06-10 18:29:44 -07:00
|
|
|
const jschar* re_chars;
|
2008-11-20 16:37:36 -08:00
|
|
|
size_t re_length;
|
2010-01-22 14:49:18 -08:00
|
|
|
TraceMonitor* tm = &JS_TRACE_MONITOR(cx);
|
2009-09-12 23:04:27 -07:00
|
|
|
Assembler *assm = tm->assembler;
|
2009-08-27 18:46:45 -07:00
|
|
|
LIns* loopLabel = NULL;
|
2008-11-21 17:54:53 -08:00
|
|
|
|
2010-01-22 14:49:18 -08:00
|
|
|
if (outOfMemory() || OverfullJITCache(tm))
|
2009-09-12 23:04:27 -07:00
|
|
|
return JS_FALSE;
|
2008-11-21 17:54:53 -08:00
|
|
|
|
2009-06-10 18:29:44 -07:00
|
|
|
re->source->getCharsAndLength(re_chars, re_length);
|
2009-07-16 17:17:35 -07:00
|
|
|
/*
|
2008-11-21 17:54:53 -08:00
|
|
|
* If the regexp is too long nanojit will assert when we
|
|
|
|
* try to insert the guard record.
|
|
|
|
*/
|
2009-07-06 13:21:32 -07:00
|
|
|
if (re_length > 1024) {
|
|
|
|
re->flags |= JSREG_NOCOMPILE;
|
2008-11-21 17:54:53 -08:00
|
|
|
return JS_FALSE;
|
2009-07-06 13:21:32 -07:00
|
|
|
}
|
2008-11-21 17:54:53 -08:00
|
|
|
|
2008-11-20 16:37:36 -08:00
|
|
|
/* At this point we have an empty fragment. */
|
2008-11-05 11:38:18 -08:00
|
|
|
LirBuffer* lirbuf = fragment->lirbuf;
|
2009-09-22 12:36:20 -07:00
|
|
|
if (outOfMemory())
|
2008-11-21 17:54:53 -08:00
|
|
|
goto fail;
|
2008-11-05 11:38:18 -08:00
|
|
|
/* FIXME Use bug 463260 smart pointer when available. */
|
2010-01-27 14:41:07 -08:00
|
|
|
lir = lirBufWriter = new LirBufWriter(lirbuf, nanojit::AvmCore::config);
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2008-11-05 11:38:18 -08:00
|
|
|
/* FIXME Use bug 463260 smart pointer when available. */
|
2008-12-11 10:55:04 -08:00
|
|
|
#ifdef NJ_VERBOSE
|
2009-06-24 20:32:00 -07:00
|
|
|
debug_only_stmt(
|
2010-01-22 14:49:18 -08:00
|
|
|
if (LogController.lcbits & LC_TMRegexp) {
|
2009-09-22 12:36:20 -07:00
|
|
|
lir = verbose_filter = new VerboseWriter(tempAlloc, lir, lirbuf->names,
|
2010-01-22 14:49:18 -08:00
|
|
|
&LogController);
|
2009-06-24 20:32:00 -07:00
|
|
|
}
|
|
|
|
)
|
2008-12-11 10:55:04 -08:00
|
|
|
#endif
|
2009-09-09 15:53:26 -07:00
|
|
|
#ifdef DEBUG
|
2010-01-21 18:41:24 -08:00
|
|
|
lir = validate_writer = new ValidateWriter(lir, "regexp writer pipeline");
|
2009-09-09 15:53:26 -07:00
|
|
|
#endif
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
/*
|
|
|
|
* Although we could just load REGlobalData::cpend from 'state', by
|
|
|
|
* passing it as a parameter, we avoid loading it every iteration.
|
|
|
|
*/
|
2008-11-04 14:51:51 -08:00
|
|
|
lir->ins0(LIR_start);
|
2009-09-01 15:30:19 -07:00
|
|
|
|
2009-08-27 18:46:45 -07:00
|
|
|
for (int i = 0; i < NumSavedRegs; ++i)
|
|
|
|
lir->insParam(i, 1);
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (int i = 0; i < NumSavedRegs; ++i)
|
|
|
|
addName(lirbuf, lirbuf->savedRegs[i], regNames[Assembler::savedRegs[i]]);
|
|
|
|
#endif
|
|
|
|
|
2008-11-04 14:51:51 -08:00
|
|
|
lirbuf->state = state = addName(lirbuf, lir->insParam(0, 0), "state");
|
2009-07-16 17:17:35 -07:00
|
|
|
lirbuf->param1 = cpend = addName(lirbuf, lir->insParam(1, 0), "cpend");
|
|
|
|
|
2009-08-27 18:46:45 -07:00
|
|
|
loopLabel = lir->ins0(LIR_label);
|
2009-09-15 15:05:53 -07:00
|
|
|
// If profiling, record where the loop label is, so that the
|
|
|
|
// assembler can insert a frag-entry-counter increment at that
|
|
|
|
// point
|
2010-01-22 14:49:18 -08:00
|
|
|
verbose_only( if (LogController.lcbits & LC_FragProfile) {
|
2009-09-15 15:05:53 -07:00
|
|
|
NanoAssert(!fragment->loopLabel);
|
|
|
|
fragment->loopLabel = loopLabel;
|
|
|
|
})
|
2009-07-16 17:17:35 -07:00
|
|
|
|
2009-11-24 11:22:20 -08:00
|
|
|
start = addName(lirbuf,
|
2009-07-16 17:17:35 -07:00
|
|
|
lir->insLoad(LIR_ldp, state,
|
|
|
|
offsetof(REGlobalData, skipped)),
|
2009-11-24 11:22:20 -08:00
|
|
|
"start");
|
2008-11-04 14:51:51 -08:00
|
|
|
|
|
|
|
if (cs->flags & JSREG_STICKY) {
|
2009-11-24 11:22:20 -08:00
|
|
|
if (!compileSticky(cs->result, start))
|
2008-11-21 17:54:53 -08:00
|
|
|
goto fail;
|
2008-11-04 14:51:51 -08:00
|
|
|
} else {
|
2009-11-24 11:22:20 -08:00
|
|
|
if (!compileAnchoring(cs->result, start))
|
2008-11-21 17:54:53 -08:00
|
|
|
goto fail;
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
|
|
|
|
2009-08-27 18:46:45 -07:00
|
|
|
guard = insertGuard(loopLabel, re_chars, re_length);
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2009-09-22 12:36:20 -07:00
|
|
|
if (outOfMemory())
|
2008-11-28 17:31:17 -08:00
|
|
|
goto fail;
|
2009-09-28 14:26:18 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Deep in the nanojit compiler, the StackFilter is trying to throw
|
|
|
|
* away stores above the VM interpreter/native stacks. We have no such
|
|
|
|
* stacks, so rely on the fact that lirbuf->sp and lirbuf->rp are null
|
|
|
|
* to ensure our stores are ignored.
|
|
|
|
*/
|
|
|
|
JS_ASSERT(!lirbuf->sp && !lirbuf->rp);
|
|
|
|
|
2010-01-25 19:06:13 -08:00
|
|
|
assm->compile(fragment, tempAlloc, /*optimize*/true
|
|
|
|
verbose_only(, lirbuf->names->labels));
|
2009-09-10 16:05:47 -07:00
|
|
|
if (assm->error() != nanojit::None)
|
2008-11-04 20:07:37 -08:00
|
|
|
goto fail;
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2008-11-20 16:37:36 -08:00
|
|
|
delete lirBufWriter;
|
2009-09-14 21:07:57 -07:00
|
|
|
#ifdef DEBUG
|
2010-01-21 18:41:24 -08:00
|
|
|
delete validate_writer;
|
2009-09-14 21:07:57 -07:00
|
|
|
#endif
|
2008-12-11 10:55:04 -08:00
|
|
|
#ifdef NJ_VERBOSE
|
2010-01-22 14:49:18 -08:00
|
|
|
debug_only_stmt( if (LogController.lcbits & LC_TMRegexp)
|
2009-09-14 21:07:57 -07:00
|
|
|
delete verbose_filter; )
|
2008-12-11 10:55:04 -08:00
|
|
|
#endif
|
2008-11-10 12:49:56 -08:00
|
|
|
return JS_TRUE;
|
2008-11-04 14:51:51 -08:00
|
|
|
fail:
|
2010-01-22 14:49:18 -08:00
|
|
|
if (outOfMemory() || OverfullJITCache(tm)) {
|
2009-08-20 17:22:47 -07:00
|
|
|
delete lirBufWriter;
|
2009-09-15 15:05:53 -07:00
|
|
|
// recover profiling data from expiring Fragments
|
|
|
|
verbose_only(
|
|
|
|
REHashMap::Iter iter(*(tm->reFragments));
|
|
|
|
while (iter.next()) {
|
|
|
|
nanojit::Fragment* frag = iter.value();
|
2010-01-22 14:49:18 -08:00
|
|
|
FragProfiling_FragFinalizer(frag, tm);
|
2009-09-15 15:05:53 -07:00
|
|
|
}
|
|
|
|
)
|
2010-01-22 14:49:18 -08:00
|
|
|
FlushJITCache(cx);
|
2008-11-20 16:37:36 -08:00
|
|
|
} else {
|
2009-08-27 18:46:45 -07:00
|
|
|
if (!guard) insertGuard(loopLabel, re_chars, re_length);
|
2009-07-06 13:21:32 -07:00
|
|
|
re->flags |= JSREG_NOCOMPILE;
|
2009-08-20 17:22:47 -07:00
|
|
|
delete lirBufWriter;
|
2008-11-20 16:37:36 -08:00
|
|
|
}
|
2009-09-25 15:45:45 -07:00
|
|
|
#ifdef DEBUG
|
2010-01-21 18:41:24 -08:00
|
|
|
delete validate_writer;
|
2009-09-25 15:45:45 -07:00
|
|
|
#endif
|
2008-12-11 10:55:04 -08:00
|
|
|
#ifdef NJ_VERBOSE
|
2010-01-22 14:49:18 -08:00
|
|
|
debug_only_stmt( if (LogController.lcbits & LC_TMRegexp)
|
2009-06-24 20:32:00 -07:00
|
|
|
delete lir; )
|
2008-12-11 10:55:04 -08:00
|
|
|
#endif
|
2008-11-10 12:49:56 -08:00
|
|
|
return JS_FALSE;
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-11-21 17:54:53 -08:00
|
|
|
/*
|
|
|
|
* Compile a regexp to native code in the given fragment.
|
|
|
|
*/
|
2008-11-10 12:49:56 -08:00
|
|
|
static inline JSBool
|
2008-11-21 17:54:53 -08:00
|
|
|
CompileRegExpToNative(JSContext* cx, JSRegExp* re, Fragment* fragment)
|
2008-11-04 14:51:51 -08:00
|
|
|
{
|
2008-11-21 17:54:53 -08:00
|
|
|
JSBool rv = JS_FALSE;
|
|
|
|
void* mark;
|
|
|
|
CompilerState state;
|
2009-09-22 12:36:20 -07:00
|
|
|
RegExpNativeCompiler rc(cx, re, &state, fragment);
|
2008-11-21 17:54:53 -08:00
|
|
|
|
|
|
|
JS_ASSERT(!fragment->code());
|
|
|
|
mark = JS_ARENA_MARK(&cx->tempPool);
|
|
|
|
if (!CompileRegExpToAST(cx, NULL, re->source, re->flags, state)) {
|
|
|
|
goto out;
|
|
|
|
}
|
2009-09-22 12:36:20 -07:00
|
|
|
rv = rc.compile();
|
2008-11-21 17:54:53 -08:00
|
|
|
out:
|
|
|
|
JS_ARENA_RELEASE(&cx->tempPool, mark);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Function type for a compiled native regexp. */
|
2009-07-16 17:17:35 -07:00
|
|
|
typedef void *(FASTCALL *NativeRegExp)(REGlobalData*, const jschar *);
|
2008-11-21 17:54:53 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a compiled native regexp if one already exists or can be created
|
|
|
|
* now, or NULL otherwise.
|
|
|
|
*/
|
|
|
|
static NativeRegExp
|
|
|
|
GetNativeRegExp(JSContext* cx, JSRegExp* re)
|
|
|
|
{
|
2009-06-10 18:29:44 -07:00
|
|
|
const jschar *re_chars;
|
2008-11-21 17:54:53 -08:00
|
|
|
size_t re_length;
|
2009-06-10 18:29:44 -07:00
|
|
|
re->source->getCharsAndLength(re_chars, re_length);
|
2009-08-18 15:32:54 -07:00
|
|
|
Fragment *fragment = LookupNativeRegExp(cx, re->flags, re_chars, re_length);
|
|
|
|
JS_ASSERT(fragment);
|
2009-09-15 15:05:53 -07:00
|
|
|
if (!fragment->code() && fragment->recordAttempts == 0) {
|
|
|
|
fragment->recordAttempts++;
|
2009-07-06 13:21:32 -07:00
|
|
|
if (!CompileRegExpToNative(cx, re, fragment))
|
|
|
|
return NULL;
|
2008-11-21 17:54:53 -08:00
|
|
|
}
|
|
|
|
union { NIns *code; NativeRegExp func; } u;
|
|
|
|
u.code = fragment->code();
|
|
|
|
return u.func;
|
2008-11-04 14:51:51 -08:00
|
|
|
}
|
2008-11-04 16:03:57 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JSRegExp *
|
|
|
|
js_NewRegExp(JSContext *cx, JSTokenStream *ts,
|
|
|
|
JSString *str, uintN flags, JSBool flat)
|
|
|
|
{
|
2008-11-10 12:49:56 -08:00
|
|
|
JSRegExp *re;
|
2008-11-10 12:04:07 -08:00
|
|
|
void *mark;
|
2008-11-10 12:49:56 -08:00
|
|
|
CompilerState state;
|
|
|
|
size_t resize;
|
2008-11-10 12:04:07 -08:00
|
|
|
jsbytecode *endPC;
|
2008-11-10 12:49:56 -08:00
|
|
|
uintN i;
|
|
|
|
|
|
|
|
re = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
mark = JS_ARENA_MARK(&cx->tempPool);
|
2008-11-10 12:49:56 -08:00
|
|
|
|
2008-11-21 17:54:53 -08:00
|
|
|
/*
|
|
|
|
* Parsing the string as flat is now expressed internally using
|
|
|
|
* a flag, so that we keep this information in the JSRegExp, but
|
|
|
|
* we keep the 'flat' parameter for now for compatibility.
|
|
|
|
*/
|
|
|
|
if (flat) flags |= JSREG_FLAT;
|
|
|
|
if (!CompileRegExpToAST(cx, ts, str, flags, state))
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
2008-11-10 12:49:56 -08:00
|
|
|
|
|
|
|
resize = offsetof(JSRegExp, program) + state.progLength + 1;
|
2009-07-27 21:10:12 -07:00
|
|
|
re = (JSRegExp *) cx->malloc(resize);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
re->nrefs = 1;
|
|
|
|
JS_ASSERT(state.classBitmapsMem <= CLASS_BITMAPS_MEM_LIMIT);
|
|
|
|
re->classCount = state.classCount;
|
|
|
|
if (re->classCount) {
|
|
|
|
re->classList = (RECharSet *)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->malloc(re->classCount * sizeof(RECharSet));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re->classList) {
|
|
|
|
js_DestroyRegExp(cx, re);
|
|
|
|
re = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (i = 0; i < re->classCount; i++)
|
|
|
|
re->classList[i].converted = JS_FALSE;
|
|
|
|
} else {
|
|
|
|
re->classList = NULL;
|
|
|
|
}
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2008-11-05 22:30:19 -08:00
|
|
|
/* Compile the bytecode version. */
|
2008-11-10 12:49:56 -08:00
|
|
|
endPC = EmitREBytecode(&state, re, state.treeDepth, re->program, state.result);
|
2008-11-05 22:30:19 -08:00
|
|
|
if (!endPC) {
|
|
|
|
js_DestroyRegExp(cx, re);
|
|
|
|
re = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*endPC++ = REOP_END;
|
|
|
|
/*
|
|
|
|
* Check whether size was overestimated and shrink using realloc.
|
|
|
|
* This is safe since no pointers to newly parsed regexp or its parts
|
|
|
|
* besides re exist here.
|
|
|
|
*/
|
2008-11-10 12:49:56 -08:00
|
|
|
if ((size_t)(endPC - re->program) != state.progLength + 1) {
|
|
|
|
JSRegExp *tmp;
|
|
|
|
JS_ASSERT((size_t)(endPC - re->program) < state.progLength + 1);
|
|
|
|
resize = offsetof(JSRegExp, program) + (endPC - re->program);
|
2009-07-27 21:10:12 -07:00
|
|
|
tmp = (JSRegExp *) cx->realloc(re, resize);
|
2008-11-05 22:30:19 -08:00
|
|
|
if (tmp)
|
2008-11-10 12:49:56 -08:00
|
|
|
re = tmp;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
re->flags = flags;
|
|
|
|
re->parenCount = state.parenCount;
|
|
|
|
re->source = str;
|
|
|
|
|
|
|
|
out:
|
|
|
|
JS_ARENA_RELEASE(&cx->tempPool, mark);
|
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSRegExp *
|
2007-11-13 14:18:17 -08:00
|
|
|
js_NewRegExpOpt(JSContext *cx, JSString *str, JSString *opt, JSBool flat)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
uintN flags;
|
2009-06-10 18:29:44 -07:00
|
|
|
const jschar *s;
|
2007-03-22 10:30:00 -07:00
|
|
|
size_t i, n;
|
|
|
|
char charBuf[2];
|
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
if (opt) {
|
2009-06-10 18:29:44 -07:00
|
|
|
opt->getCharsAndLength(s, n);
|
2007-08-15 23:23:06 -07:00
|
|
|
for (i = 0; i < n; i++) {
|
2009-02-06 14:28:48 -08:00
|
|
|
#define HANDLE_FLAG(name) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
if (flags & (name)) \
|
|
|
|
goto bad_flag; \
|
|
|
|
flags |= (name); \
|
|
|
|
JS_END_MACRO
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (s[i]) {
|
|
|
|
case 'g':
|
2009-02-06 14:28:48 -08:00
|
|
|
HANDLE_FLAG(JSREG_GLOB);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
case 'i':
|
2009-02-06 14:28:48 -08:00
|
|
|
HANDLE_FLAG(JSREG_FOLD);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
case 'm':
|
2009-02-06 14:28:48 -08:00
|
|
|
HANDLE_FLAG(JSREG_MULTILINE);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
case 'y':
|
2009-02-06 14:28:48 -08:00
|
|
|
HANDLE_FLAG(JSREG_STICKY);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
default:
|
2009-02-06 14:28:48 -08:00
|
|
|
bad_flag:
|
2007-03-22 10:30:00 -07:00
|
|
|
charBuf[0] = (char)s[i];
|
|
|
|
charBuf[1] = '\0';
|
2007-11-13 14:18:17 -08:00
|
|
|
JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR,
|
|
|
|
js_GetErrorMessage, NULL,
|
2009-02-06 14:28:48 -08:00
|
|
|
JSMSG_BAD_REGEXP_FLAG, charBuf);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-02-06 14:28:48 -08:00
|
|
|
#undef HANDLE_FLAG
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2007-11-13 14:18:17 -08:00
|
|
|
return js_NewRegExp(cx, NULL, str, flags, flat);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the current state of the match - the position in the input
|
|
|
|
* text as well as the position in the bytecode. The state of any
|
|
|
|
* parent expressions is also saved (preceding state).
|
|
|
|
* Contents of parenCount parentheses from parenIndex are also saved.
|
|
|
|
*/
|
|
|
|
static REBackTrackData *
|
|
|
|
PushBackTrackState(REGlobalData *gData, REOp op,
|
|
|
|
jsbytecode *target, REMatchState *x, const jschar *cp,
|
|
|
|
size_t parenIndex, size_t parenCount)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
REBackTrackData *result =
|
|
|
|
(REBackTrackData *) ((char *)gData->backTrackSP + gData->cursz);
|
|
|
|
|
|
|
|
size_t sz = sizeof(REBackTrackData) +
|
|
|
|
gData->stateStackTop * sizeof(REProgState) +
|
|
|
|
parenCount * sizeof(RECapture);
|
|
|
|
|
|
|
|
ptrdiff_t btsize = gData->backTrackStackSize;
|
|
|
|
ptrdiff_t btincr = ((char *)result + sz) -
|
|
|
|
((char *)gData->backTrackStack + btsize);
|
|
|
|
|
2007-03-29 11:04:10 -07:00
|
|
|
re_debug("\tBT_Push: %lu,%lu",
|
|
|
|
(unsigned long) parenIndex, (unsigned long) parenCount);
|
2007-03-22 20:35:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (btincr > 0) {
|
|
|
|
ptrdiff_t offset = (char *)result - (char *)gData->backTrackStack;
|
|
|
|
|
|
|
|
btincr = JS_ROUNDUP(btincr, btsize);
|
|
|
|
JS_ARENA_GROW_CAST(gData->backTrackStack, REBackTrackData *,
|
2008-09-12 15:11:48 -07:00
|
|
|
&gData->cx->regexpPool, btsize, btincr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gData->backTrackStack) {
|
2007-11-22 14:21:18 -08:00
|
|
|
js_ReportOutOfScriptQuota(gData->cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
gData->ok = JS_FALSE;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
gData->backTrackStackSize = btsize + btincr;
|
|
|
|
result = (REBackTrackData *) ((char *)gData->backTrackStack + offset);
|
|
|
|
}
|
|
|
|
gData->backTrackSP = result;
|
|
|
|
result->sz = gData->cursz;
|
|
|
|
gData->cursz = sz;
|
|
|
|
|
|
|
|
result->backtrack_op = op;
|
|
|
|
result->backtrack_pc = target;
|
|
|
|
result->cp = cp;
|
|
|
|
result->parenCount = parenCount;
|
2007-03-22 20:35:42 -07:00
|
|
|
result->parenIndex = parenIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result->saveStateStackTop = gData->stateStackTop;
|
|
|
|
JS_ASSERT(gData->stateStackTop);
|
|
|
|
memcpy(result + 1, gData->stateStack,
|
|
|
|
sizeof(REProgState) * result->saveStateStackTop);
|
|
|
|
|
|
|
|
if (parenCount != 0) {
|
|
|
|
memcpy((char *)(result + 1) +
|
|
|
|
sizeof(REProgState) * result->saveStateStackTop,
|
|
|
|
&x->parens[parenIndex],
|
|
|
|
sizeof(RECapture) * parenCount);
|
|
|
|
for (i = 0; i != parenCount; i++)
|
|
|
|
x->parens[parenIndex + i].index = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Consecutive literal characters.
|
|
|
|
*/
|
|
|
|
#if 0
|
|
|
|
static REMatchState *
|
|
|
|
FlatNMatcher(REGlobalData *gData, REMatchState *x, jschar *matchChars,
|
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
if (length > gData->cpend - x->cp)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i != length; i++) {
|
|
|
|
if (matchChars[i] != x->cp[i])
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
x->cp += length;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-29 10:53:03 -07:00
|
|
|
static JS_ALWAYS_INLINE REMatchState *
|
2007-03-22 10:30:00 -07:00
|
|
|
FlatNIMatcher(REGlobalData *gData, REMatchState *x, jschar *matchChars,
|
2008-02-14 15:56:53 -08:00
|
|
|
size_t length)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
JS_ASSERT(gData->cpend >= x->cp);
|
|
|
|
if (length > (size_t)(gData->cpend - x->cp))
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i != length; i++) {
|
|
|
|
if (upcase(matchChars[i]) != upcase(x->cp[i]))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
x->cp += length;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 1. Evaluate DecimalEscape to obtain an EscapeValue E.
|
|
|
|
* 2. If E is not a character then go to step 6.
|
|
|
|
* 3. Let ch be E's character.
|
|
|
|
* 4. Let A be a one-element RECharSet containing the character ch.
|
|
|
|
* 5. Call CharacterSetMatcher(A, false) and return its Matcher result.
|
|
|
|
* 6. E must be an integer. Let n be that integer.
|
|
|
|
* 7. If n=0 or n>NCapturingParens then throw a SyntaxError exception.
|
|
|
|
* 8. Return an internal Matcher closure that takes two arguments, a State x
|
|
|
|
* and a Continuation c, and performs the following:
|
|
|
|
* 1. Let cap be x's captures internal array.
|
|
|
|
* 2. Let s be cap[n].
|
|
|
|
* 3. If s is undefined, then call c(x) and return its result.
|
|
|
|
* 4. Let e be x's endIndex.
|
|
|
|
* 5. Let len be s's length.
|
|
|
|
* 6. Let f be e+len.
|
|
|
|
* 7. If f>InputLength, return failure.
|
|
|
|
* 8. If there exists an integer i between 0 (inclusive) and len (exclusive)
|
|
|
|
* such that Canonicalize(s[i]) is not the same character as
|
|
|
|
* Canonicalize(Input [e+i]), then return failure.
|
|
|
|
* 9. Let y be the State (f, cap).
|
|
|
|
* 10. Call c(y) and return its result.
|
|
|
|
*/
|
|
|
|
static REMatchState *
|
|
|
|
BackrefMatcher(REGlobalData *gData, REMatchState *x, size_t parenIndex)
|
|
|
|
{
|
|
|
|
size_t len, i;
|
|
|
|
const jschar *parenContent;
|
|
|
|
RECapture *cap = &x->parens[parenIndex];
|
|
|
|
|
|
|
|
if (cap->index == -1)
|
|
|
|
return x;
|
|
|
|
|
|
|
|
len = cap->length;
|
|
|
|
if (x->cp + len > gData->cpend)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
parenContent = &gData->cpbegin[cap->index];
|
|
|
|
if (gData->regexp->flags & JSREG_FOLD) {
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (upcase(parenContent[i]) != upcase(x->cp[i]))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (parenContent[i] != x->cp[i])
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x->cp += len;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Add a single character to the RECharSet */
|
|
|
|
static void
|
|
|
|
AddCharacterToCharSet(RECharSet *cs, jschar c)
|
|
|
|
{
|
|
|
|
uintN byteIndex = (uintN)(c >> 3);
|
|
|
|
JS_ASSERT(c <= cs->length);
|
|
|
|
cs->u.bits[byteIndex] |= 1 << (c & 0x7);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Add a character range, c1 to c2 (inclusive) to the RECharSet */
|
|
|
|
static void
|
2008-01-16 00:31:15 -08:00
|
|
|
AddCharacterRangeToCharSet(RECharSet *cs, uintN c1, uintN c2)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-04-22 09:26:37 -07:00
|
|
|
uintN i;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-16 00:31:15 -08:00
|
|
|
uintN byteIndex1 = c1 >> 3;
|
|
|
|
uintN byteIndex2 = c2 >> 3;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-04-22 09:26:37 -07:00
|
|
|
JS_ASSERT(c2 <= cs->length && c1 <= c2);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
c1 &= 0x7;
|
|
|
|
c2 &= 0x7;
|
|
|
|
|
|
|
|
if (byteIndex1 == byteIndex2) {
|
|
|
|
cs->u.bits[byteIndex1] |= ((uint8)0xFF >> (7 - (c2 - c1))) << c1;
|
|
|
|
} else {
|
|
|
|
cs->u.bits[byteIndex1] |= 0xFF << c1;
|
|
|
|
for (i = byteIndex1 + 1; i < byteIndex2; i++)
|
|
|
|
cs->u.bits[i] = 0xFF;
|
|
|
|
cs->u.bits[byteIndex2] |= (uint8)0xFF >> (7 - c2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-05 11:21:32 -07:00
|
|
|
struct CharacterRange {
|
|
|
|
jschar start;
|
|
|
|
jschar end;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following characters are taken from the ECMA-262 standard, section 7.2
|
|
|
|
* and 7.3, and the Unicode 3 standard, Table 6-1.
|
|
|
|
*/
|
|
|
|
static const CharacterRange WhiteSpaceRanges[] = {
|
|
|
|
/* TAB, LF, VT, FF, CR */
|
|
|
|
{ 0x0009, 0x000D },
|
|
|
|
/* SPACE */
|
|
|
|
{ 0x0020, 0x0020 },
|
|
|
|
/* NO-BREAK SPACE */
|
|
|
|
{ 0x00A0, 0x00A0 },
|
|
|
|
/*
|
|
|
|
* EN QUAD, EM QUAD, EN SPACE, EM SPACE, THREE-PER-EM SPACE, FOUR-PER-EM
|
|
|
|
* SPACE, SIX-PER-EM SPACE, FIGURE SPACE, PUNCTUATION SPACE, THIN SPACE,
|
|
|
|
* HAIR SPACE, ZERO WIDTH SPACE
|
|
|
|
*/
|
|
|
|
{ 0x2000, 0x200B },
|
|
|
|
/* LS, PS */
|
|
|
|
{ 0x2028, 0x2029 },
|
|
|
|
/* NARROW NO-BREAK SPACE */
|
|
|
|
{ 0x202F, 0x202F },
|
|
|
|
/* IDEOGRAPHIC SPACE */
|
|
|
|
{ 0x3000, 0x3000 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ECMA-262 standard, section 15.10.2.6. */
|
|
|
|
static const CharacterRange WordRanges[] = {
|
|
|
|
{ jschar('0'), jschar('9') },
|
|
|
|
{ jschar('A'), jschar('Z') },
|
|
|
|
{ jschar('_'), jschar('_') },
|
|
|
|
{ jschar('a'), jschar('z') }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
AddCharacterRanges(RECharSet *charSet,
|
|
|
|
const CharacterRange *range,
|
|
|
|
const CharacterRange *end)
|
|
|
|
{
|
|
|
|
for (; range < end; ++range)
|
|
|
|
AddCharacterRangeToCharSet(charSet, range->start, range->end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
AddInvertedCharacterRanges(RECharSet *charSet,
|
|
|
|
const CharacterRange *range,
|
|
|
|
const CharacterRange *end)
|
|
|
|
{
|
|
|
|
uint16 previous = 0;
|
|
|
|
for (; range < end; ++range) {
|
|
|
|
AddCharacterRangeToCharSet(charSet, previous, range->start - 1);
|
|
|
|
previous = range->end + 1;
|
|
|
|
}
|
|
|
|
AddCharacterRangeToCharSet(charSet, previous, charSet->length);
|
|
|
|
}
|
2009-07-16 17:17:35 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Compile the source of the class into a RECharSet */
|
|
|
|
static JSBool
|
2008-11-20 16:37:36 -08:00
|
|
|
ProcessCharSet(JSContext *cx, JSRegExp *re, RECharSet *charSet)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
const jschar *src, *end;
|
|
|
|
JSBool inRange = JS_FALSE;
|
|
|
|
jschar rangeStart = 0;
|
|
|
|
uintN byteLength, n;
|
|
|
|
jschar c, thisCh;
|
|
|
|
intN nDigits, i;
|
|
|
|
|
|
|
|
JS_ASSERT(!charSet->converted);
|
|
|
|
/*
|
|
|
|
* Assert that startIndex and length points to chars inside [] inside
|
|
|
|
* source string.
|
|
|
|
*/
|
|
|
|
JS_ASSERT(1 <= charSet->u.src.startIndex);
|
2009-06-10 18:29:44 -07:00
|
|
|
JS_ASSERT(charSet->u.src.startIndex < re->source->length());
|
|
|
|
JS_ASSERT(charSet->u.src.length <= re->source->length()
|
2007-03-22 10:30:00 -07:00
|
|
|
- 1 - charSet->u.src.startIndex);
|
|
|
|
|
|
|
|
charSet->converted = JS_TRUE;
|
2009-06-10 18:29:44 -07:00
|
|
|
src = re->source->chars() + charSet->u.src.startIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
end = src + charSet->u.src.length;
|
|
|
|
JS_ASSERT(src[-1] == '[');
|
|
|
|
JS_ASSERT(end[0] == ']');
|
|
|
|
|
|
|
|
byteLength = (charSet->length >> 3) + 1;
|
2009-07-27 21:10:12 -07:00
|
|
|
charSet->u.bits = (uint8 *)cx->malloc(byteLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!charSet->u.bits) {
|
2008-11-20 16:37:36 -08:00
|
|
|
JS_ReportOutOfMemory(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
memset(charSet->u.bits, 0, byteLength);
|
|
|
|
|
|
|
|
if (src == end)
|
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
if (*src == '^') {
|
|
|
|
JS_ASSERT(charSet->sense == JS_FALSE);
|
|
|
|
++src;
|
|
|
|
} else {
|
|
|
|
JS_ASSERT(charSet->sense == JS_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (src != end) {
|
|
|
|
switch (*src) {
|
|
|
|
case '\\':
|
|
|
|
++src;
|
|
|
|
c = *src++;
|
|
|
|
switch (c) {
|
|
|
|
case 'b':
|
|
|
|
thisCh = 0x8;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
thisCh = 0xC;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
thisCh = 0xA;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
thisCh = 0xD;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
thisCh = 0x9;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
thisCh = 0xB;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (src < end && JS_ISWORD(*src)) {
|
|
|
|
thisCh = (jschar)(*src++ & 0x1F);
|
|
|
|
} else {
|
|
|
|
--src;
|
|
|
|
thisCh = '\\';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
nDigits = 2;
|
|
|
|
goto lexHex;
|
|
|
|
case 'u':
|
|
|
|
nDigits = 4;
|
|
|
|
lexHex:
|
|
|
|
n = 0;
|
|
|
|
for (i = 0; (i < nDigits) && (src < end); i++) {
|
|
|
|
uintN digit;
|
|
|
|
c = *src++;
|
|
|
|
if (!isASCIIHexDigit(c, &digit)) {
|
|
|
|
/*
|
|
|
|
* Back off to accepting the original '\'
|
|
|
|
* as a literal
|
|
|
|
*/
|
|
|
|
src -= i + 1;
|
|
|
|
n = '\\';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
n = (n << 4) | digit;
|
|
|
|
}
|
|
|
|
thisCh = (jschar)n;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
/*
|
|
|
|
* This is a non-ECMA extension - decimal escapes (in this
|
|
|
|
* case, octal!) are supposed to be an error inside class
|
|
|
|
* ranges, but supported here for backwards compatibility.
|
|
|
|
*/
|
|
|
|
n = JS7_UNDEC(c);
|
|
|
|
c = *src;
|
|
|
|
if ('0' <= c && c <= '7') {
|
|
|
|
src++;
|
|
|
|
n = 8 * n + JS7_UNDEC(c);
|
|
|
|
c = *src;
|
|
|
|
if ('0' <= c && c <= '7') {
|
|
|
|
src++;
|
|
|
|
i = 8 * n + JS7_UNDEC(c);
|
|
|
|
if (i <= 0377)
|
|
|
|
n = i;
|
|
|
|
else
|
|
|
|
src--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thisCh = (jschar)n;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
AddCharacterRangeToCharSet(charSet, '0', '9');
|
|
|
|
continue; /* don't need range processing */
|
|
|
|
case 'D':
|
|
|
|
AddCharacterRangeToCharSet(charSet, 0, '0' - 1);
|
|
|
|
AddCharacterRangeToCharSet(charSet,
|
|
|
|
(jschar)('9' + 1),
|
|
|
|
(jschar)charSet->length);
|
|
|
|
continue;
|
|
|
|
case 's':
|
2008-09-05 11:21:32 -07:00
|
|
|
AddCharacterRanges(charSet, WhiteSpaceRanges,
|
|
|
|
WhiteSpaceRanges + JS_ARRAY_LENGTH(WhiteSpaceRanges));
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
case 'S':
|
2008-09-05 11:21:32 -07:00
|
|
|
AddInvertedCharacterRanges(charSet, WhiteSpaceRanges,
|
|
|
|
WhiteSpaceRanges + JS_ARRAY_LENGTH(WhiteSpaceRanges));
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
case 'w':
|
2008-09-05 11:21:32 -07:00
|
|
|
AddCharacterRanges(charSet, WordRanges,
|
|
|
|
WordRanges + JS_ARRAY_LENGTH(WordRanges));
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
case 'W':
|
2008-09-05 11:21:32 -07:00
|
|
|
AddInvertedCharacterRanges(charSet, WordRanges,
|
|
|
|
WordRanges + JS_ARRAY_LENGTH(WordRanges));
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
thisCh = c;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
thisCh = *src++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (inRange) {
|
2008-11-20 16:37:36 -08:00
|
|
|
if (re->flags & JSREG_FOLD) {
|
2008-04-22 09:26:37 -07:00
|
|
|
int i;
|
|
|
|
|
|
|
|
JS_ASSERT(rangeStart <= thisCh);
|
|
|
|
for (i = rangeStart; i <= thisCh; i++) {
|
|
|
|
jschar uch, dch;
|
|
|
|
|
|
|
|
AddCharacterToCharSet(charSet, i);
|
|
|
|
uch = upcase(i);
|
2009-12-23 18:48:53 -08:00
|
|
|
dch = inverse_upcase(i);
|
2008-04-22 09:26:37 -07:00
|
|
|
if (i != uch)
|
|
|
|
AddCharacterToCharSet(charSet, uch);
|
|
|
|
if (i != dch)
|
|
|
|
AddCharacterToCharSet(charSet, dch);
|
2007-05-01 10:51:34 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
AddCharacterRangeToCharSet(charSet, rangeStart, thisCh);
|
|
|
|
}
|
|
|
|
inRange = JS_FALSE;
|
|
|
|
} else {
|
2008-11-20 16:37:36 -08:00
|
|
|
if (re->flags & JSREG_FOLD) {
|
2007-03-22 10:30:00 -07:00
|
|
|
AddCharacterToCharSet(charSet, upcase(thisCh));
|
2009-12-23 18:48:53 -08:00
|
|
|
AddCharacterToCharSet(charSet, inverse_upcase(thisCh));
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
AddCharacterToCharSet(charSet, thisCh);
|
|
|
|
}
|
|
|
|
if (src < end - 1) {
|
|
|
|
if (*src == '-') {
|
|
|
|
++src;
|
|
|
|
inRange = JS_TRUE;
|
|
|
|
rangeStart = thisCh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-11-20 16:37:36 -08:00
|
|
|
static inline JSBool
|
|
|
|
MatcherProcessCharSet(REGlobalData *gData, RECharSet *charSet) {
|
|
|
|
JSBool rv = ProcessCharSet(gData->cx, gData->regexp, charSet);
|
|
|
|
if (!rv) gData->ok = JS_FALSE;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
js_DestroyRegExp(JSContext *cx, JSRegExp *re)
|
|
|
|
{
|
|
|
|
if (JS_ATOMIC_DECREMENT(&re->nrefs) == 0) {
|
|
|
|
if (re->classList) {
|
|
|
|
uintN i;
|
|
|
|
for (i = 0; i < re->classCount; i++) {
|
|
|
|
if (re->classList[i].converted)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(re->classList[i].u.bits);
|
2007-03-22 10:30:00 -07:00
|
|
|
re->classList[i].u.bits = NULL;
|
|
|
|
}
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(re->classList);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(re);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
ReallocStateStack(REGlobalData *gData)
|
|
|
|
{
|
|
|
|
size_t limit = gData->stateStackLimit;
|
|
|
|
size_t sz = sizeof(REProgState) * limit;
|
|
|
|
|
2008-09-12 15:11:48 -07:00
|
|
|
JS_ARENA_GROW_CAST(gData->stateStack, REProgState *,
|
|
|
|
&gData->cx->regexpPool, sz, sz);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gData->stateStack) {
|
2007-11-22 14:21:18 -08:00
|
|
|
js_ReportOutOfScriptQuota(gData->cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
gData->ok = JS_FALSE;
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
gData->stateStackLimit = limit + limit;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PUSH_STATE_STACK(data) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
++(data)->stateStackTop; \
|
|
|
|
if ((data)->stateStackTop == (data)->stateStackLimit && \
|
|
|
|
!ReallocStateStack((data))) { \
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
JS_END_MACRO
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Apply the current op against the given input to see if it's going to match
|
|
|
|
* or fail. Return false if we don't get a match, true if we do. If updatecp is
|
|
|
|
* true, then update the current state's cp. Always update startpc to the next
|
|
|
|
* op.
|
|
|
|
*/
|
2008-07-29 10:53:03 -07:00
|
|
|
static JS_ALWAYS_INLINE REMatchState *
|
2007-03-22 10:30:00 -07:00
|
|
|
SimpleMatch(REGlobalData *gData, REMatchState *x, REOp op,
|
2008-02-14 15:56:53 -08:00
|
|
|
jsbytecode **startpc, JSBool updatecp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
REMatchState *result = NULL;
|
|
|
|
jschar matchCh;
|
|
|
|
size_t parenIndex;
|
|
|
|
size_t offset, length, index;
|
|
|
|
jsbytecode *pc = *startpc; /* pc has already been incremented past op */
|
|
|
|
jschar *source;
|
|
|
|
const jschar *startcp = x->cp;
|
|
|
|
jschar ch;
|
|
|
|
RECharSet *charSet;
|
|
|
|
|
2007-03-22 20:35:42 -07:00
|
|
|
#ifdef REGEXP_DEBUG
|
|
|
|
const char *opname = reop_names[op];
|
|
|
|
re_debug("\n%06d: %*s%s", pc - gData->regexp->program,
|
|
|
|
gData->stateStackTop * 2, "", opname);
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (op) {
|
|
|
|
case REOP_EMPTY:
|
|
|
|
result = x;
|
|
|
|
break;
|
|
|
|
case REOP_BOL:
|
|
|
|
if (x->cp != gData->cpbegin) {
|
|
|
|
if (!gData->cx->regExpStatics.multiline &&
|
|
|
|
!(gData->regexp->flags & JSREG_MULTILINE)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!RE_IS_LINE_TERM(x->cp[-1]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = x;
|
|
|
|
break;
|
|
|
|
case REOP_EOL:
|
|
|
|
if (x->cp != gData->cpend) {
|
|
|
|
if (!gData->cx->regExpStatics.multiline &&
|
|
|
|
!(gData->regexp->flags & JSREG_MULTILINE)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!RE_IS_LINE_TERM(*x->cp))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = x;
|
|
|
|
break;
|
|
|
|
case REOP_WBDRY:
|
|
|
|
if ((x->cp == gData->cpbegin || !JS_ISWORD(x->cp[-1])) ^
|
|
|
|
!(x->cp != gData->cpend && JS_ISWORD(*x->cp))) {
|
|
|
|
result = x;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_WNONBDRY:
|
|
|
|
if ((x->cp == gData->cpbegin || !JS_ISWORD(x->cp[-1])) ^
|
|
|
|
(x->cp != gData->cpend && JS_ISWORD(*x->cp))) {
|
|
|
|
result = x;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_DOT:
|
|
|
|
if (x->cp != gData->cpend && !RE_IS_LINE_TERM(*x->cp)) {
|
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_DIGIT:
|
2007-04-27 18:27:38 -07:00
|
|
|
if (x->cp != gData->cpend && JS7_ISDEC(*x->cp)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_NONDIGIT:
|
2007-04-27 18:27:38 -07:00
|
|
|
if (x->cp != gData->cpend && !JS7_ISDEC(*x->cp)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_ALNUM:
|
|
|
|
if (x->cp != gData->cpend && JS_ISWORD(*x->cp)) {
|
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_NONALNUM:
|
|
|
|
if (x->cp != gData->cpend && !JS_ISWORD(*x->cp)) {
|
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_SPACE:
|
|
|
|
if (x->cp != gData->cpend && JS_ISSPACE(*x->cp)) {
|
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_NONSPACE:
|
|
|
|
if (x->cp != gData->cpend && !JS_ISSPACE(*x->cp)) {
|
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_BACKREF:
|
|
|
|
pc = ReadCompactIndex(pc, &parenIndex);
|
|
|
|
JS_ASSERT(parenIndex < gData->regexp->parenCount);
|
|
|
|
result = BackrefMatcher(gData, x, parenIndex);
|
|
|
|
break;
|
|
|
|
case REOP_FLAT:
|
|
|
|
pc = ReadCompactIndex(pc, &offset);
|
2009-06-10 18:29:44 -07:00
|
|
|
JS_ASSERT(offset < gData->regexp->source->length());
|
2007-03-22 10:30:00 -07:00
|
|
|
pc = ReadCompactIndex(pc, &length);
|
|
|
|
JS_ASSERT(1 <= length);
|
2009-06-10 18:29:44 -07:00
|
|
|
JS_ASSERT(length <= gData->regexp->source->length() - offset);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (length <= (size_t)(gData->cpend - x->cp)) {
|
2009-06-10 18:29:44 -07:00
|
|
|
source = gData->regexp->source->chars() + offset;
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug_chars(source, length);
|
2007-03-22 10:30:00 -07:00
|
|
|
for (index = 0; index != length; index++) {
|
|
|
|
if (source[index] != x->cp[index])
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
x->cp += length;
|
|
|
|
result = x;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_FLAT1:
|
|
|
|
matchCh = *pc++;
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug(" '%c' == '%c'", (char)matchCh, (char)*x->cp);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (x->cp != gData->cpend && *x->cp == matchCh) {
|
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_FLATi:
|
|
|
|
pc = ReadCompactIndex(pc, &offset);
|
2009-06-10 18:29:44 -07:00
|
|
|
JS_ASSERT(offset < gData->regexp->source->length());
|
2007-03-22 10:30:00 -07:00
|
|
|
pc = ReadCompactIndex(pc, &length);
|
|
|
|
JS_ASSERT(1 <= length);
|
2009-06-10 18:29:44 -07:00
|
|
|
JS_ASSERT(length <= gData->regexp->source->length() - offset);
|
|
|
|
source = gData->regexp->source->chars();
|
2007-03-22 10:30:00 -07:00
|
|
|
result = FlatNIMatcher(gData, x, source + offset, length);
|
|
|
|
break;
|
|
|
|
case REOP_FLAT1i:
|
|
|
|
matchCh = *pc++;
|
|
|
|
if (x->cp != gData->cpend && upcase(*x->cp) == upcase(matchCh)) {
|
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_UCFLAT1:
|
|
|
|
matchCh = GET_ARG(pc);
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug(" '%c' == '%c'", (char)matchCh, (char)*x->cp);
|
2007-03-22 10:30:00 -07:00
|
|
|
pc += ARG_LEN;
|
|
|
|
if (x->cp != gData->cpend && *x->cp == matchCh) {
|
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_UCFLAT1i:
|
|
|
|
matchCh = GET_ARG(pc);
|
|
|
|
pc += ARG_LEN;
|
|
|
|
if (x->cp != gData->cpend && upcase(*x->cp) == upcase(matchCh)) {
|
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_CLASS:
|
|
|
|
pc = ReadCompactIndex(pc, &index);
|
|
|
|
JS_ASSERT(index < gData->regexp->classCount);
|
|
|
|
if (x->cp != gData->cpend) {
|
|
|
|
charSet = &gData->regexp->classList[index];
|
|
|
|
JS_ASSERT(charSet->converted);
|
|
|
|
ch = *x->cp;
|
|
|
|
index = ch >> 3;
|
2009-09-17 15:48:00 -07:00
|
|
|
if (ch <= charSet->length &&
|
2007-03-22 10:30:00 -07:00
|
|
|
(charSet->u.bits[index] & (1 << (ch & 0x7)))) {
|
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REOP_NCLASS:
|
|
|
|
pc = ReadCompactIndex(pc, &index);
|
|
|
|
JS_ASSERT(index < gData->regexp->classCount);
|
|
|
|
if (x->cp != gData->cpend) {
|
|
|
|
charSet = &gData->regexp->classList[index];
|
|
|
|
JS_ASSERT(charSet->converted);
|
|
|
|
ch = *x->cp;
|
|
|
|
index = ch >> 3;
|
2009-09-17 15:48:00 -07:00
|
|
|
if (ch > charSet->length ||
|
2007-03-22 10:30:00 -07:00
|
|
|
!(charSet->u.bits[index] & (1 << (ch & 0x7)))) {
|
|
|
|
result = x;
|
|
|
|
result->cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
JS_ASSERT(JS_FALSE);
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
if (!updatecp)
|
|
|
|
x->cp = startcp;
|
|
|
|
*startpc = pc;
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug(" * ");
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
x->cp = startcp;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-29 10:53:03 -07:00
|
|
|
static JS_ALWAYS_INLINE REMatchState *
|
2008-02-14 15:56:53 -08:00
|
|
|
ExecuteREBytecode(REGlobalData *gData, REMatchState *x)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
REMatchState *result = NULL;
|
|
|
|
REBackTrackData *backTrackData;
|
|
|
|
jsbytecode *nextpc, *testpc;
|
|
|
|
REOp nextop;
|
|
|
|
RECapture *cap;
|
|
|
|
REProgState *curState;
|
|
|
|
const jschar *startcp;
|
|
|
|
size_t parenIndex, k;
|
|
|
|
size_t parenSoFar = 0;
|
|
|
|
|
|
|
|
jschar matchCh1, matchCh2;
|
|
|
|
RECharSet *charSet;
|
|
|
|
|
|
|
|
JSBool anchor;
|
2008-11-10 12:49:56 -08:00
|
|
|
jsbytecode *pc = gData->regexp->program;
|
2007-03-22 10:30:00 -07:00
|
|
|
REOp op = (REOp) *pc++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the first node is a simple match, step the index into the string
|
|
|
|
* until that match is made, or fail if it can't be found at all.
|
|
|
|
*/
|
|
|
|
if (REOP_IS_SIMPLE(op) && !(gData->regexp->flags & JSREG_STICKY)) {
|
|
|
|
anchor = JS_FALSE;
|
|
|
|
while (x->cp <= gData->cpend) {
|
|
|
|
nextpc = pc; /* reset back to start each time */
|
|
|
|
result = SimpleMatch(gData, x, op, &nextpc, JS_TRUE);
|
|
|
|
if (result) {
|
|
|
|
anchor = JS_TRUE;
|
|
|
|
x = result;
|
|
|
|
pc = nextpc; /* accept skip to next opcode */
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
JS_ASSERT(op < REOP_LIMIT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gData->skipped++;
|
|
|
|
x->cp++;
|
|
|
|
}
|
|
|
|
if (!anchor)
|
2007-03-22 20:35:42 -07:00
|
|
|
goto bad;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2007-03-22 20:35:42 -07:00
|
|
|
#ifdef REGEXP_DEBUG
|
|
|
|
const char *opname = reop_names[op];
|
|
|
|
re_debug("\n%06d: %*s%s", pc - gData->regexp->program,
|
|
|
|
gData->stateStackTop * 2, "", opname);
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
if (REOP_IS_SIMPLE(op)) {
|
|
|
|
result = SimpleMatch(gData, x, op, &pc, JS_TRUE);
|
|
|
|
} else {
|
|
|
|
curState = &gData->stateStack[gData->stateStackTop];
|
|
|
|
switch (op) {
|
|
|
|
case REOP_END:
|
2007-03-22 20:35:42 -07:00
|
|
|
goto good;
|
2007-03-22 10:30:00 -07:00
|
|
|
case REOP_ALTPREREQ2:
|
|
|
|
nextpc = pc + GET_OFFSET(pc); /* start of next op */
|
|
|
|
pc += ARG_LEN;
|
|
|
|
matchCh2 = GET_ARG(pc);
|
|
|
|
pc += ARG_LEN;
|
|
|
|
k = GET_ARG(pc);
|
|
|
|
pc += ARG_LEN;
|
|
|
|
|
|
|
|
if (x->cp != gData->cpend) {
|
|
|
|
if (*x->cp == matchCh2)
|
|
|
|
goto doAlt;
|
|
|
|
|
|
|
|
charSet = &gData->regexp->classList[k];
|
2008-11-20 16:37:36 -08:00
|
|
|
if (!charSet->converted && !MatcherProcessCharSet(gData, charSet))
|
2007-03-22 20:35:42 -07:00
|
|
|
goto bad;
|
2007-03-22 10:30:00 -07:00
|
|
|
matchCh1 = *x->cp;
|
|
|
|
k = matchCh1 >> 3;
|
2009-09-17 15:48:00 -07:00
|
|
|
if ((matchCh1 > charSet->length ||
|
2007-03-22 10:30:00 -07:00
|
|
|
!(charSet->u.bits[k] & (1 << (matchCh1 & 0x7)))) ^
|
|
|
|
charSet->sense) {
|
|
|
|
goto doAlt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REOP_ALTPREREQ:
|
|
|
|
nextpc = pc + GET_OFFSET(pc); /* start of next op */
|
|
|
|
pc += ARG_LEN;
|
|
|
|
matchCh1 = GET_ARG(pc);
|
|
|
|
pc += ARG_LEN;
|
|
|
|
matchCh2 = GET_ARG(pc);
|
|
|
|
pc += ARG_LEN;
|
|
|
|
if (x->cp == gData->cpend ||
|
|
|
|
(*x->cp != matchCh1 && *x->cp != matchCh2)) {
|
|
|
|
result = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* else false thru... */
|
|
|
|
|
|
|
|
case REOP_ALT:
|
|
|
|
doAlt:
|
|
|
|
nextpc = pc + GET_OFFSET(pc); /* start of next alternate */
|
|
|
|
pc += ARG_LEN; /* start of this alternate */
|
|
|
|
curState->parenSoFar = parenSoFar;
|
|
|
|
PUSH_STATE_STACK(gData);
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
startcp = x->cp;
|
|
|
|
if (REOP_IS_SIMPLE(op)) {
|
|
|
|
if (!SimpleMatch(gData, x, op, &pc, JS_TRUE)) {
|
|
|
|
op = (REOp) *nextpc++;
|
|
|
|
pc = nextpc;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result = x;
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
}
|
|
|
|
nextop = (REOp) *nextpc++;
|
|
|
|
if (!PushBackTrackState(gData, nextop, nextpc, x, startcp, 0, 0))
|
2007-03-22 20:35:42 -07:00
|
|
|
goto bad;
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Occurs at (successful) end of REOP_ALT,
|
|
|
|
*/
|
|
|
|
case REOP_JUMP:
|
|
|
|
/*
|
|
|
|
* If we have not gotten a result here, it is because of an
|
|
|
|
* empty match. Do the same thing REOP_EMPTY would do.
|
|
|
|
*/
|
|
|
|
if (!result)
|
|
|
|
result = x;
|
|
|
|
|
|
|
|
--gData->stateStackTop;
|
|
|
|
pc += GET_OFFSET(pc);
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Occurs at last (successful) end of REOP_ALT,
|
|
|
|
*/
|
|
|
|
case REOP_ENDALT:
|
|
|
|
/*
|
|
|
|
* If we have not gotten a result here, it is because of an
|
|
|
|
* empty match. Do the same thing REOP_EMPTY would do.
|
|
|
|
*/
|
|
|
|
if (!result)
|
|
|
|
result = x;
|
|
|
|
|
|
|
|
--gData->stateStackTop;
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_LPAREN:
|
|
|
|
pc = ReadCompactIndex(pc, &parenIndex);
|
2007-03-29 11:04:10 -07:00
|
|
|
re_debug("[ %lu ]", (unsigned long) parenIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ASSERT(parenIndex < gData->regexp->parenCount);
|
|
|
|
if (parenIndex + 1 > parenSoFar)
|
|
|
|
parenSoFar = parenIndex + 1;
|
|
|
|
x->parens[parenIndex].index = x->cp - gData->cpbegin;
|
|
|
|
x->parens[parenIndex].length = 0;
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_RPAREN:
|
2007-09-05 21:26:11 -07:00
|
|
|
{
|
|
|
|
ptrdiff_t delta;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
pc = ReadCompactIndex(pc, &parenIndex);
|
|
|
|
JS_ASSERT(parenIndex < gData->regexp->parenCount);
|
|
|
|
cap = &x->parens[parenIndex];
|
2007-09-05 21:26:11 -07:00
|
|
|
delta = x->cp - (gData->cpbegin + cap->index);
|
|
|
|
cap->length = (delta < 0) ? 0 : (size_t) delta;
|
2007-03-22 10:30:00 -07:00
|
|
|
op = (REOp) *pc++;
|
2007-03-23 09:13:06 -07:00
|
|
|
|
|
|
|
if (!result)
|
|
|
|
result = x;
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
2007-09-05 21:26:11 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
case REOP_ASSERT:
|
|
|
|
nextpc = pc + GET_OFFSET(pc); /* start of term after ASSERT */
|
|
|
|
pc += ARG_LEN; /* start of ASSERT child */
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
testpc = pc;
|
|
|
|
if (REOP_IS_SIMPLE(op) &&
|
|
|
|
!SimpleMatch(gData, x, op, &testpc, JS_FALSE)) {
|
|
|
|
result = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
curState->u.assertion.top =
|
|
|
|
(char *)gData->backTrackSP - (char *)gData->backTrackStack;
|
|
|
|
curState->u.assertion.sz = gData->cursz;
|
|
|
|
curState->index = x->cp - gData->cpbegin;
|
|
|
|
curState->parenSoFar = parenSoFar;
|
|
|
|
PUSH_STATE_STACK(gData);
|
|
|
|
if (!PushBackTrackState(gData, REOP_ASSERTTEST,
|
|
|
|
nextpc, x, x->cp, 0, 0)) {
|
2007-03-22 20:35:42 -07:00
|
|
|
goto bad;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_ASSERT_NOT:
|
|
|
|
nextpc = pc + GET_OFFSET(pc);
|
|
|
|
pc += ARG_LEN;
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
testpc = pc;
|
|
|
|
if (REOP_IS_SIMPLE(op) /* Note - fail to fail! */ &&
|
|
|
|
SimpleMatch(gData, x, op, &testpc, JS_FALSE) &&
|
|
|
|
*testpc == REOP_ASSERTNOTTEST) {
|
|
|
|
result = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
curState->u.assertion.top
|
|
|
|
= (char *)gData->backTrackSP -
|
|
|
|
(char *)gData->backTrackStack;
|
|
|
|
curState->u.assertion.sz = gData->cursz;
|
|
|
|
curState->index = x->cp - gData->cpbegin;
|
|
|
|
curState->parenSoFar = parenSoFar;
|
|
|
|
PUSH_STATE_STACK(gData);
|
|
|
|
if (!PushBackTrackState(gData, REOP_ASSERTNOTTEST,
|
|
|
|
nextpc, x, x->cp, 0, 0)) {
|
2007-03-22 20:35:42 -07:00
|
|
|
goto bad;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_ASSERTTEST:
|
|
|
|
--gData->stateStackTop;
|
|
|
|
--curState;
|
|
|
|
x->cp = gData->cpbegin + curState->index;
|
|
|
|
gData->backTrackSP =
|
|
|
|
(REBackTrackData *) ((char *)gData->backTrackStack +
|
|
|
|
curState->u.assertion.top);
|
|
|
|
gData->cursz = curState->u.assertion.sz;
|
|
|
|
if (result)
|
|
|
|
result = x;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REOP_ASSERTNOTTEST:
|
|
|
|
--gData->stateStackTop;
|
|
|
|
--curState;
|
|
|
|
x->cp = gData->cpbegin + curState->index;
|
|
|
|
gData->backTrackSP =
|
|
|
|
(REBackTrackData *) ((char *)gData->backTrackStack +
|
|
|
|
curState->u.assertion.top);
|
|
|
|
gData->cursz = curState->u.assertion.sz;
|
|
|
|
result = (!result) ? x : NULL;
|
|
|
|
break;
|
|
|
|
case REOP_STAR:
|
|
|
|
curState->u.quantifier.min = 0;
|
|
|
|
curState->u.quantifier.max = (uintN)-1;
|
|
|
|
goto quantcommon;
|
|
|
|
case REOP_PLUS:
|
|
|
|
curState->u.quantifier.min = 1;
|
|
|
|
curState->u.quantifier.max = (uintN)-1;
|
|
|
|
goto quantcommon;
|
|
|
|
case REOP_OPT:
|
|
|
|
curState->u.quantifier.min = 0;
|
|
|
|
curState->u.quantifier.max = 1;
|
|
|
|
goto quantcommon;
|
|
|
|
case REOP_QUANT:
|
|
|
|
pc = ReadCompactIndex(pc, &k);
|
|
|
|
curState->u.quantifier.min = k;
|
|
|
|
pc = ReadCompactIndex(pc, &k);
|
|
|
|
/* max is k - 1 to use one byte for (uintN)-1 sentinel. */
|
|
|
|
curState->u.quantifier.max = k - 1;
|
|
|
|
JS_ASSERT(curState->u.quantifier.min
|
|
|
|
<= curState->u.quantifier.max);
|
|
|
|
quantcommon:
|
|
|
|
if (curState->u.quantifier.max == 0) {
|
|
|
|
pc = pc + GET_OFFSET(pc);
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
result = x;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Step over <next> */
|
|
|
|
nextpc = pc + ARG_LEN;
|
|
|
|
op = (REOp) *nextpc++;
|
|
|
|
startcp = x->cp;
|
|
|
|
if (REOP_IS_SIMPLE(op)) {
|
|
|
|
if (!SimpleMatch(gData, x, op, &nextpc, JS_TRUE)) {
|
|
|
|
if (curState->u.quantifier.min == 0)
|
|
|
|
result = x;
|
|
|
|
else
|
|
|
|
result = NULL;
|
|
|
|
pc = pc + GET_OFFSET(pc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
op = (REOp) *nextpc++;
|
|
|
|
result = x;
|
|
|
|
}
|
|
|
|
curState->index = startcp - gData->cpbegin;
|
|
|
|
curState->continue_op = REOP_REPEAT;
|
|
|
|
curState->continue_pc = pc;
|
|
|
|
curState->parenSoFar = parenSoFar;
|
|
|
|
PUSH_STATE_STACK(gData);
|
|
|
|
if (curState->u.quantifier.min == 0 &&
|
|
|
|
!PushBackTrackState(gData, REOP_REPEAT, pc, x, startcp,
|
|
|
|
0, 0)) {
|
2007-03-22 20:35:42 -07:00
|
|
|
goto bad;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
pc = nextpc;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_ENDCHILD: /* marks the end of a quantifier child */
|
|
|
|
pc = curState[-1].continue_pc;
|
2007-07-05 13:37:47 -07:00
|
|
|
op = (REOp) curState[-1].continue_op;
|
2007-04-13 17:27:12 -07:00
|
|
|
|
|
|
|
if (!result)
|
|
|
|
result = x;
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_REPEAT:
|
|
|
|
--curState;
|
|
|
|
do {
|
|
|
|
--gData->stateStackTop;
|
|
|
|
if (!result) {
|
|
|
|
/* Failed, see if we have enough children. */
|
|
|
|
if (curState->u.quantifier.min == 0)
|
|
|
|
goto repeatDone;
|
|
|
|
goto break_switch;
|
|
|
|
}
|
|
|
|
if (curState->u.quantifier.min == 0 &&
|
|
|
|
x->cp == gData->cpbegin + curState->index) {
|
|
|
|
/* matched an empty string, that'll get us nowhere */
|
|
|
|
result = NULL;
|
|
|
|
goto break_switch;
|
|
|
|
}
|
|
|
|
if (curState->u.quantifier.min != 0)
|
|
|
|
curState->u.quantifier.min--;
|
|
|
|
if (curState->u.quantifier.max != (uintN) -1)
|
|
|
|
curState->u.quantifier.max--;
|
|
|
|
if (curState->u.quantifier.max == 0)
|
|
|
|
goto repeatDone;
|
|
|
|
nextpc = pc + ARG_LEN;
|
|
|
|
nextop = (REOp) *nextpc;
|
|
|
|
startcp = x->cp;
|
|
|
|
if (REOP_IS_SIMPLE(nextop)) {
|
|
|
|
nextpc++;
|
|
|
|
if (!SimpleMatch(gData, x, nextop, &nextpc, JS_TRUE)) {
|
|
|
|
if (curState->u.quantifier.min == 0)
|
|
|
|
goto repeatDone;
|
|
|
|
result = NULL;
|
|
|
|
goto break_switch;
|
|
|
|
}
|
|
|
|
result = x;
|
|
|
|
}
|
|
|
|
curState->index = startcp - gData->cpbegin;
|
|
|
|
PUSH_STATE_STACK(gData);
|
|
|
|
if (curState->u.quantifier.min == 0 &&
|
|
|
|
!PushBackTrackState(gData, REOP_REPEAT,
|
|
|
|
pc, x, startcp,
|
|
|
|
curState->parenSoFar,
|
|
|
|
parenSoFar -
|
|
|
|
curState->parenSoFar)) {
|
2007-03-22 20:35:42 -07:00
|
|
|
goto bad;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} while (*nextpc == REOP_ENDCHILD);
|
|
|
|
pc = nextpc;
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
parenSoFar = curState->parenSoFar;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
repeatDone:
|
|
|
|
result = x;
|
|
|
|
pc += GET_OFFSET(pc);
|
|
|
|
goto break_switch;
|
|
|
|
|
|
|
|
case REOP_MINIMALSTAR:
|
|
|
|
curState->u.quantifier.min = 0;
|
|
|
|
curState->u.quantifier.max = (uintN)-1;
|
|
|
|
goto minimalquantcommon;
|
|
|
|
case REOP_MINIMALPLUS:
|
|
|
|
curState->u.quantifier.min = 1;
|
|
|
|
curState->u.quantifier.max = (uintN)-1;
|
|
|
|
goto minimalquantcommon;
|
|
|
|
case REOP_MINIMALOPT:
|
|
|
|
curState->u.quantifier.min = 0;
|
|
|
|
curState->u.quantifier.max = 1;
|
|
|
|
goto minimalquantcommon;
|
|
|
|
case REOP_MINIMALQUANT:
|
|
|
|
pc = ReadCompactIndex(pc, &k);
|
|
|
|
curState->u.quantifier.min = k;
|
|
|
|
pc = ReadCompactIndex(pc, &k);
|
|
|
|
/* See REOP_QUANT comments about k - 1. */
|
|
|
|
curState->u.quantifier.max = k - 1;
|
|
|
|
JS_ASSERT(curState->u.quantifier.min
|
|
|
|
<= curState->u.quantifier.max);
|
|
|
|
minimalquantcommon:
|
|
|
|
curState->index = x->cp - gData->cpbegin;
|
|
|
|
curState->parenSoFar = parenSoFar;
|
|
|
|
PUSH_STATE_STACK(gData);
|
|
|
|
if (curState->u.quantifier.min != 0) {
|
|
|
|
curState->continue_op = REOP_MINIMALREPEAT;
|
|
|
|
curState->continue_pc = pc;
|
|
|
|
/* step over <next> */
|
|
|
|
pc += OFFSET_LEN;
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
} else {
|
|
|
|
if (!PushBackTrackState(gData, REOP_MINIMALREPEAT,
|
|
|
|
pc, x, x->cp, 0, 0)) {
|
2007-03-22 20:35:42 -07:00
|
|
|
goto bad;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
--gData->stateStackTop;
|
|
|
|
pc = pc + GET_OFFSET(pc);
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REOP_MINIMALREPEAT:
|
|
|
|
--gData->stateStackTop;
|
|
|
|
--curState;
|
|
|
|
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug("{%d,%d}", curState->u.quantifier.min,
|
|
|
|
curState->u.quantifier.max);
|
|
|
|
#define PREPARE_REPEAT() \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
curState->index = x->cp - gData->cpbegin; \
|
|
|
|
curState->continue_op = REOP_MINIMALREPEAT; \
|
|
|
|
curState->continue_pc = pc; \
|
|
|
|
pc += ARG_LEN; \
|
|
|
|
for (k = curState->parenSoFar; k < parenSoFar; k++) \
|
|
|
|
x->parens[k].index = -1; \
|
|
|
|
PUSH_STATE_STACK(gData); \
|
|
|
|
op = (REOp) *pc++; \
|
|
|
|
JS_ASSERT(op < REOP_LIMIT); \
|
|
|
|
JS_END_MACRO
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!result) {
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug(" - ");
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Non-greedy failure - try to consume another child.
|
|
|
|
*/
|
|
|
|
if (curState->u.quantifier.max == (uintN) -1 ||
|
|
|
|
curState->u.quantifier.max > 0) {
|
2007-03-22 20:35:42 -07:00
|
|
|
PREPARE_REPEAT();
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Don't need to adjust pc since we're going to pop. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (curState->u.quantifier.min == 0 &&
|
|
|
|
x->cp == gData->cpbegin + curState->index) {
|
|
|
|
/* Matched an empty string, that'll get us nowhere. */
|
|
|
|
result = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (curState->u.quantifier.min != 0)
|
|
|
|
curState->u.quantifier.min--;
|
|
|
|
if (curState->u.quantifier.max != (uintN) -1)
|
|
|
|
curState->u.quantifier.max--;
|
|
|
|
if (curState->u.quantifier.min != 0) {
|
2007-03-22 20:35:42 -07:00
|
|
|
PREPARE_REPEAT();
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
curState->index = x->cp - gData->cpbegin;
|
|
|
|
curState->parenSoFar = parenSoFar;
|
|
|
|
PUSH_STATE_STACK(gData);
|
|
|
|
if (!PushBackTrackState(gData, REOP_MINIMALREPEAT,
|
|
|
|
pc, x, x->cp,
|
|
|
|
curState->parenSoFar,
|
|
|
|
parenSoFar - curState->parenSoFar)) {
|
2007-03-22 20:35:42 -07:00
|
|
|
goto bad;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
--gData->stateStackTop;
|
|
|
|
pc = pc + GET_OFFSET(pc);
|
|
|
|
op = (REOp) *pc++;
|
|
|
|
JS_ASSERT(op < REOP_LIMIT);
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
JS_ASSERT(JS_FALSE);
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
break_switch:;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the match failed and there's a backtrack option, take it.
|
|
|
|
* Otherwise this is a complete and utter failure.
|
|
|
|
*/
|
|
|
|
if (!result) {
|
|
|
|
if (gData->cursz == 0)
|
|
|
|
return NULL;
|
2009-02-10 03:45:36 -08:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(gData->cx)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
gData->ok = JS_FALSE;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Potentially detect explosive regex here. */
|
|
|
|
gData->backTrackCount++;
|
|
|
|
if (gData->backTrackLimit &&
|
|
|
|
gData->backTrackCount >= gData->backTrackLimit) {
|
|
|
|
JS_ReportErrorNumber(gData->cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_REGEXP_TOO_COMPLEX);
|
|
|
|
gData->ok = JS_FALSE;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
backTrackData = gData->backTrackSP;
|
|
|
|
gData->cursz = backTrackData->sz;
|
|
|
|
gData->backTrackSP =
|
|
|
|
(REBackTrackData *) ((char *)backTrackData - backTrackData->sz);
|
|
|
|
x->cp = backTrackData->cp;
|
|
|
|
pc = backTrackData->backtrack_pc;
|
2007-07-05 13:37:47 -07:00
|
|
|
op = (REOp) backTrackData->backtrack_op;
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ASSERT(op < REOP_LIMIT);
|
|
|
|
gData->stateStackTop = backTrackData->saveStateStackTop;
|
|
|
|
JS_ASSERT(gData->stateStackTop);
|
|
|
|
|
|
|
|
memcpy(gData->stateStack, backTrackData + 1,
|
|
|
|
sizeof(REProgState) * backTrackData->saveStateStackTop);
|
|
|
|
curState = &gData->stateStack[gData->stateStackTop - 1];
|
|
|
|
|
|
|
|
if (backTrackData->parenCount) {
|
|
|
|
memcpy(&x->parens[backTrackData->parenIndex],
|
|
|
|
(char *)(backTrackData + 1) +
|
|
|
|
sizeof(REProgState) * backTrackData->saveStateStackTop,
|
|
|
|
sizeof(RECapture) * backTrackData->parenCount);
|
|
|
|
parenSoFar = backTrackData->parenIndex + backTrackData->parenCount;
|
|
|
|
} else {
|
|
|
|
for (k = curState->parenSoFar; k < parenSoFar; k++)
|
|
|
|
x->parens[k].index = -1;
|
|
|
|
parenSoFar = curState->parenSoFar;
|
|
|
|
}
|
2007-03-22 20:35:42 -07:00
|
|
|
|
2007-03-29 11:04:10 -07:00
|
|
|
re_debug("\tBT_Pop: %ld,%ld",
|
|
|
|
(unsigned long) backTrackData->parenIndex,
|
|
|
|
(unsigned long) backTrackData->parenCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
x = result;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Continue with the expression.
|
|
|
|
*/
|
|
|
|
op = (REOp)*pc++;
|
|
|
|
JS_ASSERT(op < REOP_LIMIT);
|
|
|
|
}
|
2007-03-22 20:35:42 -07:00
|
|
|
|
2007-05-30 23:31:05 -07:00
|
|
|
bad:
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug("\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2007-05-30 23:31:05 -07:00
|
|
|
|
|
|
|
good:
|
2007-03-22 20:35:42 -07:00
|
|
|
re_debug("\n");
|
2007-05-30 23:31:05 -07:00
|
|
|
return x;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static REMatchState *
|
|
|
|
MatchRegExp(REGlobalData *gData, REMatchState *x)
|
|
|
|
{
|
2009-07-16 17:17:35 -07:00
|
|
|
const jschar *cpOrig = x->cp;
|
|
|
|
|
2008-11-04 16:03:57 -08:00
|
|
|
#ifdef JS_TRACER
|
2008-11-21 17:54:53 -08:00
|
|
|
NativeRegExp native;
|
2008-11-04 14:51:51 -08:00
|
|
|
|
2008-11-10 12:49:56 -08:00
|
|
|
/* Run with native regexp if possible. */
|
2009-07-16 17:17:35 -07:00
|
|
|
if (TRACING_ENABLED(gData->cx) &&
|
2009-07-06 13:21:32 -07:00
|
|
|
!(gData->regexp->flags & JSREG_NOCOMPILE) &&
|
2008-11-21 17:54:53 -08:00
|
|
|
(native = GetNativeRegExp(gData->cx, gData->regexp))) {
|
2009-07-16 17:17:35 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For efficient native execution, store offset as a direct pointer into
|
|
|
|
* the buffer and convert back after execution finishes.
|
|
|
|
*/
|
|
|
|
gData->skipped = (ptrdiff_t)cpOrig;
|
2008-11-10 12:04:07 -08:00
|
|
|
|
2009-01-27 17:10:44 -08:00
|
|
|
#ifdef JS_JIT_SPEW
|
2009-06-24 20:32:00 -07:00
|
|
|
debug_only_stmt({
|
2009-02-03 16:25:12 -08:00
|
|
|
VOUCH_DOES_NOT_REQUIRE_STACK();
|
|
|
|
JSStackFrame *caller = (JS_ON_TRACE(gData->cx))
|
|
|
|
? NULL
|
|
|
|
: js_GetScriptedCaller(gData->cx, NULL);
|
2009-06-24 20:32:00 -07:00
|
|
|
debug_only_printf(LC_TMRegexp,
|
|
|
|
"entering REGEXP trace at %s:%u@%u, code: %p\n",
|
|
|
|
caller ? caller->script->filename : "<unknown>",
|
|
|
|
caller ? js_FramePCToLineNumber(gData->cx, caller) : 0,
|
|
|
|
caller ? FramePCOffset(caller) : 0,
|
|
|
|
JS_FUNC_TO_DATA_PTR(void *, native));
|
2009-02-03 16:25:12 -08:00
|
|
|
})
|
2009-01-27 17:10:44 -08:00
|
|
|
#endif
|
2008-11-10 12:49:56 -08:00
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
void *result;
|
2008-11-10 12:49:56 -08:00
|
|
|
#if defined(JS_NO_FASTCALL) && defined(NANOJIT_IA32)
|
2009-07-16 17:17:35 -07:00
|
|
|
/*
|
|
|
|
* Although a NativeRegExp takes one argument and SIMULATE_FASTCALL is
|
|
|
|
* passing two, the second goes into 'edx' and can safely be ignored.
|
|
|
|
*/
|
|
|
|
SIMULATE_FASTCALL(result, gData, gData->cpend, native);
|
2008-11-10 12:49:56 -08:00
|
|
|
#else
|
2009-07-16 17:17:35 -07:00
|
|
|
result = native(gData, gData->cpend);
|
2008-11-10 12:49:56 -08:00
|
|
|
#endif
|
2009-07-16 17:17:35 -07:00
|
|
|
debug_only_print0(LC_TMRegexp, "leaving REGEXP trace\n");
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
2008-11-10 12:49:56 -08:00
|
|
|
|
2009-07-16 17:17:35 -07:00
|
|
|
/* Restore REGlobalData::skipped and fill REMatchState. */
|
|
|
|
x->cp = (const jschar *)gData->stateStack;
|
|
|
|
gData->skipped = (const jschar *)gData->skipped - cpOrig;
|
|
|
|
return x;
|
2008-11-10 12:49:56 -08:00
|
|
|
}
|
2008-11-04 16:03:57 -08:00
|
|
|
#endif
|
2009-07-16 17:17:35 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Have to include the position beyond the last character
|
|
|
|
* in order to detect end-of-input/line condition.
|
|
|
|
*/
|
2009-07-16 17:17:35 -07:00
|
|
|
for (const jschar *p = cpOrig; p <= gData->cpend; p++) {
|
|
|
|
gData->skipped = p - cpOrig;
|
|
|
|
x->cp = p;
|
|
|
|
for (uintN j = 0; j < gData->regexp->parenCount; j++)
|
2007-03-22 10:30:00 -07:00
|
|
|
x->parens[j].index = -1;
|
2009-07-16 17:17:35 -07:00
|
|
|
REMatchState *result = ExecuteREBytecode(gData, x);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gData->ok || result || (gData->regexp->flags & JSREG_STICKY))
|
|
|
|
return result;
|
|
|
|
gData->backTrackSP = gData->backTrackStack;
|
|
|
|
gData->cursz = 0;
|
|
|
|
gData->stateStackTop = 0;
|
2009-07-16 17:17:35 -07:00
|
|
|
p = cpOrig + gData->skipped;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MIN_BACKTRACK_LIMIT 400000
|
|
|
|
|
|
|
|
static REMatchState *
|
|
|
|
InitMatch(JSContext *cx, REGlobalData *gData, JSRegExp *re, size_t length)
|
|
|
|
{
|
|
|
|
REMatchState *result;
|
|
|
|
uintN i;
|
|
|
|
|
|
|
|
gData->backTrackStackSize = INITIAL_BACKTRACK;
|
|
|
|
JS_ARENA_ALLOCATE_CAST(gData->backTrackStack, REBackTrackData *,
|
2008-09-12 15:11:48 -07:00
|
|
|
&cx->regexpPool,
|
2007-03-22 10:30:00 -07:00
|
|
|
INITIAL_BACKTRACK);
|
|
|
|
if (!gData->backTrackStack)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
gData->backTrackSP = gData->backTrackStack;
|
|
|
|
gData->cursz = 0;
|
|
|
|
gData->backTrackCount = 0;
|
|
|
|
gData->backTrackLimit = 0;
|
|
|
|
if (JS_GetOptions(cx) & JSOPTION_RELIMIT) {
|
|
|
|
gData->backTrackLimit = length * length * length; /* O(n^3) */
|
|
|
|
if (gData->backTrackLimit < MIN_BACKTRACK_LIMIT)
|
|
|
|
gData->backTrackLimit = MIN_BACKTRACK_LIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
gData->stateStackLimit = INITIAL_STATESTACK;
|
|
|
|
JS_ARENA_ALLOCATE_CAST(gData->stateStack, REProgState *,
|
2008-09-12 15:11:48 -07:00
|
|
|
&cx->regexpPool,
|
2007-03-22 10:30:00 -07:00
|
|
|
sizeof(REProgState) * INITIAL_STATESTACK);
|
|
|
|
if (!gData->stateStack)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
gData->stateStackTop = 0;
|
|
|
|
gData->cx = cx;
|
|
|
|
gData->regexp = re;
|
|
|
|
gData->ok = JS_TRUE;
|
|
|
|
|
|
|
|
JS_ARENA_ALLOCATE_CAST(result, REMatchState *,
|
2008-09-12 15:11:48 -07:00
|
|
|
&cx->regexpPool,
|
2007-03-22 10:30:00 -07:00
|
|
|
offsetof(REMatchState, parens)
|
|
|
|
+ re->parenCount * sizeof(RECapture));
|
|
|
|
if (!result)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
for (i = 0; i < re->classCount; i++) {
|
|
|
|
if (!re->classList[i].converted &&
|
2008-11-20 16:37:36 -08:00
|
|
|
!MatcherProcessCharSet(gData, &re->classList[i])) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
bad:
|
2007-11-22 14:21:18 -08:00
|
|
|
js_ReportOutOfScriptQuota(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
gData->ok = JS_FALSE;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_ExecuteRegExp(JSContext *cx, JSRegExp *re, JSString *str, size_t *indexp,
|
|
|
|
JSBool test, jsval *rval)
|
|
|
|
{
|
|
|
|
REGlobalData gData;
|
|
|
|
REMatchState *x, *result;
|
|
|
|
|
|
|
|
const jschar *cp, *ep;
|
|
|
|
size_t i, length, start;
|
|
|
|
JSSubString *morepar;
|
|
|
|
JSBool ok;
|
|
|
|
JSRegExpStatics *res;
|
|
|
|
ptrdiff_t matchlen;
|
|
|
|
uintN num, morenum;
|
|
|
|
JSString *parstr, *matchstr;
|
|
|
|
JSObject *obj;
|
|
|
|
|
|
|
|
RECapture *parsub = NULL;
|
2008-09-12 15:11:48 -07:00
|
|
|
void *mark;
|
|
|
|
int64 *timestamp;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It's safe to load from cp because JSStrings have a zero at the end,
|
|
|
|
* and we never let cp get beyond cpend.
|
|
|
|
*/
|
|
|
|
start = *indexp;
|
2009-06-10 18:29:44 -07:00
|
|
|
str->getCharsAndLength(cp, length);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (start > length)
|
|
|
|
start = length;
|
|
|
|
gData.cpbegin = cp;
|
|
|
|
gData.cpend = cp + length;
|
|
|
|
cp += start;
|
|
|
|
gData.start = start;
|
|
|
|
gData.skipped = 0;
|
|
|
|
|
2008-09-12 15:11:48 -07:00
|
|
|
if (!cx->regexpPool.first.next) {
|
|
|
|
/*
|
|
|
|
* The first arena in the regexpPool must have a timestamp at its base.
|
|
|
|
*/
|
|
|
|
JS_ARENA_ALLOCATE_CAST(timestamp, int64 *,
|
|
|
|
&cx->regexpPool, sizeof *timestamp);
|
|
|
|
if (!timestamp)
|
|
|
|
return JS_FALSE;
|
|
|
|
*timestamp = JS_Now();
|
|
|
|
}
|
|
|
|
mark = JS_ARENA_MARK(&cx->regexpPool);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
x = InitMatch(cx, &gData, re, length);
|
|
|
|
|
|
|
|
if (!x) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
x->cp = cp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call the recursive matcher to do the real work. Return null on mismatch
|
|
|
|
* whether testing or not. On match, return an extended Array object.
|
|
|
|
*/
|
|
|
|
result = MatchRegExp(&gData, x);
|
|
|
|
ok = gData.ok;
|
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
if (!result) {
|
|
|
|
*rval = JSVAL_NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
cp = result->cp;
|
|
|
|
i = cp - gData.cpbegin;
|
|
|
|
*indexp = i;
|
|
|
|
matchlen = i - (start + gData.skipped);
|
2009-01-06 09:18:35 -08:00
|
|
|
JS_ASSERT(matchlen >= 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
ep = cp;
|
|
|
|
cp -= matchlen;
|
|
|
|
|
|
|
|
if (test) {
|
|
|
|
/*
|
|
|
|
* Testing for a match and updating cx->regExpStatics: don't allocate
|
|
|
|
* an array object, do return true.
|
|
|
|
*/
|
|
|
|
*rval = JSVAL_TRUE;
|
|
|
|
|
|
|
|
/* Avoid warning. (gcc doesn't detect that obj is needed iff !test); */
|
|
|
|
obj = NULL;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The array returned on match has element 0 bound to the matched
|
|
|
|
* string, elements 1 through state.parenCount bound to the paren
|
|
|
|
* matches, an index property telling the length of the left context,
|
|
|
|
* and an input property referring to the input string.
|
|
|
|
*/
|
2008-02-18 13:01:47 -08:00
|
|
|
obj = js_NewSlowArrayObject(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*rval = OBJECT_TO_JSVAL(obj);
|
|
|
|
|
|
|
|
#define DEFVAL(val, id) { \
|
|
|
|
ok = js_DefineProperty(cx, obj, id, val, \
|
|
|
|
JS_PropertyStub, JS_PropertyStub, \
|
2009-08-26 14:28:36 -07:00
|
|
|
JSPROP_ENUMERATE); \
|
2009-09-25 05:30:11 -07:00
|
|
|
if (!ok) \
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out; \
|
|
|
|
}
|
|
|
|
|
2009-06-10 18:29:44 -07:00
|
|
|
matchstr = js_NewDependentString(cx, str, cp - str->chars(),
|
2009-01-06 09:18:35 -08:00
|
|
|
matchlen);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!matchstr) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
DEFVAL(STRING_TO_JSVAL(matchstr), INT_TO_JSID(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
res = &cx->regExpStatics;
|
|
|
|
res->input = str;
|
|
|
|
res->parenCount = re->parenCount;
|
|
|
|
if (re->parenCount == 0) {
|
|
|
|
res->lastParen = js_EmptySubString;
|
|
|
|
} else {
|
|
|
|
for (num = 0; num < re->parenCount; num++) {
|
|
|
|
parsub = &result->parens[num];
|
|
|
|
if (num < 9) {
|
|
|
|
if (parsub->index == -1) {
|
|
|
|
res->parens[num].chars = NULL;
|
|
|
|
res->parens[num].length = 0;
|
|
|
|
} else {
|
|
|
|
res->parens[num].chars = gData.cpbegin + parsub->index;
|
|
|
|
res->parens[num].length = parsub->length;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
morenum = num - 9;
|
|
|
|
morepar = res->moreParens;
|
|
|
|
if (!morepar) {
|
|
|
|
res->moreLength = 10;
|
|
|
|
morepar = (JSSubString*)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->malloc(10 * sizeof(JSSubString));
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (morenum >= res->moreLength) {
|
|
|
|
res->moreLength += 10;
|
|
|
|
morepar = (JSSubString*)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->realloc(morepar,
|
|
|
|
res->moreLength * sizeof(JSSubString));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (!morepar) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
res->moreParens = morepar;
|
|
|
|
if (parsub->index == -1) {
|
|
|
|
morepar[morenum].chars = NULL;
|
|
|
|
morepar[morenum].length = 0;
|
|
|
|
} else {
|
|
|
|
morepar[morenum].chars = gData.cpbegin + parsub->index;
|
|
|
|
morepar[morenum].length = parsub->length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (test)
|
|
|
|
continue;
|
|
|
|
if (parsub->index == -1) {
|
2009-08-26 14:28:36 -07:00
|
|
|
ok = js_DefineProperty(cx, obj, INT_TO_JSID(num + 1), JSVAL_VOID, NULL, NULL,
|
|
|
|
JSPROP_ENUMERATE);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2009-01-06 09:18:35 -08:00
|
|
|
parstr = js_NewDependentString(cx, str,
|
|
|
|
gData.cpbegin + parsub->index -
|
2009-06-10 18:29:44 -07:00
|
|
|
str->chars(),
|
2009-01-06 09:18:35 -08:00
|
|
|
parsub->length);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!parstr) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-08-26 14:28:36 -07:00
|
|
|
ok = js_DefineProperty(cx, obj, INT_TO_JSID(num + 1), STRING_TO_JSVAL(parstr),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-09-25 05:30:11 -07:00
|
|
|
if (!ok)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (parsub->index == -1) {
|
|
|
|
res->lastParen = js_EmptySubString;
|
|
|
|
} else {
|
|
|
|
res->lastParen.chars = gData.cpbegin + parsub->index;
|
|
|
|
res->lastParen.length = parsub->length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!test) {
|
|
|
|
/*
|
|
|
|
* Define the index and input properties last for better for/in loop
|
|
|
|
* order (so they come after the elements).
|
|
|
|
*/
|
|
|
|
DEFVAL(INT_TO_JSVAL(start + gData.skipped),
|
|
|
|
ATOM_TO_JSID(cx->runtime->atomState.indexAtom));
|
|
|
|
DEFVAL(STRING_TO_JSVAL(str),
|
|
|
|
ATOM_TO_JSID(cx->runtime->atomState.inputAtom));
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef DEFVAL
|
|
|
|
|
|
|
|
res->lastMatch.chars = cp;
|
|
|
|
res->lastMatch.length = matchlen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For JS1.3 and ECMAv2, emulate Perl5 exactly:
|
|
|
|
*
|
|
|
|
* js1.3 "hi", "hi there" "hihitherehi therebye"
|
|
|
|
*/
|
2009-06-10 18:29:44 -07:00
|
|
|
res->leftContext.chars = str->chars();
|
2007-03-22 10:30:00 -07:00
|
|
|
res->leftContext.length = start + gData.skipped;
|
|
|
|
res->rightContext.chars = ep;
|
|
|
|
res->rightContext.length = gData.cpend - ep;
|
|
|
|
|
|
|
|
out:
|
2008-09-12 15:11:48 -07:00
|
|
|
JS_ARENA_RELEASE(&cx->regexpPool, mark);
|
2007-03-22 10:30:00 -07:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
|
2009-08-22 09:59:55 -07:00
|
|
|
static jsdouble
|
|
|
|
GetRegExpLastIndex(JSObject *obj)
|
|
|
|
{
|
|
|
|
JS_ASSERT(obj->getClass() == &js_RegExpClass);
|
|
|
|
|
|
|
|
jsval v = obj->fslots[JSSLOT_REGEXP_LAST_INDEX];
|
|
|
|
if (JSVAL_IS_INT(v))
|
|
|
|
return JSVAL_TO_INT(v);
|
|
|
|
JS_ASSERT(JSVAL_IS_DOUBLE(v));
|
|
|
|
return *JSVAL_TO_DOUBLE(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static jsval
|
|
|
|
GetRegExpLastIndexValue(JSObject *obj)
|
|
|
|
{
|
|
|
|
JS_ASSERT(obj->getClass() == &js_RegExpClass);
|
|
|
|
return obj->fslots[JSSLOT_REGEXP_LAST_INDEX];
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
SetRegExpLastIndex(JSContext *cx, JSObject *obj, jsdouble lastIndex)
|
|
|
|
{
|
|
|
|
JS_ASSERT(obj->getClass() == &js_RegExpClass);
|
|
|
|
|
|
|
|
return JS_NewNumberValue(cx, lastIndex,
|
|
|
|
&obj->fslots[JSSLOT_REGEXP_LAST_INDEX]);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
|
|
|
regexp_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|
|
|
{
|
|
|
|
jsint slot;
|
|
|
|
JSRegExp *re;
|
|
|
|
|
|
|
|
if (!JSVAL_IS_INT(id))
|
|
|
|
return JS_TRUE;
|
2008-07-07 14:00:56 -07:00
|
|
|
while (OBJ_GET_CLASS(cx, obj) != &js_RegExpClass) {
|
|
|
|
obj = OBJ_GET_PROTO(cx, obj);
|
|
|
|
if (!obj)
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
slot = JSVAL_TO_INT(id);
|
2009-08-22 09:59:55 -07:00
|
|
|
if (slot == REGEXP_LAST_INDEX) {
|
|
|
|
*vp = GetRegExpLastIndexValue(obj);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2009-08-04 14:06:55 -07:00
|
|
|
re = (JSRegExp *) obj->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (re) {
|
|
|
|
switch (slot) {
|
|
|
|
case REGEXP_SOURCE:
|
|
|
|
*vp = STRING_TO_JSVAL(re->source);
|
|
|
|
break;
|
|
|
|
case REGEXP_GLOBAL:
|
|
|
|
*vp = BOOLEAN_TO_JSVAL((re->flags & JSREG_GLOB) != 0);
|
|
|
|
break;
|
|
|
|
case REGEXP_IGNORE_CASE:
|
|
|
|
*vp = BOOLEAN_TO_JSVAL((re->flags & JSREG_FOLD) != 0);
|
|
|
|
break;
|
|
|
|
case REGEXP_MULTILINE:
|
|
|
|
*vp = BOOLEAN_TO_JSVAL((re->flags & JSREG_MULTILINE) != 0);
|
|
|
|
break;
|
|
|
|
case REGEXP_STICKY:
|
|
|
|
*vp = BOOLEAN_TO_JSVAL((re->flags & JSREG_STICKY) != 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
regexp_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|
|
|
{
|
|
|
|
JSBool ok;
|
|
|
|
jsint slot;
|
|
|
|
jsdouble lastIndex;
|
|
|
|
|
|
|
|
ok = JS_TRUE;
|
|
|
|
if (!JSVAL_IS_INT(id))
|
|
|
|
return ok;
|
2008-07-07 14:00:56 -07:00
|
|
|
while (OBJ_GET_CLASS(cx, obj) != &js_RegExpClass) {
|
|
|
|
obj = OBJ_GET_PROTO(cx, obj);
|
|
|
|
if (!obj)
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
slot = JSVAL_TO_INT(id);
|
|
|
|
if (slot == REGEXP_LAST_INDEX) {
|
2008-03-06 15:24:08 -08:00
|
|
|
if (!JS_ValueToNumber(cx, *vp, &lastIndex))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
lastIndex = js_DoubleToInteger(lastIndex);
|
2009-08-22 09:59:55 -07:00
|
|
|
ok = SetRegExpLastIndex(cx, obj, lastIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2009-07-15 17:48:22 -07:00
|
|
|
#define REGEXP_PROP_ATTRS (JSPROP_PERMANENT | JSPROP_SHARED)
|
|
|
|
#define RO_REGEXP_PROP_ATTRS (REGEXP_PROP_ATTRS | JSPROP_READONLY)
|
|
|
|
|
|
|
|
#define G regexp_getProperty
|
|
|
|
#define S regexp_setProperty
|
|
|
|
|
|
|
|
static JSPropertySpec regexp_props[] = {
|
|
|
|
{"source", REGEXP_SOURCE, RO_REGEXP_PROP_ATTRS,G,S},
|
|
|
|
{"global", REGEXP_GLOBAL, RO_REGEXP_PROP_ATTRS,G,S},
|
|
|
|
{"ignoreCase", REGEXP_IGNORE_CASE, RO_REGEXP_PROP_ATTRS,G,S},
|
|
|
|
{"lastIndex", REGEXP_LAST_INDEX, REGEXP_PROP_ATTRS,G,S},
|
|
|
|
{"multiline", REGEXP_MULTILINE, RO_REGEXP_PROP_ATTRS,G,S},
|
|
|
|
{"sticky", REGEXP_STICKY, RO_REGEXP_PROP_ATTRS,G,S},
|
|
|
|
{0,0,0,0,0}
|
|
|
|
};
|
|
|
|
|
|
|
|
#undef G
|
|
|
|
#undef S
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* RegExp class static properties and their Perl counterparts:
|
|
|
|
*
|
|
|
|
* RegExp.input $_
|
|
|
|
* RegExp.multiline $*
|
|
|
|
* RegExp.lastMatch $&
|
|
|
|
* RegExp.lastParen $+
|
|
|
|
* RegExp.leftContext $`
|
|
|
|
* RegExp.rightContext $'
|
|
|
|
*/
|
|
|
|
enum regexp_static_tinyid {
|
|
|
|
REGEXP_STATIC_INPUT = -1,
|
|
|
|
REGEXP_STATIC_MULTILINE = -2,
|
|
|
|
REGEXP_STATIC_LAST_MATCH = -3,
|
|
|
|
REGEXP_STATIC_LAST_PAREN = -4,
|
|
|
|
REGEXP_STATIC_LEFT_CONTEXT = -5,
|
|
|
|
REGEXP_STATIC_RIGHT_CONTEXT = -6
|
|
|
|
};
|
|
|
|
|
2008-12-18 12:06:45 -08:00
|
|
|
void
|
|
|
|
js_InitRegExpStatics(JSContext *cx)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-12-18 12:06:45 -08:00
|
|
|
/*
|
|
|
|
* To avoid multiple allocations in InitMatch(), the arena size parameter
|
|
|
|
* should be at least as big as:
|
|
|
|
* INITIAL_BACKTRACK
|
|
|
|
* + (sizeof(REProgState) * INITIAL_STATESTACK)
|
|
|
|
* + (offsetof(REMatchState, parens) + avgParanSize * sizeof(RECapture))
|
|
|
|
*/
|
2009-11-18 13:33:53 -08:00
|
|
|
JS_InitArenaPool(&cx->regexpPool, "regexp",
|
|
|
|
12 * 1024 - 40, /* FIXME: bug 421435 */
|
|
|
|
sizeof(void *), &cx->scriptStackQuota);
|
2008-12-18 12:06:45 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ClearRegExpStatics(cx);
|
|
|
|
}
|
|
|
|
|
2009-04-14 19:32:17 -07:00
|
|
|
JS_FRIEND_API(void)
|
2009-08-27 18:07:44 -07:00
|
|
|
js_SaveAndClearRegExpStatics(JSContext *cx, JSRegExpStatics *statics,
|
|
|
|
JSTempValueRooter *tvr)
|
2009-04-14 19:32:17 -07:00
|
|
|
{
|
2009-08-28 15:34:31 -07:00
|
|
|
*statics = cx->regExpStatics;
|
|
|
|
JS_PUSH_TEMP_ROOT_STRING(cx, statics->input, tvr);
|
|
|
|
/*
|
|
|
|
* Prevent JS_ClearRegExpStatics from freeing moreParens, since we've only
|
|
|
|
* moved it elsewhere (into statics->moreParens).
|
|
|
|
*/
|
|
|
|
cx->regExpStatics.moreParens = NULL;
|
|
|
|
JS_ClearRegExpStatics(cx);
|
2009-04-14 19:32:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_FRIEND_API(void)
|
|
|
|
js_RestoreRegExpStatics(JSContext *cx, JSRegExpStatics *statics,
|
|
|
|
JSTempValueRooter *tvr)
|
|
|
|
{
|
2009-08-28 15:34:31 -07:00
|
|
|
/* Clear/free any new JSRegExpStatics data before clobbering. */
|
|
|
|
JS_ClearRegExpStatics(cx);
|
|
|
|
cx->regExpStatics = *statics;
|
|
|
|
JS_POP_TEMP_ROOT(cx, tvr);
|
2009-04-14 19:32:17 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2008-12-18 12:06:45 -08:00
|
|
|
js_TraceRegExpStatics(JSTracer *trc, JSContext *acx)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-12-18 12:06:45 -08:00
|
|
|
JSRegExpStatics *res = &acx->regExpStatics;
|
|
|
|
|
|
|
|
if (res->input)
|
|
|
|
JS_CALL_STRING_TRACER(trc, res->input, "res->input");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
js_FreeRegExpStatics(JSContext *cx)
|
|
|
|
{
|
2009-08-27 18:07:44 -07:00
|
|
|
JS_ClearRegExpStatics(cx);
|
2008-12-18 12:06:45 -08:00
|
|
|
JS_FinishArenaPool(&cx->regexpPool);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
regexp_static_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|
|
|
{
|
|
|
|
jsint slot;
|
|
|
|
JSRegExpStatics *res;
|
|
|
|
JSString *str;
|
|
|
|
JSSubString *sub;
|
|
|
|
|
|
|
|
res = &cx->regExpStatics;
|
|
|
|
if (!JSVAL_IS_INT(id))
|
|
|
|
return JS_TRUE;
|
|
|
|
slot = JSVAL_TO_INT(id);
|
|
|
|
switch (slot) {
|
|
|
|
case REGEXP_STATIC_INPUT:
|
|
|
|
*vp = res->input ? STRING_TO_JSVAL(res->input)
|
|
|
|
: JS_GetEmptyStringValue(cx);
|
|
|
|
return JS_TRUE;
|
|
|
|
case REGEXP_STATIC_MULTILINE:
|
|
|
|
*vp = BOOLEAN_TO_JSVAL(res->multiline);
|
|
|
|
return JS_TRUE;
|
|
|
|
case REGEXP_STATIC_LAST_MATCH:
|
|
|
|
sub = &res->lastMatch;
|
|
|
|
break;
|
|
|
|
case REGEXP_STATIC_LAST_PAREN:
|
|
|
|
sub = &res->lastParen;
|
|
|
|
break;
|
|
|
|
case REGEXP_STATIC_LEFT_CONTEXT:
|
|
|
|
sub = &res->leftContext;
|
|
|
|
break;
|
|
|
|
case REGEXP_STATIC_RIGHT_CONTEXT:
|
|
|
|
sub = &res->rightContext;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sub = REGEXP_PAREN_SUBSTRING(res, slot);
|
|
|
|
break;
|
|
|
|
}
|
2007-07-28 09:57:30 -07:00
|
|
|
str = js_NewStringCopyN(cx, sub->chars, sub->length);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
|
|
|
*vp = STRING_TO_JSVAL(str);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
regexp_static_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|
|
|
{
|
|
|
|
JSRegExpStatics *res;
|
|
|
|
|
|
|
|
if (!JSVAL_IS_INT(id))
|
|
|
|
return JS_TRUE;
|
|
|
|
res = &cx->regExpStatics;
|
|
|
|
/* XXX use if-else rather than switch to keep MSVC1.52 from crashing */
|
|
|
|
if (JSVAL_TO_INT(id) == REGEXP_STATIC_INPUT) {
|
|
|
|
if (!JSVAL_IS_STRING(*vp) &&
|
|
|
|
!JS_ConvertValue(cx, *vp, JSTYPE_STRING, vp)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
res->input = JSVAL_TO_STRING(*vp);
|
|
|
|
} else if (JSVAL_TO_INT(id) == REGEXP_STATIC_MULTILINE) {
|
|
|
|
if (!JSVAL_IS_BOOLEAN(*vp) &&
|
|
|
|
!JS_ConvertValue(cx, *vp, JSTYPE_BOOLEAN, vp)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
res->multiline = JSVAL_TO_BOOLEAN(*vp);
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-06-18 19:52:50 -07:00
|
|
|
#define REGEXP_STATIC_PROP_ATTRS (REGEXP_PROP_ATTRS | JSPROP_ENUMERATE)
|
|
|
|
#define RO_REGEXP_STATIC_PROP_ATTRS (REGEXP_STATIC_PROP_ATTRS | JSPROP_READONLY)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static JSPropertySpec regexp_static_props[] = {
|
|
|
|
{"input",
|
|
|
|
REGEXP_STATIC_INPUT,
|
2007-06-18 19:52:50 -07:00
|
|
|
REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_setProperty},
|
|
|
|
{"multiline",
|
|
|
|
REGEXP_STATIC_MULTILINE,
|
2007-06-18 19:52:50 -07:00
|
|
|
REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_setProperty},
|
|
|
|
{"lastMatch",
|
|
|
|
REGEXP_STATIC_LAST_MATCH,
|
2007-06-18 19:52:50 -07:00
|
|
|
RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
|
|
|
{"lastParen",
|
|
|
|
REGEXP_STATIC_LAST_PAREN,
|
2007-06-18 19:52:50 -07:00
|
|
|
RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
|
|
|
{"leftContext",
|
|
|
|
REGEXP_STATIC_LEFT_CONTEXT,
|
2007-06-18 19:52:50 -07:00
|
|
|
RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
|
|
|
{"rightContext",
|
|
|
|
REGEXP_STATIC_RIGHT_CONTEXT,
|
2007-06-18 19:52:50 -07:00
|
|
|
RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
|
|
|
|
|
|
|
/* XXX should have block scope and local $1, etc. */
|
2007-06-18 19:52:50 -07:00
|
|
|
{"$1", 0, RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
2007-06-18 19:52:50 -07:00
|
|
|
{"$2", 1, RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
2007-06-18 19:52:50 -07:00
|
|
|
{"$3", 2, RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
2007-06-18 19:52:50 -07:00
|
|
|
{"$4", 3, RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
2007-06-18 19:52:50 -07:00
|
|
|
{"$5", 4, RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
2007-06-18 19:52:50 -07:00
|
|
|
{"$6", 5, RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
2007-06-18 19:52:50 -07:00
|
|
|
{"$7", 6, RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
2007-06-18 19:52:50 -07:00
|
|
|
{"$8", 7, RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
2007-06-18 19:52:50 -07:00
|
|
|
{"$9", 8, RO_REGEXP_STATIC_PROP_ATTRS,
|
2007-03-22 10:30:00 -07:00
|
|
|
regexp_static_getProperty, regexp_static_getProperty},
|
|
|
|
|
|
|
|
{0,0,0,0,0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
regexp_finalize(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2009-08-04 14:06:55 -07:00
|
|
|
JSRegExp *re = (JSRegExp *) obj->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re)
|
|
|
|
return;
|
|
|
|
js_DestroyRegExp(cx, re);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Forward static prototype. */
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
regexp_exec_sub(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
|
|
|
|
JSBool test, jsval *rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static JSBool
|
|
|
|
regexp_call(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
return regexp_exec_sub(cx, JSVAL_TO_OBJECT(argv[-2]), argc, argv,
|
|
|
|
JS_FALSE, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_XDR
|
|
|
|
|
|
|
|
#include "jsxdrapi.h"
|
|
|
|
|
2009-05-06 16:03:10 -07:00
|
|
|
JSBool
|
|
|
|
js_XDRRegExpObject(JSXDRState *xdr, JSObject **objp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSRegExp *re;
|
|
|
|
JSString *source;
|
|
|
|
uint32 flagsword;
|
|
|
|
JSObject *obj;
|
|
|
|
|
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
2009-08-04 14:06:55 -07:00
|
|
|
re = (JSRegExp *) (*objp)->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re)
|
|
|
|
return JS_FALSE;
|
|
|
|
source = re->source;
|
2007-07-08 02:03:34 -07:00
|
|
|
flagsword = (uint32)re->flags;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (!JS_XDRString(xdr, &source) ||
|
|
|
|
!JS_XDRUint32(xdr, &flagsword)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
2009-07-14 17:06:09 -07:00
|
|
|
obj = js_NewObject(xdr->cx, &js_RegExpClass, NULL, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
2008-07-20 05:53:21 -07:00
|
|
|
STOBJ_CLEAR_PARENT(obj);
|
|
|
|
STOBJ_CLEAR_PROTO(obj);
|
2007-07-08 02:03:34 -07:00
|
|
|
re = js_NewRegExp(xdr->cx, NULL, source, (uint8)flagsword, JS_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re)
|
|
|
|
return JS_FALSE;
|
2009-08-04 14:06:55 -07:00
|
|
|
obj->setPrivate(re);
|
2009-08-22 09:59:55 -07:00
|
|
|
js_ClearRegExpLastIndex(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
*objp = obj;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !JS_HAS_XDR */
|
|
|
|
|
2009-05-06 16:03:10 -07:00
|
|
|
#define js_XDRRegExpObject NULL
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#endif /* !JS_HAS_XDR */
|
|
|
|
|
2007-04-16 23:53:37 -07:00
|
|
|
static void
|
|
|
|
regexp_trace(JSTracer *trc, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-04 14:06:55 -07:00
|
|
|
JSRegExp *re = (JSRegExp *) obj->getPrivate();
|
2007-04-16 23:53:37 -07:00
|
|
|
if (re && re->source)
|
|
|
|
JS_CALL_STRING_TRACER(trc, re->source, "source");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSClass js_RegExpClass = {
|
|
|
|
js_RegExp_str,
|
2009-08-22 09:59:55 -07:00
|
|
|
JSCLASS_HAS_PRIVATE |
|
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(REGEXP_CLASS_FIXED_RESERVED_SLOTS) |
|
2007-12-20 15:29:31 -08:00
|
|
|
JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_RegExp),
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
2009-07-15 17:48:22 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub,
|
|
|
|
JS_ConvertStub, regexp_finalize,
|
|
|
|
NULL, NULL,
|
|
|
|
regexp_call, NULL,
|
2009-05-06 16:03:10 -07:00
|
|
|
js_XDRRegExpObject, NULL,
|
2007-04-16 23:53:37 -07:00
|
|
|
JS_CLASS_TRACE(regexp_trace), 0
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static const jschar empty_regexp_ucstr[] = {'(', '?', ':', ')', 0};
|
|
|
|
|
|
|
|
JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
js_regexp_toString(JSContext *cx, JSObject *obj, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSRegExp *re;
|
|
|
|
const jschar *source;
|
|
|
|
jschar *chars;
|
|
|
|
size_t length, nflags;
|
|
|
|
uintN flags;
|
|
|
|
JSString *str;
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!JS_InstanceOf(cx, obj, &js_RegExpClass, vp + 2))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2009-08-04 14:06:55 -07:00
|
|
|
re = (JSRegExp *) obj->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re) {
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(cx->runtime->emptyString);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-06-10 18:29:44 -07:00
|
|
|
re->source->getCharsAndLength(source, length);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (length == 0) {
|
|
|
|
source = empty_regexp_ucstr;
|
2007-11-26 07:18:43 -08:00
|
|
|
length = JS_ARRAY_LENGTH(empty_regexp_ucstr) - 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
length += 2;
|
|
|
|
nflags = 0;
|
|
|
|
for (flags = re->flags; flags != 0; flags &= flags - 1)
|
|
|
|
nflags++;
|
2009-07-27 21:10:12 -07:00
|
|
|
chars = (jschar*) cx->malloc((length + nflags + 1) * sizeof(jschar));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!chars) {
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
chars[0] = '/';
|
|
|
|
js_strncpy(&chars[1], source, length - 2);
|
|
|
|
chars[length-1] = '/';
|
|
|
|
if (nflags) {
|
|
|
|
if (re->flags & JSREG_GLOB)
|
|
|
|
chars[length++] = 'g';
|
|
|
|
if (re->flags & JSREG_FOLD)
|
|
|
|
chars[length++] = 'i';
|
|
|
|
if (re->flags & JSREG_MULTILINE)
|
|
|
|
chars[length++] = 'm';
|
|
|
|
if (re->flags & JSREG_STICKY)
|
|
|
|
chars[length++] = 'y';
|
|
|
|
}
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
chars[length] = 0;
|
|
|
|
|
2007-08-15 23:23:06 -07:00
|
|
|
str = js_NewString(cx, chars, length);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(chars);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
regexp_toString(JSContext *cx, uintN argc, jsval *vp)
|
|
|
|
{
|
2008-02-17 16:12:33 -08:00
|
|
|
JSObject *obj;
|
|
|
|
|
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
|
|
|
return obj && js_regexp_toString(cx, obj, vp);
|
2007-08-01 21:33:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
regexp_compile_sub(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
|
|
|
|
jsval *rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSString *opt, *str;
|
|
|
|
JSRegExp *oldre, *re;
|
|
|
|
JSObject *obj2;
|
|
|
|
size_t length, nbytes;
|
|
|
|
const jschar *cp, *start, *end;
|
|
|
|
jschar *nstart, *ncp, *tmp;
|
|
|
|
|
|
|
|
if (!JS_InstanceOf(cx, obj, &js_RegExpClass, argv))
|
|
|
|
return JS_FALSE;
|
|
|
|
opt = NULL;
|
|
|
|
if (argc == 0) {
|
|
|
|
str = cx->runtime->emptyString;
|
|
|
|
} else {
|
|
|
|
if (JSVAL_IS_OBJECT(argv[0])) {
|
|
|
|
/*
|
|
|
|
* If we get passed in a RegExp object we construct a new
|
|
|
|
* RegExp that is a duplicate of it by re-compiling the
|
|
|
|
* original source code. ECMA requires that it be an error
|
|
|
|
* here if the flags are specified. (We must use the flags
|
|
|
|
* from the original RegExp also).
|
|
|
|
*/
|
|
|
|
obj2 = JSVAL_TO_OBJECT(argv[0]);
|
|
|
|
if (obj2 && OBJ_GET_CLASS(cx, obj2) == &js_RegExpClass) {
|
|
|
|
if (argc >= 2 && !JSVAL_IS_VOID(argv[1])) { /* 'flags' passed */
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_NEWREGEXP_FLAGGED);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
JS_LOCK_OBJ(cx, obj2);
|
2009-08-05 07:14:24 -07:00
|
|
|
re = (JSRegExp *) obj2->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re) {
|
|
|
|
JS_UNLOCK_OBJ(cx, obj2);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
re = js_NewRegExp(cx, NULL, re->source, re->flags, JS_FALSE);
|
|
|
|
JS_UNLOCK_OBJ(cx, obj2);
|
|
|
|
goto created;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str = js_ValueToString(cx, argv[0]);
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
|
|
|
argv[0] = STRING_TO_JSVAL(str);
|
|
|
|
if (argc > 1) {
|
|
|
|
if (JSVAL_IS_VOID(argv[1])) {
|
|
|
|
opt = NULL;
|
|
|
|
} else {
|
|
|
|
opt = js_ValueToString(cx, argv[1]);
|
|
|
|
if (!opt)
|
|
|
|
return JS_FALSE;
|
|
|
|
argv[1] = STRING_TO_JSVAL(opt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Escape any naked slashes in the regexp source. */
|
2009-06-10 18:29:44 -07:00
|
|
|
str->getCharsAndLength(start, length);
|
2007-03-22 10:30:00 -07:00
|
|
|
end = start + length;
|
|
|
|
nstart = ncp = NULL;
|
|
|
|
for (cp = start; cp < end; cp++) {
|
|
|
|
if (*cp == '/' && (cp == start || cp[-1] != '\\')) {
|
|
|
|
nbytes = (++length + 1) * sizeof(jschar);
|
|
|
|
if (!nstart) {
|
2009-07-27 21:10:12 -07:00
|
|
|
nstart = (jschar *) cx->malloc(nbytes);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!nstart)
|
|
|
|
return JS_FALSE;
|
|
|
|
ncp = nstart + (cp - start);
|
|
|
|
js_strncpy(nstart, start, cp - start);
|
|
|
|
} else {
|
2009-07-27 21:10:12 -07:00
|
|
|
tmp = (jschar *) cx->realloc(nstart, nbytes);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!tmp) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(nstart);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
ncp = tmp + (ncp - nstart);
|
|
|
|
nstart = tmp;
|
|
|
|
}
|
|
|
|
*ncp++ = '\\';
|
|
|
|
}
|
|
|
|
if (nstart)
|
|
|
|
*ncp++ = *cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nstart) {
|
|
|
|
/* Don't forget to store the backstop after the new string. */
|
|
|
|
JS_ASSERT((size_t)(ncp - nstart) == length);
|
|
|
|
*ncp = 0;
|
2007-08-15 23:23:06 -07:00
|
|
|
str = js_NewString(cx, nstart, length);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(nstart);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
argv[0] = STRING_TO_JSVAL(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-13 14:18:17 -08:00
|
|
|
re = js_NewRegExpOpt(cx, str, opt, JS_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
created:
|
|
|
|
if (!re)
|
|
|
|
return JS_FALSE;
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2009-08-04 14:06:55 -07:00
|
|
|
oldre = (JSRegExp *) obj->getPrivate();
|
|
|
|
obj->setPrivate(re);
|
2009-08-22 09:59:55 -07:00
|
|
|
js_ClearRegExpLastIndex(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
if (oldre)
|
|
|
|
js_DestroyRegExp(cx, oldre);
|
|
|
|
*rval = OBJECT_TO_JSVAL(obj);
|
2009-08-22 09:59:55 -07:00
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
static JSBool
|
|
|
|
regexp_compile(JSContext *cx, uintN argc, jsval *vp)
|
|
|
|
{
|
2008-02-17 16:12:33 -08:00
|
|
|
JSObject *obj;
|
|
|
|
|
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
|
|
|
return obj && regexp_compile_sub(cx, obj, argc, vp + 2, vp);
|
2007-08-01 21:33:52 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
|
|
|
regexp_exec_sub(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
|
|
|
|
JSBool test, jsval *rval)
|
|
|
|
{
|
|
|
|
JSBool ok, sticky;
|
|
|
|
JSRegExp *re;
|
|
|
|
jsdouble lastIndex;
|
|
|
|
JSString *str;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
ok = JS_InstanceOf(cx, obj, &js_RegExpClass, argv);
|
|
|
|
if (!ok)
|
|
|
|
return JS_FALSE;
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2009-08-04 14:06:55 -07:00
|
|
|
re = (JSRegExp *) obj->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!re) {
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NB: we must reach out: after this paragraph, in order to drop re. */
|
|
|
|
HOLD_REGEXP(cx, re);
|
|
|
|
sticky = (re->flags & JSREG_STICKY) != 0;
|
2009-08-22 09:59:55 -07:00
|
|
|
lastIndex = (re->flags & (JSREG_GLOB | JSREG_STICKY))
|
|
|
|
? GetRegExpLastIndex(obj)
|
|
|
|
: 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
|
|
|
|
/* Now that obj is unlocked, it's safe to (potentially) grab the GC lock. */
|
|
|
|
if (argc == 0) {
|
|
|
|
str = cx->regExpStatics.input;
|
|
|
|
if (!str) {
|
|
|
|
const char *bytes = js_GetStringBytes(cx, re->source);
|
|
|
|
|
|
|
|
if (bytes) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_NO_INPUT,
|
|
|
|
bytes,
|
|
|
|
(re->flags & JSREG_GLOB) ? "g" : "",
|
|
|
|
(re->flags & JSREG_FOLD) ? "i" : "",
|
|
|
|
(re->flags & JSREG_MULTILINE) ? "m" : "",
|
|
|
|
(re->flags & JSREG_STICKY) ? "y" : "");
|
|
|
|
}
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
str = js_ValueToString(cx, argv[0]);
|
|
|
|
if (!str) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
argv[0] = STRING_TO_JSVAL(str);
|
|
|
|
}
|
|
|
|
|
2009-06-10 18:29:44 -07:00
|
|
|
if (lastIndex < 0 || str->length() < lastIndex) {
|
2009-08-22 09:59:55 -07:00
|
|
|
js_ClearRegExpLastIndex(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
*rval = JSVAL_NULL;
|
|
|
|
} else {
|
|
|
|
i = (size_t) lastIndex;
|
|
|
|
ok = js_ExecuteRegExp(cx, re, str, &i, test, rval);
|
|
|
|
if (ok &&
|
|
|
|
((re->flags & JSREG_GLOB) || (*rval != JSVAL_NULL && sticky))) {
|
2009-08-22 09:59:55 -07:00
|
|
|
if (*rval == JSVAL_NULL)
|
|
|
|
js_ClearRegExpLastIndex(obj);
|
|
|
|
else
|
|
|
|
ok = SetRegExpLastIndex(cx, obj, i);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
DROP_REGEXP(cx, re);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
regexp_exec(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
return regexp_exec_sub(cx, JS_THIS_OBJECT(cx, vp), argc, vp + 2, JS_FALSE,
|
|
|
|
vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
regexp_test(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!regexp_exec_sub(cx, JS_THIS_OBJECT(cx, vp), argc, vp + 2, JS_TRUE, vp))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
if (*vp != JSVAL_TRUE)
|
|
|
|
*vp = JSVAL_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSFunctionSpec regexp_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_toSource_str, regexp_toString, 0,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_toString_str, regexp_toString, 0,0),
|
|
|
|
JS_FN("compile", regexp_compile, 2,0),
|
|
|
|
JS_FN("exec", regexp_exec, 1,0),
|
2009-04-16 15:34:14 -07:00
|
|
|
JS_FN("test", regexp_test, 1,0),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
RegExp(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
2008-12-09 08:38:32 -08:00
|
|
|
if (!JS_IsConstructing(cx)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* If first arg is regexp and no flags are given, just return the arg.
|
2007-08-01 21:33:52 -07:00
|
|
|
* (regexp_compile_sub detects the regexp + flags case and throws a
|
2007-03-22 10:30:00 -07:00
|
|
|
* TypeError.) See 10.15.3.1.
|
|
|
|
*/
|
|
|
|
if ((argc < 2 || JSVAL_IS_VOID(argv[1])) &&
|
|
|
|
!JSVAL_IS_PRIMITIVE(argv[0]) &&
|
|
|
|
OBJ_GET_CLASS(cx, JSVAL_TO_OBJECT(argv[0])) == &js_RegExpClass) {
|
|
|
|
*rval = argv[0];
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, replace obj with a new RegExp object. */
|
2009-07-14 17:06:09 -07:00
|
|
|
obj = js_NewObject(cx, &js_RegExpClass, NULL, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/*
|
2007-08-01 21:33:52 -07:00
|
|
|
* regexp_compile_sub does not use rval to root its temporaries so we
|
|
|
|
* can use it to root obj.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
*rval = OBJECT_TO_JSVAL(obj);
|
|
|
|
}
|
2007-08-01 21:33:52 -07:00
|
|
|
return regexp_compile_sub(cx, obj, argc, argv, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_InitRegExpClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2009-03-04 19:26:16 -08:00
|
|
|
JSObject *proto = js_InitClass(cx, obj, NULL, &js_RegExpClass, RegExp, 1,
|
|
|
|
regexp_props, regexp_methods,
|
2009-05-05 14:26:06 -07:00
|
|
|
regexp_static_props, NULL);
|
2009-03-04 19:26:16 -08:00
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-03-04 19:26:16 -08:00
|
|
|
JSObject *ctor = JS_GetConstructor(cx, proto);
|
|
|
|
if (!ctor)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2009-03-04 19:26:16 -08:00
|
|
|
|
|
|
|
/* Give RegExp.prototype private data so it matches the empty string. */
|
|
|
|
jsval rval;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!JS_AliasProperty(cx, ctor, "input", "$_") ||
|
|
|
|
!JS_AliasProperty(cx, ctor, "multiline", "$*") ||
|
|
|
|
!JS_AliasProperty(cx, ctor, "lastMatch", "$&") ||
|
|
|
|
!JS_AliasProperty(cx, ctor, "lastParen", "$+") ||
|
|
|
|
!JS_AliasProperty(cx, ctor, "leftContext", "$`") ||
|
2009-03-04 19:26:16 -08:00
|
|
|
!JS_AliasProperty(cx, ctor, "rightContext", "$'") ||
|
|
|
|
!regexp_compile_sub(cx, proto, 0, NULL, &rval)) {
|
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_NewRegExpObject(JSContext *cx, JSTokenStream *ts,
|
|
|
|
jschar *chars, size_t length, uintN flags)
|
|
|
|
{
|
|
|
|
JSString *str;
|
|
|
|
JSObject *obj;
|
|
|
|
JSRegExp *re;
|
|
|
|
|
2007-07-28 09:57:30 -07:00
|
|
|
str = js_NewStringCopyN(cx, chars, length);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
2009-01-15 17:54:05 -08:00
|
|
|
JSAutoTempValueRooter tvr(cx, str);
|
2007-03-22 10:30:00 -07:00
|
|
|
re = js_NewRegExp(cx, ts, str, flags, JS_FALSE);
|
|
|
|
if (!re)
|
|
|
|
return NULL;
|
2009-07-14 17:06:09 -07:00
|
|
|
obj = js_NewObject(cx, &js_RegExpClass, NULL, NULL);
|
2009-08-04 14:06:55 -07:00
|
|
|
if (!obj) {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_DestroyRegExp(cx, re);
|
2009-08-04 14:06:55 -07:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-08-04 14:06:55 -07:00
|
|
|
obj->setPrivate(re);
|
2009-08-22 09:59:55 -07:00
|
|
|
js_ClearRegExpLastIndex(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-01-26 18:41:30 -08:00
|
|
|
JSObject * JS_FASTCALL
|
2010-02-01 17:20:37 -08:00
|
|
|
js_CloneRegExpObject(JSContext *cx, JSObject *obj, JSObject *proto)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-02-19 09:44:23 -08:00
|
|
|
JS_ASSERT(obj->getClass() == &js_RegExpClass);
|
|
|
|
JS_ASSERT(proto);
|
|
|
|
JS_ASSERT(proto->getClass() == &js_RegExpClass);
|
|
|
|
JSObject *clone = js_NewObjectWithGivenProto(cx, &js_RegExpClass, proto,
|
|
|
|
NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!clone)
|
|
|
|
return NULL;
|
2010-01-26 18:41:30 -08:00
|
|
|
JSRegExp *re = static_cast<JSRegExp *>(obj->getPrivate());
|
|
|
|
clone->setPrivate(re);
|
|
|
|
js_ClearRegExpLastIndex(clone);
|
|
|
|
HOLD_REGEXP(cx, re);
|
2007-03-22 10:30:00 -07:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2010-01-26 18:41:30 -08:00
|
|
|
#ifdef JS_TRACER
|
2010-02-01 17:20:37 -08:00
|
|
|
JS_DEFINE_CALLINFO_3(extern, OBJECT, js_CloneRegExpObject, CONTEXT, OBJECT, OBJECT, 0, 0)
|
2010-01-26 18:41:30 -08:00
|
|
|
#endif
|
|
|
|
|
2009-09-23 08:54:50 -07:00
|
|
|
bool
|
|
|
|
js_ContainsRegExpMetaChars(const jschar *chars, size_t length)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2009-09-23 11:16:30 -07:00
|
|
|
jschar c = chars[i];
|
2009-09-23 08:54:50 -07:00
|
|
|
switch (c) {
|
|
|
|
/* Taken from the PatternCharacter production in 15.10.1. */
|
|
|
|
case '^': case '$': case '\\': case '.': case '*': case '+':
|
|
|
|
case '?': case '(': case ')': case '[': case ']': case '{':
|
|
|
|
case '}': case '|':
|
|
|
|
return true;
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|