Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Unified Diff: src/api.cc

Issue 3585010: API: expose RegExp. (Closed)
Patch Set: Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 350410a3cc6d0998fcc0f259a73985c2a9f25bde..0ad64553b660fa38add5c518c64c91a1c772dfbc 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -1991,6 +1991,15 @@ void v8::Date::CheckCast(v8::Value* that) {
}
+void v8::RegExp::CheckCast(v8::Value* that) {
+ if (IsDeadCheck("v8::RegExp::Cast()")) return;
+ i::Handle<i::Object> obj = Utils::OpenHandle(that);
+ ApiCheck(obj->IsJSRegExp(),
+ "v8::RegExp::Cast()",
+ "Could not convert to regular expression");
+}
+
+
bool Value::BooleanValue() const {
if (IsDeadCheck("v8::Value::BooleanValue()")) return false;
LOG_API("BooleanValue");
@@ -3712,6 +3721,64 @@ double v8::Date::NumberValue() const {
}
+static i::Handle<i::String> RegExpFlagsToString(RegExp::Flags flags) {
+ char flags_buf[3];
+ int num_flags = 0;
+ if ((flags & RegExp::kGlobal) != 0) flags_buf[num_flags++] = 'g';
+ if ((flags & RegExp::kMultiline) != 0) flags_buf[num_flags++] = 'm';
+ if ((flags & RegExp::kIgnoreCase) != 0) flags_buf[num_flags++] = 'i';
+ ASSERT(num_flags <= static_cast<int>(sizeof(flags_buf)));
antonm 2010/10/04 14:44:55 You might prefer to use ARRAY_SIZE macro here
Vitaly Repeshko 2010/10/04 15:04:42 Done. But it's longer and still requires a cast to
+ if (num_flags == 0) return i::Factory::empty_string();
antonm 2010/10/04 14:44:55 should we have this special case here? if it's pe
Vitaly Repeshko 2010/10/04 15:04:42 Changed to Factory::LookupSymbol.
+ return i::Factory::NewStringFromAscii(
+ i::Vector<const char>(flags_buf, num_flags));
+}
+
+
+Local<v8::RegExp> v8::RegExp::New(Handle<String> pattern,
+ Flags flags) {
+ EnsureInitialized("v8::RegExp::New()");
+ LOG_API("RegExp::New");
+ ENTER_V8;
+ EXCEPTION_PREAMBLE();
+ i::Handle<i::String> flags_string = RegExpFlagsToString(flags);
+ i::Object** argv[2] = {
+ i::Handle<i::Object>::cast(Utils::OpenHandle(*pattern)).location(),
+ i::Handle<i::Object>::cast(flags_string).location()
+ };
+ i::Handle<i::Object> obj = i::Execution::New(
Mads Ager (chromium) 2010/10/05 08:55:46 I think this code can be simplified by creating a
+ i::Handle<i::JSFunction>(i::Top::global_context()->regexp_function()),
+ 2, argv,
+ &has_pending_exception);
+ EXCEPTION_BAILOUT_CHECK(Local<v8::RegExp>());
+ if (!obj->IsJSRegExp()) return Local<v8::RegExp>();
antonm 2010/10/04 14:44:55 could this condition be true?
Vitaly Repeshko 2010/10/04 15:04:42 With the current spec only if there's a bug in reg
+ return Utils::ToLocal(i::Handle<i::JSRegExp>::cast(obj));
+}
+
+
+Local<v8::String> v8::RegExp::GetSource() const {
+ if (IsDeadCheck("v8::RegExp::GetSource()")) return Local<v8::String>();
+ i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
+ return Utils::ToLocal(i::Handle<i::String>(obj->Pattern()));
+}
+
+
+// Assert that the static flags cast in GetFlags is valid.
+#define REGEXP_FLAG_ASSERT_EQ(api_flag, internal_flag) \
Mads Ager (chromium) 2010/10/05 08:55:46 Is this really worth the macro. Writing this out w
+ STATIC_ASSERT(static_cast<int>(v8::RegExp::api_flag) == \
+ static_cast<int>(i::JSRegExp::internal_flag))
+REGEXP_FLAG_ASSERT_EQ(kNone, NONE);
+REGEXP_FLAG_ASSERT_EQ(kGlobal, GLOBAL);
+REGEXP_FLAG_ASSERT_EQ(kIgnoreCase, IGNORE_CASE);
+REGEXP_FLAG_ASSERT_EQ(kMultiline, MULTILINE);
+#undef REGEXP_FLAG_ASSERT_EQ
+
+v8::RegExp::Flags v8::RegExp::GetFlags() const {
+ if (IsDeadCheck("v8::RegExp::GetFlags()")) return v8::RegExp::kNone;
+ i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
+ return static_cast<RegExp::Flags>(obj->GetFlags().value());
+}
+
+
Local<v8::Array> v8::Array::New(int length) {
EnsureInitialized("v8::Array::New()");
LOG_API("Array::New");

Powered by Google App Engine
This is Rietveld 408576698