Skip to content

Commit 4b7075a

Browse files
authored
Remove fancy bool (#1915)
Someone who knows rust and cgo better than me may also wanna look at this, cause I know neither, but my local tests appear to be running? <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Change `is_done` type from `bool` to `int` in callback functions across Rust and Go code for compatibility. > > - **Behavior**: > - Change `is_done` type from `bool` to `int` in `CallbackFn` in `lib.rs` and `baml_cffi_generated.h`. > - Update `safe_trigger_callback()` in `lib.rs` to convert `bool` to `int` for `is_done`. > - Modify `trigger_callback()` and `error_callback()` in `callbacks.go` to use `int` for `is_done`. > - **Code Cleanup**: > - Remove `#include <stdbool.h>` from `exports.go`, `lib.go`, and `callbacks.go` as `bool` is no longer used. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup> for 49cd820. You can [customize](https://app.ellipsis.dev/BoundaryML/settings/summaries) this summary. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent 338cd47 commit 4b7075a

6 files changed

Lines changed: 21 additions & 16 deletions

File tree

engine/language_client_cffi/src/lib.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ use std::os::raw::c_char;
8787

8888
use baml_types::BamlValue;
8989

90-
pub type CallbackFn = extern "C" fn(call_id: u32, is_done: bool, content: *const i8, length: usize);
90+
pub type CallbackFn = extern "C" fn(call_id: u32, is_done: i32, content: *const i8, length: usize);
9191

9292
/// cbindgen:ignore
9393
static RESULT_CALLBACK_FN: OnceCell<CallbackFn> = OnceCell::new();
@@ -122,21 +122,27 @@ fn safe_trigger_callback(id: u32, is_done: bool, result: Result<FunctionResult>)
122122
Some(Ok(content)) => {
123123
let mut builder = flatbuffers::FlatBufferBuilder::new();
124124
let content = ctypes::serialize_baml_value_with_meta(&content.0, &mut builder);
125-
callback_fn(id, is_done, content.as_ptr() as *const i8, content.len());
125+
let is_done_int = if is_done { 1 } else { 0 };
126+
callback_fn(
127+
id,
128+
is_done_int,
129+
content.as_ptr() as *const i8,
130+
content.len(),
131+
);
126132
}
127133
Some(Err(e)) => {
128134
// let c_message = CString::new(e.to_string()).unwrap();
129135
let message = e.to_string();
130-
error_callback_fn(id, true, message.as_ptr() as *const i8, message.len());
136+
error_callback_fn(id, 1, message.as_ptr() as *const i8, message.len());
131137
}
132138
None => {
133139
let message = "No result from baml".to_string();
134-
error_callback_fn(id, true, message.as_ptr() as *const i8, message.len());
140+
error_callback_fn(id, 1, message.as_ptr() as *const i8, message.len());
135141
}
136142
},
137143
Err(e) => {
138144
let message = format!("Error: {}", e);
139-
error_callback_fn(id, true, message.as_ptr() as *const i8, message.len());
145+
error_callback_fn(id, 1, message.as_ptr() as *const i8, message.len());
140146
}
141147
}
142148
}

engine/language_client_go/baml_go/exports.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
#cgo CFLAGS: -O3 -g
1111
#include <baml_cffi_wrapper.h>
1212
#include <stdlib.h>
13-
#include <stdbool.h>
1413
#include <stdint.h>
1514
#include <string.h>
1615
*/

engine/language_client_go/baml_go/lib.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
#include <dlfcn.h>
2727
#include <baml_cffi_wrapper.h>
2828
#include <stdlib.h>
29-
#include <stdbool.h>
3029
#include <string.h>
3130
#include <stdint.h>
3231
#include <string.h>

engine/language_client_go/include/baml_cffi_generated.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
#include <stdint.h>
1212
#include <stdlib.h>
1313

14-
typedef void (*CallbackFn)(uint32_t call_id, bool is_done, const int8_t *content, uintptr_t length);
14+
typedef void (*CallbackFn)(uint32_t call_id,
15+
int32_t is_done,
16+
const int8_t *content,
17+
uintptr_t length);
1518

1619
const char *version(void);
1720

engine/language_client_go/pkg/callbacks.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package baml
22

33
/*
44
#include <stdlib.h>
5-
#include <stdbool.h>
65
#include <stdint.h>
76
*/
87
import "C"
@@ -59,7 +58,7 @@ func SetTypeMap(t TypeMap) {
5958
}
6059

6160
//export error_callback
62-
func error_callback(id C.uint32_t, isDone C.bool, content *C.int8_t, length C.int) {
61+
func error_callback(id C.uint32_t, isDone C.int, content *C.int8_t, length C.int) {
6362
fmt.Println("Error callback")
6463
callbackMutex.RLock()
6564
id_uint := uint32(id)
@@ -86,7 +85,7 @@ func error_callback(id C.uint32_t, isDone C.bool, content *C.int8_t, length C.in
8685
}
8786

8887
//export trigger_callback
89-
func trigger_callback(id C.uint32_t, isDone C.bool, content *C.int8_t, length C.int) {
88+
func trigger_callback(id C.uint32_t, isDone C.int, content *C.int8_t, length C.int) {
9089
callbackMutex.RLock()
9190
id_uint := uint32(id)
9291
callback, exists := dynamicCallbacks[id_uint]
@@ -100,7 +99,7 @@ func trigger_callback(id C.uint32_t, isDone C.bool, content *C.int8_t, length C.
10099
decoded_data := Decode(&parsed_data)
101100

102101
var res ResultCallback
103-
if isDone {
102+
if isDone == 1 {
104103
res = ResultCallback{HasData: true, Data: &decoded_data}
105104
} else {
106105
res = ResultCallback{HasStreamData: true, StreamData: &decoded_data}
@@ -117,7 +116,7 @@ func trigger_callback(id C.uint32_t, isDone C.bool, content *C.int8_t, length C.
117116
break
118117
}
119118

120-
if bool(isDone) || force_close {
119+
if isDone == 1 || force_close {
121120
close(callback.channel)
122121
callbackMutex.Lock()
123122
defer callbackMutex.Unlock()

engine/language_client_go/pkg/runtime.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package baml
22

33
/*
44
#include <stdlib.h>
5-
#include <stdbool.h>
65
7-
extern void trigger_callback(uint32_t, bool, const int8_t *, int);
8-
extern void error_callback(uint32_t, bool, const int8_t *, int);
6+
extern void trigger_callback(uint32_t, int, const int8_t *, int);
7+
extern void error_callback(uint32_t, int, const int8_t *, int);
98
*/
109
import "C"
1110

0 commit comments

Comments
 (0)