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

Side by Side Diff: lib/regexp.cc

Issue 8363034: Report NullPointerException when we try to construct a regular expression (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 9 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | vm/ast.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/bootstrap_natives.h" 5 #include "vm/bootstrap_natives.h"
6 6
7 #include "vm/assert.h" 7 #include "vm/assert.h"
8 #include "vm/dart_entry.h"
8 #include "vm/exceptions.h" 9 #include "vm/exceptions.h"
9 #include "vm/native_entry.h" 10 #include "vm/native_entry.h"
10 #include "vm/object.h" 11 #include "vm/object.h"
11 12
12 #include "lib/regexp_jsc.h" 13 #include "lib/regexp_jsc.h"
13 14
14 namespace dart { 15 namespace dart {
15 16
17 static void CheckAndThrowExceptionIfNull(const Instance& obj) {
18 if (obj.IsNull()) {
19 GrowableArray<const Object*> args;
20 Exceptions::ThrowByType(Exceptions::kNullPointer, args);
21 }
22 }
23
24
16 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_factory, 4) { 25 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_factory, 4) {
17 ASSERT(TypeArguments::CheckedHandle(arguments->At(0)).IsNull()); 26 ASSERT(TypeArguments::CheckedHandle(arguments->At(0)).IsNull());
18 const String& pattern = String::CheckedHandle(arguments->At(1)); 27 const String& pattern = String::CheckedHandle(arguments->At(1));
28 CheckAndThrowExceptionIfNull(pattern);
19 const Instance& handle_multi_line = Instance::CheckedHandle(arguments->At(2)); 29 const Instance& handle_multi_line = Instance::CheckedHandle(arguments->At(2));
20 const Instance& handle_ignore_case = 30 const Instance& handle_ignore_case =
21 Instance::CheckedHandle(arguments->At(3)); 31 Instance::CheckedHandle(arguments->At(3));
22 bool ignore_case = handle_ignore_case.raw() == Bool::True(); 32 bool ignore_case = handle_ignore_case.raw() == Bool::True();
23 bool multi_line = handle_multi_line.raw() == Bool::True(); 33 bool multi_line = handle_multi_line.raw() == Bool::True();
24 const JSRegExp& new_regex = JSRegExp::Handle( 34 const JSRegExp& new_regex = JSRegExp::Handle(
25 Jscre::Compile(pattern, multi_line, ignore_case)); 35 Jscre::Compile(pattern, multi_line, ignore_case));
26 arguments->SetReturn(new_regex); 36 arguments->SetReturn(new_regex);
27 } 37 }
28 38
29 39
30 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_getPattern, 1) { 40 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_getPattern, 1) {
31 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->At(0)); 41 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->At(0));
42 ASSERT(!regexp.IsNull());
32 const String& result = String::Handle(regexp.pattern()); 43 const String& result = String::Handle(regexp.pattern());
33 arguments->SetReturn(result); 44 arguments->SetReturn(result);
34 } 45 }
35 46
36 47
37 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_multiLine, 1) { 48 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_multiLine, 1) {
38 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->At(0)); 49 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->At(0));
50 ASSERT(!regexp.IsNull());
39 const Bool& result = Bool::Handle(Bool::Get(regexp.is_multi_line())); 51 const Bool& result = Bool::Handle(Bool::Get(regexp.is_multi_line()));
40 arguments->SetReturn(result); 52 arguments->SetReturn(result);
41 } 53 }
42 54
43 55
44 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_ignoreCase, 1) { 56 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_ignoreCase, 1) {
45 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->At(0)); 57 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->At(0));
58 ASSERT(!regexp.IsNull());
46 const Bool& result = Bool::Handle(Bool::Get(regexp.is_ignore_case())); 59 const Bool& result = Bool::Handle(Bool::Get(regexp.is_ignore_case()));
47 arguments->SetReturn(result); 60 arguments->SetReturn(result);
48 } 61 }
49 62
50 63
51 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_getGroupCount, 1) { 64 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_getGroupCount, 1) {
52 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->At(0)); 65 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->At(0));
66 ASSERT(!regexp.IsNull());
53 if (regexp.is_initialized()) { 67 if (regexp.is_initialized()) {
54 const Smi& result = Smi::Handle(regexp.num_bracket_expressions()); 68 const Smi& result = Smi::Handle(regexp.num_bracket_expressions());
55 arguments->SetReturn(result); 69 arguments->SetReturn(result);
56 return; 70 return;
57 } 71 }
58 const String& pattern = String::Handle(regexp.pattern()); 72 const String& pattern = String::Handle(regexp.pattern());
59 const String& errmsg = 73 const String& errmsg =
60 String::Handle(String::New("Regular expression is not initialized yet")); 74 String::Handle(String::New("Regular expression is not initialized yet"));
61 GrowableArray<const Object*> args; 75 GrowableArray<const Object*> args;
62 args.Add(&pattern); 76 args.Add(&pattern);
63 args.Add(&errmsg); 77 args.Add(&errmsg);
64 Exceptions::ThrowByType(Exceptions::kIllegalJSRegExp, args); 78 Exceptions::ThrowByType(Exceptions::kIllegalJSRegExp, args);
65 } 79 }
66 80
67 81
68 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_ExecuteMatch, 3) { 82 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_ExecuteMatch, 3) {
69 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->At(0)); 83 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->At(0));
84 ASSERT(!regexp.IsNull());
70 const String& str = String::CheckedHandle(arguments->At(1)); 85 const String& str = String::CheckedHandle(arguments->At(1));
86 CheckAndThrowExceptionIfNull(str);
turnidge 2011/10/27 17:08:19 I just wanted to point out that by putting the che
siva 2011/10/28 06:14:43 Doing null pointer checks and throwing NullPointer
71 const Smi& start_index = Smi::CheckedHandle(arguments->At(2)); 87 const Smi& start_index = Smi::CheckedHandle(arguments->At(2));
72 const Array& result = 88 const Array& result =
73 Array::Handle(Jscre::Execute(regexp, str, start_index.Value())); 89 Array::Handle(Jscre::Execute(regexp, str, start_index.Value()));
74 arguments->SetReturn(result); 90 arguments->SetReturn(result);
75 } 91 }
76 92
77 } // namespace dart 93 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | vm/ast.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698