natives.h 2.94 KB
Newer Older
1
// Copyright 2011 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 6
#ifndef V8_SNAPSHOT_NATIVES_H_
#define V8_SNAPSHOT_NATIVES_H_
7

8
#include "include/v8.h"
9
#include "src/objects.h"
10 11 12 13
#include "src/vector.h"

namespace v8 { class StartupData; }  // Forward declaration.

14 15
namespace v8 {
namespace internal {
16

17 18 19 20 21 22 23
enum NativeType {
  CORE,
  EXTRAS,
  EXPERIMENTAL_EXTRAS,
  D8,
  TEST
};
24

25 26
// Extra handling for V8_EXPORT_PRIVATE in combination with USING_V8_SHARED
// since definition of methods of classes marked as dllimport is not allowed.
27
template <NativeType type>
28
#ifdef USING_V8_SHARED
29
class NativesCollection {
30 31 32 33
#else
class V8_EXPORT_PRIVATE NativesCollection {
#endif  // USING_V8_SHARED

34
 public:
35 36
  // The following methods are implemented in js2c-generated code:

37 38
  // Number of built-in scripts.
  static int GetBuiltinsCount();
39 40
  // Number of debugger implementation scripts.
  static int GetDebuggerCount();
41

42 43 44 45
  // These are used to access built-in scripts.  The debugger implementation
  // scripts have an index in the interval [0, GetDebuggerCount()).  The
  // non-debugger scripts have an index in the interval [GetDebuggerCount(),
  // GetNativesCount()).
46
  static int GetIndex(const char* name);
47
  static Vector<const char> GetScriptSource(int index);
48
  static Vector<const char> GetScriptName(int index);
49
  static Vector<const char> GetScriptsSource();
50 51
};

52
typedef NativesCollection<CORE> Natives;
53
typedef NativesCollection<EXTRAS> ExtraNatives;
54
typedef NativesCollection<EXPERIMENTAL_EXTRAS> ExperimentalExtraNatives;
55

56

57 58 59
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
// Used for reading the natives at runtime. Implementation in natives-empty.cc
void SetNativesFromFile(StartupData* natives_blob);
60
void ReadNatives();
61
void DisposeNatives();
62 63
#endif

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
class NativesExternalStringResource final
    : public v8::String::ExternalOneByteStringResource {
 public:
  NativesExternalStringResource(NativeType type, int index);

  const char* data() const override { return data_; }
  size_t length() const override { return length_; }

  v8::String::ExternalOneByteStringResource* EncodeForSerialization() const {
    DCHECK(type_ == CORE || type_ == EXTRAS);
    intptr_t val = (index_ << 1) | ((type_ == CORE) ? 0 : 1);
    val = val << kPointerSizeLog2;  // Pointer align.
    return reinterpret_cast<v8::String::ExternalOneByteStringResource*>(val);
  }

  // Decode from serialization.
  static NativesExternalStringResource* DecodeForDeserialization(
      const v8::String::ExternalOneByteStringResource* encoded) {
    intptr_t val = reinterpret_cast<intptr_t>(encoded) >> kPointerSizeLog2;
    NativeType type = (val & 1) ? EXTRAS : CORE;
    int index = static_cast<int>(val >> 1);
    return new NativesExternalStringResource(type, index);
  }

 private:
  const char* data_;
  size_t length_;
  NativeType type_;
  int index_;
};

95 96
}  // namespace internal
}  // namespace v8
97

98
#endif  // V8_SNAPSHOT_NATIVES_H_