natives.h 2.42 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
enum NativeType {
  EXTRAS,
  TEST
};
21

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

31
 public:
32 33
  // The following methods are implemented in js2c-generated code:

34 35 36
  // Number of built-in scripts.
  static int GetBuiltinsCount();
  static int GetIndex(const char* name);
37
  static Vector<const char> GetScriptSource(int index);
38
  static Vector<const char> GetScriptName(int index);
39
  static Vector<const char> GetScriptsSource();
40 41
};

42
typedef NativesCollection<EXTRAS> ExtraNatives;
43

44

45 46 47
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
// Used for reading the natives at runtime. Implementation in natives-empty.cc
void SetNativesFromFile(StartupData* natives_blob);
48
void ReadNatives();
49
void DisposeNatives();
50 51
#endif

52 53 54 55 56 57 58 59 60
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 {
61 62
    DCHECK(type_ == EXTRAS);
    intptr_t val = (index_ << 1) | 1;
63
    val = val << kSystemPointerSizeLog2;  // Pointer align.
64 65 66 67 68 69
    return reinterpret_cast<v8::String::ExternalOneByteStringResource*>(val);
  }

  // Decode from serialization.
  static NativesExternalStringResource* DecodeForDeserialization(
      const v8::String::ExternalOneByteStringResource* encoded) {
70 71
    intptr_t val =
        reinterpret_cast<intptr_t>(encoded) >> kSystemPointerSizeLog2;
72
    DCHECK(val & 1);
73
    int index = static_cast<int>(val >> 1);
74
    return new NativesExternalStringResource(EXTRAS, index);
75 76 77 78 79 80 81 82 83
  }

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

84 85
}  // namespace internal
}  // namespace v8
86

87
#endif  // V8_SNAPSHOT_NATIVES_H_