11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef V8_OBJECTS_JS_REGEXP_INL_H_ 61cb0ef41Sopenharmony_ci#define V8_OBJECTS_JS_REGEXP_INL_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "src/objects/js-regexp.h" 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci#include "src/objects/js-array-inl.h" 111cb0ef41Sopenharmony_ci#include "src/objects/objects-inl.h" // Needed for write barriers 121cb0ef41Sopenharmony_ci#include "src/objects/smi.h" 131cb0ef41Sopenharmony_ci#include "src/objects/string.h" 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci// Has to be the last include (doesn't have include guards): 161cb0ef41Sopenharmony_ci#include "src/objects/object-macros.h" 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cinamespace v8 { 191cb0ef41Sopenharmony_cinamespace internal { 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci#include "torque-generated/src/objects/js-regexp-tq-inl.inc" 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciTQ_OBJECT_CONSTRUCTORS_IMPL(JSRegExp) 241cb0ef41Sopenharmony_ciTQ_OBJECT_CONSTRUCTORS_IMPL(JSRegExpResult) 251cb0ef41Sopenharmony_ciTQ_OBJECT_CONSTRUCTORS_IMPL(JSRegExpResultIndices) 261cb0ef41Sopenharmony_ciTQ_OBJECT_CONSTRUCTORS_IMPL(JSRegExpResultWithIndices) 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciACCESSORS(JSRegExp, last_index, Object, kLastIndexOffset) 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciJSRegExp::Type JSRegExp::type_tag() const { 311cb0ef41Sopenharmony_ci Object data = this->data(); 321cb0ef41Sopenharmony_ci if (data.IsUndefined()) return JSRegExp::NOT_COMPILED; 331cb0ef41Sopenharmony_ci Smi smi = Smi::cast(FixedArray::cast(data).get(kTagIndex)); 341cb0ef41Sopenharmony_ci return static_cast<JSRegExp::Type>(smi.value()); 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ciint JSRegExp::capture_count() const { 381cb0ef41Sopenharmony_ci switch (type_tag()) { 391cb0ef41Sopenharmony_ci case ATOM: 401cb0ef41Sopenharmony_ci return 0; 411cb0ef41Sopenharmony_ci case EXPERIMENTAL: 421cb0ef41Sopenharmony_ci case IRREGEXP: 431cb0ef41Sopenharmony_ci return Smi::ToInt(DataAt(kIrregexpCaptureCountIndex)); 441cb0ef41Sopenharmony_ci default: 451cb0ef41Sopenharmony_ci UNREACHABLE(); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci} 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ciint JSRegExp::max_register_count() const { 501cb0ef41Sopenharmony_ci CHECK_EQ(type_tag(), IRREGEXP); 511cb0ef41Sopenharmony_ci return Smi::ToInt(DataAt(kIrregexpMaxRegisterCountIndex)); 521cb0ef41Sopenharmony_ci} 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ciString JSRegExp::atom_pattern() const { 551cb0ef41Sopenharmony_ci DCHECK_EQ(type_tag(), ATOM); 561cb0ef41Sopenharmony_ci return String::cast(DataAt(JSRegExp::kAtomPatternIndex)); 571cb0ef41Sopenharmony_ci} 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciString JSRegExp::source() const { 601cb0ef41Sopenharmony_ci return String::cast(TorqueGeneratedClass::source()); 611cb0ef41Sopenharmony_ci} 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ciJSRegExp::Flags JSRegExp::flags() const { 641cb0ef41Sopenharmony_ci Smi smi = Smi::cast(TorqueGeneratedClass::flags()); 651cb0ef41Sopenharmony_ci return Flags(smi.value()); 661cb0ef41Sopenharmony_ci} 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ciString JSRegExp::EscapedPattern() { 691cb0ef41Sopenharmony_ci DCHECK(this->source().IsString()); 701cb0ef41Sopenharmony_ci return String::cast(source()); 711cb0ef41Sopenharmony_ci} 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ciObject JSRegExp::capture_name_map() { 741cb0ef41Sopenharmony_ci DCHECK(TypeSupportsCaptures(type_tag())); 751cb0ef41Sopenharmony_ci Object value = DataAt(kIrregexpCaptureNameMapIndex); 761cb0ef41Sopenharmony_ci DCHECK_NE(value, Smi::FromInt(JSRegExp::kUninitializedValue)); 771cb0ef41Sopenharmony_ci return value; 781cb0ef41Sopenharmony_ci} 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_civoid JSRegExp::set_capture_name_map(Handle<FixedArray> capture_name_map) { 811cb0ef41Sopenharmony_ci if (capture_name_map.is_null()) { 821cb0ef41Sopenharmony_ci SetDataAt(JSRegExp::kIrregexpCaptureNameMapIndex, Smi::zero()); 831cb0ef41Sopenharmony_ci } else { 841cb0ef41Sopenharmony_ci SetDataAt(JSRegExp::kIrregexpCaptureNameMapIndex, *capture_name_map); 851cb0ef41Sopenharmony_ci } 861cb0ef41Sopenharmony_ci} 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ciObject JSRegExp::DataAt(int index) const { 891cb0ef41Sopenharmony_ci DCHECK(type_tag() != NOT_COMPILED); 901cb0ef41Sopenharmony_ci return FixedArray::cast(data()).get(index); 911cb0ef41Sopenharmony_ci} 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_civoid JSRegExp::SetDataAt(int index, Object value) { 941cb0ef41Sopenharmony_ci DCHECK(type_tag() != NOT_COMPILED); 951cb0ef41Sopenharmony_ci // Only implementation data can be set this way. 961cb0ef41Sopenharmony_ci DCHECK_GE(index, kFirstTypeSpecificIndex); 971cb0ef41Sopenharmony_ci FixedArray::cast(data()).set(index, value); 981cb0ef41Sopenharmony_ci} 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_cibool JSRegExp::HasCompiledCode() const { 1011cb0ef41Sopenharmony_ci if (type_tag() != IRREGEXP) return false; 1021cb0ef41Sopenharmony_ci Smi uninitialized = Smi::FromInt(kUninitializedValue); 1031cb0ef41Sopenharmony_ci#ifdef DEBUG 1041cb0ef41Sopenharmony_ci DCHECK(DataAt(kIrregexpLatin1CodeIndex).IsCodeT() || 1051cb0ef41Sopenharmony_ci DataAt(kIrregexpLatin1CodeIndex) == uninitialized); 1061cb0ef41Sopenharmony_ci DCHECK(DataAt(kIrregexpUC16CodeIndex).IsCodeT() || 1071cb0ef41Sopenharmony_ci DataAt(kIrregexpUC16CodeIndex) == uninitialized); 1081cb0ef41Sopenharmony_ci DCHECK(DataAt(kIrregexpLatin1BytecodeIndex).IsByteArray() || 1091cb0ef41Sopenharmony_ci DataAt(kIrregexpLatin1BytecodeIndex) == uninitialized); 1101cb0ef41Sopenharmony_ci DCHECK(DataAt(kIrregexpUC16BytecodeIndex).IsByteArray() || 1111cb0ef41Sopenharmony_ci DataAt(kIrregexpUC16BytecodeIndex) == uninitialized); 1121cb0ef41Sopenharmony_ci#endif // DEBUG 1131cb0ef41Sopenharmony_ci return (DataAt(kIrregexpLatin1CodeIndex) != uninitialized || 1141cb0ef41Sopenharmony_ci DataAt(kIrregexpUC16CodeIndex) != uninitialized); 1151cb0ef41Sopenharmony_ci} 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_civoid JSRegExp::DiscardCompiledCodeForSerialization() { 1181cb0ef41Sopenharmony_ci DCHECK(HasCompiledCode()); 1191cb0ef41Sopenharmony_ci Smi uninitialized = Smi::FromInt(kUninitializedValue); 1201cb0ef41Sopenharmony_ci SetDataAt(kIrregexpLatin1CodeIndex, uninitialized); 1211cb0ef41Sopenharmony_ci SetDataAt(kIrregexpUC16CodeIndex, uninitialized); 1221cb0ef41Sopenharmony_ci SetDataAt(kIrregexpLatin1BytecodeIndex, uninitialized); 1231cb0ef41Sopenharmony_ci SetDataAt(kIrregexpUC16BytecodeIndex, uninitialized); 1241cb0ef41Sopenharmony_ci} 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci} // namespace internal 1271cb0ef41Sopenharmony_ci} // namespace v8 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci#include "src/objects/object-macros-undef.h" 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci#endif // V8_OBJECTS_JS_REGEXP_INL_H_ 132