Skip to content

Commit d9bda99

Browse files
SersemPecaalehander92
authored andcommitted
fix: Fix tests and apply some stylistic code changes
1 parent c060ba2 commit d9bda99

File tree

7 files changed

+167
-211
lines changed

7 files changed

+167
-211
lines changed

src/db-backend/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ required-features = ["io-transport"]
7474
[features]
7575
debugging = []
7676

77-
default = ["browser-transport"]
77+
default = ["io-transport"]
7878

7979
io-transport = ["dep:expanduser"]
8080
browser-transport = ["dep:serde-wasm-bindgen", "dep:wasm-bindgen",

src/db-backend/src/c_compat.rs

Lines changed: 10 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
#[cfg(feature = "browser-transport")]
21
extern crate alloc;
32

43
use alloc::alloc::{alloc as sys_alloc, dealloc as sys_dealloc, realloc as sys_realloc, Layout};
54
use core::ffi::{c_char, c_int, c_void};
65
use core::mem::{align_of, size_of};
76
use core::ptr::null_mut;
8-
use std::ffi::CStr;
97

10-
#[cfg(feature = "browser-transport")]
118
#[global_allocator]
129
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
1310

@@ -134,23 +131,17 @@ pub extern "C" fn calloc(nmemb: usize, size: usize) -> *mut c_void {
134131

135132
#[no_mangle]
136133
pub extern "C" fn fprintf(_stream: *mut c_void, _fmt: *const c_char, _arg: *const c_void) -> c_int {
137-
// Pretend 0 chars written.
138-
0
134+
unreachable!("Running in wasm mode. Should not be calling `fprintf`");
139135
}
140136

141137
#[no_mangle]
142138
pub extern "C" fn fclose(_stream: *mut c_void) -> c_int {
143-
0
139+
unreachable!("Running in wasm mode. Should not be calling `fclose`");
144140
}
145141

146142
#[no_mangle]
147143
pub extern "C" fn snprintf(buf: *mut c_char, n: usize, _fmt: *const c_char, _arg: usize) -> c_int {
148-
unsafe {
149-
if !buf.is_null() && n > 0 {
150-
*buf = 0;
151-
}
152-
}
153-
0
144+
unreachable!("Running in wasm mode. Should not be calling `sprintf`");
154145
}
155146

156147
// Dummy va_list type for signature compatibility
@@ -161,74 +152,26 @@ pub struct va_list__dummy {
161152

162153
#[no_mangle]
163154
pub extern "C" fn vsnprintf(buf: *mut c_char, n: usize, _fmt: *const c_char, _ap: *mut va_list__dummy) -> c_int {
164-
unsafe {
165-
if !buf.is_null() && n > 0 {
166-
*buf = 0;
167-
}
168-
}
169-
0
155+
unreachable!("Running in wasm mode. Should not be calling `vsprintf`");
170156
}
171157

172158
#[no_mangle]
173159
pub extern "C" fn abort() -> ! {
174-
#[cfg(feature = "browser-transport")]
175160
wasm_bindgen::throw_str("abort");
176161
}
177162

178163
#[inline]
179164
unsafe fn cstr_prefix_len(mut p: *const u8, limit: usize) -> usize {
180-
// Count up to first NUL or `limit` bytes.
181-
let mut i = 0usize;
182-
while i < limit {
183-
let b = *p;
184-
if b == 0 {
185-
break;
186-
}
187-
i += 1;
188-
p = p.add(1);
189-
}
190-
i
165+
unreachable!("Running in wasm mode. Should not be calling `vsprintf`");
191166
}
192167

193168
#[no_mangle]
194169
pub extern "C" fn strncmp(s1: *const c_char, s2: *const c_char, n: usize) -> c_int {
195-
if n == 0 {
196-
return 0;
197-
}
198-
if s1.is_null() || s2.is_null() {
199-
return 0;
200-
} // benign stub behavior
201-
202-
// SAFETY: caller promises valid C strings; we only touch up to `n` bytes or NUL.
203-
let a = s1 as *const u8;
204-
let b = s2 as *const u8;
205-
let len = unsafe {
206-
let l1 = cstr_prefix_len(a, n);
207-
let l2 = cstr_prefix_len(b, n);
208-
core::cmp::min(core::cmp::min(l1, l2), n)
209-
};
210-
211-
// Compare common prefix (unsigned char semantics)
212-
for i in 0..len {
213-
let av = unsafe { *a.add(i) } as u8;
214-
let bv = unsafe { *b.add(i) } as u8;
215-
if av != bv {
216-
return (av as c_int) - (bv as c_int);
217-
}
218-
}
219-
220-
// If we stopped early (before n), one string may have ended (NUL) earlier.
221-
if len < n {
222-
let av = unsafe { *a.add(len) } as u8; // 0 if NUL, else next byte
223-
let bv = unsafe { *b.add(len) } as u8;
224-
return (av as c_int) - (bv as c_int);
225-
}
226-
0
170+
unreachable!("Running in wasm mode. Should not be calling `vsprintf`");
227171
}
228172

229173
#[no_mangle]
230174
pub extern "C" fn clock() -> c_int {
231-
#[cfg(feature = "browser-transport")]
232175
{
233176
// js_sys::Date::now() -> f64 milliseconds since epoch
234177

@@ -240,41 +183,22 @@ pub extern "C" fn clock() -> c_int {
240183

241184
#[inline]
242185
fn write_host(_s: &str, _stream: *mut c_void) {
243-
// no-op
186+
unreachable!("Running in wasm mode. Should not be calling `vsprintf`");
244187
}
245188

246189
/// int fputc(int c, FILE *stream)
247190
#[no_mangle]
248191
pub extern "C" fn fputc(c: c_int, stream: *mut c_void) -> c_int {
249-
let ch = (c as u8) as char;
250-
let mut buf = [0u8; 4];
251-
let s = ch.encode_utf8(&mut buf);
252-
write_host(s, stream);
253-
// C fputc returns the written character cast to unsigned char as int
254-
(c as u8) as c_int
192+
unreachable!("Running in wasm mode. Should not be calling `vsprintf`");
255193
}
256194

257195
/// int fputs(const char *s, FILE *stream)
258196
#[no_mangle]
259197
pub extern "C" fn fputs(s: *const c_char, stream: *mut c_void) -> c_int {
260-
if s.is_null() {
261-
return -1;
262-
}
263-
// SAFETY: caller promises valid NUL-terminated string
264-
let bytes = unsafe { CStr::from_ptr(s) }.to_bytes();
265-
let text = core::str::from_utf8(bytes).unwrap_or("<invalid utf-8>");
266-
write_host(text, stream);
267-
// C fputs returns a nonnegative value on success (commonly a non-portable value, so we return len)
268-
bytes.len() as c_int
198+
unreachable!("Running in wasm mode. Should not be calling `vsprintf`");
269199
}
270200

271201
#[no_mangle]
272202
pub extern "C" fn fdopen(fd: c_int, _mode: *const c_char) -> *mut c_void {
273-
// Provide distinct, non-null sentinel handles for stdin/stdout/stderr (0/1/2).
274-
// We never dereference these; other stubs ignore `stream` and treat any non-null as valid.
275-
if (0..=2).contains(&fd) {
276-
(fd as usize + 1) as *mut c_void
277-
} else {
278-
null_mut()
279-
}
203+
unreachable!("Running in wasm mode. Should not be calling `vsprintf`");
280204
}

src/db-backend/src/dap.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
use crate::dap_types::{self, OutputEventBody, SetBreakpointsArguments, StoppedEventBody};
22
use crate::task::{self, CtUpdatedTableResponseBody};
3-
use log::{error, info};
3+
use crate::transport::DapResult;
44
use serde::{de::DeserializeOwned, de::Error as SerdeError, Deserialize, Serialize};
55
use serde_json::Value;
6-
use std::io::BufRead;
76
use std::path::PathBuf;
87

9-
type DapResult<T> = std::result::Result<T, DapError>;
10-
11-
use crate::dap_error::DapError;
12-
138
#[derive(Serialize, Deserialize, Default, Debug, PartialEq, Clone)]
149
pub struct ProtocolMessage {
1510
pub seq: i64,
@@ -368,10 +363,14 @@ pub fn to_json(message: &DapMessage) -> DapResult<String> {
368363
}
369364

370365
#[cfg(feature = "io-transport")]
371-
pub fn read_dap_message_from_reader<R: BufRead>(reader: &mut R) -> DapResult<DapMessage> {
366+
pub fn read_dap_message_from_reader<R: std::io::BufRead>(reader: &mut R) -> DapResult<DapMessage> {
367+
use log::info;
368+
372369
info!("from_reader");
373370
let mut header = String::new();
374371
reader.read_line(&mut header).map_err(|e| {
372+
use log::error;
373+
375374
error!("Read Line: {:?}", e);
376375
serde_json::Error::custom(e.to_string())
377376
})?;

0 commit comments

Comments
 (0)