Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
BoundarySpan.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
20#pragma once
21
22#include "../core/Types.hpp"
23
24#ifdef __EMSCRIPTEN__
25#include <emscripten/heap.h>
26#endif
27
28#include <cstdint>
29#include <cstdio>
30
31namespace esengine {
32
33namespace detail {
34#ifndef __EMSCRIPTEN__
35// The bounded region a native host marshals JS buffers through (its heap arena).
36// Zero until the host registers one, and then this gate is as strict on a device
37// as it is in the sandbox.
38inline uintptr_t g_boundaryBase = 0;
40#endif
41
44inline bool boundaryContains(uintptr_t ptr, u64 bytes, uintptr_t* base, u64* size) {
45#ifdef __EMSCRIPTEN__
46 // wasm: linear memory, addressed from 0.
47 *base = 0;
48 *size = static_cast<u64>(emscripten_get_heap_size());
49#else
50 *base = g_boundaryBase;
51 *size = static_cast<u64>(g_boundarySize);
52 if (*size == 0) {
53 // No host region registered: a pointer here came from C++, not from JS
54 // (or the host predates the arena). Null/overflow checks still applied.
55 return true;
56 }
57#endif
58 if (static_cast<u64>(ptr) < static_cast<u64>(*base)) return false;
59 const u64 offset = static_cast<u64>(ptr) - static_cast<u64>(*base);
60 return offset <= *size && bytes <= *size - offset;
61}
62} // namespace detail
63
64#ifndef __EMSCRIPTEN__
70inline void setBoundaryRegion(uintptr_t base, usize size) {
73}
74#endif
75
84template <typename T>
85inline const T* boundarySpan(uintptr_t ptr, u64 count, const char* what) {
86 if (ptr == 0) {
87 std::fprintf(stderr, "[boundary] %s: null buffer pointer; rejected\n", what);
88 return nullptr;
89 }
90 const u64 bytes = count * static_cast<u64>(sizeof(T));
91 if (count != 0 && bytes / count != sizeof(T)) {
92 std::fprintf(stderr, "[boundary] %s: byte size overflows (count %llu); rejected\n",
93 what, static_cast<unsigned long long>(count));
94 return nullptr;
95 }
96 uintptr_t base = 0;
97 u64 size = 0;
98 if (!detail::boundaryContains(ptr, bytes, &base, &size)) {
99 std::fprintf(stderr,
100 "[boundary] %s: span [%llu + %llu bytes] leaves the heap "
101 "(%llu bytes at %llu); rejected\n",
102 what, static_cast<unsigned long long>(ptr),
103 static_cast<unsigned long long>(bytes),
104 static_cast<unsigned long long>(size),
105 static_cast<unsigned long long>(base));
106 return nullptr;
107 }
108 return reinterpret_cast<const T*>(ptr);
109}
110
112template <typename T>
113inline T* boundarySpanMut(uintptr_t ptr, u64 count, const char* what) {
114 return const_cast<T*>(boundarySpan<T>(ptr, count, what));
115}
116
117} // namespace esengine
Core type definitions and utilities for ESEngine.
uintptr_t g_boundaryBase
bool boundaryContains(uintptr_t ptr, u64 bytes, uintptr_t *base, u64 *size)
const T * boundarySpan(uintptr_t ptr, u64 count, const char *what)
Validates a JS-supplied pointer to count elements of T and returns it typed.
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
T * boundarySpanMut(uintptr_t ptr, u64 count, const char *what)
Mutable variant of boundarySpan, for entry points that WRITE into a JS buffer.
void setBoundaryRegion(uintptr_t base, usize size)
Declare the region a native host lets JS address (its heap arena).