| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2008, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2008, 2009 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2008 Collabora Ltd. | 3 * Copyright (C) 2008 Collabora Ltd. |
| 4 * Copyright (C) 2011 Peter Varga (pvarga@webkit.org), University of Szeged | 4 * Copyright (C) 2011 Peter Varga (pvarga@webkit.org), University of Szeged |
| 5 * Copyright (C) 2013 Google Inc. All rights reserved. | 5 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 6 * | 6 * |
| 7 * Redistribution and use in source and binary forms, with or without | 7 * Redistribution and use in source and binary forms, with or without |
| 8 * modification, are permitted provided that the following conditions | 8 * modification, are permitted provided that the following conditions |
| 9 * are met: | 9 * are met: |
| 10 * 1. Redistributions of source code must retain the above copyright | 10 * 1. Redistributions of source code must retain the above copyright |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 #include "bindings/v8/V8Binding.h" | 32 #include "bindings/v8/V8Binding.h" |
| 33 #include "bindings/v8/V8PerIsolateData.h" | 33 #include "bindings/v8/V8PerIsolateData.h" |
| 34 #include "bindings/v8/V8ScriptRunner.h" | 34 #include "bindings/v8/V8ScriptRunner.h" |
| 35 | 35 |
| 36 namespace WebCore { | 36 namespace WebCore { |
| 37 | 37 |
| 38 ScriptRegexp::ScriptRegexp(const String& pattern, TextCaseSensitivity caseSensit
ivity, MultilineMode multilineMode) | 38 ScriptRegexp::ScriptRegexp(const String& pattern, TextCaseSensitivity caseSensit
ivity, MultilineMode multilineMode) |
| 39 { | 39 { |
| 40 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 40 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 41 v8::HandleScope handleScope(isolate); | 41 v8::HandleScope handleScope(isolate); |
| 42 v8::Context::Scope contextScope(V8PerIsolateData::from(isolate)->ensureRegex
Context()); | 42 v8::Context::Scope contextScope(V8PerIsolateData::from(isolate)->ensureDomIn
JSContext()); |
| 43 v8::TryCatch tryCatch; | 43 v8::TryCatch tryCatch; |
| 44 | 44 |
| 45 unsigned flags = v8::RegExp::kNone; | 45 unsigned flags = v8::RegExp::kNone; |
| 46 if (caseSensitivity == TextCaseInsensitive) | 46 if (caseSensitivity == TextCaseInsensitive) |
| 47 flags |= v8::RegExp::kIgnoreCase; | 47 flags |= v8::RegExp::kIgnoreCase; |
| 48 if (multilineMode == MultilineEnabled) | 48 if (multilineMode == MultilineEnabled) |
| 49 flags |= v8::RegExp::kMultiline; | 49 flags |= v8::RegExp::kMultiline; |
| 50 | 50 |
| 51 v8::Local<v8::RegExp> regex = v8::RegExp::New(v8String(isolate, pattern), st
atic_cast<v8::RegExp::Flags>(flags)); | 51 v8::Local<v8::RegExp> regex = v8::RegExp::New(v8String(isolate, pattern), st
atic_cast<v8::RegExp::Flags>(flags)); |
| 52 | 52 |
| 53 // If the regex failed to compile we'll get an empty handle. | 53 // If the regex failed to compile we'll get an empty handle. |
| 54 if (!regex.IsEmpty()) | 54 if (!regex.IsEmpty()) |
| 55 m_regex.set(isolate, regex); | 55 m_regex.set(isolate, regex); |
| 56 } | 56 } |
| 57 | 57 |
| 58 int ScriptRegexp::match(const String& string, int startFrom, int* matchLength) c
onst | 58 int ScriptRegexp::match(const String& string, int startFrom, int* matchLength) c
onst |
| 59 { | 59 { |
| 60 if (matchLength) | 60 if (matchLength) |
| 61 *matchLength = 0; | 61 *matchLength = 0; |
| 62 | 62 |
| 63 if (m_regex.isEmpty() || string.isNull()) | 63 if (m_regex.isEmpty() || string.isNull()) |
| 64 return -1; | 64 return -1; |
| 65 | 65 |
| 66 // v8 strings are limited to int. | 66 // v8 strings are limited to int. |
| 67 if (string.length() > INT_MAX) | 67 if (string.length() > INT_MAX) |
| 68 return -1; | 68 return -1; |
| 69 | 69 |
| 70 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 70 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 71 v8::HandleScope handleScope(isolate); | 71 v8::HandleScope handleScope(isolate); |
| 72 v8::Context::Scope contextScope(V8PerIsolateData::from(isolate)->ensureRegex
Context()); | 72 v8::Context::Scope contextScope(V8PerIsolateData::from(isolate)->ensureDomIn
JSContext()); |
| 73 v8::TryCatch tryCatch; | 73 v8::TryCatch tryCatch; |
| 74 | 74 |
| 75 v8::Local<v8::RegExp> regex = m_regex.newLocal(isolate); | 75 v8::Local<v8::RegExp> regex = m_regex.newLocal(isolate); |
| 76 v8::Local<v8::Function> exec = regex->Get(v8AtomicString(isolate, "exec")).A
s<v8::Function>(); | 76 v8::Local<v8::Function> exec = regex->Get(v8AtomicString(isolate, "exec")).A
s<v8::Function>(); |
| 77 v8::Handle<v8::Value> argv[] = { v8String(isolate, string.substring(startFro
m)) }; | 77 v8::Handle<v8::Value> argv[] = { v8String(isolate, string.substring(startFro
m)) }; |
| 78 v8::Local<v8::Value> returnValue = V8ScriptRunner::callInternalFunction(exec
, regex, WTF_ARRAY_LENGTH(argv), argv, isolate); | 78 v8::Local<v8::Value> returnValue = V8ScriptRunner::callInternalFunction(exec
, regex, WTF_ARRAY_LENGTH(argv), argv, isolate); |
| 79 | 79 |
| 80 // RegExp#exec returns null if there's no match, otherwise it returns an | 80 // RegExp#exec returns null if there's no match, otherwise it returns an |
| 81 // Array of strings with the first being the whole match string and others | 81 // Array of strings with the first being the whole match string and others |
| 82 // being subgroups. The Array also has some random properties tacked on like | 82 // being subgroups. The Array also has some random properties tacked on like |
| 83 // "index" which is the offset of the match. | 83 // "index" which is the offset of the match. |
| 84 // | 84 // |
| 85 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Obje
cts/RegExp/exec | 85 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Obje
cts/RegExp/exec |
| 86 | 86 |
| 87 if (!returnValue->IsArray()) | 87 if (!returnValue->IsArray()) |
| 88 return -1; | 88 return -1; |
| 89 | 89 |
| 90 v8::Local<v8::Array> result = returnValue.As<v8::Array>(); | 90 v8::Local<v8::Array> result = returnValue.As<v8::Array>(); |
| 91 int matchOffset = result->Get(v8AtomicString(isolate, "index"))->ToInt32()->
Value(); | 91 int matchOffset = result->Get(v8AtomicString(isolate, "index"))->ToInt32()->
Value(); |
| 92 if (matchLength) { | 92 if (matchLength) { |
| 93 v8::Local<v8::String> match = result->Get(0).As<v8::String>(); | 93 v8::Local<v8::String> match = result->Get(0).As<v8::String>(); |
| 94 *matchLength = match->Length(); | 94 *matchLength = match->Length(); |
| 95 } | 95 } |
| 96 | 96 |
| 97 return matchOffset + startFrom; | 97 return matchOffset + startFrom; |
| 98 } | 98 } |
| 99 | 99 |
| 100 } // namespace WebCore | 100 } // namespace WebCore |
| OLD | NEW |