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