@@ -23,7 +23,8 @@ JavaScript runtimes. For a more user-focussed explanation, take a look at the
2323 * [ Canonical definitions] ( #canonical-definitions )
2424 * [ Canonical ABI] ( #canonical-built-ins )
2525 * [ Canonical built-ins] ( #canonical-built-ins )
26- * [ Start definitions] ( #-start-definitions )
26+ * [ Value definitions] ( #value-definitions )
27+ * [ Start definitions] ( #start-definitions )
2728 * [ Import and export definitions] ( #import-and-export-definitions )
2829* [ Component invariants] ( #component-invariants )
2930* [ JavaScript embedding] ( #JavaScript-embedding )
@@ -87,6 +88,7 @@ definition ::= core-prefix(<core:module>)
8788 | <start> 🪺
8889 | <import>
8990 | <export>
91+ | <value> 🪙
9092
9193where core-prefix(X) parses '(' 'core' Y ')' when X parses '(' Y ')'
9294```
@@ -296,7 +298,7 @@ contain any valid UTF-8 string).
296298
297299🪙 The ` value ` sort refers to a value that is provided and consumed during
298300instantiation. How this works is described in the
299- [ start definitions] ( #start -definitions ) section.
301+ [ value definitions] ( #value -definitions ) section.
300302
301303To see a non-trivial example of component instantiation, we'll first need to
302304introduce a few other definitions below that allow components to import, define
@@ -564,10 +566,12 @@ externdesc ::= (<sort> (type <u32>) )
564566 | <functype>
565567 | <componenttype>
566568 | <instancetype>
567- | (value <valtype >) 🪙
569+ | (value <valuebound >) 🪙
568570 | (type <typebound>)
569571typebound ::= (eq <typeidx>)
570572 | (sub resource)
573+ valuebound ::= (eq <valueidx>)
574+ | <valtype>
571575
572576where bind-id(X) parses '(' sort <id>? Y ')' when X parses '(' sort Y ')'
573577```
@@ -1341,22 +1345,102 @@ number of threads that can be expected to execute concurrently.
13411345See the [ CanonicalABI.md] ( CanonicalABI.md#canonical-definitions ) for detailed
13421346definitions of each of these built-ins and their interactions.
13431347
1348+ ### 🪙 Value Definitions
13441349
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:
13461355
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:
13511356``` 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+ )
13531443```
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 ` .
13601444
13611445As with all definition sorts, values may be imported and exported by
13621446components. As an example value import:
@@ -1366,6 +1450,34 @@ components. As an example value import:
13661450As this example suggests, value imports can serve as generalized [ environment
13671451variables] , allowing not just ` string ` , but the full range of ` valtype ` .
13681452
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+
13691481With this, we can define a component that imports a string and computes a new
13701482exported string at instantiation time:
13711483``` wasm
@@ -1927,6 +2039,9 @@ and will be added over the coming months to complete the MVP proposal:
19272039[ Index Space ] : https://webassembly.github.io/spec/core/syntax/modules.html#indices
19282040[ Abbreviations ] : https://webassembly.github.io/spec/core/text/conventions.html#abbreviations
19292041
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
19302045[ `core:name` ] : https://webassembly.github.io/spec/core/syntax/values.html#syntax-name
19312046[ `core:module` ] : https://webassembly.github.io/spec/core/text/modules.html#text-module
19322047[ `core:type` ] : https://webassembly.github.io/spec/core/text/modules.html#types
0 commit comments