js-regexp-inl.h 3.74 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_JS_REGEXP_INL_H_
#define V8_OBJECTS_JS_REGEXP_INL_H_

#include "src/objects/js-regexp.h"

10
#include "src/objects/js-array-inl.h"
11
#include "src/objects/objects-inl.h"  // Needed for write barriers
12
#include "src/objects/smi.h"
13 14 15 16 17 18 19 20
#include "src/objects/string.h"

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

21
TQ_OBJECT_CONSTRUCTORS_IMPL(JSRegExp)
22 23 24 25 26 27 28
// TQ_OBJECT_CONSTRUCTORS_IMPL(JSRegExpResult, JSArray)
OBJECT_CONSTRUCTORS_IMPL(JSRegExpResult, JSArray)
OBJECT_CONSTRUCTORS_IMPL(JSRegExpResultIndices, JSArray)

// CAST_ACCESSOR(JSRegExp)
CAST_ACCESSOR(JSRegExpResult)
CAST_ACCESSOR(JSRegExpResultIndices)
29

30 31
ACCESSORS(JSRegExp, last_index, Object, kLastIndexOffset)

32 33 34 35
ACCESSORS(JSRegExpResult, cached_indices_or_match_info, Object,
          kCachedIndicesOrMatchInfoOffset)
ACCESSORS(JSRegExpResult, names, Object, kNamesOffset)

36
JSRegExp::Type JSRegExp::TypeTag() const {
37
  Object data = this->data();
38 39 40
  if (data.IsUndefined()) return JSRegExp::NOT_COMPILED;
  Smi smi = Smi::cast(FixedArray::cast(data).get(kTagIndex));
  return static_cast<JSRegExp::Type>(smi.value());
41 42 43 44 45 46 47 48 49 50 51 52 53 54
}

int JSRegExp::CaptureCount() {
  switch (TypeTag()) {
    case ATOM:
      return 0;
    case IRREGEXP:
      return Smi::ToInt(DataAt(kIrregexpCaptureCountIndex));
    default:
      UNREACHABLE();
  }
}

JSRegExp::Flags JSRegExp::GetFlags() {
55
  DCHECK(this->data().IsFixedArray());
56
  Object data = this->data();
57 58
  Smi smi = Smi::cast(FixedArray::cast(data).get(kFlagsIndex));
  return Flags(smi.value());
59 60
}

61
String JSRegExp::Pattern() {
62
  DCHECK(this->data().IsFixedArray());
63
  Object data = this->data();
64
  String pattern = String::cast(FixedArray::cast(data).get(kSourceIndex));
65 66 67
  return pattern;
}

68
Object JSRegExp::CaptureNameMap() {
69
  DCHECK(this->data().IsFixedArray());
70
  DCHECK_EQ(TypeTag(), IRREGEXP);
71
  Object value = DataAt(kIrregexpCaptureNameMapIndex);
72 73 74 75
  DCHECK_NE(value, Smi::FromInt(JSRegExp::kUninitializedValue));
  return value;
}

76
Object JSRegExp::DataAt(int index) const {
77
  DCHECK(TypeTag() != NOT_COMPILED);
78
  return FixedArray::cast(data()).get(index);
79 80
}

81
void JSRegExp::SetDataAt(int index, Object value) {
82
  DCHECK(TypeTag() != NOT_COMPILED);
83 84
  DCHECK_GE(index,
            kDataIndex);  // Only implementation data can be set this way.
85
  FixedArray::cast(data()).set(index, value);
86 87
}

88
bool JSRegExp::HasCompiledCode() const {
89
  if (TypeTag() != IRREGEXP) return false;
90
  Smi uninitialized = Smi::FromInt(kUninitializedValue);
91
#ifdef DEBUG
92
  DCHECK(DataAt(kIrregexpLatin1CodeIndex).IsCode() ||
93
         DataAt(kIrregexpLatin1CodeIndex) == uninitialized);
94
  DCHECK(DataAt(kIrregexpUC16CodeIndex).IsCode() ||
95 96 97 98 99
         DataAt(kIrregexpUC16CodeIndex) == uninitialized);
  DCHECK(DataAt(kIrregexpLatin1BytecodeIndex).IsByteArray() ||
         DataAt(kIrregexpLatin1BytecodeIndex) == uninitialized);
  DCHECK(DataAt(kIrregexpUC16BytecodeIndex).IsByteArray() ||
         DataAt(kIrregexpUC16BytecodeIndex) == uninitialized);
100 101 102
#endif  // DEBUG
  return (DataAt(kIrregexpLatin1CodeIndex) != uninitialized ||
          DataAt(kIrregexpUC16CodeIndex) != uninitialized);
103 104 105 106
}

void JSRegExp::DiscardCompiledCodeForSerialization() {
  DCHECK(HasCompiledCode());
107 108 109 110 111
  Smi uninitialized = Smi::FromInt(kUninitializedValue);
  SetDataAt(kIrregexpLatin1CodeIndex, uninitialized);
  SetDataAt(kIrregexpUC16CodeIndex, uninitialized);
  SetDataAt(kIrregexpLatin1BytecodeIndex, uninitialized);
  SetDataAt(kIrregexpUC16BytecodeIndex, uninitialized);
112 113
}

114 115 116 117 118 119
}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_JS_REGEXP_INL_H_