Skip to content

Commit

Permalink
Fix compilation error on no_std+alloc environment.
Browse files Browse the repository at this point in the history
Also changed the definition of `CallError::stacktrace` function
  • Loading branch information
GrayJack committed Jun 2, 2021
1 parent 77b91d0 commit 4648995
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to the library should be put here
- **BREAKING:** `JanetAbstract::new` now takes a value
- **BREAKING:** Make the `janetrs::types` module private and export everything inside it in the upper module
- **BREAKING:** Modify `From<&str>` for `Janet` to return a Janet keyword if `&str` starts with `:`
- **BREAKING:** Modify `CallError::stacktrace` function.
- Add ability to change some Janet behavior using the `amalgation` feature using environment variables
- Add `DeepEq` trait
- Add `dedup`, `dedup_by` and `dedup_by_key` for `JanetArray`
Expand All @@ -27,6 +28,7 @@ All notable changes to the library should be put here
- Create `janetrs_version` crate to use as common code used by `janet_version` macro and `janetrs::util` module
- Expose `jcatch!` macro only if Janet version supports the underlying mechanism
- Fix some clippy lints
- Fix compilation on no_std environment.
- Implement `DeepEq` for most types
- Implement `Debug` and `Display` for `JanetSymbol`
- Implement `Debug` and `Display` for `JanetKeyword`
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "janetrs"
version = "0.2.0"
version = "0.3.0"
authors = ["Eric Shimizu Karbstein <[email protected]>"]
description = "High level binding for Janet programming language"
repository = "https://github.com/GrayJack/janetrs"
Expand All @@ -19,7 +19,7 @@ targets = [
"x86_64-unknown-freebsd",
"x86_64-unknown-netbsd",
"x86_64-apple-darwin",
"x86_64-sun-solaris",
"x86_64-unknown-illumos",
"aarch64-unknown-linux-gnu",
"i686-unknown-linux-gnu",
"x86_64-pc-windows-gnu",
Expand All @@ -40,7 +40,7 @@ janetrs_version = "0.1.0"
[dev-dependencies]

[features]
default = ["std", "unicode"]
# default = ["std", "unicode"]
# To use Error trait and alocations
std = ["bstr/std"]
# Use to statically link to janet runtime and have Janet client
Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ macro_rules! jpanic {
$crate::util::_panic($crate::Janet::from($msg));
};
($msg:expr, $($arg:tt)+) => {
$crate::util::_panic($crate::Janet::from(format!($msg, $($arg)+).as_str()));
$crate::util::_panic($crate::Janet::from(alloc::format!($msg, $($arg)+).as_str()));
};
}

Expand Down
26 changes: 23 additions & 3 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use core::{
fmt::{self, Display, Write},
};

use alloc::{string::String, vec::Vec};

#[cfg(feature = "std")]
use std::error;

Expand Down Expand Up @@ -562,16 +564,16 @@ impl fmt::Display for Janet {
TaggedJanet::String(s) => fmt::Display::fmt(&s, f),
TaggedJanet::Abstract(a) => {
// Special cases for integers
if let Ok(int) = dbg!(a.get::<i64>()) {
if let Ok(int) = a.get::<i64>() {
return fmt::Display::fmt(int, f);
}

if let Ok(int) = dbg!(a.get::<u64>()) {
if let Ok(int) = a.get::<u64>() {
return fmt::Display::fmt(int, f);
}

// If the abstract has tostring function
if let Some(tostring) = dbg!(a.type_info()).tostring {
if let Some(tostring) = a.type_info().tostring {
let buff = JanetBuffer::with_capacity(30);
unsafe { tostring(a.raw, buff.raw) }
return fmt::Display::fmt(&buff, f);
Expand Down Expand Up @@ -1480,7 +1482,9 @@ string_impl_partial_eq!(JanetString<'_>, str);
string_impl_partial_eq!(JanetString<'_>, &'a str);
string_impl_partial_eq!(JanetString<'_>, bstr::BStr);
string_impl_partial_eq!(JanetString<'_>, &'a bstr::BStr);
#[cfg(feature = "std")]
string_impl_partial_eq!(JanetString<'_>, bstr::BString);
#[cfg(feature = "std")]
string_impl_partial_eq!(JanetString<'_>, &'a bstr::BString);

string_impl_partial_ord!(JanetString<'_>, Vec<u8>);
Expand All @@ -1491,7 +1495,9 @@ string_impl_partial_ord!(JanetString<'_>, str);
string_impl_partial_ord!(JanetString<'_>, &'a str);
string_impl_partial_ord!(JanetString<'_>, bstr::BStr);
string_impl_partial_ord!(JanetString<'_>, &'a bstr::BStr);
#[cfg(feature = "std")]
string_impl_partial_ord!(JanetString<'_>, bstr::BString);
#[cfg(feature = "std")]
string_impl_partial_ord!(JanetString<'_>, &'a bstr::BString);

string_impl_partial_eq!(JanetBuffer<'_>, Vec<u8>);
Expand All @@ -1502,7 +1508,9 @@ string_impl_partial_eq!(JanetBuffer<'_>, str);
string_impl_partial_eq!(JanetBuffer<'_>, &'a str);
string_impl_partial_eq!(JanetBuffer<'_>, bstr::BStr);
string_impl_partial_eq!(JanetBuffer<'_>, &'a bstr::BStr);
#[cfg(feature = "std")]
string_impl_partial_eq!(JanetBuffer<'_>, bstr::BString);
#[cfg(feature = "std")]
string_impl_partial_eq!(JanetBuffer<'_>, &'a bstr::BString);

string_impl_partial_ord!(JanetBuffer<'_>, Vec<u8>);
Expand All @@ -1513,7 +1521,9 @@ string_impl_partial_ord!(JanetBuffer<'_>, str);
string_impl_partial_ord!(JanetBuffer<'_>, &'a str);
string_impl_partial_ord!(JanetBuffer<'_>, bstr::BStr);
string_impl_partial_ord!(JanetBuffer<'_>, &'a bstr::BStr);
#[cfg(feature = "std")]
string_impl_partial_ord!(JanetBuffer<'_>, bstr::BString);
#[cfg(feature = "std")]
string_impl_partial_ord!(JanetBuffer<'_>, &'a bstr::BString);

string_impl_partial_eq!(JanetSymbol<'_>, Vec<u8>);
Expand All @@ -1524,7 +1534,9 @@ string_impl_partial_eq!(JanetSymbol<'_>, str);
string_impl_partial_eq!(JanetSymbol<'_>, &'a str);
string_impl_partial_eq!(JanetSymbol<'_>, bstr::BStr);
string_impl_partial_eq!(JanetSymbol<'_>, &'a bstr::BStr);
#[cfg(feature = "std")]
string_impl_partial_eq!(JanetSymbol<'_>, bstr::BString);
#[cfg(feature = "std")]
string_impl_partial_eq!(JanetSymbol<'_>, &'a bstr::BString);

string_impl_partial_ord!(JanetSymbol<'_>, Vec<u8>);
Expand All @@ -1535,7 +1547,9 @@ string_impl_partial_ord!(JanetSymbol<'_>, str);
string_impl_partial_ord!(JanetSymbol<'_>, &'a str);
string_impl_partial_ord!(JanetSymbol<'_>, bstr::BStr);
string_impl_partial_ord!(JanetSymbol<'_>, &'a bstr::BStr);
#[cfg(feature = "std")]
string_impl_partial_ord!(JanetSymbol<'_>, bstr::BString);
#[cfg(feature = "std")]
string_impl_partial_ord!(JanetSymbol<'_>, &'a bstr::BString);

string_impl_partial_eq!(JanetKeyword<'_>, Vec<u8>);
Expand All @@ -1546,7 +1560,9 @@ string_impl_partial_eq!(JanetKeyword<'_>, str);
string_impl_partial_eq!(JanetKeyword<'_>, &'a str);
string_impl_partial_eq!(JanetKeyword<'_>, bstr::BStr);
string_impl_partial_eq!(JanetKeyword<'_>, &'a bstr::BStr);
#[cfg(feature = "std")]
string_impl_partial_eq!(JanetKeyword<'_>, bstr::BString);
#[cfg(feature = "std")]
string_impl_partial_eq!(JanetKeyword<'_>, &'a bstr::BString);

string_impl_partial_ord!(JanetKeyword<'_>, Vec<u8>);
Expand All @@ -1557,15 +1573,19 @@ string_impl_partial_ord!(JanetKeyword<'_>, str);
string_impl_partial_ord!(JanetKeyword<'_>, &'a str);
string_impl_partial_ord!(JanetKeyword<'_>, bstr::BStr);
string_impl_partial_ord!(JanetKeyword<'_>, &'a bstr::BStr);
#[cfg(feature = "std")]
string_impl_partial_ord!(JanetKeyword<'_>, bstr::BString);
#[cfg(feature = "std")]
string_impl_partial_ord!(JanetKeyword<'_>, &'a bstr::BString);

#[cfg(all(test, any(feature = "amalgation", feature = "link-system")))]
mod tests {
use super::*;
#[cfg(feature = "std")]
use core::hash::{Hash, Hasher};

#[cfg_attr(feature = "std", test)]
#[cfg(feature = "std")]
fn janet_eq_hash() -> Result<(), crate::client::Error> {
let _client = crate::client::JanetClient::init()?;

Expand Down
1 change: 1 addition & 0 deletions src/types/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2634,6 +2634,7 @@ impl FusedIterator for IntoIter<'_> {}
mod tests {
use super::*;
use crate::{array, client::JanetClient};
use alloc::vec;

#[test]
fn creation() -> Result<(), crate::client::Error> {
Expand Down
36 changes: 31 additions & 5 deletions src/types/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ use core::{
ptr,
};

#[cfg(not(feature = "std"))]
use core::fmt::Write;

#[cfg(feature = "std")]
use std::error;
use std::{
error,
io::{self, Write},
};

use const_fn::const_fn;

Expand Down Expand Up @@ -92,18 +98,38 @@ impl<'data> CallError<'data> {
self.fiber
}

/// Display the stacktrace in the Stderr
/// Display the stacktrace in the given `output`
#[cfg(feature = "std")]
#[inline]
pub fn stacktrace<W: Write + ?Sized>(&mut self, output: &mut W) -> io::Result<()> {
if let CallErrorKind::Run = self.kind {
if let Some(ref mut fiber) = self.fiber {
fiber.display_stacktrace(self.value);
} else {
writeln!(output, "There is no stacktrace.")?;
}
} else {
writeln!(output, "There is no stacktrace.")?;
}

Ok(())
}

/// Display the stacktrace in the given `output`
#[cfg(not(feature = "std"))]
#[inline]
pub fn stacktrace(&mut self) {
pub fn stacktrace<W: Write + ?Sized>(&mut self, output: &mut W) -> fmt::Result {
if let CallErrorKind::Run = self.kind {
if let Some(ref mut fiber) = self.fiber {
fiber.display_stacktrace(self.value);
} else {
eprintln!("There is no stacktrace.")
output.write_str("There is no stacktrace.\n")?;
}
} else {
eprintln!("There is no stacktrace.")
output.write_str("There is no stacktrace.\n")?;
}

Ok(())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,6 @@ impl Ord for JanetTuple<'_> {
x @ Greater => x,
Equal => {
for (s, o) in self.iter().zip(other.iter()) {
dbg!(s, o);
match s.cmp(o) {
x @ Less => return x,
x @ Greater => return x,
Expand Down Expand Up @@ -1275,6 +1274,7 @@ impl FusedIterator for IntoIter<'_> {}
mod tests {
use super::*;
use crate::{client::JanetClient, tuple, *};
use alloc::vec;

#[test]
fn builder() -> Result<(), crate::client::Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn c_func(
doc: Option<&str>,
) {
if let Some(prefix) = namespace {
let full_name = format!("{}/{}", prefix.trim(), fn_name.trim());
let full_name = alloc::format!("{}/{}", prefix.trim(), fn_name.trim());
let mut cfun = JanetTable::with_capacity(2);

cfun.insert(JanetKeyword::new("value"), f);
Expand Down

0 comments on commit 4648995

Please sign in to comment.