| 1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 | |
| 5 | #include "platform/assert.h" |
| 6 | #include "vm/bootstrap_natives.h" |
| 7 | #include "vm/exceptions.h" |
| 8 | #include "vm/native_entry.h" |
| 9 | #include "vm/object.h" |
| 10 | #include "vm/regexp_assembler_bytecode.h" |
| 11 | #include "vm/regexp_parser.h" |
| 12 | #include "vm/thread.h" |
| 13 | |
| 14 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 15 | #include "vm/regexp_assembler_ir.h" |
| 16 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 17 | |
| 18 | namespace dart { |
| 19 | |
| 20 | DEFINE_NATIVE_ENTRY(RegExp_factory, 0, 6) { |
| 21 | ASSERT( |
| 22 | TypeArguments::CheckedHandle(zone, arguments->NativeArgAt(0)).IsNull()); |
| 23 | GET_NON_NULL_NATIVE_ARGUMENT(String, pattern, arguments->NativeArgAt(1)); |
| 24 | GET_NON_NULL_NATIVE_ARGUMENT(Instance, handle_multi_line, |
| 25 | arguments->NativeArgAt(2)); |
| 26 | GET_NON_NULL_NATIVE_ARGUMENT(Instance, handle_case_sensitive, |
| 27 | arguments->NativeArgAt(3)); |
| 28 | GET_NON_NULL_NATIVE_ARGUMENT(Instance, handle_unicode, |
| 29 | arguments->NativeArgAt(4)); |
| 30 | GET_NON_NULL_NATIVE_ARGUMENT(Instance, handle_dot_all, |
| 31 | arguments->NativeArgAt(5)); |
| 32 | bool ignore_case = handle_case_sensitive.raw() != Bool::True().raw(); |
| 33 | bool multi_line = handle_multi_line.raw() == Bool::True().raw(); |
| 34 | bool unicode = handle_unicode.raw() == Bool::True().raw(); |
| 35 | bool dot_all = handle_dot_all.raw() == Bool::True().raw(); |
| 36 | |
| 37 | RegExpFlags flags; |
| 38 | |
| 39 | if (ignore_case) flags.SetIgnoreCase(); |
| 40 | if (multi_line) flags.SetMultiLine(); |
| 41 | if (unicode) flags.SetUnicode(); |
| 42 | if (dot_all) flags.SetDotAll(); |
| 43 | |
| 44 | // Parse the pattern once in order to throw any format exceptions within |
| 45 | // the factory constructor. It is parsed again upon compilation. |
| 46 | RegExpCompileData compileData; |
| 47 | // Throws an exception on parsing failure. |
| 48 | RegExpParser::ParseRegExp(pattern, flags, &compileData); |
| 49 | |
| 50 | // Create a RegExp object containing only the initial parameters. |
| 51 | return RegExpEngine::CreateRegExp(thread, pattern, flags); |
| 52 | } |
| 53 | |
| 54 | DEFINE_NATIVE_ENTRY(RegExp_getPattern, 0, 1) { |
| 55 | const RegExp& regexp = RegExp::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 56 | ASSERT(!regexp.IsNull()); |
| 57 | return regexp.pattern(); |
| 58 | } |
| 59 | |
| 60 | DEFINE_NATIVE_ENTRY(RegExp_getIsMultiLine, 0, 1) { |
| 61 | const RegExp& regexp = RegExp::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 62 | ASSERT(!regexp.IsNull()); |
| 63 | return Bool::Get(regexp.flags().IsMultiLine()).raw(); |
| 64 | } |
| 65 | |
| 66 | DEFINE_NATIVE_ENTRY(RegExp_getIsUnicode, 0, 1) { |
| 67 | const RegExp& regexp = RegExp::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 68 | ASSERT(!regexp.IsNull()); |
| 69 | return Bool::Get(regexp.flags().IsUnicode()).raw(); |
| 70 | } |
| 71 | |
| 72 | DEFINE_NATIVE_ENTRY(RegExp_getIsDotAll, 0, 1) { |
| 73 | const RegExp& regexp = RegExp::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 74 | ASSERT(!regexp.IsNull()); |
| 75 | return Bool::Get(regexp.flags().IsDotAll()).raw(); |
| 76 | } |
| 77 | |
| 78 | DEFINE_NATIVE_ENTRY(RegExp_getIsCaseSensitive, 0, 1) { |
| 79 | const RegExp& regexp = RegExp::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 80 | ASSERT(!regexp.IsNull()); |
| 81 | return Bool::Get(!regexp.flags().IgnoreCase()).raw(); |
| 82 | } |
| 83 | |
| 84 | DEFINE_NATIVE_ENTRY(RegExp_getGroupCount, 0, 1) { |
| 85 | const RegExp& regexp = RegExp::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 86 | ASSERT(!regexp.IsNull()); |
| 87 | if (regexp.is_initialized()) { |
| 88 | return regexp.num_bracket_expressions(); |
| 89 | } |
| 90 | const String& pattern = String::Handle(regexp.pattern()); |
| 91 | const String& errmsg = |
| 92 | String::Handle(String::New("Regular expression is not initialized yet." )); |
| 93 | const String& message = String::Handle(String::Concat(errmsg, pattern)); |
| 94 | const Array& args = Array::Handle(Array::New(1)); |
| 95 | args.SetAt(0, message); |
| 96 | Exceptions::ThrowByType(Exceptions::kFormat, args); |
| 97 | return Object::null(); |
| 98 | } |
| 99 | |
| 100 | DEFINE_NATIVE_ENTRY(RegExp_getGroupNameMap, 0, 1) { |
| 101 | const RegExp& regexp = RegExp::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 102 | ASSERT(!regexp.IsNull()); |
| 103 | if (regexp.is_initialized()) { |
| 104 | return regexp.capture_name_map(); |
| 105 | } |
| 106 | const String& pattern = String::Handle(regexp.pattern()); |
| 107 | const String& errmsg = String::Handle( |
| 108 | String::New("Regular expression is not initialized yet. " )); |
| 109 | const String& message = String::Handle(String::Concat(errmsg, pattern)); |
| 110 | const Array& args = Array::Handle(Array::New(1)); |
| 111 | args.SetAt(0, message); |
| 112 | Exceptions::ThrowByType(Exceptions::kFormat, args); |
| 113 | return Object::null(); |
| 114 | } |
| 115 | |
| 116 | static ObjectPtr ExecuteMatch(Zone* zone, |
| 117 | NativeArguments* arguments, |
| 118 | bool sticky) { |
| 119 | const RegExp& regexp = RegExp::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 120 | ASSERT(!regexp.IsNull()); |
| 121 | GET_NON_NULL_NATIVE_ARGUMENT(String, subject, arguments->NativeArgAt(1)); |
| 122 | GET_NON_NULL_NATIVE_ARGUMENT(Smi, start_index, arguments->NativeArgAt(2)); |
| 123 | |
| 124 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 125 | if (!FLAG_interpret_irregexp) { |
| 126 | return IRRegExpMacroAssembler::Execute(regexp, subject, start_index, |
| 127 | /*sticky=*/sticky, zone); |
| 128 | } |
| 129 | #endif |
| 130 | return BytecodeRegExpMacroAssembler::Interpret(regexp, subject, start_index, |
| 131 | /*sticky=*/sticky, zone); |
| 132 | } |
| 133 | |
| 134 | DEFINE_NATIVE_ENTRY(RegExp_ExecuteMatch, 0, 3) { |
| 135 | // This function is intrinsified. See Intrinsifier::RegExp_ExecuteMatch. |
| 136 | return ExecuteMatch(zone, arguments, /*sticky=*/false); |
| 137 | } |
| 138 | |
| 139 | DEFINE_NATIVE_ENTRY(RegExp_ExecuteMatchSticky, 0, 3) { |
| 140 | // This function is intrinsified. See Intrinsifier::RegExp_ExecuteMatchSticky. |
| 141 | return ExecuteMatch(zone, arguments, /*sticky=*/true); |
| 142 | } |
| 143 | |
| 144 | } // namespace dart |
| 145 | |