blob: c05380d79af0110144894182a1925cb8a4fd8b58 [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,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +000065 Handle<String> pattern,
66 JSRegExp::Flags flags);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000067
68 static Handle<Object> AtomExec(Handle<JSRegExp> regexp,
69 Handle<String> subject,
70 Handle<Object> index);
71
72 static Handle<Object> AtomExecGlobal(Handle<JSRegExp> regexp,
73 Handle<String> subject);
74
75 static Handle<Object> JsreCompile(Handle<JSRegExp> re,
76 Handle<String> pattern,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +000077 JSRegExp::Flags flags);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000078
79 static Handle<Object> JsreExec(Handle<JSRegExp> regexp,
80 Handle<String> subject,
81 Handle<Object> index);
82
ager@chromium.org236ad962008-09-25 09:45:57 +000083 static Handle<Object> JsreExecGlobal(Handle<JSRegExp> regexp,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084 Handle<String> subject);
85
86 static void NewSpaceCollectionPrologue();
87 static void OldSpaceCollectionPrologue();
88
89 private:
90 // Converts a source string to a 16 bit flat string. The string
91 // will be either sequential or it will be a SlicedString backed
92 // by a flat string.
93 static Handle<String> StringToTwoByte(Handle<String> pattern);
94 static Handle<String> CachedStringToTwoByte(Handle<String> pattern);
95
96 static String* last_ascii_string_;
97 static String* two_byte_cached_string_;
98
99 // Returns the caputure from the re.
ager@chromium.org236ad962008-09-25 09:45:57 +0000100 static int JsreCapture(Handle<JSRegExp> re);
101 static ByteArray* JsreInternal(Handle<JSRegExp> re);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102
103 // Call jsRegExpExecute once
ager@chromium.org236ad962008-09-25 09:45:57 +0000104 static Handle<Object> JsreExecOnce(Handle<JSRegExp> regexp,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 int num_captures,
106 Handle<String> subject,
107 int previous_index,
108 const uc16* utf8_subject,
109 int* ovector,
110 int ovector_length);
111
112 // Set the subject cache. The previous string buffer is not deleted, so the
113 // caller should ensure that it doesn't leak.
114 static void SetSubjectCache(String* subject, char* utf8_subject,
115 int uft8_length, int character_position,
116 int utf8_position);
117
118 // A one element cache of the last utf8_subject string and its length. The
119 // subject JS String object is cached in the heap. We also cache a
120 // translation between position and utf8 position.
121 static char* utf8_subject_cache_;
122 static int utf8_length_cache_;
123 static int utf8_position_;
124 static int character_position_;
125};
126
127
128} } // namespace v8::internal
129
130#endif // V8_JSREGEXP_H_