1
1
use heck:: ShoutySnakeCase ;
2
2
use witx:: * ;
3
3
4
- pub ( crate ) fn to_c_header ( doc : & Document , inputs_str : & str ) -> String {
4
+ pub fn to_c_header ( doc : & Document , inputs_str : & str ) -> String {
5
5
let mut ret = String :: new ( ) ;
6
6
7
7
ret. push_str ( & format ! (
@@ -27,6 +27,10 @@ pub(crate) fn to_c_header(doc: &Document, inputs_str: &str) -> String {
27
27
#error <wasi/api.h> is only supported on WASI platforms.
28
28
#endif
29
29
30
+ #ifndef __wasm32__
31
+ #error <wasi/api.h> only supports wasm32; doesn't yet support wasm64
32
+ #endif
33
+
30
34
#include <stddef.h>
31
35
#include <stdint.h>
32
36
@@ -38,6 +42,7 @@ _Static_assert(_Alignof(int32_t) == 4, "non-wasi data layout");
38
42
_Static_assert(_Alignof(uint32_t) == 4, "non-wasi data layout");
39
43
_Static_assert(_Alignof(int64_t) == 8, "non-wasi data layout");
40
44
_Static_assert(_Alignof(uint64_t) == 8, "non-wasi data layout");
45
+ _Static_assert(_Alignof(void*) == 4, "non-wasi data layout");
41
46
42
47
#ifdef __cplusplus
43
48
extern "C" {{
@@ -117,6 +122,19 @@ fn print_alias(ret: &mut String, name: &Id, dest: &TypeRef) {
117
122
) ) ;
118
123
}
119
124
ret. push_str ( "\n " ) ;
125
+
126
+ ret. push_str ( & format ! (
127
+ "_Static_assert(sizeof(__wasi_{}_t) == {}, \" witx calculated size\" );\n " ,
128
+ ident_name( name) ,
129
+ dest. mem_size_align( ) . size
130
+ ) ) ;
131
+ ret. push_str ( & format ! (
132
+ "_Static_assert(_Alignof(__wasi_{}_t) == {}, \" witx calculated align\" );\n " ,
133
+ ident_name( name) ,
134
+ dest. mem_size_align( ) . align
135
+ ) ) ;
136
+
137
+ ret. push_str ( "\n " ) ;
120
138
}
121
139
}
122
140
}
@@ -146,6 +164,19 @@ fn print_enum(ret: &mut String, name: &Id, e: &EnumDatatype) {
146
164
) ) ;
147
165
ret. push_str ( "\n " ) ;
148
166
}
167
+
168
+ ret. push_str ( & format ! (
169
+ "_Static_assert(sizeof(__wasi_{}_t) == {}, \" witx calculated size\" );\n " ,
170
+ ident_name( name) ,
171
+ e. repr. mem_size( )
172
+ ) ) ;
173
+ ret. push_str ( & format ! (
174
+ "_Static_assert(_Alignof(__wasi_{}_t) == {}, \" witx calculated align\" );\n " ,
175
+ ident_name( name) ,
176
+ e. repr. mem_align( )
177
+ ) ) ;
178
+
179
+ ret. push_str ( "\n " ) ;
149
180
}
150
181
151
182
fn print_int ( ret : & mut String , name : & Id , i : & IntDatatype ) {
@@ -213,6 +244,19 @@ fn print_flags(ret: &mut String, name: &Id, f: &FlagsDatatype) {
213
244
) ) ;
214
245
ret. push_str ( "\n " ) ;
215
246
}
247
+
248
+ ret. push_str ( & format ! (
249
+ "_Static_assert(sizeof(__wasi_{}_t) == {}, \" witx calculated size\" );\n " ,
250
+ ident_name( name) ,
251
+ f. repr. mem_size( ) ,
252
+ ) ) ;
253
+ ret. push_str ( & format ! (
254
+ "_Static_assert(_Alignof(__wasi_{}_t) == {}, \" witx calculated align\" );\n " ,
255
+ ident_name( name) ,
256
+ f. repr. mem_align( ) ,
257
+ ) ) ;
258
+
259
+ ret. push_str ( "\n " ) ;
216
260
}
217
261
218
262
fn print_struct ( ret : & mut String , name : & Id , s : & StructDatatype ) {
@@ -239,6 +283,28 @@ fn print_struct(ret: &mut String, name: &Id, s: &StructDatatype) {
239
283
240
284
ret. push_str ( & format ! ( "}} __wasi_{}_t;\n " , ident_name( name) ) ) ;
241
285
ret. push_str ( "\n " ) ;
286
+
287
+ ret. push_str ( & format ! (
288
+ "_Static_assert(sizeof(__wasi_{}_t) == {}, \" witx calculated size\" );\n " ,
289
+ ident_name( name) ,
290
+ s. mem_size( )
291
+ ) ) ;
292
+ ret. push_str ( & format ! (
293
+ "_Static_assert(_Alignof(__wasi_{}_t) == {}, \" witx calculated align\" );\n " ,
294
+ ident_name( name) ,
295
+ s. mem_align( )
296
+ ) ) ;
297
+
298
+ for layout in s. member_layout ( ) {
299
+ ret. push_str ( & format ! (
300
+ "_Static_assert(offsetof(__wasi_{}_t, {}) == {}, \" witx calculated offset\" );\n " ,
301
+ ident_name( name) ,
302
+ ident_name( & layout. member. name) ,
303
+ layout. offset
304
+ ) ) ;
305
+ }
306
+
307
+ ret. push_str ( "\n " ) ;
242
308
}
243
309
244
310
fn print_union ( ret : & mut String , name : & Id , u : & UnionDatatype ) {
@@ -262,10 +328,36 @@ fn print_union(ret: &mut String, name: &Id, u: &UnionDatatype) {
262
328
263
329
ret. push_str ( & format ! ( "}} __wasi_{}_t;\n " , ident_name( name) ) ) ;
264
330
ret. push_str ( "\n " ) ;
331
+
332
+ ret. push_str ( & format ! (
333
+ "_Static_assert(sizeof(__wasi_{}_t) == {}, \" witx calculated size\" );\n " ,
334
+ ident_name( name) ,
335
+ u. mem_size( )
336
+ ) ) ;
337
+ ret. push_str ( & format ! (
338
+ "_Static_assert(_Alignof(__wasi_{}_t) == {}, \" witx calculated align\" );\n " ,
339
+ ident_name( name) ,
340
+ u. mem_align( )
341
+ ) ) ;
342
+
343
+ ret. push_str ( "\n " ) ;
265
344
}
266
345
267
- fn print_handle ( ret : & mut String , name : & Id , _h : & HandleDatatype ) {
346
+ fn print_handle ( ret : & mut String , name : & Id , h : & HandleDatatype ) {
268
347
ret. push_str ( & format ! ( "typedef int __wasi_{}_t;" , ident_name( name) ) ) ;
348
+
349
+ ret. push_str ( & format ! (
350
+ "_Static_assert(sizeof(__wasi_{}_t) == {}, \" witx calculated size\" );\n " ,
351
+ ident_name( name) ,
352
+ h. mem_size( )
353
+ ) ) ;
354
+ ret. push_str ( & format ! (
355
+ "_Static_assert(_Alignof(__wasi_{}_t) == {}, \" witx calculated align\" );\n " ,
356
+ ident_name( name) ,
357
+ h. mem_align( )
358
+ ) ) ;
359
+
360
+ ret. push_str ( "\n " ) ;
269
361
}
270
362
271
363
fn print_module ( ret : & mut String , m : & Module ) {
0 commit comments