-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwasm-memory.cpp
More file actions
135 lines (119 loc) · 4.37 KB
/
wasm-memory.cpp
File metadata and controls
135 lines (119 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "wasm.h"
#include "wasm-memory.h"
#include "store.h"
#ifdef GDNATIVE
#define INTERFACE_DEFINE interface = { { 3, 1 }, this, &_get_data, &_get_partial_data, &_put_data, &_put_partial_data, &_get_available_bytes, NULL }
#define INTERFACE_INIT net_api->godot_net_bind_stream_peer(_owner, &interface)
namespace {
godot_error _get_data(void* user, uint8_t* buffer, int bytes) { return ((godot::WasmMemory*)user)->get_data(buffer, bytes); }
godot_error _get_partial_data(void* user, uint8_t* buffer, int bytes, int* received) { return ((godot::WasmMemory*)user)->get_partial_data(buffer, bytes, *received); }
godot_error _put_data(void* user, const uint8_t* buffer, int bytes) { return ((godot::WasmMemory*)user)->put_data(buffer, bytes); }
godot_error _put_partial_data(void* user, const uint8_t* buffer, int bytes, int* sent) { return ((godot::WasmMemory*)user)->put_partial_data(buffer, bytes, *sent); }
int _get_available_bytes(const void* user) { return ((godot::WasmMemory*)user)->get_available_bytes(); }
}
#else
#define INTERFACE_DEFINE
#define INTERFACE_INIT
#endif
namespace godot {
void WasmMemory::REGISTRATION_METHOD() {
#ifdef GDNATIVE
register_method("inspect", &WasmMemory::inspect);
register_method("grow", &WasmMemory::grow);
register_method("seek", &WasmMemory::seek);
register_method("get_position", &WasmMemory::get_position);
#else
ClassDB::bind_method(D_METHOD("inspect"), &WasmMemory::inspect);
ClassDB::bind_method(D_METHOD("grow", "pages"), &WasmMemory::grow);
ClassDB::bind_method(D_METHOD("seek", "p_pos"), &WasmMemory::seek);
ClassDB::bind_method(D_METHOD("get_position"), &WasmMemory::get_position);
#endif
}
WasmMemory::WasmMemory() {
INTERFACE_DEFINE;
memory = NULL;
pointer = 0;
}
WasmMemory::~WasmMemory() {
set_memory(NULL);
}
void WasmMemory::_init() {
INTERFACE_INIT;
}
void WasmMemory::set_memory(const wasm_memory_t* memory_new) {
if (memory != NULL) wasm_memory_delete(memory);
memory = (wasm_memory_t*)memory_new;
}
wasm_memory_t* WasmMemory::get_memory() const {
return memory;
}
Dictionary WasmMemory::inspect() const {
if (memory == NULL) return Dictionary();
auto limits = wasm_memorytype_limits(wasm_memory_type(memory));
Dictionary dict;
dict["min"] = limits->min * PAGE_SIZE;
dict["max"] = limits->max * PAGE_SIZE;
dict["current"] = wasm_memory_size(memory) * PAGE_SIZE;
return dict;
}
godot_error WasmMemory::grow(uint32_t pages) {
if (!memory) { // Create new memory
const wasm_limits_t limits = { pages, wasm_limits_max_default };
memory = wasm_memory_new(STORE, wasm_memorytype_new(&limits));
return memory ? OK : FAILED;
}
FAIL_IF(memory == NULL, "Invalid memory", ERR_INVALID_DATA);
return wasm_memory_grow(memory, pages) ? OK : FAILED;
}
Ref<WasmMemory> WasmMemory::seek(int p_pos) {
Ref<WasmMemory> ref = Ref<WasmMemory>(this);
FAIL_IF(p_pos < 0, "Invalid memory position", ref);
pointer = p_pos;
return ref;
}
uint32_t WasmMemory::get_position() const {
return pointer;
}
godot_error WasmMemory::INTERFACE_GET_DATA {
FAIL_IF(memory == NULL, "Invalid memory", ERR_INVALID_DATA);
byte_t* data = wasm_memory_data(memory);
FAIL_IF(data == NULL, "Invalid memory state", ERR_INVALID_DATA);
memcpy(buffer, data + pointer, bytes);
pointer += bytes;
#ifndef GODOT_MODULE
*received = bytes;
#endif
return OK;
}
godot_error WasmMemory::INTERFACE_GET_PARTIAL_DATA {
#ifdef GODOT_MODULE
received = bytes;
return get_data(buffer, bytes);
#else
return _get_data(buffer, bytes, received);
#endif
}
godot_error WasmMemory::INTERFACE_PUT_DATA {
FAIL_IF(memory == NULL, "Invalid memory", ERR_INVALID_DATA);
if (bytes <= 0) return OK;
byte_t* data = wasm_memory_data(memory);
FAIL_IF(data == NULL, "Invalid memory state", ERR_INVALID_DATA);
memcpy(data + pointer, buffer, bytes);
pointer += bytes;
#ifndef GODOT_MODULE
*sent = bytes;
#endif
return OK;
}
godot_error WasmMemory::INTERFACE_PUT_PARTIAL_DATA {
#ifdef GODOT_MODULE
sent = bytes;
return put_data(buffer, bytes);
#else
return _put_data(buffer, bytes, sent);
#endif
}
int32_t WasmMemory::INTERFACE_GET_AVAILABLE_BYTES {
return 0; // Not relevant
}
}