@@ -64,33 +64,16 @@ Pairs toPairs(std::string_view buffer) {
64
64
if (buffer.size () < sizeof (uint32_t )) {
65
65
return {};
66
66
}
67
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
68
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
69
- auto size = __builtin_bswap32 (*reinterpret_cast <const uint32_t *>(b));
70
- #else
71
- auto size = *reinterpret_cast <const uint32_t *>(b);
72
- #endif
67
+ auto size = le32toh (*reinterpret_cast <const uint32_t *>(b));
73
68
b += sizeof (uint32_t );
74
69
if (sizeof (uint32_t ) + size * 2 * sizeof (uint32_t ) > buffer.size ()) {
75
70
return {};
76
71
}
77
72
result.resize (size);
78
73
for (uint32_t i = 0 ; i < size; i++) {
79
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
80
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
81
- result[i].first =
82
- std::string_view (nullptr , __builtin_bswap32 (*reinterpret_cast <const uint32_t *>(b)));
83
- #else
84
- result[i].first = std::string_view (nullptr , *reinterpret_cast <const uint32_t *>(b));
85
- #endif
74
+ result[i].first = std::string_view (nullptr , le32toh (*reinterpret_cast <const uint32_t *>(b)));
86
75
b += sizeof (uint32_t );
87
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
88
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
89
- result[i].second =
90
- std::string_view (nullptr , __builtin_bswap32 (*reinterpret_cast <const uint32_t *>(b)));
91
- #else
92
- result[i].second = std::string_view (nullptr , *reinterpret_cast <const uint32_t *>(b));
93
- #endif
76
+ result[i].second = std::string_view (nullptr , le32toh (*reinterpret_cast <const uint32_t *>(b)));
94
77
b += sizeof (uint32_t );
95
78
}
96
79
for (auto &p : result) {
@@ -111,20 +94,10 @@ bool getPairs(ContextBase *context, const Pairs &result, uint64_t ptr_ptr, uint6
111
94
uint64_t ptr;
112
95
char *buffer = static_cast <char *>(context->wasm ()->allocMemory (size, &ptr));
113
96
marshalPairs (result, buffer);
114
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
115
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
116
- if (!context->wasmVm ()->setWord (ptr_ptr, Word (__builtin_bswap32 (ptr)))) {
117
- #else
118
97
if (!context->wasmVm ()->setWord (ptr_ptr, Word (ptr))) {
119
- #endif
120
98
return false ;
121
99
}
122
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
123
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
124
- if (!context->wasmVm ()->setWord (size_ptr, Word (__builtin_bswap32 (size)))) {
125
- #else
126
100
if (!context->wasmVm ()->setWord (size_ptr, Word (size))) {
127
- #endif
128
101
return false ;
129
102
}
130
103
return true ;
@@ -284,21 +257,10 @@ Word call_foreign_function(Word function_name, Word function_name_size, Word arg
284
257
result_size = s;
285
258
return result;
286
259
});
287
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
288
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
289
- if (results && !context->wasmVm ()->setWord (results, Word (__builtin_bswap32 (address)))) {
290
- #else
291
260
if (results && !context->wasmVm ()->setWord (results, Word (address))) {
292
- #endif
293
261
return WasmResult::InvalidMemoryAccess;
294
262
}
295
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
296
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
297
- if (results_size &&
298
- !context->wasmVm ()->setWord (results_size, Word (__builtin_bswap32 (result_size)))) {
299
- #else
300
263
if (results_size && !context->wasmVm ()->setWord (results_size, Word (result_size))) {
301
- #endif
302
264
return WasmResult::InvalidMemoryAccess;
303
265
}
304
266
if (!results) {
@@ -500,12 +462,7 @@ Word get_header_map_size(Word type, Word result_ptr) {
500
462
if (result != WasmResult::Ok) {
501
463
return result;
502
464
}
503
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
504
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
505
- if (!context->wasmVm ()->setWord (result_ptr, Word (__builtin_bswap32 (size)))) {
506
- #else
507
465
if (!context->wasmVm ()->setWord (result_ptr, Word (size))) {
508
- #endif
509
466
return WasmResult::InvalidMemoryAccess;
510
467
}
511
468
return WasmResult::Ok;
@@ -546,12 +503,7 @@ Word get_buffer_status(Word type, Word length_ptr, Word flags_ptr) {
546
503
}
547
504
auto length = buffer->size ();
548
505
uint32_t flags = 0 ;
549
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
550
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
551
- if (!context->wasmVm ()->setWord (length_ptr, Word (__builtin_bswap32 (length)))) {
552
- #else
553
506
if (!context->wasmVm ()->setWord (length_ptr, Word (length))) {
554
- #endif
555
507
return WasmResult::InvalidMemoryAccess;
556
508
}
557
509
if (!context->wasm ()->setDatatype (flags_ptr, flags)) {
@@ -760,13 +712,8 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
760
712
}
761
713
const uint32_t *iovec = reinterpret_cast <const uint32_t *>(memslice.value ().data ());
762
714
if (iovec [1 ] /* buf_len */ ) {
763
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
764
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
765
- memslice = context->wasmVm ()->getMemory (__builtin_bswap32 (iovec [0 ]) /* buf */ ,
766
- __builtin_bswap32 (iovec [1 ]) /* buf_len */ );
767
- #else
768
- memslice = context->wasmVm ()->getMemory (iovec [0 ] /* buf */ , iovec [1 ] /* buf_len */ );
769
- #endif
715
+ memslice = context->wasmVm ()->getMemory (le32toh (iovec [0 ]) /* buf */ ,
716
+ le32toh (iovec [1 ]) /* buf_len */ );
770
717
if (!memslice) {
771
718
return 21 ; // __WASI_EFAULT
772
719
}
@@ -798,12 +745,7 @@ Word wasi_unstable_fd_write(Word fd, Word iovs, Word iovs_len, Word nwritten_ptr
798
745
if (result != 0 ) { // __WASI_ESUCCESS
799
746
return result;
800
747
}
801
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
802
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
803
- if (!context->wasmVm ()->setWord (nwritten_ptr, Word (__builtin_bswap32 (nwritten)))) {
804
- #else
805
748
if (!context->wasmVm ()->setWord (nwritten_ptr, Word (nwritten))) {
806
- #endif
807
749
return 21 ; // __WASI_EFAULT
808
750
}
809
751
return 0 ; // __WASI_ESUCCESS
@@ -857,12 +799,7 @@ Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
857
799
auto word_size = context->wasmVm ()->getWordSize ();
858
800
auto &envs = context->wasm ()->envs ();
859
801
for (auto e : envs) {
860
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
861
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
862
- if (!context->wasmVm ()->setWord (environ_array_ptr, __builtin_bswap32 (environ_buf))) {
863
- #else
864
802
if (!context->wasmVm ()->setWord (environ_array_ptr, environ_buf)) {
865
- #endif
866
803
return 21 ; // __WASI_EFAULT
867
804
}
868
805
@@ -887,12 +824,7 @@ Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
887
824
Word wasi_unstable_environ_sizes_get (Word count_ptr, Word buf_size_ptr) {
888
825
auto context = contextOrEffectiveContext ();
889
826
auto &envs = context->wasm ()->envs ();
890
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
891
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
892
- if (!context->wasmVm ()->setWord (count_ptr, Word (__builtin_bswap32 (envs.size ())))) {
893
- #else
894
827
if (!context->wasmVm ()->setWord (count_ptr, Word (envs.size ()))) {
895
- #endif
896
828
return 21 ; // __WASI_EFAULT
897
829
}
898
830
@@ -901,12 +833,7 @@ Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr) {
901
833
// len(key) + len(value) + 1('=') + 1(null terminator)
902
834
size += e.first .size () + e.second .size () + 2 ;
903
835
}
904
- #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
905
- __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
906
- if (!context->wasmVm ()->setWord (buf_size_ptr, Word (__builtin_bswap32 (size)))) {
907
- #else
908
836
if (!context->wasmVm ()->setWord (buf_size_ptr, Word (size))) {
909
- #endif
910
837
return 21 ; // __WASI_EFAULT
911
838
}
912
839
return 0 ; // __WASI_ESUCCESS
0 commit comments