@@ -59,6 +59,32 @@ A single-byte unsigned integer indicating a [value type](AstSemantics.md#types).
59
59
* ` 3 ` indicating type ` f32 `
60
60
* ` 4 ` indicating type ` f64 `
61
61
62
+ ### definition_kind
63
+ A single-byte unsigned integer indicating the kind of definition being imported or defined:
64
+ * ` 0 ` indicating a ` Function ` [ import] ( Modules.md#imports ) or [ definition] ( Modules.md#function-and-code-sections )
65
+ * ` 1 ` indicating a ` Table ` [ import] ( Modules.md#imports ) or [ definition] ( Modules.md#table-section )
66
+ * ` 2 ` indicating a ` Memory ` [ import] ( Modules.md#imports ) or [ definition] ( Modules.md#linear-memory-section )
67
+ * ` 3 ` indicating a ` Global ` [ import] ( Modules.md#imports ) or [ definition] ( Modules.md#global-section )
68
+
69
+ ### resizable_definition
70
+ A packed tuple that describes the import or definition of a resizable
71
+ [ table] ( AstSemantics.md#table ) or [ memory] ( AstSemantics.md#resizing ) :
72
+
73
+ | Field | Type | Description |
74
+ | ----- | ----- | ----- |
75
+ | flags | ` varuint32 ` | described below |
76
+ | initial | ` varuint32 ` | initial length (in units of table elements or wasm pages) |
77
+ | maximum | ` varuint32 ` ? | only present if specified by ` flags ` |
78
+
79
+ The ` varuint32 ` "flags" field assigns the following meaning to the bits:
80
+ * ` 0x1 ` : this is a * default* [ table] ( AstSemantics.md#table ) /[ memory] ( AstSemantics.md#linear-memory )
81
+ (in the MVP, * every* memory/table import/definition must be flagged as default)
82
+ * ` 0x2 ` : indicates that the memory/table has a specified maximum
83
+
84
+ ### init_expr
85
+ The encoding of an [ initializer expression] ( Modules.md#initializer-expression )
86
+ is simply the encoding of the operator (` *.const ` or ` get_global ` ) followed by
87
+ its immediate (constant value or global index).
62
88
63
89
# Definitions
64
90
@@ -110,6 +136,7 @@ The content of each section is encoded in its `payload_str`.
110
136
* [ Function] ( #function-section ) section
111
137
* [ Table] ( #table-section ) section
112
138
* [ Memory] ( #memory-section ) section
139
+ * [ Global] ( #global-section ) section
113
140
* [ Export] ( #export-section ) section
114
141
* [ Start] ( #start-section ) section
115
142
* [ Code] ( #code-section ) section
@@ -127,13 +154,13 @@ ID: `type`
127
154
The type section declares all function signatures that will be used in the module.
128
155
129
156
| Field | Type | Description |
130
- | ----- | ----- | ----- |
157
+ | ----- | ---- | ------ ----- |
131
158
| count | ` varuint32 ` | count of type entries to follow |
132
159
| entries | ` type_entry* ` | repeated type entries as described below |
133
160
134
161
#### Type entry
135
162
| Field | Type | Description |
136
- | ----- | ----- | ----- |
163
+ | ----- | ---- | ------ ----- |
137
164
| form | ` varuint7 ` | ` 0x40 ` , indicating a function type |
138
165
| param_count | ` varuint32 ` | the number of parameters to the function |
139
166
| param_types | ` value_type* ` | the parameter types of the function |
@@ -149,18 +176,45 @@ ID: `import`
149
176
The import section declares all imports that will be used in the module.
150
177
151
178
| Field | Type | Description |
152
- | ----- | ----- | ----- |
179
+ | ----- | ---- | ------ ----- |
153
180
| count | ` varuint32 ` | count of import entries to follow |
154
181
| entries | ` import_entry* ` | repeated import entries as described below |
155
182
156
183
#### Import entry
157
184
| Field | Type | Description |
158
- | ----- | ----- | ----- |
159
- | sig_index | ` varuint32 ` | signature index of the import |
185
+ | ----- | ---- | ----------- |
160
186
| module_len | ` varuint32 ` | module string length |
161
187
| module_str | ` bytes ` | module string of ` module_len ` bytes |
162
- | function_len | ` varuint32 ` | function string length |
163
- | function_str | ` bytes ` | function string of ` function_len ` bytes |
188
+ | field_len | ` varuint32 ` | field name length |
189
+ | field_str | ` bytes ` | field name string of ` field_len ` bytes |
190
+ | kind | ` definition_kind ` | the kind of definition being imported |
191
+
192
+ Followed by, if the ` kind ` is ` Function ` :
193
+
194
+ | Field | Type | Description |
195
+ | ----- | ---- | ----------- |
196
+ | sig_index | ` varuint32 ` | signature index of the import |
197
+
198
+ or, if the ` kind ` is ` Table ` :
199
+
200
+ | Field | Type | Description |
201
+ | ----- | ---- | ----------- |
202
+ | table_index | ` varuint32 ` | [ table index] ( Modules.md#table-index-space ) ; must be 0 in the MVP |
203
+ | | ` resizable_definition ` | see [ above] ( #resizable_definition ) |
204
+
205
+ or, if the ` kind ` is ` Memory ` :
206
+
207
+ | Field | Type | Description |
208
+ | ----- | ---- | ----------- |
209
+ | memory_index | ` varuint32 ` | [ linear memory index] ( Modules.md#linear-memory-index-space ) ; must be 0 in the MVP |
210
+ | | ` resizable_definition ` | see [ above] ( #resizable_definition ) |
211
+
212
+ or, if the ` kind ` is ` Global ` :
213
+
214
+ | Field | Type | Description |
215
+ | ----- | ---- | ----------- |
216
+ | type | ` value_type ` | type of the imported global |
217
+ | mutability | ` uint8 ` | ` 0 ` if immutable, ` 1 ` if mutable; must be ` 0 ` in the MVP |
164
218
165
219
### Function section
166
220
@@ -170,52 +224,79 @@ The function section _declares_ the signatures of all functions in the
170
224
module (their definitions appear in the [ code section] ( #code-section ) ).
171
225
172
226
| Field | Type | Description |
173
- | ----- | ----- | ----- |
227
+ | ----- | ---- | ------ ----- |
174
228
| count | ` varuint32 ` | count of signature indices to follow |
175
229
| types | ` varuint32* ` | sequence of indices into the type section |
176
230
177
231
### Table section
178
232
179
233
ID: ` table `
180
234
181
- The table section defines the module's
182
- [ indirect function table] ( AstSemantics.md#calls ) .
235
+ The encoding of a [ Table section] ( Modules.md#table-section ) :
183
236
184
237
| Field | Type | Description |
185
- | ----- | ----- | ----- |
186
- | count | ` varuint32 ` | count of entries to follow |
187
- | entries | ` varuint32* ` | repeated indexes into the function section |
238
+ | ----- | ---- | ------ ----- |
239
+ | element_type | ` varuint7 ` | ` 0x40 ` , indicating [ ` anyfunc ` ] ( AstSemantics.md#table ) |
240
+ | | ` resizable_definition ` | see [ above ] ( #resizable_definition ) |
188
241
189
242
### Memory section
190
243
191
244
ID: ` memory `
192
245
193
- The memory section declares the size and characteristics of the memory
194
- associated with the module.
246
+ The encoding of a [ Memory section ] ( Modules.md#linear-memory-section ) is simply
247
+ a ` resizable_definition ` :
195
248
196
249
| Field | Type | Description |
197
- | ----- | ----- | ----- |
198
- | initial | ` varuint32 ` | initial memory size in 64KiB pages |
199
- | maximum | ` varuint32 ` | maximum memory size in 64KiB pages |
200
- | exported | ` uint8 ` | ` 1 ` if the memory is visible outside the module |
250
+ | ----- | ---- | ----------- |
251
+ | | ` resizable_definition ` | see [ above] ( #resizable_definition ) |
252
+
253
+ Note that the initial/maximum fields are specified in units of
254
+ [ WebAssembly pages] ( AstSemantics.md#linear-memory ) .
255
+
256
+ ### Global section
257
+
258
+ ID: ` global `
259
+
260
+ The encoding of the [ Global section] ( Modules.md#global-section ) :
261
+
262
+ | Field | Type | Description |
263
+ | ----- | ---- | ----------- |
264
+ | count | ` variable_entry ` | count of global [ variable entries] ( #variable-entry ) as described below |
265
+ | globals | ` global_entry* ` | global variable entries, as described below |
266
+
267
+ #### Global Entry
268
+
269
+ Each ` global_entry ` declares a number of global variables of a given type and mutability.
270
+ It is legal to have several entries with the same type.
271
+
272
+ | Field | Type | Description |
273
+ | ----- | ---- | ----------- |
274
+ | count | ` varuint32 ` | number of global variables of the following type/mutability |
275
+ | type | ` value_type ` | type of the variables |
276
+ | mutability | ` uint8 ` | ` 0 ` if immutable, ` 1 ` if mutable |
201
277
202
278
### Export section
203
279
204
280
ID: ` export `
205
281
206
- The export section declares all exports from the module.
282
+ The encoding of the [ Export section ] ( Modules.md#exports ) :
207
283
208
284
| Field | Type | Description |
209
- | ----- | ----- | ----- |
285
+ | ----- | ---- | ------ ----- |
210
286
| count | ` varuint32 ` | count of export entries to follow |
211
287
| entries | ` export_entry* ` | repeated export entries as described below |
212
288
213
289
#### Export entry
214
290
| Field | Type | Description |
215
- | ----- | ----- | ----- |
216
- | func_index | ` varuint32 ` | index into the function table |
217
- | function_len | ` varuint32 ` | function string length |
218
- | function_str | ` bytes ` | function string of ` function_len ` bytes |
291
+ | ----- | ---- | ----------- |
292
+ | field_len | ` varuint32 ` | field name string length |
293
+ | field_str | ` bytes ` | field name string of ` field_len ` bytes |
294
+ | kind | ` definition_kind ` | the kind of definition being exported |
295
+ | index | ` varuint32 ` | the index into the corresponding [ index space] ( Modules.md ) |
296
+
297
+ For example, if the "kind" is ` Function ` , then "index" is a
298
+ [ function index] ( Modules.md#function-index-space ) . Note that, in the MVP, the
299
+ only valid index value for a memory or table export is 0.
219
300
220
301
### Start section
221
302
@@ -224,7 +305,7 @@ ID: `start`
224
305
The start section declares the [ start function] ( Modules.md#module-start-function ) .
225
306
226
307
| Field | Type | Description |
227
- | ----- | ----- | ----- |
308
+ | ----- | ---- | ------ ----- |
228
309
| index | ` varuint32 ` | start function index |
229
310
230
311
### Code section
@@ -236,8 +317,8 @@ The count of function declared in the [function section](#function-section)
236
317
and function bodies defined in this section must be the same and the ` i ` th
237
318
declaration corresponds to the ` i ` th function body.
238
319
239
- | Field | Type | Description |
240
- | ----- | ----- | ----- | ----- |
320
+ | Field | Type | Description |
321
+ | ----- | ---- | ------ ----- |
241
322
| count | ` varuint32 ` | count of function bodies to follow |
242
323
| bodies | ` function_body* ` | sequence of [ Function Bodies] ( #function-bodies ) |
243
324
@@ -249,18 +330,39 @@ The data section declares the initialized data that is loaded
249
330
into the linear memory.
250
331
251
332
| Field | Type | Description |
252
- | ----- | ----- | ----- |
333
+ | ----- | ---- | ------ ----- |
253
334
| count | ` varuint32 ` | count of data segments to follow |
254
335
| entries | ` data_segment* ` | repeated data segments as described below |
255
336
256
337
a ` data_segment ` is:
257
338
258
339
| Field | Type | Description |
259
- | ----- | ----- | ----- |
260
- | offset | ` varuint32 ` | the offset in linear memory at which to store the data |
340
+ | ----- | ---- | ----------- |
341
+ | index | ` varuint32 ` | the [ linear memory index] ( Modules.md#linear-memory-index-space ) (0 in the MVP) |
342
+ | offset | ` init_expr ` | an ` i32 ` initializer expression that computes the offset at which to place the data |
261
343
| size | ` varuint32 ` | size of ` data ` (in bytes) |
262
344
| data | ` bytes ` | sequence of ` size ` bytes |
263
345
346
+ ### Element section
347
+
348
+ ID: ` elem `
349
+
350
+ The encoding of the [ Elements section] ( Modules.md#elements-section ) :
351
+
352
+ | Field | Type | Description |
353
+ | ----- | ---- | ----------- |
354
+ | count | ` varuint32 ` | count of element segments to follow |
355
+ | entries | ` elem_segment* ` | repeated element segments as described below |
356
+
357
+ a ` elem_segment ` is:
358
+
359
+ | Field | Type | Description |
360
+ | ----- | ---- | ----------- |
361
+ | index | ` varuint32 ` | the [ table index] ( Modules.md#table-index-space ) (0 in the MVP) |
362
+ | offset | ` init_expr ` | an ` i32 ` initializer expression that computes the offset at which to place the elements |
363
+ | num_elem | ` varuint32 ` | number of elements to follow |
364
+ | elems | ` varuint32* ` | sequence of [ function indices] ( Modules.md#function-index-space ) |
365
+
264
366
### Name section
265
367
266
368
ID: ` name `
@@ -273,7 +375,7 @@ environment, the names in this section will be used as the names of functions
273
375
and locals in the [ text format] ( TextFormat.md ) .
274
376
275
377
| Field | Type | Description |
276
- | ----- | ----- | ----- |
378
+ | ----- | ---- | ------ ----- |
277
379
| count | ` varuint32 ` | count of entries to follow |
278
380
| entries | ` function_names* ` | sequence of names |
279
381
@@ -284,7 +386,7 @@ functions.
284
386
#### Function names
285
387
286
388
| Field | Type | Description |
287
- | ----- | ----- | ----- |
389
+ | ----- | ---- | ------ ----- |
288
390
| fun_name_len | ` varuint32 ` | string length, in bytes |
289
391
| fun_name_str | ` bytes ` | valid utf8 encoding |
290
392
| local_count | ` varuint32 ` | count of local names to follow |
@@ -296,7 +398,7 @@ count may be greater or less than the actual number of locals.
296
398
#### Local name
297
399
298
400
| Field | Type | Description |
299
- | ----- | ----- | ----- |
401
+ | ----- | ---- | ------ ----- |
300
402
| local_name_len | ` varuint32 ` | string length, in bytes |
301
403
| local_name_str | ` bytes ` | valid utf8 encoding |
302
404
@@ -309,8 +411,8 @@ Each node in the abstract syntax tree corresponds to an operator, such as `i32.a
309
411
Operators are encoding by an opcode byte followed by immediate bytes (if any), followed by children
310
412
nodes (if any).
311
413
312
- | Field | Type | Description |
313
- | ----- | ----- | ----- |
414
+ | Field | Type | Description |
415
+ | ----- | ---- | ------ ----- |
314
416
| body_size | ` varuint32 ` | size of function body to follow, in bytes |
315
417
| local_count | ` varuint32 ` | number of local entries |
316
418
| locals | ` local_entry* ` | local variables |
@@ -322,7 +424,7 @@ Each local entry declares a number of local variables of a given type.
322
424
It is legal to have several entries with the same type.
323
425
324
426
| Field | Type | Description |
325
- | ----- | ----- | ----- |
427
+ | ----- | ---- | ------ ----- |
326
428
| count | ` varuint32 ` | number of local variables of the following type |
327
429
| type | ` value_type ` | type of the variables |
328
430
@@ -374,9 +476,10 @@ out of range, `br_table` branches to the default target.
374
476
| ` get_local ` | ` 0x14 ` | local_index : ` varuint32 ` | read a local variable or parameter |
375
477
| ` set_local ` | ` 0x15 ` | local_index : ` varuint32 ` | write a local variable or parameter |
376
478
| ` tee_local ` | ` 0x19 ` | local_index : ` varuint32 ` | write a local variable or parameter and return the same value |
377
- | ` call ` | ` 0x16 ` | argument_count : ` varuint1 ` , function_index : ` varuint32 ` | call a function by its index |
479
+ | ` get_global ` | ` 0x18 ` | global_index : ` varuint32 ` | read a global variable |
480
+ | ` set_global ` | ` 0x19 ` | global_index : ` varuint32 ` | write a global variable |
481
+ | ` call ` | ` 0x16 ` | argument_count : ` varuint1 ` , function_index : ` varuint32 ` | call a function by its [ index] ( Modules.md#function-index-space ) |
378
482
| ` call_indirect ` | ` 0x17 ` | argument_count : ` varuint1 ` , type_index : ` varuint32 ` | call a function indirect with an expected signature |
379
- | ` call_import ` | ` 0x18 ` | argument_count : ` varuint1 ` , import_index : ` varuint32 ` | call an imported function by its index |
380
483
381
484
The counts following the different call opcodes specify the number of preceding operands taken as arguments.
382
485
0 commit comments