File
Blob: src/rust/jsg/ffi-inl.h
| 1 | // Copyright (c) 2026 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | // This header contains template implementations that require complete type definitions. |
| 8 | // It should be included after workerd/rust/jsg/v8.rs.h which defines the complete types. |
| 9 | |
| 10 | #include <workerd/rust/jsg/ffi.h> |
| 11 | #include <workerd/rust/jsg/v8.rs.h> |
| 12 | |
| 13 | namespace workerd::rust::jsg { |
| 14 | |
| 15 | // Local<T> |
| 16 | static_assert(sizeof(v8::Local<v8::Value>) == 8, "Size should match"); |
| 17 | static_assert(alignof(v8::Local<v8::Value>) == 8, "Alignment should match"); |
| 18 | |
| 19 | template <typename T> |
| 20 | inline Local to_ffi(v8::Local<T>&& value) { |
| 21 | size_t result; |
| 22 | auto ptr_void = reinterpret_cast<void*>(&result); |
| 23 | new (ptr_void) v8::Local<T>(kj::mv(value)); |
| 24 | return Local{result}; |
| 25 | } |
| 26 | |
| 27 | template <typename T> |
| 28 | inline v8::Local<T> local_from_ffi(Local&& value) { |
| 29 | auto ptr_void = reinterpret_cast<void*>(&value.ptr); |
| 30 | return *reinterpret_cast<v8::Local<T>*>(ptr_void); |
| 31 | } |
| 32 | |
| 33 | template <typename T> |
| 34 | inline const v8::Local<T>& local_as_ref_from_ffi(const Local& value) { |
| 35 | auto ptr_void = reinterpret_cast<const void*>(&value.ptr); |
| 36 | return *reinterpret_cast<const v8::Local<T>*>(ptr_void); |
| 37 | } |
| 38 | |
| 39 | // MaybeLocal<T> |
| 40 | // v8::MaybeLocal<T> is exactly one Local<T> field at offset 0, so it has the same |
| 41 | // size and layout as ffi::Local (one pointer-sized word, zero means empty). |
| 42 | static_assert(sizeof(v8::MaybeLocal<v8::Value>) == sizeof(MaybeLocal), "Size should match"); |
| 43 | static_assert(alignof(v8::MaybeLocal<v8::Value>) == alignof(MaybeLocal), "Alignment should match"); |
| 44 | |
| 45 | template <typename T> |
| 46 | inline MaybeLocal maybe_local_to_ffi(v8::MaybeLocal<T> value) { |
| 47 | size_t result; |
| 48 | auto ptr_void = reinterpret_cast<void*>(&result); |
| 49 | new (ptr_void) v8::MaybeLocal<T>(value); |
| 50 | return MaybeLocal{result}; |
| 51 | } |
| 52 | |
| 53 | // Global<T> |
| 54 | // |
| 55 | // ffi::Global stores only the strong v8::Global<v8::Value> in `ptr`. |
| 56 | // The traced v8::TracedReference<v8::Data> slot lives in Global<T>::traced_ptr |
| 57 | // on the Rust side (as UnsafeCell<usize>), passed by raw pointer to |
| 58 | // wrappable_visit_global and wrappable_global_reset. |
| 59 | static_assert(sizeof(v8::Global<v8::Value>) == sizeof(size_t), "Global must be pointer-sized"); |
| 60 | static_assert(sizeof(v8::TracedReference<v8::Data>) == sizeof(size_t), |
| 61 | "TracedReference must be pointer-sized"); |
| 62 | static_assert(sizeof(Global) == sizeof(size_t), "ffi::Global must hold exactly one pointer slot"); |
| 63 | static_assert(alignof(v8::Global<v8::Value>) == alignof(Global), "Alignment should match"); |
| 64 | |
| 65 | template <typename T> |
| 66 | inline Global to_ffi(v8::Global<T>&& value) { |
| 67 | size_t strong_slot; |
| 68 | auto ptr_void = reinterpret_cast<void*>(&strong_slot); |
| 69 | new (ptr_void) v8::Global<T>(kj::mv(value)); |
| 70 | return Global{strong_slot}; |
| 71 | } |
| 72 | |
| 73 | template <typename T> |
| 74 | inline v8::Global<T> global_from_ffi(Global&& value) { |
| 75 | auto ptr_void = reinterpret_cast<void*>(&value.ptr); |
| 76 | return kj::mv(*reinterpret_cast<v8::Global<T>*>(ptr_void)); |
| 77 | } |
| 78 | |
| 79 | template <typename T> |
| 80 | inline const v8::Global<T>& global_as_ref_from_ffi(const Global& value) { |
| 81 | auto ptr_void = reinterpret_cast<const void*>(&value.ptr); |
| 82 | return *reinterpret_cast<const v8::Global<T>*>(ptr_void); |
| 83 | } |
| 84 | |
| 85 | template <typename T> |
| 86 | inline v8::Global<T>* global_as_ref_from_ffi(Global& value) { |
| 87 | auto ptr_void = reinterpret_cast<void*>(&value.ptr); |
| 88 | return reinterpret_cast<v8::Global<T>*>(ptr_void); |
| 89 | } |
| 90 | |
| 91 | // TracedReference |
| 92 | static_assert(sizeof(v8::TracedReference<v8::Data>) == sizeof(TracedReference), |
| 93 | "TracedReference size must match"); |
| 94 | static_assert(alignof(v8::TracedReference<v8::Data>) == alignof(TracedReference), |
| 95 | "TracedReference alignment must match"); |
| 96 | |
| 97 | inline v8::TracedReference<v8::Data>& traced_ref_from_ffi(TracedReference& value) { |
| 98 | return *reinterpret_cast<v8::TracedReference<v8::Data>*>(&value); |
| 99 | } |
| 100 | |
| 101 | // GcVisitor - wraps a pointer to jsg::GcVisitor |
| 102 | static_assert(sizeof(::workerd::jsg::GcVisitor*) == sizeof(GcVisitor), "Size should match"); |
| 103 | static_assert(alignof(::workerd::jsg::GcVisitor*) == alignof(GcVisitor), "Alignment should match"); |
| 104 | |
| 105 | inline GcVisitor to_ffi(::workerd::jsg::GcVisitor* visitor) { |
| 106 | return GcVisitor{reinterpret_cast<size_t>(visitor)}; |
| 107 | } |
| 108 | |
| 109 | inline ::workerd::jsg::GcVisitor* gc_visitor_from_ffi(GcVisitor* value) { |
| 110 | return reinterpret_cast<::workerd::jsg::GcVisitor*>(value->ptr); |
| 111 | } |
| 112 | |
| 113 | } // namespace workerd::rust::jsg |