Skip to content

Commit faf42c9

Browse files
authored
Merge pull request #93 from WebAssembly/produce-valid-wit
Tweak variant/result mangling to produce valid wit
2 parents f76b64a + 0591a1c commit faf42c9

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

design/mvp/CanonicalABI.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,10 @@ def mangle_flags(labels):
15091509
return 'flags { ' + ', '.join(labels) + ' }'
15101510

15111511
def mangle_varianttype(cases):
1512-
mangled_cases = (c.label + '(' + mangle_maybevaltype(c.t) + ')' for c in cases)
1512+
mangled_cases = ('{label}{payload}'.format(
1513+
label = c.label,
1514+
payload = '' if c.t is None else '(' + mangle_valtype(c.t) + ')')
1515+
for c in cases)
15131516
return 'variant { ' + ', '.join(mangled_cases) + ' }'
15141517

15151518
def mangle_enumtype(labels):
@@ -1522,12 +1525,11 @@ def mangle_optiontype(t):
15221525
return 'option<' + mangle_valtype(t) + '>'
15231526

15241527
def mangle_resulttype(ok, error):
1525-
return 'result<' + mangle_maybevaltype(ok) + ', ' + mangle_maybevaltype(error) + '>'
1526-
1527-
def mangle_maybevaltype(t):
1528-
if t is None:
1529-
return '_'
1530-
return mangle_valtype(t)
1528+
match (ok, error):
1529+
case (None, None) : return 'result'
1530+
case (None, _) : return 'result<_, ' + mangle_valtype(error) + '>'
1531+
case (_, None) : return 'result<' + mangle_valtype(ok) + '>'
1532+
case (_, _) : return 'result<' + mangle_valtype(ok) + ', ' + mangle_valtype(error) + '>'
15311533
```
15321534
As an example, given a component type:
15331535
```wasm

design/mvp/canonical-abi/definitions.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,10 @@ def mangle_flags(labels):
11541154
return 'flags { ' + ', '.join(labels) + ' }'
11551155

11561156
def mangle_varianttype(cases):
1157-
mangled_cases = (c.label + '(' + mangle_maybevaltype(c.t) + ')' for c in cases)
1157+
mangled_cases = ('{label}{payload}'.format(
1158+
label = c.label,
1159+
payload = '' if c.t is None else '(' + mangle_valtype(c.t) + ')')
1160+
for c in cases)
11581161
return 'variant { ' + ', '.join(mangled_cases) + ' }'
11591162

11601163
def mangle_enumtype(labels):
@@ -1167,12 +1170,12 @@ def mangle_optiontype(t):
11671170
return 'option<' + mangle_valtype(t) + '>'
11681171

11691172
def mangle_resulttype(ok, error):
1170-
return 'result<' + mangle_maybevaltype(ok) + ', ' + mangle_maybevaltype(error) + '>'
1173+
match (ok, error):
1174+
case (None, None) : return 'result'
1175+
case (None, _) : return 'result<_, ' + mangle_valtype(error) + '>'
1176+
case (_, None) : return 'result<' + mangle_valtype(ok) + '>'
1177+
case (_, _) : return 'result<' + mangle_valtype(ok) + ', ' + mangle_valtype(error) + '>'
11711178

1172-
def mangle_maybevaltype(t):
1173-
if t is None:
1174-
return '_'
1175-
return mangle_valtype(t)
11761179

11771180
## Lifting Canonical Modules
11781181

design/mvp/canonical-abi/run_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,11 @@ def test_mangle_functype(params, results, expect):
397397
test_mangle_functype([Flags(['a','b'])], [Enum(['a','b'])],
398398
'func flags { a, b } -> enum { a, b }')
399399
test_mangle_functype([Variant([Case('a',None),Case('b',U8())])], [Union([U8(),List(String())])],
400-
'func variant { a(_), b(u8) } -> union { u8, list<string> }')
400+
'func variant { a, b(u8) } -> union { u8, list<string> }')
401401
test_mangle_functype([Option(Bool())],[Option(List(U8()))],
402402
'func option<bool> -> option<list<u8>>')
403403
test_mangle_functype([], [('a',Result(None,None)),('b',Result(U8(),None)),('c',Result(None,U8()))],
404-
'func() -> (a: result<_, _>, b: result<u8, _>, c: result<_, u8>)')
404+
'func() -> (a: result, b: result<u8>, c: result<_, u8>)')
405405

406406
def test_cabi(ct, expect):
407407
got = canonical_module_type(ct)

0 commit comments

Comments
 (0)