blob: 75d5c91d4debe25c3e512f4977e6ec864f6cd552 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_JSREGEXP_H_
29#define V8_JSREGEXP_H_
30
31namespace v8 { namespace internal {
32
33class RegExpImpl {
34 public:
35 // Creates a regular expression literal in the old space.
36 // This function calls the garbage collector if necessary.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000037 static Handle<Object> CreateRegExpLiteral(Handle<JSFunction> constructor,
38 Handle<String> pattern,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039 Handle<String> flags,
40 bool* has_pending_exception);
41
42 // Returns a string representation of a regular expression.
43 // Implements RegExp.prototype.toString, see ECMA-262 section 15.10.6.4.
44 // This function calls the garbage collector if necessary.
45 static Handle<String> ToString(Handle<Object> value);
46
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000047 static Handle<Object> Compile(Handle<JSRegExp> re,
48 Handle<String> pattern,
49 Handle<String> flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
51 // Implements RegExp.prototype.exec(string) function.
52 // See ECMA-262 section 15.10.6.2.
53 // This function calls the garbage collector if necessary.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000054 static Handle<Object> Exec(Handle<JSRegExp> regexp,
55 Handle<String> subject,
56 Handle<Object> index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000057
58 // Call RegExp.prototyp.exec(string) in a loop.
59 // Used by String.prototype.match and String.prototype.replace.
60 // This function calls the garbage collector if necessary.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000061 static Handle<Object> ExecGlobal(Handle<JSRegExp> regexp,
62 Handle<String> subject);
63
64 static Handle<Object> AtomCompile(Handle<JSRegExp> re,
65 Handle<String> pattern);
66
67 static Handle<Object> AtomExec(Handle<JSRegExp> regexp,
68 Handle<String> subject,
69 Handle<Object> index);
70
71 static Handle<Object> AtomExecGlobal(Handle<JSRegExp> regexp,
72 Handle<String> subject);
73
74 static Handle<Object> JsreCompile(Handle<JSRegExp> re,
75 Handle<String> pattern,
76 Handle<String> flags);
77
78 static Handle<Object> JsreExec(Handle<JSRegExp> regexp,
79 Handle<String> subject,
80 Handle<Object> index);
81
ager@chromium.org236ad962008-09-25 09:45:57 +000082 static Handle<Object> JsreExecGlobal(Handle<JSRegExp> regexp,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000083 Handle<String> subject);
84
85 static void NewSpaceCollectionPrologue();
86 static void OldSpaceCollectionPrologue();
87
88 private:
89 // Converts a source string to a 16 bit flat string. The string
90 // will be either sequential or it will be a SlicedString backed
91 // by a flat string.
92 static Handle<String> StringToTwoByte(Handle<String> pattern);
93 static Handle<String> CachedStringToTwoByte(Handle<String> pattern);
94
95 static String* last_ascii_string_;
96 static String* two_byte_cached_string_;
97
98 // Returns the caputure from the re.
ager@chromium.org236ad962008-09-25 09:45:57 +000099 static int JsreCapture(Handle<JSRegExp> re);
100 static ByteArray* JsreInternal(Handle<JSRegExp> re);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101
102 // Call jsRegExpExecute once
ager@chromium.org236ad962008-09-25 09:45:57 +0000103 static Handle<Object> JsreExecOnce(Handle<JSRegExp> regexp,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104 int num_captures,
105 Handle<String> subject,
106 int previous_index,
107 const uc16* utf8_subject,
108 int* ovector,
109 int ovector_length);
110
111 // Set the subject cache. The previous string buffer is not deleted, so the
112 // caller should ensure that it doesn't leak.
113 static void SetSubjectCache(String* subject, char* utf8_subject,
114 int uft8_length, int character_position,
115 int utf8_position);
116
117 // A one element cache of the last utf8_subject string and its length. The
118 // subject JS String object is cached in the heap. We also cache a
119 // translation between position and utf8 position.
120 static char* utf8_subject_cache_;
121 static int utf8_length_cache_;
122 static int utf8_position_;
123 static int character_position_;
124};
125
126
127} } // namespace v8::internal
128
129#endif // V8_JSREGEXP_H_