ast-value-factory.cc 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
// Copyright 2014 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "src/ast-value-factory.h"

#include "src/api.h"
#include "src/objects.h"

namespace v8 {
namespace internal {

namespace {

// For using StringToArrayIndex.
class OneByteStringStream {
 public:
  explicit OneByteStringStream(Vector<const byte> lb) :
      literal_bytes_(lb), pos_(0) {}

  bool HasMore() { return pos_ < literal_bytes_.length(); }
  uint16_t GetNext() { return literal_bytes_[pos_++]; }

 private:
  Vector<const byte> literal_bytes_;
  int pos_;
};

}

class AstRawStringInternalizationKey : public HashTableKey {
 public:
  explicit AstRawStringInternalizationKey(const AstRawString* string)
      : string_(string) {}

59
  bool IsMatch(Object* other) override {
60 61 62 63 64 65
    if (string_->is_one_byte_)
      return String::cast(other)->IsOneByteEqualTo(string_->literal_bytes_);
    return String::cast(other)->IsTwoByteEqualTo(
        Vector<const uint16_t>::cast(string_->literal_bytes_));
  }

66
  uint32_t Hash() override { return string_->hash() >> Name::kHashShift; }
67

68
  uint32_t HashForObject(Object* key) override {
69 70 71
    return String::cast(key)->Hash();
  }

72
  Handle<Object> AsHandle(Isolate* isolate) override {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    if (string_->is_one_byte_)
      return isolate->factory()->NewOneByteInternalizedString(
          string_->literal_bytes_, string_->hash());
    return isolate->factory()->NewTwoByteInternalizedString(
        Vector<const uint16_t>::cast(string_->literal_bytes_), string_->hash());
  }

 private:
  const AstRawString* string_;
};


void AstRawString::Internalize(Isolate* isolate) {
  if (!string_.is_null()) return;
  if (literal_bytes_.length() == 0) {
    string_ = isolate->factory()->empty_string();
  } else {
    AstRawStringInternalizationKey key(this);
    string_ = StringTable::LookupKey(isolate, &key);
  }
}


bool AstRawString::AsArrayIndex(uint32_t* index) const {
  if (!string_.is_null())
    return string_->AsArrayIndex(index);
  if (!is_one_byte_ || literal_bytes_.length() == 0 ||
      literal_bytes_.length() > String::kMaxArrayIndexSize)
    return false;
  OneByteStringStream stream(literal_bytes_);
  return StringToArrayIndex(&stream, index);
}


bool AstRawString::IsOneByteEqualTo(const char* data) const {
  int length = static_cast<int>(strlen(data));
  if (is_one_byte_ && literal_bytes_.length() == length) {
    const char* token = reinterpret_cast<const char*>(literal_bytes_.start());
    return !strncmp(token, data, length);
  }
  return false;
}


void AstConsString::Internalize(Isolate* isolate) {
  // AstRawStrings are internalized before AstConsStrings so left and right are
  // already internalized.
  string_ = isolate->factory()
                ->NewConsString(left_->string(), right_->string())
                .ToHandleChecked();
}


bool AstValue::IsPropertyName() const {
  if (type_ == STRING) {
    uint32_t index;
    return !string_->AsArrayIndex(&index);
  }
  return false;
}


bool AstValue::BooleanValue() const {
  switch (type_) {
    case STRING:
138
      DCHECK(string_ != NULL);
139 140 141 142
      return !string_->IsEmpty();
    case SYMBOL:
      UNREACHABLE();
      break;
143
    case NUMBER_WITH_DOT:
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    case NUMBER:
      return DoubleToBoolean(number_);
    case SMI:
      return smi_ != 0;
    case BOOLEAN:
      return bool_;
    case NULL_TYPE:
      return false;
    case THE_HOLE:
      UNREACHABLE();
      break;
    case UNDEFINED:
      return false;
  }
  UNREACHABLE();
  return false;
}


void AstValue::Internalize(Isolate* isolate) {
  switch (type_) {
    case STRING:
166
      DCHECK(string_ != NULL);
167
      // Strings are already internalized.
168
      DCHECK(!string_->string().is_null());
169 170
      break;
    case SYMBOL:
171 172 173 174 175 176 177
      if (symbol_name_[0] == 'i') {
        DCHECK_EQ(0, strcmp(symbol_name_, "iterator_symbol"));
        value_ = isolate->factory()->iterator_symbol();
      } else {
        DCHECK_EQ(0, strcmp(symbol_name_, "home_object_symbol"));
        value_ = isolate->factory()->home_object_symbol();
      }
178
      break;
179
    case NUMBER_WITH_DOT:
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    case NUMBER:
      value_ = isolate->factory()->NewNumber(number_, TENURED);
      break;
    case SMI:
      value_ = handle(Smi::FromInt(smi_), isolate);
      break;
    case BOOLEAN:
      if (bool_) {
        value_ = isolate->factory()->true_value();
      } else {
        value_ = isolate->factory()->false_value();
      }
      break;
    case NULL_TYPE:
      value_ = isolate->factory()->null_value();
      break;
    case THE_HOLE:
      value_ = isolate->factory()->the_hole_value();
      break;
    case UNDEFINED:
      value_ = isolate->factory()->undefined_value();
      break;
  }
}


206
AstRawString* AstValueFactory::GetOneByteStringInternal(
207 208 209 210 211 212 213
    Vector<const uint8_t> literal) {
  uint32_t hash = StringHasher::HashSequentialString<uint8_t>(
      literal.start(), literal.length(), hash_seed_);
  return GetString(hash, true, literal);
}


214
AstRawString* AstValueFactory::GetTwoByteStringInternal(
215 216 217 218 219 220 221 222
    Vector<const uint16_t> literal) {
  uint32_t hash = StringHasher::HashSequentialString<uint16_t>(
      literal.start(), literal.length(), hash_seed_);
  return GetString(hash, false, Vector<const byte>::cast(literal));
}


const AstRawString* AstValueFactory::GetString(Handle<String> literal) {
223 224 225 226 227 228 229 230 231 232 233 234 235 236
  // For the FlatContent to stay valid, we shouldn't do any heap
  // allocation. Make sure we won't try to internalize the string in GetString.
  AstRawString* result = NULL;
  Isolate* saved_isolate = isolate_;
  isolate_ = NULL;
  {
    DisallowHeapAllocation no_gc;
    String::FlatContent content = literal->GetFlatContent();
    if (content.IsOneByte()) {
      result = GetOneByteStringInternal(content.ToOneByteVector());
    } else {
      DCHECK(content.IsTwoByte());
      result = GetTwoByteStringInternal(content.ToUC16Vector());
    }
237
  }
238 239 240
  isolate_ = saved_isolate;
  if (isolate_) result->Internalize(isolate_);
  return result;
241 242 243 244 245
}


const AstConsString* AstValueFactory::NewConsString(
    const AstString* left, const AstString* right) {
246 247
  // This Vector will be valid as long as the Collector is alive (meaning that
  // the AstRawString will not be moved).
248
  AstConsString* new_string = new (zone_) AstConsString(left, right);
249
  strings_.Add(new_string);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  if (isolate_) {
    new_string->Internalize(isolate_);
  }
  return new_string;
}


void AstValueFactory::Internalize(Isolate* isolate) {
  if (isolate_) {
    // Everything is already internalized.
    return;
  }
  // Strings need to be internalized before values, because values refer to
  // strings.
  for (int i = 0; i < strings_.length(); ++i) {
    strings_[i]->Internalize(isolate);
  }
  for (int i = 0; i < values_.length(); ++i) {
    values_[i]->Internalize(isolate);
  }
  isolate_ = isolate;
}


const AstValue* AstValueFactory::NewString(const AstRawString* string) {
  AstValue* value = new (zone_) AstValue(string);
276
  DCHECK(string != NULL);
277
  if (isolate_) {
278
    value->Internalize(isolate_);
279
  }
280
  values_.Add(value);
281 282 283 284 285 286 287 288 289
  return value;
}


const AstValue* AstValueFactory::NewSymbol(const char* name) {
  AstValue* value = new (zone_) AstValue(name);
  if (isolate_) {
    value->Internalize(isolate_);
  }
290
  values_.Add(value);
291 292 293 294
  return value;
}


295 296
const AstValue* AstValueFactory::NewNumber(double number, bool with_dot) {
  AstValue* value = new (zone_) AstValue(number, with_dot);
297 298 299
  if (isolate_) {
    value->Internalize(isolate_);
  }
300
  values_.Add(value);
301 302 303 304 305 306 307 308 309 310
  return value;
}


const AstValue* AstValueFactory::NewSmi(int number) {
  AstValue* value =
      new (zone_) AstValue(AstValue::SMI, number);
  if (isolate_) {
    value->Internalize(isolate_);
  }
311
  values_.Add(value);
312 313 314 315
  return value;
}


316 317 318 319 320 321
#define GENERATE_VALUE_GETTER(value, initializer) \
  if (!value) {                                   \
    value = new (zone_) AstValue(initializer);    \
    if (isolate_) {                               \
      value->Internalize(isolate_);               \
    }                                             \
322
    values_.Add(value);                           \
323 324 325 326
  }                                               \
  return value;


327
const AstValue* AstValueFactory::NewBoolean(bool b) {
328 329 330 331
  if (b) {
    GENERATE_VALUE_GETTER(true_value_, true);
  } else {
    GENERATE_VALUE_GETTER(false_value_, false);
332 333 334 335 336
  }
}


const AstValue* AstValueFactory::NewNull() {
337
  GENERATE_VALUE_GETTER(null_value_, AstValue::NULL_TYPE);
338 339 340 341
}


const AstValue* AstValueFactory::NewUndefined() {
342
  GENERATE_VALUE_GETTER(undefined_value_, AstValue::UNDEFINED);
343 344 345 346
}


const AstValue* AstValueFactory::NewTheHole() {
347
  GENERATE_VALUE_GETTER(the_hole_value_, AstValue::THE_HOLE);
348 349 350
}


351 352
#undef GENERATE_VALUE_GETTER

353 354
AstRawString* AstValueFactory::GetString(uint32_t hash, bool is_one_byte,
                                         Vector<const byte> literal_bytes) {
355 356 357 358 359
  // literal_bytes here points to whatever the user passed, and this is OK
  // because we use vector_compare (which checks the contents) to compare
  // against the AstRawStrings which are in the string_table_. We should not
  // return this AstRawString.
  AstRawString key(is_one_byte, literal_bytes, hash);
360
  HashMap::Entry* entry = string_table_.LookupOrInsert(&key, hash);
361 362 363 364 365 366 367 368
  if (entry->value == NULL) {
    // Copy literal contents for later comparison.
    int length = literal_bytes.length();
    byte* new_literal_bytes = zone_->NewArray<byte>(length);
    memcpy(new_literal_bytes, literal_bytes.start(), length);
    AstRawString* new_string = new (zone_) AstRawString(
        is_one_byte, Vector<const byte>(new_literal_bytes, length), hash);
    entry->key = new_string;
369
    strings_.Add(new_string);
370 371 372 373 374 375 376 377 378
    if (isolate_) {
      new_string->Internalize(isolate_);
    }
    entry->value = reinterpret_cast<void*>(1);
  }
  return reinterpret_cast<AstRawString*>(entry->key);
}


379 380 381 382 383 384 385 386 387
bool AstValueFactory::AstRawStringCompare(void* a, void* b) {
  const AstRawString* lhs = static_cast<AstRawString*>(a);
  const AstRawString* rhs = static_cast<AstRawString*>(b);
  if (lhs->is_one_byte() != rhs->is_one_byte()) return false;
  if (lhs->hash() != rhs->hash()) return false;
  int len = lhs->byte_length();
  if (rhs->byte_length() != len) return false;
  return memcmp(lhs->raw_data(), rhs->raw_data(), len) == 0;
}
388 389
}  // namespace internal
}  // namespace v8