1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code, released
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
43 * JS regular expression interface.
55 extern js::Class js_RegExpClass
;
61 typedef Vector
<int, 20, SystemAllocPolicy
> MatchPairs
;
62 MatchPairs matchPairs
;
63 /* The input that was used to produce matchPairs. */
64 JSString
*matchPairsInput
;
65 /* The input last set on the statics. */
66 JSString
*pendingInput
;
68 RegExpStatics
*bufferLink
;
71 bool createDependent(JSContext
*cx
, size_t start
, size_t end
, Value
*out
) const;
73 void copyTo(RegExpStatics
&dst
) {
74 dst
.matchPairs
.clear();
75 /* 'save' has already reserved space in matchPairs */
76 JS_ALWAYS_TRUE(dst
.matchPairs
.append(matchPairs
));
77 dst
.matchPairsInput
= matchPairsInput
;
78 dst
.pendingInput
= pendingInput
;
83 if (bufferLink
&& !bufferLink
->copied
) {
85 bufferLink
->copied
= true;
89 bool save(JSContext
*cx
, RegExpStatics
*buffer
) {
90 JS_ASSERT(!buffer
->copied
&& !buffer
->bufferLink
);
91 buffer
->bufferLink
= bufferLink
;
93 if (!buffer
->matchPairs
.reserve(matchPairs
.length())) {
94 js_ReportOutOfMemory(cx
);
101 if (bufferLink
->copied
)
102 bufferLink
->copyTo(*this);
103 bufferLink
= bufferLink
->bufferLink
;
106 void checkInvariants() {
108 if (pairCount() == 0) {
109 JS_ASSERT(!matchPairsInput
);
113 /* Pair count is non-zero, so there must be match pairs input. */
114 JS_ASSERT(matchPairsInput
);
115 size_t mpiLen
= matchPairsInput
->length();
117 /* Both members of the first pair must be non-negative. */
118 JS_ASSERT(pairIsPresent(0));
119 JS_ASSERT(get(0, 1) >= 0);
121 /* Present pairs must be valid. */
122 for (size_t i
= 0; i
< pairCount(); ++i
) {
123 if (!pairIsPresent(i
))
125 int start
= get(i
, 0);
126 int limit
= get(i
, 1);
127 JS_ASSERT(mpiLen
>= size_t(limit
) && limit
>= start
&& start
>= 0);
133 * Since the first pair indicates the whole match, the paren pair numbers have to be in the
134 * range [1, pairCount).
136 void checkParenNum(size_t pairNum
) const {
137 JS_CRASH_UNLESS(1 <= pairNum
);
138 JS_CRASH_UNLESS(pairNum
< pairCount());
141 bool pairIsPresent(size_t pairNum
) const {
142 return getCrash(pairNum
, 0) >= 0;
145 /* Precondition: paren is present. */
146 size_t getParenLength(size_t pairNum
) const {
147 checkParenNum(pairNum
);
148 JS_CRASH_UNLESS(pairIsPresent(pairNum
));
149 return getCrash(pairNum
, 1) - getCrash(pairNum
, 0);
152 int get(size_t pairNum
, bool which
) const {
153 JS_ASSERT(pairNum
< pairCount());
154 return matchPairs
[2 * pairNum
+ which
];
157 int getCrash(size_t pairNum
, bool which
) const {
158 JS_CRASH_UNLESS(pairNum
< pairCountCrash());
159 return get(pairNum
, which
);
163 * Check whether the index at |checkValidIndex| is valid (>= 0).
164 * If so, construct a string for it and place it in |*out|.
165 * If not, place undefined in |*out|.
167 bool makeMatch(JSContext
*cx
, size_t checkValidIndex
, size_t pairNum
, Value
*out
) const;
169 static const uintN allFlags
= JSREG_FOLD
| JSREG_GLOB
| JSREG_STICKY
| JSREG_MULTILINE
;
171 struct InitBuffer
{};
172 explicit RegExpStatics(InitBuffer
) : bufferLink(NULL
), copied(false) {}
174 friend class PreserveRegExpStatics
;
177 RegExpStatics() : bufferLink(NULL
), copied(false) { clear(); }
179 static RegExpStatics
*extractFrom(JSObject
*global
);
183 bool updateFromMatch(JSContext
*cx
, JSString
*input
, int *buf
, size_t matchItemCount
) {
185 pendingInput
= input
;
187 if (!matchPairs
.resizeUninitialized(matchItemCount
)) {
188 js_ReportOutOfMemory(cx
);
192 for (size_t i
= 0; i
< matchItemCount
; ++i
)
193 matchPairs
[i
] = buf
[i
];
195 matchPairsInput
= input
;
199 void setMultiline(bool enabled
) {
202 flags
= flags
| JSREG_MULTILINE
;
204 flags
= flags
& ~JSREG_MULTILINE
;
211 matchPairsInput
= NULL
;
215 /* Corresponds to JSAPI functionality to set the pending RegExp input. */
216 void reset(JSString
*newInput
, bool newMultiline
) {
219 pendingInput
= newInput
;
220 setMultiline(newMultiline
);
224 void setPendingInput(JSString
*newInput
) {
226 pendingInput
= newInput
;
232 * When there is a match present, the pairCount is at least 1 for the whole
233 * match. There is one additional pair per parenthesis.
235 * Getting a parenCount requires there to be a match result as a precondition.
239 size_t pairCount() const {
240 JS_ASSERT(matchPairs
.length() % 2 == 0);
241 return matchPairs
.length() / 2;
244 size_t pairCountCrash() const {
245 JS_CRASH_UNLESS(matchPairs
.length() % 2 == 0);
250 size_t parenCount() const {
251 size_t pc
= pairCount();
256 JSString
*getPendingInput() const { return pendingInput
; }
257 uintN
getFlags() const { return flags
; }
258 bool multiline() const { return flags
& JSREG_MULTILINE
; }
260 size_t matchStart() const {
261 int start
= get(0, 0);
262 JS_ASSERT(start
>= 0);
263 return size_t(start
);
266 size_t matchLimit() const {
267 int limit
= get(0, 1);
268 JS_ASSERT(size_t(limit
) >= matchStart() && limit
>= 0);
269 return size_t(limit
);
272 /* Returns whether results for a non-empty match are present. */
273 bool matched() const {
274 JS_ASSERT(pairCount() > 0);
275 JS_ASSERT_IF(get(0, 1) == -1, get(1, 1) == -1);
276 return get(0, 1) - get(0, 0) > 0;
279 void mark(JSTracer
*trc
) const {
281 JS_CALL_STRING_TRACER(trc
, pendingInput
, "res->pendingInput");
283 JS_CALL_STRING_TRACER(trc
, matchPairsInput
, "res->matchPairsInput");
286 /* Value creators. */
288 bool createPendingInput(JSContext
*cx
, Value
*out
) const;
289 bool createLastMatch(JSContext
*cx
, Value
*out
) const { return makeMatch(cx
, 0, 0, out
); }
290 bool createLastParen(JSContext
*cx
, Value
*out
) const;
291 bool createLeftContext(JSContext
*cx
, Value
*out
) const;
292 bool createRightContext(JSContext
*cx
, Value
*out
) const;
294 /* @param pairNum Any number >= 1. */
295 bool createParen(JSContext
*cx
, size_t pairNum
, Value
*out
) const {
296 JS_CRASH_UNLESS(pairNum
>= 1);
297 if (pairNum
>= pairCount()) {
298 out
->setString(cx
->runtime
->emptyString
);
301 return makeMatch(cx
, pairNum
* 2, pairNum
, out
);
304 /* Substring creators. */
306 void getParen(size_t pairNum
, JSSubString
*out
) const;
307 void getLastMatch(JSSubString
*out
) const;
308 void getLastParen(JSSubString
*out
) const;
309 void getLeftContext(JSSubString
*out
) const;
310 void getRightContext(JSSubString
*out
) const;
313 class PreserveRegExpStatics
315 RegExpStatics
*const original
;
316 RegExpStatics buffer
;
319 explicit PreserveRegExpStatics(RegExpStatics
*original
)
320 : original(original
),
321 buffer(RegExpStatics::InitBuffer())
324 bool init(JSContext
*cx
) {
325 return original
->save(cx
, &buffer
);
328 ~PreserveRegExpStatics() {
336 VALUE_IS_REGEXP(JSContext
*cx
, js::Value v
)
338 return !v
.isPrimitive() && v
.toObject().isRegExp();
341 inline const js::Value
&
342 JSObject::getRegExpLastIndex() const
344 JS_ASSERT(isRegExp());
345 return getSlot(JSSLOT_REGEXP_LAST_INDEX
);
349 JSObject::setRegExpLastIndex(const js::Value
&v
)
351 JS_ASSERT(isRegExp());
352 setSlot(JSSLOT_REGEXP_LAST_INDEX
, v
);
356 JSObject::setRegExpLastIndex(jsdouble d
)
358 JS_ASSERT(isRegExp());
359 setSlot(JSSLOT_REGEXP_LAST_INDEX
, js::NumberValue(d
));
363 JSObject::zeroRegExpLastIndex()
365 JS_ASSERT(isRegExp());
366 getSlotRef(JSSLOT_REGEXP_LAST_INDEX
).setInt32(0);
369 namespace js
{ class AutoStringRooter
; }
372 JSObject::isRegExp() const
374 return getClass() == &js_RegExpClass
;
377 extern JS_FRIEND_API(JSBool
)
378 js_ObjectIsRegExp(JSObject
*obj
);
381 js_InitRegExpClass(JSContext
*cx
, JSObject
*obj
);
384 * Export js_regexp_toString to the decompiler.
387 js_regexp_toString(JSContext
*cx
, JSObject
*obj
, js::Value
*vp
);
389 extern JS_FRIEND_API(JSObject
*) JS_FASTCALL
390 js_CloneRegExpObject(JSContext
*cx
, JSObject
*obj
, JSObject
*proto
);
393 * Move data from |cx|'s regexp statics to |statics| and root the input string in |tvr| if it's
396 extern JS_FRIEND_API(void)
397 js_SaveAndClearRegExpStatics(JSContext
*cx
, js::RegExpStatics
*res
, js::AutoStringRooter
*tvr
);
399 /* Move the data from |statics| into |cx|. */
400 extern JS_FRIEND_API(void)
401 js_RestoreRegExpStatics(JSContext
*cx
, js::RegExpStatics
*res
);
404 js_XDRRegExpObject(JSXDRState
*xdr
, JSObject
**objp
);
407 js_regexp_exec(JSContext
*cx
, uintN argc
, js::Value
*vp
);
409 js_regexp_test(JSContext
*cx
, uintN argc
, js::Value
*vp
);
411 #endif /* jsregexp_h___ */