Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ci/cbindgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def visit_Struct(self, node):
decl.name = '_anon_' + str(anon_decl)

if node.name in self.forward_declared:
self.ret += "{}._fields = [ # type: ignore\n".format(node.name)
self.ret += "{}._fields_ = [\n".format(node.name)
else:
self.ret += "class {}(Structure):\n".format(node.name)
self.ret += " _fields_ = [\n"
Expand Down Expand Up @@ -189,7 +189,10 @@ def type_name(ty, ptr=False, typing=False):
return "POINTER({})".format(type_name(ty, False, typing))

if isinstance(ty, c_ast.IdentifierType):
if ty.names == ['unsigned', 'char']:
return "int" if typing else "c_ubyte"
assert(len(ty.names) == 1)

if ty.names[0] == "void":
return "None"
elif ty.names[0] == "_Bool":
Expand Down Expand Up @@ -218,6 +221,8 @@ def type_name(ty, ptr=False, typing=False):
return "float" if typing else "c_double"
elif ty.names[0] == "size_t":
return "int" if typing else "c_size_t"
elif ty.names[0] == "ptrdiff_t":
return "int" if typing else "c_ssize_t"
elif ty.names[0] == "char":
return "c_char"
elif ty.names[0] == "int":
Expand Down
79 changes: 79 additions & 0 deletions tests/component/test_component.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import unittest
import tempfile

from wasmtime import *
from wasmtime.component import Component, ExportIndex

class TestComponent(unittest.TestCase):
def test_smoke(self):
Component(Engine(), '(component)')
Component(Engine(), bytes(b'\0asm\x0d\0\x01\0'))
Component(Engine(), bytearray(b'\0asm\x0d\0\x01\0'))

with self.assertRaises(WasmtimeError):
Component(Engine(), '(component2)')
with self.assertRaises(WasmtimeError):
Component(Engine(), bytes(b'\0asm\x01\0\0\0'))

def test_invalid(self):
with self.assertRaises(TypeError):
Component(1, b'') # type: ignore
with self.assertRaises(TypeError):
Component(Engine(), 2) # type: ignore
with self.assertRaises(WasmtimeError):
Component(Engine(), b'')
with self.assertRaises(WasmtimeError):
Component(Engine(), b'\x00')

def test_serialize(self):
engine = Engine()
component = Component(engine, '(component)')
encoded = component.serialize()
component = Component.deserialize(engine, encoded)
with tempfile.TemporaryDirectory() as d:
path = d + '/component.bin'
with open(path, 'wb') as f:
f.write(encoded)
component = Component.deserialize_file(engine, path)

# Run the destructor for `Component` which has an mmap to the file
# which prevents deletion on Windows.
del component

def test_exports(self):
engine = Engine()

c = Component(engine, '(component)')
self.assertIsNone(c.get_export_index('foo'))
self.assertIsNone(c.get_export_index('foo', instance = None))

c = Component(engine, """
(component
(core module (export "foo"))
)
""")
foo = c.get_export_index('foo')
self.assertIsNotNone(foo)
self.assertIsNone(c.get_export_index('foo', instance = foo))
self.assertIsInstance(foo, ExportIndex)

c = Component(engine, """
(component
(core module $a)
(instance (export "x")
(export "m" (core module $a))
)
)
""")
self.assertIsNotNone(c.get_export_index('x'))
self.assertIsNotNone(c.get_export_index('m', instance = c.get_export_index('x')))

c2 = Component(engine, """
(component
(core module $a)
(instance (export "x")
(export "m" (core module $a))
)
)
""")
self.assertIsNone(c2.get_export_index('m', instance = c.get_export_index('x')))
Loading