Skip to content

Commit

Permalink
Adds no_std support for some sub-crates
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Jan 19, 2023
1 parent 20af6a7 commit 3763f24
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 41 deletions.
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions boa_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ repository.workspace = true
rust-version.workspace = true

[features]
serde = ["boa_interner/serde", "dep:serde"]
serde = ["dep:serde", "boa_interner/serde"]

fuzz = ["arbitrary", "boa_interner/fuzz", "num-bigint/arbitrary"]
arbitrary = ["dep:arbitrary", "boa_interner/arbitrary", "num-bigint/arbitrary"]

[dependencies]
boa_interner.workspace = true
boa_macros.workspace = true
rustc-hash = "1.1.0"
serde = { version = "1.0.152", features = ["derive"], optional = true }
bitflags = "1.3.2"
num-bigint = "0.4.3"
arbitrary = { version = "1", optional = true, features = ["derive"] }
serde = { version = "1.0.152", features = ["derive"], optional = true }
arbitrary = { version = "1", features = ["derive"], optional = true }
2 changes: 1 addition & 1 deletion boa_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ intl = [
"dep:sys-locale",
]

fuzz = ["boa_ast/fuzz", "boa_interner/fuzz"]
fuzz = ["boa_ast/arbitrary", "boa_interner/arbitrary"]

# Enable Boa's VM instruction flowgraph generator.
flowgraph = []
Expand Down
8 changes: 5 additions & 3 deletions boa_icu_provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "boa_icu_provider"
description = "ICU4X data provider for the Boa JavaScript engine."
keywords = ["javascript", "cldr", "unicode"]
categories = ["internationalization"]
categories = ["internationalization", "no-std"]
version.workspace = true
edition.workspace = true
authors.workspace = true
Expand All @@ -15,13 +15,15 @@ rust-version.workspace = true
[dependencies]
icu_provider = { version = "1.0.1", features = ["serde", "sync"] }
icu_provider_blob = "1.0.0"
icu_provider_adapters = { version = "1.0.0", features = ["serde"]}
icu_provider_adapters = { version = "1.0.0", features = ["serde"] }
once_cell = {version = "1.17.0", default-features = false, features = ["critical-section"]}
icu_datagen = { version = "1.0.2", optional = true }
log = { version = "0.4.17", optional = true }
simple_logger = { version = "4.0.0", optional = true }
once_cell = "1.17.0"

[features]
default = ["std"]
std = ["once_cell/std"]
bin = ["dep:icu_datagen", "dep:simple_logger", "dep:log"]

[[bin]]
Expand Down
2 changes: 2 additions & 0 deletions boa_icu_provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@
clippy::pedantic,
clippy::nursery,
)]
#![cfg_attr(not(feature = "bin"), no_std)]

/// Gets the path to the directory where the generated data is stored.
#[cfg(feature = "bin")]
#[must_use]
#[doc(hidden)]
pub fn data_root() -> std::path::PathBuf {
Expand Down
16 changes: 9 additions & 7 deletions boa_interner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "boa_interner"
description = "String interner for the Boa JavaScript engine."
keywords = ["javascript", "js", "string", "interner"]
categories = ["data-structures"]
categories = ["data-structures", "no-std"]
version.workspace = true
edition.workspace = true
authors.workspace = true
Expand All @@ -11,14 +11,16 @@ repository.workspace = true
rust-version.workspace = true

[features]
fuzz = ["arbitrary"]
default = ["std"]
std = ["once_cell/std"]

[dependencies]
boa_macros.workspace = true
serde = { version = "1.0.152", features = ["derive"], optional = true }
phf = { version = "0.11.1", features = ["macros"] }
rustc-hash = "1.1.0"
phf = { version = "0.11.1", default-features = false, features = ["macros"] }
rustc-hash = { version = "1.1.0", default-features = false }
static_assertions = "1.1.0"
once_cell = "1.17.0"
once_cell = {version = "1.17.0", default-features = false, features = ["critical-section"]}
indexmap = "1.9.2"
arbitrary = { version = "1", optional = true, features = ["derive"] }
serde = { version = "1.0.152", features = ["derive"], optional = true }
arbitrary = { version = "1", features = ["derive"], optional = true }
hashbrown = { version = "0.13.2", default-features = false, features = ["inline-more"] }
2 changes: 2 additions & 0 deletions boa_interner/src/fixed_string.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

use crate::interned_str::InternedStr;

#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions boa_interner/src/interned_str.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{hash::Hash, ptr::NonNull};
use core::{hash::Hash, ptr::NonNull};

/// Wrapper for an interned str pointer, required to
/// quickly check using a hash if a string is inside an [`Interner`][`super::Interner`].
Expand Down Expand Up @@ -69,7 +69,7 @@ impl<Char> Hash for InternedStr<Char>
where
Char: Hash,
{
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
// SAFETY:
// The caller must ensure `ptr` is still valid throughout the
// lifetime of `self`.
Expand Down
13 changes: 8 additions & 5 deletions boa_interner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
// TODO deny once false positive is fixed (https://github.com/rust-lang/rust-clippy/issues/9626).
clippy::trait_duplication_in_bounds
)]
#![cfg_attr(not(feature = "arbitrary"), no_std)]

extern crate alloc;

extern crate static_assertions as sa;

Expand All @@ -100,8 +103,8 @@ mod sym;
#[cfg(test)]
mod tests;

use alloc::{borrow::Cow, format, string::String};
use raw::RawInterner;
use std::borrow::Cow;

pub use sym::*;

Expand Down Expand Up @@ -213,10 +216,10 @@ impl<'a, 'b> JSInternedStrRef<'a, 'b> {
}
}

impl std::fmt::Display for JSInternedStrRef<'_, '_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for JSInternedStrRef<'_, '_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.join_with_context(
std::fmt::Display::fmt,
core::fmt::Display::fmt,
|js, f| {
char::decode_utf16(js.iter().copied())
.map(|r| match r {
Expand Down Expand Up @@ -395,7 +398,7 @@ impl Interner {
// We only manipulate valid UTF-8 `str`s and convert them to `[u8]` for convenience,
// so converting back to a `str` is safe.
let utf8 = unsafe {
std::str::from_utf8_unchecked(
core::str::from_utf8_unchecked(
self.utf8_interner
.index(index)
.expect("both interners must have the same size"),
Expand Down
16 changes: 10 additions & 6 deletions boa_interner/src/raw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::{fixed_string::FixedString, interned_str::InternedStr};
use rustc_hash::FxHashMap;
use std::hash::Hash;
use alloc::vec::Vec;
use core::hash::{BuildHasherDefault, Hash};
use hashbrown::HashMap;
use rustc_hash::FxHasher;

type Map<T, U> = HashMap<T, U, BuildHasherDefault<FxHasher>>;

/// Raw string interner, generic by a char type.
#[derive(Debug)]
Expand All @@ -17,7 +21,7 @@ pub(super) struct RawInterner<Char> {
// please check out https://github.com/Robbepop/string-interner/pull/47 first.
// This doesn't implement that method, since implementing it increases
// our memory footprint.
symbol_cache: FxHashMap<InternedStr<Char>, usize>,
symbol_cache: Map<InternedStr<Char>, usize>,
spans: Vec<InternedStr<Char>>,
head: FixedString<Char>,
full: Vec<FixedString<Char>>,
Expand All @@ -26,7 +30,7 @@ pub(super) struct RawInterner<Char> {
impl<Char> Default for RawInterner<Char> {
fn default() -> Self {
Self {
symbol_cache: FxHashMap::default(),
symbol_cache: Map::default(),
spans: Vec::default(),
head: FixedString::default(),
full: Vec::default(),
Expand All @@ -38,7 +42,7 @@ impl<Char> RawInterner<Char> {
/// Creates a new `RawInterner` with the specified capacity.
pub(super) fn with_capacity(capacity: usize) -> Self {
Self {
symbol_cache: FxHashMap::default(),
symbol_cache: Map::default(),
spans: Vec::with_capacity(capacity),
head: FixedString::new(capacity),
full: Vec::new(),
Expand Down Expand Up @@ -167,7 +171,7 @@ where
let new_cap =
(usize::max(self.head.capacity(), string.len()) + 1).next_power_of_two();
let new_head = FixedString::new(new_cap);
let old_head = std::mem::replace(&mut self.head, new_head);
let old_head = core::mem::replace(&mut self.head, new_head);

// If the user creates an `Interner`
// with `Interner::with_capacity(BIG_NUMBER)` and
Expand Down
21 changes: 12 additions & 9 deletions boa_interner/src/sym.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
use boa_macros::utf16;
use core::{hash::BuildHasherDefault, num::NonZeroUsize};
use indexmap::IndexSet;
use once_cell::sync::Lazy;
use std::num::NonZeroUsize;
use rustc_hash::FxHasher;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
type Set<T> = IndexSet<T, BuildHasherDefault<FxHasher>>;

/// The string symbol type for Boa.
///
/// This symbol type is internally a `NonZeroUsize`, which makes it pointer-width in size and it's
/// optimized so that it can occupy 1 pointer width even in an `Option` type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Sym {
value: NonZeroUsize,
}
Expand Down Expand Up @@ -162,8 +165,8 @@ macro_rules! create_static_strings {
/// `COMMON_STRINGS_UTF8`, `COMMON_STRINGS_UTF16` and the constants
/// defined in [`Sym`] must always be in sync.
// FIXME: use phf when const expressions are allowed. https://github.com/rust-phf/rust-phf/issues/188
pub(super) static COMMON_STRINGS_UTF16: Lazy<IndexSet<&'static [u16]>> = Lazy::new(|| {
let mut set = IndexSet::with_capacity(COMMON_STRINGS_UTF8.len());
pub(super) static COMMON_STRINGS_UTF16: Lazy<Set<&'static [u16]>> = Lazy::new(|| {
let mut set = Set::with_capacity_and_hasher(COMMON_STRINGS_UTF8.len(), BuildHasherDefault::default());
$( set.insert(utf16!($s)); )+
set
});
Expand Down
2 changes: 1 addition & 1 deletion boa_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn derive_trace(mut s: Structure<'_>) -> proc_macro2::TokenStream {
// type and encourage people to use Finalize. This implementation will
// call `Finalize::finalize` if it is safe to do so.
let drop_impl = s.unbound_impl(
quote!(::std::ops::Drop),
quote!(::core::ops::Drop),
quote! {
fn drop(&mut self) {
if ::boa_gc::finalizer_safe() {
Expand Down
3 changes: 2 additions & 1 deletion boa_profiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@
clippy::pedantic,
clippy::nursery,
)]
#![cfg_attr(not(feature = "profiler"), no_std)]

use std::fmt::{self, Debug};
use core::fmt::{self, Debug};

#[cfg(feature = "profiler")]
use measureme::{EventId, Profiler as MeasuremeProfiler, StringId, TimingGuard};
Expand Down
2 changes: 1 addition & 1 deletion boa_unicode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "boa_unicode"
description = "Unicode support for the Boa JavaScript engine."
keywords = ["javascript", "compiler", "lexer", "parser", "unicode"]
categories = ["parsing"]
categories = ["parsing", "no-std"]
version.workspace = true
edition.workspace = true
authors.workspace = true
Expand Down
1 change: 1 addition & 0 deletions boa_unicode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
clippy::nursery,
)]
#![allow(clippy::redundant_pub_crate)]
#![no_std]

mod tables;
#[cfg(test)]
Expand Down

0 comments on commit 3763f24

Please sign in to comment.