Coverage for tests/codegen/test_external_types.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-20 16:25 +0000

1from . import bindgen, REALLOC 

2from wasmtime import Store 

3 

4module = """ 

5(component $OuterComp 

6 

7 (type $types 

8 (instance 

9 (type $runtime-value (variant 

10 (case "id" string) (case "id2" string))) 

11 (export $runtime-value-export "runtime-value" 

12 (type (eq $runtime-value))) 

13 ) 

14 ) 

15 (import "types" (instance $types (type $types))) 

16 (alias export $types "runtime-value" (type $runtime-value-export)) 

17 

18 (import "host" (instance $inst 

19 (alias outer $OuterComp 1 (type $runtime-value)) 

20 (export $export "runtime-value" (type (eq $runtime-value))) 

21 (export "some-fn" (func (param "v" $export) (result $export))) 

22 )) 

23 

24 (core module $libc 

25 (memory (export "mem") 1) 

26 {} 

27 ) 

28 (core instance $libc (instantiate $libc)) 

29 

30 (core module $mod 

31 (import "libc" "mem" (memory 1)) 

32 (import "" "lowered-fn-import" 

33 (func $lowered-fn (param i32 i32 i32 i32))) 

34 

35 (func (export "core-fn") (param i32 i32 i32) (result i32) 

36 (call $lowered-fn 

37 (local.get 0) (local.get 1) (local.get 2) (local.get 2)) 

38 (local.get 2)) 

39 ) 

40 

41 (core func $lowered-fn 

42 (canon lower (func $inst "some-fn") (memory $libc "mem") 

43 (realloc (func $libc "realloc")))) 

44 

45 (core instance $inst 

46 (instantiate $mod 

47 (with "libc" (instance $libc)) 

48 (with "" (instance 

49 (export "lowered-fn-import" (func $lowered-fn)) 

50 )) 

51 ) 

52 ) 

53 

54 (type $runtime-value (variant (case "id" string) (case "id2" string))) 

55 (func $lifted-core-fn (param "a" $runtime-value) (result $runtime-value) 

56 (canon lift (core func $inst "core-fn") (memory $libc "mem") 

57 (realloc (func $libc "realloc")))) 

58 

59 (instance (export "e") 

60 (export "runtime-value" (type $runtime-value)) 

61 (export "some-fn" (func $lifted-core-fn)) 

62 ) 

63 

64) 

65""".format(REALLOC) 

66bindgen('external_types', module) 

67 

68from .generated.external_types import Root, RootImports, imports 

69from .generated.external_types.imports import host 

70from .generated.external_types import e 

71 

72 

73class Host(imports.HostHost): 

74 def some_fn(self, v: host.RuntimeValue) -> host.RuntimeValue: 

75 return v 

76 

77 

78def test_bindings(tmp_path): 

79 store = Store() 

80 wasm = Root(store, RootImports(None, host=Host())) 

81 

82 exports = wasm.e() 

83 rt_id = exports.some_fn(store, e.RuntimeValueId('1234')) 

84 assert rt_id == e.RuntimeValueId('1234')