Coverage for tests/component/test_instance.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-12-01 19:40 +0000

1import unittest 

2 

3from wasmtime import Engine, WasmtimeError, Store 

4from wasmtime.component import Linker, Component, Instance 

5 

6 

7class TestInstance(unittest.TestCase): 

8 def test_init(self): 

9 with self.assertRaises(WasmtimeError): 

10 Instance() 

11 

12 def test_smoke(self): 

13 engine = Engine() 

14 linker = Linker(engine) 

15 component = Component(engine, '(component)') 

16 store = Store(engine) 

17 

18 instance = linker.instantiate(store, component) 

19 self.assertIsNotNone(instance) 

20 self.assertIsNone(instance.get_export_index(store, 'hello')) 

21 

22 component = Component(engine, """ 

23 (component 

24 (core module (export "foo")) 

25 ) 

26 """) 

27 instance = linker.instantiate(store, component) 

28 self.assertIsNotNone(instance.get_export_index(store, 'foo')) 

29 

30 component = Component(engine, """ 

31 (component 

32 (core module $a) 

33 (instance (export "x") 

34 (export "m" (core module $a)) 

35 ) 

36 ) 

37 """) 

38 instance = linker.instantiate(store, component) 

39 e1 = instance.get_export_index(store, 'x') 

40 e2 = component.get_export_index('x') 

41 assert(e1 is not None) 

42 assert(e2 is not None) 

43 self.assertIsNotNone(instance.get_export_index(store, 'm', instance = e1)) 

44 self.assertIsNotNone(instance.get_export_index(store, 'm', instance = e2)) 

45 

46 self.assertIsNone(instance.get_func(store, e1)) 

47 self.assertIsNone(instance.get_func(store, e2)) 

48 

49 def test_get_func(self): 

50 engine = Engine() 

51 linker = Linker(engine) 

52 store = Store(engine) 

53 

54 component = Component(engine, """ 

55 (component 

56 (core module $a 

57 (func (export "foo")) 

58 ) 

59 (core instance $a (instantiate $a)) 

60 (func (export "a") (canon lift (core func $a "foo"))) 

61 ) 

62 """) 

63 instance = linker.instantiate(store, component) 

64 index = instance.get_export_index(store, 'a') 

65 assert(index is not None) 

66 func = instance.get_func(store, index) 

67 self.assertIsNotNone(func) 

68 

69 self.assertIsNotNone(instance.get_func(store, 'a')) 

70 self.assertIsNone(instance.get_func(store, 'b'))