@@ -23,7 +23,8 @@ JavaScript runtimes. For a more user-focussed explanation, take a look at the
23
23
* [ Canonical definitions] ( #canonical-definitions )
24
24
* [ Canonical ABI] ( #canonical-built-ins )
25
25
* [ Canonical built-ins] ( #canonical-built-ins )
26
- * [ Start definitions] ( #-start-definitions )
26
+ * [ Value definitions] ( #value-definitions )
27
+ * [ Start definitions] ( #start-definitions )
27
28
* [ Import and export definitions] ( #import-and-export-definitions )
28
29
* [ Component invariants] ( #component-invariants )
29
30
* [ JavaScript embedding] ( #JavaScript-embedding )
@@ -87,6 +88,7 @@ definition ::= core-prefix(<core:module>)
87
88
| <start> 🪺
88
89
| <import>
89
90
| <export>
91
+ | <value> 🪙
90
92
91
93
where core-prefix(X) parses '(' 'core' Y ')' when X parses '(' Y ')'
92
94
```
@@ -296,7 +298,7 @@ contain any valid UTF-8 string).
296
298
297
299
🪙 The ` value ` sort refers to a value that is provided and consumed during
298
300
instantiation. How this works is described in the
299
- [ start definitions] ( #start -definitions ) section.
301
+ [ value definitions] ( #value -definitions ) section.
300
302
301
303
To see a non-trivial example of component instantiation, we'll first need to
302
304
introduce a few other definitions below that allow components to import, define
@@ -564,10 +566,12 @@ externdesc ::= (<sort> (type <u32>) )
564
566
| <functype>
565
567
| <componenttype>
566
568
| <instancetype>
567
- | (value <valtype >) 🪙
569
+ | (value <valuebound >) 🪙
568
570
| (type <typebound>)
569
571
typebound ::= (eq <typeidx>)
570
572
| (sub resource)
573
+ valuebound ::= (eq <valueidx>)
574
+ | <valtype>
571
575
572
576
where bind-id(X) parses '(' sort <id>? Y ')' when X parses '(' sort Y ')'
573
577
```
@@ -1341,22 +1345,102 @@ number of threads that can be expected to execute concurrently.
1341
1345
See the [ CanonicalABI.md] ( CanonicalABI.md#canonical-definitions ) for detailed
1342
1346
definitions of each of these built-ins and their interactions.
1343
1347
1348
+ ### 🪙 Value Definitions
1344
1349
1345
- ### 🪙 Start Definitions
1350
+ Value definitions (in the value index space) are like immutable ` global ` definitions
1351
+ in Core WebAssembly except that validation requires them to be consumed exactly
1352
+ once at instantiation-time (i.e., they are [ linear] ).
1353
+
1354
+ Components may define values in the value index space using following syntax:
1346
1355
1347
- Like modules, components can have start functions that are called during
1348
- instantiation. Unlike modules, components can call start functions at multiple
1349
- points during instantiation with each such call having parameters and results.
1350
- Thus, ` start ` definitions in components look like function calls:
1351
1356
``` ebnf
1352
- start ::= (start <funcidx> (value <valueidx>)* (result (value <id>?))*)
1357
+ value ::= (value <id>? <valtype> <val>)
1358
+ val ::= false | true
1359
+ | <core:i64>
1360
+ | <core:f64>
1361
+ | '<core:char>'
1362
+ | <core:name>
1363
+ | (record <val>+)
1364
+ | (variant "<label>" <val>?)
1365
+ | (list <val>*)
1366
+ | (tuple <val>+)
1367
+ | (flags "<label>"*)
1368
+ | (enum "<label>")
1369
+ | none | (some <val>)
1370
+ | ok | (ok <val>) | error | (error <val>)
1371
+ ```
1372
+
1373
+ Where ` val ` definition must match the ` valtype ` specified.
1374
+
1375
+ For example:
1376
+ ``` wasm
1377
+ (component
1378
+ (value $a bool true)
1379
+ (value $b u8 1)
1380
+ (value $c u16 2)
1381
+ (value $d u32 3)
1382
+ (value $e u64 4)
1383
+ (value $f u8 5)
1384
+ (value $g u16 6)
1385
+ (value $h u32 7)
1386
+ (value $i u64 8)
1387
+ (value $j f32 9.1)
1388
+ (value $k f64 9.2)
1389
+ (value $l char 'a')
1390
+ (value $m string "hello")
1391
+ (value $n (record (field "a" bool) (field "b" u8)) (record true 1))
1392
+ (value $o (variant (case "a" bool) (case "b" u8)) (variant "b" 1))
1393
+ (value $p (list (result (option u8)))
1394
+ (list
1395
+ error
1396
+ (ok (some 1))
1397
+ (ok none)
1398
+ error
1399
+ (ok (some 2))
1400
+ )
1401
+ )
1402
+ (value $q (tuple u8 u16 u32) (tuple 1 2 3))
1403
+
1404
+ (type $abc (flags "a" "b" "c"))
1405
+ (value $r $abc (flags "a" "c"))
1406
+
1407
+ (value $s (enum "a" "b" "c") (enum "b"))
1408
+
1409
+ (type $complex
1410
+ (tuple
1411
+ (record
1412
+ (field "a" (option string))
1413
+ (field "b" (tuple (option u8) string))
1414
+ )
1415
+ (list char)
1416
+ $abc
1417
+ string
1418
+ )
1419
+ )
1420
+ (value $complex1 (type $complex)
1421
+ (tuple
1422
+ (record
1423
+ none
1424
+ (tuple none "empty")
1425
+ )
1426
+ (list)
1427
+ (flags)
1428
+ ""
1429
+ )
1430
+ )
1431
+ (value $complex2 (type $complex)
1432
+ (tuple
1433
+ (record
1434
+ (some "example")
1435
+ (tuple (some 42) "hello")
1436
+ )
1437
+ (list 'a' 'b' 'c')
1438
+ (flags "b" "a")
1439
+ "hi"
1440
+ )
1441
+ )
1442
+ )
1353
1443
```
1354
- The ` (value <valueidx>)* ` list specifies the arguments passed to ` funcidx ` by
1355
- indexing into the * value index space* . Value definitions (in the value index
1356
- space) are like immutable ` global ` definitions in Core WebAssembly except that
1357
- validation requires them to be consumed exactly once at instantiation-time
1358
- (i.e., they are [ linear] ). The arity and types of the two value lists are
1359
- validated to match the signature of ` funcidx ` .
1360
1444
1361
1445
As with all definition sorts, values may be imported and exported by
1362
1446
components. As an example value import:
@@ -1366,6 +1450,34 @@ components. As an example value import:
1366
1450
As this example suggests, value imports can serve as generalized [ environment
1367
1451
variables] , allowing not just ` string ` , but the full range of ` valtype ` .
1368
1452
1453
+ Values can also be exported:
1454
+ ``` wasm
1455
+ (value $default_url string "https://example.com")
1456
+ (export "default-url" (value $default_url))
1457
+ ```
1458
+
1459
+ ` default-url ` value export will infer ` (eq $default_url) ` value bound in the component type.
1460
+
1461
+ Include a type ascription to explicitly set type to ` string ` :
1462
+
1463
+ ```
1464
+ (value $default_url string "https://example.com")
1465
+ (export "default-url" (value $default_url) (value string))
1466
+ ```
1467
+
1468
+ ### 🪙 Start Definitions
1469
+
1470
+ Like modules, components can have start functions that are called during
1471
+ instantiation. Unlike modules, components can call start functions at multiple
1472
+ points during instantiation with each such call having parameters and results.
1473
+ Thus, ` start ` definitions in components look like function calls:
1474
+ ``` ebnf
1475
+ start ::= (start <funcidx> (value <valueidx>)* (result (value <id>?))*)
1476
+ ```
1477
+ The ` (value <valueidx>)* ` list specifies the arguments passed to ` funcidx ` by
1478
+ indexing into the * value index space* . The arity and types of the two value lists are
1479
+ validated to match the signature of ` funcidx ` .
1480
+
1369
1481
With this, we can define a component that imports a string and computes a new
1370
1482
exported string at instantiation time:
1371
1483
``` wasm
@@ -1927,6 +2039,9 @@ and will be added over the coming months to complete the MVP proposal:
1927
2039
[ Index Space ] : https://webassembly.github.io/spec/core/syntax/modules.html#indices
1928
2040
[ Abbreviations ] : https://webassembly.github.io/spec/core/text/conventions.html#abbreviations
1929
2041
2042
+ [ `core:i64` ] : https://webassembly.github.io/spec/core/syntax/values.html#integers
2043
+ [ `core:f64` ] : https://webassembly.github.io/spec/core/syntax/values.html#floating-point
2044
+ [ `core:char` ] : https://webassembly.github.io/spec/core/syntax/values.html#syntax-name
1930
2045
[ `core:name` ] : https://webassembly.github.io/spec/core/syntax/values.html#syntax-name
1931
2046
[ `core:module` ] : https://webassembly.github.io/spec/core/text/modules.html#text-module
1932
2047
[ `core:type` ] : https://webassembly.github.io/spec/core/text/modules.html#types
0 commit comments