Skip to content

Commit 4f3045b

Browse files
authored
Add bool support (#99)
* bool support * remove borrows
1 parent e9359fd commit 4f3045b

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn package_all_the_things(out_dir: &Path) -> Result<()> {
142142
run(Command::new(clang)
143143
.arg("-shared")
144144
.arg("-o")
145-
.arg(&out_dir.join(name))
145+
.arg(out_dir.join(name))
146146
.arg("-Wl,--whole-archive")
147147
.arg(&path)
148148
.arg("-Wl,--no-whole-archive")

runtime/src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use {
1212
pyo3::{
1313
exceptions::PyAssertionError,
1414
intern,
15-
types::{PyBytes, PyDict, PyList, PyMapping, PyModule, PyString, PyTuple},
15+
types::{PyBool, PyBytes, PyDict, PyList, PyMapping, PyModule, PyString, PyTuple},
1616
Py, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
1717
},
1818
std::{
@@ -512,6 +512,15 @@ pub unsafe extern "C" fn componentize_py_free(ptr: *mut u8, size: usize, align:
512512
alloc::dealloc(ptr, Layout::from_size_align(size, align).unwrap())
513513
}
514514

515+
#[export_name = "componentize-py#ToCanonBool"]
516+
pub extern "C" fn componentize_py_to_canon_bool(_py: &Python, value: &PyAny) -> u32 {
517+
if value.is_true().unwrap() {
518+
1
519+
} else {
520+
0
521+
}
522+
}
523+
515524
#[export_name = "componentize-py#ToCanonI32"]
516525
pub extern "C" fn componentize_py_to_canon_i32(_py: &Python, value: &PyAny) -> i32 {
517526
value.extract().unwrap()
@@ -687,6 +696,11 @@ pub extern "C" fn componentize_py_get_list_element<'a>(
687696
value.downcast::<PyList>().unwrap().get_item(index).unwrap()
688697
}
689698

699+
#[export_name = "componentize-py#FromCanonBool"]
700+
pub extern "C" fn componentize_py_from_canon_bool<'a>(py: &'a Python<'a>, value: u32) -> &'a PyAny {
701+
PyBool::new(*py, value != 0)
702+
}
703+
690704
#[export_name = "componentize-py#FromCanonI32"]
691705
pub extern "C" fn componentize_py_from_canon_i32<'a>(py: &'a Python<'a>, value: i32) -> &'a PyAny {
692706
value.to_object(*py).into_ref(*py).downcast().unwrap()

src/bindgen.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ pub static IMPORT_SIGNATURES: &[(&str, &[ValType], &[ValType])] = &[
3737
&[ValType::I32],
3838
),
3939
("componentize-py#Free", &[ValType::I32; 3], &[]),
40+
(
41+
"componentize-py#ToCanonBool",
42+
&[ValType::I32; 2],
43+
&[ValType::I32],
44+
),
4045
(
4146
"componentize-py#ToCanonI32",
4247
&[ValType::I32; 2],
@@ -78,6 +83,11 @@ pub static IMPORT_SIGNATURES: &[(&str, &[ValType], &[ValType])] = &[
7883
&[ValType::I32; 3],
7984
&[ValType::I32],
8085
),
86+
(
87+
"componentize-py#FromCanonBool",
88+
&[ValType::I32; 2],
89+
&[ValType::I32],
90+
),
8191
(
8292
"componentize-py#FromCanonI32",
8393
&[ValType::I32; 2],
@@ -575,7 +585,14 @@ impl<'a> FunctionBindgen<'a> {
575585

576586
fn to_canon(&mut self, ty: Type, context: u32, value: u32) {
577587
match ty {
578-
Type::Bool | Type::U8 | Type::U16 | Type::U32 | Type::S8 | Type::S16 | Type::S32 => {
588+
Type::Bool => {
589+
self.push(Ins::LocalGet(context));
590+
self.push(Ins::LocalGet(value));
591+
self.push(Ins::Call(
592+
*IMPORTS.get("componentize-py#ToCanonBool").unwrap(),
593+
));
594+
}
595+
Type::U8 | Type::U16 | Type::U32 | Type::S8 | Type::S16 | Type::S32 => {
579596
self.push(Ins::LocalGet(context));
580597
self.push(Ins::LocalGet(value));
581598
self.push(Ins::Call(
@@ -850,7 +867,16 @@ impl<'a> FunctionBindgen<'a> {
850867

851868
fn store(&mut self, ty: Type, context: u32, value: u32, destination: u32) {
852869
match ty {
853-
Type::Bool | Type::U8 | Type::S8 => {
870+
Type::Bool => {
871+
self.push(Ins::LocalGet(destination));
872+
self.push(Ins::LocalGet(context));
873+
self.push(Ins::LocalGet(value));
874+
self.push(Ins::Call(
875+
*IMPORTS.get("componentize-py#ToCanonBool").unwrap(),
876+
));
877+
self.push(Ins::I32Store8(mem_arg(0, 0)));
878+
}
879+
Type::U8 | Type::S8 => {
854880
self.push(Ins::LocalGet(destination));
855881
self.to_canon(ty, context, value);
856882
self.push(Ins::I32Store8(mem_arg(0, 0)));
@@ -1384,7 +1410,14 @@ impl<'a> FunctionBindgen<'a> {
13841410

13851411
fn from_canon(&mut self, ty: Type, context: u32, value: &[u32]) {
13861412
match ty {
1387-
Type::Bool | Type::U8 | Type::U16 | Type::U32 | Type::S8 | Type::S16 | Type::S32 => {
1413+
Type::Bool => {
1414+
self.push(Ins::LocalGet(context));
1415+
self.push(Ins::LocalGet(value[0]));
1416+
self.push(Ins::Call(
1417+
*IMPORTS.get("componentize-py#FromCanonBool").unwrap(),
1418+
));
1419+
}
1420+
Type::U8 | Type::U16 | Type::U32 | Type::S8 | Type::S16 | Type::S32 => {
13881421
self.push(Ins::LocalGet(context));
13891422
self.push(Ins::LocalGet(value[0]));
13901423
self.push(Ins::Call(

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ pub async fn componentize(
584584
.with_context(|| format!("unable to open {path}"))?,
585585
DirPerms::all(),
586586
FilePerms::all(),
587-
&index.to_string(),
587+
index.to_string(),
588588
);
589589
}
590590

src/summary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,8 +2397,8 @@ impl<'a> TypeNames<'a> {
23972397

23982398
fn type_name(&mut self, ty: Type, seen: &HashSet<TypeId>, resource: Option<TypeId>) -> String {
23992399
match ty {
2400-
Type::Bool
2401-
| Type::U8
2400+
Type::Bool => "bool".into(),
2401+
Type::U8
24022402
| Type::U16
24032403
| Type::U32
24042404
| Type::U64

0 commit comments

Comments
 (0)