From 91b9a92336a5226d61a8313be8a5d9eb8cda04ae Mon Sep 17 00:00:00 2001 From: "vitalyr@chromium.org" Date: Tue, 5 Oct 2010 11:51:41 +0000 Subject: [PATCH] Addressing Mads' comments from http://codereview.chromium.org/3585010/show. Review URL: http://codereview.chromium.org/3613009 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5588 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- include/v8.h | 17 +++++++++++++++++ src/api.cc | 11 +++-------- src/execution.cc | 13 +++++++++++++ src/execution.h | 5 +++++ test/cctest/test-api.cc | 7 +++++++ 5 files changed, 45 insertions(+), 8 deletions(-) diff --git a/include/v8.h b/include/v8.h index 0c2cf8c..a00fec9 100644 --- a/include/v8.h +++ b/include/v8.h @@ -1372,6 +1372,10 @@ class Date : public Value { */ class RegExp : public Value { public: + /** + * Regular expression flag bits. They can be or'ed to enable a set + * of flags. + */ enum Flags { kNone = 0, kGlobal = 1, @@ -1379,6 +1383,16 @@ class RegExp : public Value { kMultiline = 4 }; + /** + * Creates a regular expression from the given pattern string and + * the flags bit field. May throw a JavaScript exception as + * described in ECMA-252, 15.10.4.1. + * + * For example, + * RegExp::New(v8::String::New("foo"), + * static_cast(kGlobal | kMultiline)) + * is equivalent to evaluating "/foo/gm". + */ V8EXPORT static Local New(Handle pattern, Flags flags); @@ -1388,6 +1402,9 @@ class RegExp : public Value { */ V8EXPORT Local GetSource() const; + /** + * Returns the flags bit field. + */ V8EXPORT Flags GetFlags() const; static inline RegExp* Cast(v8::Value* obj); diff --git a/src/api.cc b/src/api.cc index 37c3bfe..962723d 100644 --- a/src/api.cc +++ b/src/api.cc @@ -3754,14 +3754,9 @@ Local v8::RegExp::New(Handle pattern, LOG_API("RegExp::New"); ENTER_V8; EXCEPTION_PREAMBLE(); - i::Handle flags_string = RegExpFlagsToString(flags); - i::Object** argv[2] = { - i::Handle::cast(Utils::OpenHandle(*pattern)).location(), - i::Handle::cast(flags_string).location() - }; - i::Handle obj = i::Execution::New( - i::Handle(i::Top::global_context()->regexp_function()), - 2, argv, + i::Handle obj = i::Execution::NewJSRegExp( + Utils::OpenHandle(*pattern), + RegExpFlagsToString(flags), &has_pending_exception); EXCEPTION_BAILOUT_CHECK(Local()); return Utils::ToLocal(i::Handle::cast(obj)); diff --git a/src/execution.cc b/src/execution.cc index 5421678..6862324 100644 --- a/src/execution.cc +++ b/src/execution.cc @@ -473,6 +473,19 @@ Handle Execution::NewDate(double time, bool* exc) { #undef RETURN_NATIVE_CALL +Handle Execution::NewJSRegExp(Handle pattern, + Handle flags, + bool* exc) { + Handle re_obj = RegExpImpl::CreateRegExpLiteral( + Handle(Top::global_context()->regexp_function()), + pattern, + flags, + exc); + if (*exc) return Handle(); + return Handle::cast(re_obj); +} + + Handle Execution::CharAt(Handle string, uint32_t index) { int int_index = static_cast(index); if (int_index < 0 || int_index >= string->length()) { diff --git a/src/execution.h b/src/execution.h index 2823503..15d85ef 100644 --- a/src/execution.h +++ b/src/execution.h @@ -105,6 +105,11 @@ class Execution : public AllStatic { // Create a new date object from 'time'. static Handle NewDate(double time, bool* exc); + // Create a new regular expression object from 'pattern' and 'flags'. + static Handle NewJSRegExp(Handle pattern, + Handle flags, + bool* exc); + // Used to implement [] notation on strings (calls JS code) static Handle CharAt(Handle str, uint32_t index); diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index c8dd07f..8058bff 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -11715,4 +11715,11 @@ TEST(RegExp) { context->Global()->Set(v8_str("re"), re); ExpectTrue("re.test('FoobarbaZ')"); + + v8::TryCatch try_catch; + re = v8::RegExp::New(v8_str("foo["), v8::RegExp::kNone); + CHECK(re.IsEmpty()); + CHECK(try_catch.HasCaught()); + context->Global()->Set(v8_str("ex"), try_catch.Exception()); + ExpectTrue("ex instanceof SyntaxError"); } -- 2.7.4