ast-value-factory.h 12.9 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
// 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.

#ifndef V8_AST_VALUE_FACTORY_H_
#define V8_AST_VALUE_FACTORY_H_

#include "src/api.h"
#include "src/hashmap.h"
#include "src/utils.h"

// AstString, AstValue and AstValueFactory are for storing strings and values
// independent of the V8 heap and internalizing them later. During parsing,
// AstStrings and AstValues are created and stored outside the heap, in
// AstValueFactory. After parsing, the strings and values are internalized
// (moved into the V8 heap).
namespace v8 {
namespace internal {

class AstString : public ZoneObject {
 public:
  virtual ~AstString() {}

  virtual int length() const = 0;
  bool IsEmpty() const { return length() == 0; }

  // Puts the string into the V8 heap.
  virtual void Internalize(Isolate* isolate) = 0;

  // This function can be called after internalizing.
  V8_INLINE Handle<String> string() const {
55
    DCHECK(!string_.is_null());
56 57 58 59 60 61 62 63 64 65 66
    return string_;
  }

 protected:
  // This is null until the string is internalized.
  Handle<String> string_;
};


class AstRawString : public AstString {
 public:
67
  int length() const override {
68 69 70 71 72
    if (is_one_byte_)
      return literal_bytes_.length();
    return literal_bytes_.length() / 2;
  }

73 74
  int byte_length() const { return literal_bytes_.length(); }

75
  void Internalize(Isolate* isolate) override;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

  bool AsArrayIndex(uint32_t* index) const;

  // The string is not null-terminated, use length() to find out the length.
  const unsigned char* raw_data() const {
    return literal_bytes_.start();
  }
  bool is_one_byte() const { return is_one_byte_; }
  bool IsOneByteEqualTo(const char* data) const;
  uint16_t FirstCharacter() const {
    if (is_one_byte_)
      return literal_bytes_[0];
    const uint16_t* c =
        reinterpret_cast<const uint16_t*>(literal_bytes_.start());
    return *c;
  }

  // For storing AstRawStrings in a hash map.
  uint32_t hash() const {
    return hash_;
  }
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
 private:
  friend class AstValueFactory;
  friend class AstRawStringInternalizationKey;

  AstRawString(bool is_one_byte, const Vector<const byte>& literal_bytes,
            uint32_t hash)
      : is_one_byte_(is_one_byte), literal_bytes_(literal_bytes), hash_(hash) {}

  AstRawString()
      : is_one_byte_(true),
        hash_(0) {}

  bool is_one_byte_;

  // Points to memory owned by Zone.
  Vector<const byte> literal_bytes_;
  uint32_t hash_;
};


class AstConsString : public AstString {
 public:
  AstConsString(const AstString* left, const AstString* right)
      : left_(left),
        right_(right) {}

124
  int length() const override { return left_->length() + right_->length(); }
125

126
  void Internalize(Isolate* isolate) override;
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

 private:
  friend class AstValueFactory;

  const AstString* left_;
  const AstString* right_;
};


// AstValue is either a string, a number, a string array, a boolean, or a
// special value (null, undefined, the hole).
class AstValue : public ZoneObject {
 public:
  bool IsString() const {
    return type_ == STRING;
  }

  bool IsNumber() const {
145
    return type_ == NUMBER || type_ == NUMBER_WITH_DOT || type_ == SMI;
146 147
  }

148 149
  bool ContainsDot() const { return type_ == NUMBER_WITH_DOT; }

150 151 152 153 154 155 156 157
  const AstRawString* AsString() const {
    if (type_ == STRING)
      return string_;
    UNREACHABLE();
    return 0;
  }

  double AsNumber() const {
158
    if (type_ == NUMBER || type_ == NUMBER_WITH_DOT)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
      return number_;
    if (type_ == SMI)
      return smi_;
    UNREACHABLE();
    return 0;
  }

  bool EqualsString(const AstRawString* string) const {
    return type_ == STRING && string_ == string;
  }

  bool IsPropertyName() const;

  bool BooleanValue() const;

174 175
  bool IsTheHole() const { return type_ == THE_HOLE; }

176 177 178 179 180 181 182
  void Internalize(Isolate* isolate);

  // Can be called after Internalize has been called.
  V8_INLINE Handle<Object> value() const {
    if (type_ == STRING) {
      return string_->string();
    }
183
    DCHECK(!value_.is_null());
184 185 186 187 188 189 190 191 192 193
    return value_;
  }

 private:
  friend class AstValueFactory;

  enum Type {
    STRING,
    SYMBOL,
    NUMBER,
194
    NUMBER_WITH_DOT,
195 196 197 198 199 200 201 202 203 204 205
    SMI,
    BOOLEAN,
    NULL_TYPE,
    UNDEFINED,
    THE_HOLE
  };

  explicit AstValue(const AstRawString* s) : type_(STRING) { string_ = s; }

  explicit AstValue(const char* name) : type_(SYMBOL) { symbol_name_ = name; }

206 207 208 209 210 211 212 213
  explicit AstValue(double n, bool with_dot) {
    if (with_dot) {
      type_ = NUMBER_WITH_DOT;
    } else {
      type_ = NUMBER;
    }
    number_ = n;
  }
214 215

  AstValue(Type t, int i) : type_(t) {
216
    DCHECK(type_ == SMI);
217 218 219 220 221 222
    smi_ = i;
  }

  explicit AstValue(bool b) : type_(BOOLEAN) { bool_ = b; }

  explicit AstValue(Type t) : type_(t) {
223
    DCHECK(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  }

  Type type_;

  // Uninternalized value.
  union {
    const AstRawString* string_;
    double number_;
    int smi_;
    bool bool_;
    ZoneList<const AstRawString*>* strings_;
    const char* symbol_name_;
  };

  // Internalized value (empty before internalized).
  Handle<Object> value_;
};


243
// For generating constants.
244 245 246
#define STRING_CONSTANTS(F)                                                \
  F(anonymous_function, "(anonymous function)")                            \
  F(arguments, "arguments")                                                \
247
  F(concat_iterable_to_array, "$concatIterableToArray")                    \
248 249 250 251 252 253 254 255 256 257 258 259
  F(constructor, "constructor")                                            \
  F(default, "default")                                                    \
  F(done, "done")                                                          \
  F(dot, ".")                                                              \
  F(dot_for, ".for")                                                       \
  F(dot_generator, ".generator")                                           \
  F(dot_generator_object, ".generator_object")                             \
  F(dot_iterator, ".iterator")                                             \
  F(dot_module, ".module")                                                 \
  F(dot_result, ".result")                                                 \
  F(empty, "")                                                             \
  F(eval, "eval")                                                          \
260
  F(get_template_callsite, "$getTemplateCallSite")                         \
261 262 263 264 265
  F(initialize_const_global, "initializeConstGlobal")                      \
  F(initialize_var_global, "initializeVarGlobal")                          \
  F(is_construct_call, "_IsConstructCall")                                 \
  F(is_spec_object, "_IsSpecObject")                                       \
  F(let, "let")                                                            \
266 267 268
  F(make_reference_error, "MakeReferenceError")                            \
  F(make_syntax_error, "MakeSyntaxError")                                  \
  F(make_type_error, "MakeTypeError")                                      \
269
  F(native, "native")                                                      \
270
  F(new_target, ".new.target")                                             \
271 272 273
  F(next, "next")                                                          \
  F(proto, "__proto__")                                                    \
  F(prototype, "prototype")                                                \
274 275 276 277
  F(reflect_apply, "$reflectApply")                                        \
  F(reflect_construct, "$reflectConstruct")                                \
  F(spread_arguments, "$spreadArguments")                                  \
  F(spread_iterable, "$spreadIterable")                                    \
278
  F(this, "this")                                                          \
279
  F(this_function, ".this_function")                                       \
280
  F(throw_iterator_result_not_an_object, "ThrowIteratorResultNotAnObject") \
281
  F(to_string, "$toString")                                                \
282
  F(undefined, "undefined")                                                \
283 284 285
  F(use_asm, "use asm")                                                    \
  F(use_strong, "use strong")                                              \
  F(use_strict, "use strict")                                              \
286
  F(value, "value")
287

288 289 290 291 292 293
#define OTHER_CONSTANTS(F) \
  F(true_value)            \
  F(false_value)           \
  F(null_value)            \
  F(undefined_value)       \
  F(the_hole_value)
294 295 296 297

class AstValueFactory {
 public:
  AstValueFactory(Zone* zone, uint32_t hash_seed)
298
      : string_table_(AstRawStringCompare),
299 300 301
        zone_(zone),
        isolate_(NULL),
        hash_seed_(hash_seed) {
302
#define F(name, str) name##_string_ = NULL;
303
    STRING_CONSTANTS(F)
304 305 306
#undef F
#define F(name) name##_ = NULL;
    OTHER_CONSTANTS(F)
307 308 309
#undef F
  }

310 311
  Zone* zone() const { return zone_; }

312 313 314
  const AstRawString* GetOneByteString(Vector<const uint8_t> literal) {
    return GetOneByteStringInternal(literal);
  }
315 316 317 318
  const AstRawString* GetOneByteString(const char* string) {
    return GetOneByteString(Vector<const uint8_t>(
        reinterpret_cast<const uint8_t*>(string), StrLength(string)));
  }
319 320 321
  const AstRawString* GetTwoByteString(Vector<const uint16_t> literal) {
    return GetTwoByteStringInternal(literal);
  }
322 323 324 325 326 327 328 329 330
  const AstRawString* GetString(Handle<String> literal);
  const AstConsString* NewConsString(const AstString* left,
                                     const AstString* right);

  void Internalize(Isolate* isolate);
  bool IsInternalized() {
    return isolate_ != NULL;
  }

331 332 333 334 335
#define F(name, str)                                                    \
  const AstRawString* name##_string() {                                 \
    if (name##_string_ == NULL) {                                       \
      const char* data = str;                                           \
      name##_string_ = GetOneByteString(                                \
336
          Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), \
337 338 339
                                static_cast<int>(strlen(data))));       \
    }                                                                   \
    return name##_string_;                                              \
340 341 342 343 344 345 346
  }
  STRING_CONSTANTS(F)
#undef F

  const AstValue* NewString(const AstRawString* string);
  // A JavaScript symbol (ECMA-262 edition 6).
  const AstValue* NewSymbol(const char* name);
347
  const AstValue* NewNumber(double number, bool with_dot = false);
348 349 350 351 352 353 354 355
  const AstValue* NewSmi(int number);
  const AstValue* NewBoolean(bool b);
  const AstValue* NewStringList(ZoneList<const AstRawString*>* strings);
  const AstValue* NewNull();
  const AstValue* NewUndefined();
  const AstValue* NewTheHole();

 private:
356 357 358 359
  AstRawString* GetOneByteStringInternal(Vector<const uint8_t> literal);
  AstRawString* GetTwoByteStringInternal(Vector<const uint16_t> literal);
  AstRawString* GetString(uint32_t hash, bool is_one_byte,
                          Vector<const byte> literal_bytes);
360

361 362
  static bool AstRawStringCompare(void* a, void* b);

363 364 365 366 367 368 369 370 371 372 373
  // All strings are copied here, one after another (no NULLs inbetween).
  HashMap string_table_;
  // For keeping track of all AstValues and AstRawStrings we've created (so that
  // they can be internalized later).
  List<AstValue*> values_;
  List<AstString*> strings_;
  Zone* zone_;
  Isolate* isolate_;

  uint32_t hash_seed_;

374
#define F(name, str) const AstRawString* name##_string_;
375 376
  STRING_CONSTANTS(F)
#undef F
377 378 379 380

#define F(name) AstValue* name##_;
  OTHER_CONSTANTS(F)
#undef F
381 382 383 384
};
} }  // namespace v8::internal

#undef STRING_CONSTANTS
385
#undef OTHER_CONSTANTS
386 387

#endif  // V8_AST_VALUE_FACTORY_H_