preparse-data.cc 2.82 KB
Newer Older
1
// Copyright 2010 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/base/logging.h"
6
#include "src/compiler.h"
7 8
#include "src/globals.h"
#include "src/hashmap.h"
9 10
#include "src/preparse-data.h"
#include "src/preparse-data-format.h"
11

12 13 14 15
namespace v8 {
namespace internal {


16
CompleteParserRecorder::CompleteParserRecorder()
17
    : function_store_(0) {
18 19 20 21 22 23 24
  preamble_[PreparseDataConstants::kMagicOffset] =
      PreparseDataConstants::kMagicNumber;
  preamble_[PreparseDataConstants::kVersionOffset] =
      PreparseDataConstants::kCurrentVersion;
  preamble_[PreparseDataConstants::kHasErrorOffset] = false;
  preamble_[PreparseDataConstants::kFunctionsSizeOffset] = 0;
  preamble_[PreparseDataConstants::kSizeOffset] = 0;
25
  DCHECK_EQ(5, PreparseDataConstants::kHeaderSize);
26 27 28 29 30 31
#ifdef DEBUG
  prev_start_ = -1;
#endif
}


32
void CompleteParserRecorder::LogMessage(int start_pos,
33 34 35 36
                                        int end_pos,
                                        const char* message,
                                        const char* arg_opt,
                                        bool is_reference_error) {
37
  if (HasError()) return;
38 39 40 41 42 43 44 45
  preamble_[PreparseDataConstants::kHasErrorOffset] = true;
  function_store_.Reset();
  STATIC_ASSERT(PreparseDataConstants::kMessageStartPos == 0);
  function_store_.Add(start_pos);
  STATIC_ASSERT(PreparseDataConstants::kMessageEndPos == 1);
  function_store_.Add(end_pos);
  STATIC_ASSERT(PreparseDataConstants::kMessageArgCountPos == 2);
  function_store_.Add((arg_opt == NULL) ? 0 : 1);
46 47 48
  STATIC_ASSERT(PreparseDataConstants::kIsReferenceErrorPos == 3);
  function_store_.Add(is_reference_error ? 1 : 0);
  STATIC_ASSERT(PreparseDataConstants::kMessageTextPos == 4);
49
  WriteString(CStrVector(message));
50
  if (arg_opt != NULL) WriteString(CStrVector(arg_opt));
51 52 53
}


54
void CompleteParserRecorder::WriteString(Vector<const char> str) {
55 56 57 58 59 60
  function_store_.Add(str.length());
  for (int i = 0; i < str.length(); i++) {
    function_store_.Add(str[i]);
  }
}

61

62
ScriptData* CompleteParserRecorder::GetScriptData() {
63
  int function_size = function_store_.size();
64
  int total_size = PreparseDataConstants::kHeaderSize + function_size;
65
  unsigned* data = NewArray<unsigned>(total_size);
66
  preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size;
67
  MemCopy(data, preamble_, sizeof(preamble_));
68
  if (function_size > 0) {
69 70
    function_store_.WriteTo(Vector<unsigned>(
        data + PreparseDataConstants::kHeaderSize, function_size));
71
  }
72
  DCHECK(IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment));
73 74 75 76
  ScriptData* result = new ScriptData(reinterpret_cast<byte*>(data),
                                      total_size * sizeof(unsigned));
  result->AcquireDataOwnership();
  return result;
77 78 79 80
}


} }  // namespace v8::internal.