V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
api-inl.h
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_API_INL_H_
6 #define V8_API_INL_H_
7 
8 #include "src/api.h"
9 #include "src/handles-inl.h"
10 #include "src/objects-inl.h"
11 #include "src/objects/stack-frame-info.h"
12 
13 namespace v8 {
14 
15 template <typename T>
16 inline T ToCData(v8::internal::Object* obj) {
17  STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
18  if (obj == v8::internal::Smi::kZero) return nullptr;
19  return reinterpret_cast<T>(
20  v8::internal::Foreign::cast(obj)->foreign_address());
21 }
22 
23 template <>
24 inline v8::internal::Address ToCData(v8::internal::Object* obj) {
25  if (obj == v8::internal::Smi::kZero) return v8::internal::kNullAddress;
26  return v8::internal::Foreign::cast(obj)->foreign_address();
27 }
28 
29 template <typename T>
31  v8::internal::Isolate* isolate, T obj) {
32  STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
33  if (obj == nullptr) return handle(v8::internal::Smi::kZero, isolate);
34  return isolate->factory()->NewForeign(
35  reinterpret_cast<v8::internal::Address>(obj));
36 }
37 
38 template <>
41  if (obj == v8::internal::kNullAddress) {
42  return handle(v8::internal::Smi::kZero, isolate);
43  }
44  return isolate->factory()->NewForeign(obj);
45 }
46 
47 template <class From, class To>
48 inline Local<To> Utils::Convert(v8::internal::Handle<From> obj) {
49  DCHECK(obj.is_null() || (obj->IsSmi() || !obj->IsTheHole()));
50  return Local<To>(reinterpret_cast<To*>(obj.location()));
51 }
52 
53 // Implementations of ToLocal
54 
55 #define MAKE_TO_LOCAL(Name, From, To) \
56  Local<v8::To> Utils::Name(v8::internal::Handle<v8::internal::From> obj) { \
57  return Convert<v8::internal::From, v8::To>(obj); \
58  }
59 
60 #define MAKE_TO_LOCAL_TYPED_ARRAY(Type, typeName, TYPE, ctype) \
61  Local<v8::Type##Array> Utils::ToLocal##Type##Array( \
62  v8::internal::Handle<v8::internal::JSTypedArray> obj) { \
63  DCHECK(obj->type() == v8::internal::kExternal##Type##Array); \
64  return Convert<v8::internal::JSTypedArray, v8::Type##Array>(obj); \
65  }
66 
67 MAKE_TO_LOCAL(ToLocal, Context, Context)
68 MAKE_TO_LOCAL(ToLocal, Object, Value)
69 MAKE_TO_LOCAL(ToLocal, Module, Module)
70 MAKE_TO_LOCAL(ToLocal, Name, Name)
71 MAKE_TO_LOCAL(ToLocal, String, String)
72 MAKE_TO_LOCAL(ToLocal, Symbol, Symbol)
73 MAKE_TO_LOCAL(ToLocal, JSRegExp, RegExp)
74 MAKE_TO_LOCAL(ToLocal, JSReceiver, Object)
75 MAKE_TO_LOCAL(ToLocal, JSObject, Object)
76 MAKE_TO_LOCAL(ToLocal, JSFunction, Function)
77 MAKE_TO_LOCAL(ToLocal, JSArray, Array)
78 MAKE_TO_LOCAL(ToLocal, JSMap, Map)
79 MAKE_TO_LOCAL(ToLocal, JSSet, Set)
80 MAKE_TO_LOCAL(ToLocal, JSProxy, Proxy)
81 MAKE_TO_LOCAL(ToLocal, JSArrayBuffer, ArrayBuffer)
82 MAKE_TO_LOCAL(ToLocal, JSArrayBufferView, ArrayBufferView)
83 MAKE_TO_LOCAL(ToLocal, JSDataView, DataView)
84 MAKE_TO_LOCAL(ToLocal, JSTypedArray, TypedArray)
85 MAKE_TO_LOCAL(ToLocalShared, JSArrayBuffer, SharedArrayBuffer)
86 
87 TYPED_ARRAYS(MAKE_TO_LOCAL_TYPED_ARRAY)
88 
89 MAKE_TO_LOCAL(ToLocal, FunctionTemplateInfo, FunctionTemplate)
90 MAKE_TO_LOCAL(ToLocal, ObjectTemplateInfo, ObjectTemplate)
91 MAKE_TO_LOCAL(SignatureToLocal, FunctionTemplateInfo, Signature)
92 MAKE_TO_LOCAL(AccessorSignatureToLocal, FunctionTemplateInfo, AccessorSignature)
93 MAKE_TO_LOCAL(MessageToLocal, Object, Message)
94 MAKE_TO_LOCAL(PromiseToLocal, JSObject, Promise)
95 MAKE_TO_LOCAL(StackTraceToLocal, FixedArray, StackTrace)
96 MAKE_TO_LOCAL(StackFrameToLocal, StackFrameInfo, StackFrame)
97 MAKE_TO_LOCAL(NumberToLocal, Object, Number)
98 MAKE_TO_LOCAL(IntegerToLocal, Object, Integer)
99 MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32)
100 MAKE_TO_LOCAL(ToLocal, BigInt, BigInt);
101 MAKE_TO_LOCAL(ExternalToLocal, JSObject, External)
102 MAKE_TO_LOCAL(CallableToLocal, JSReceiver, Function)
103 MAKE_TO_LOCAL(ToLocalPrimitive, Object, Primitive)
104 MAKE_TO_LOCAL(ToLocal, FixedArray, PrimitiveArray)
105 MAKE_TO_LOCAL(ScriptOrModuleToLocal, Script, ScriptOrModule)
106 
107 #undef MAKE_TO_LOCAL_TYPED_ARRAY
108 #undef MAKE_TO_LOCAL
109 
110 // Implementations of OpenHandle
111 
112 #define MAKE_OPEN_HANDLE(From, To) \
113  v8::internal::Handle<v8::internal::To> Utils::OpenHandle( \
114  const v8::From* that, bool allow_empty_handle) { \
115  DCHECK(allow_empty_handle || that != nullptr); \
116  DCHECK(that == nullptr || \
117  (*reinterpret_cast<v8::internal::Object* const*>(that))->Is##To()); \
118  return v8::internal::Handle<v8::internal::To>( \
119  reinterpret_cast<v8::internal::Address*>( \
120  const_cast<v8::From*>(that))); \
121  }
122 
123 OPEN_HANDLE_LIST(MAKE_OPEN_HANDLE)
124 
125 #undef MAKE_OPEN_HANDLE
126 #undef OPEN_HANDLE_LIST
127 
128 namespace internal {
129 
130 Handle<Context> HandleScopeImplementer::MicrotaskContext() {
131  if (!microtask_context_.is_null()) {
132  return Handle<Context>(microtask_context_, isolate_);
133  }
134  return Handle<Context>::null();
135 }
136 
137 Handle<Context> HandleScopeImplementer::LastEnteredContext() {
138  if (entered_contexts_.empty()) return Handle<Context>::null();
139  return Handle<Context>(entered_contexts_.back(), isolate_);
140 }
141 
142 Handle<Context> HandleScopeImplementer::LastEnteredOrMicrotaskContext() {
143  if (MicrotaskContextIsLastEnteredContext()) return MicrotaskContext();
144  if (entered_contexts_.empty()) return Handle<Context>::null();
145  return Handle<Context>(entered_contexts_.back(), isolate_);
146 }
147 
148 } // namespace internal
149 } // namespace v8
150 
151 #endif // V8_API_INL_H_
Definition: libplatform.h:13