/src/WasmEdge/include/runtime/instance/data.h
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | //===-- wasmedge/runtime/instance/data.h - Data Instance definition -------===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the data instance definition in store manager. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "common/span.h" |
17 | | #include "common/types.h" |
18 | | |
19 | | #include <vector> |
20 | | |
21 | | namespace WasmEdge { |
22 | | namespace Runtime { |
23 | | namespace Instance { |
24 | | |
25 | | class DataInstance { |
26 | | public: |
27 | | DataInstance() = delete; |
28 | | DataInstance(const uint32_t Offset, Span<const Byte> Init) noexcept |
29 | 0 | : Off(Offset), Data(Init.begin(), Init.end()) {} |
30 | | |
31 | | /// Get offset in data instance. |
32 | 0 | uint32_t getOffset() const noexcept { return Off; } |
33 | | |
34 | | /// Get data in data instance. |
35 | 0 | Span<const Byte> getData() const noexcept { return Data; } |
36 | | |
37 | | /// Load bytes to value. |
38 | 0 | ValVariant loadValue(uint32_t Offset, uint32_t N) const noexcept { |
39 | 0 | assuming(N <= 16); |
40 | | // Check the data boundary. |
41 | 0 | if (unlikely(static_cast<uint64_t>(Offset) + static_cast<uint64_t>(N) > |
42 | 0 | Data.size())) { |
43 | 0 | return 0; |
44 | 0 | } |
45 | | // Load the data to the value. |
46 | 0 | uint128_t Value; |
47 | 0 | std::memcpy(&Value, &Data[Offset], N); |
48 | 0 | return Value; |
49 | 0 | } |
50 | | |
51 | | /// Clear data in data instance. |
52 | 0 | void clear() { Data.clear(); } |
53 | | |
54 | | private: |
55 | | /// \name Data of data instance. |
56 | | /// @{ |
57 | | const uint32_t Off; |
58 | | std::vector<Byte> Data; |
59 | | /// @} |
60 | | }; |
61 | | |
62 | | } // namespace Instance |
63 | | } // namespace Runtime |
64 | | } // namespace WasmEdge |