Skip to content

Commit 237e542

Browse files
committed
feat(allocator): add no_std support
1 parent f437a95 commit 237e542

15 files changed

+40
-36
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ jobs:
4646
cache-key: warm
4747
- run: cargo ck
4848
- run: cargo test --all-features
49+
- run: cargo test -p oxc_allocator --no-default-features --features serialize
4950
- run: git diff --exit-code # Must commit everything
5051

5152
test-windows:
@@ -132,7 +133,6 @@ jobs:
132133
rustup target add wasm32-unknown-unknown
133134
cargo check -p oxc_wasm --target wasm32-unknown-unknown
134135
- uses: ./.github/actions/pnpm
135-
136136
- run: just build-wasm debug
137137
- working-directory: npm/oxc-wasm
138138
run: pnpm run check

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ oxc_resolver = "5"
157157
oxc_sourcemap = "3"
158158

159159
#
160-
allocator-api2 = "0.2.21"
160+
allocator-api2 = { version = "0.2.21", default-features = false, features = ["alloc"] }
161161
assert-unchecked = "0.1.2"
162162
base64 = "0.22.1"
163163
bitflags = "2.8.0"
@@ -218,7 +218,7 @@ self_cell = "1.1.0"
218218
seq-macro = "0.3.5"
219219
serde-wasm-bindgen = "0.6.5"
220220
sha1 = "0.10.6"
221-
simdutf8 = { version = "0.1.5", features = ["aarch64_neon"] }
221+
simdutf8 = { version = "0.1.5", default-features = false, features = ["aarch64_neon"] }
222222
similar = "2.7.0"
223223
similar-asserts = "1.6.1"
224224
smallvec = "1.14.0"

crates/oxc_allocator/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ workspace = true
1919
doctest = false
2020

2121
[dependencies]
22-
oxc_estree = { workspace = true, optional = true }
23-
2422
allocator-api2 = { workspace = true }
2523
assert-unchecked = { workspace = true }
2624
bumpalo = { workspace = true, features = ["allocator-api2", "collections"] }
@@ -33,7 +31,10 @@ serde = { workspace = true, optional = true }
3331
[dev-dependencies]
3432
serde = { workspace = true }
3533
serde_json = { workspace = true }
34+
oxc_estree = { workspace = true }
3635

3736
[features]
37+
default = ["std"]
38+
std = ["simdutf8/std"]
3839
from_raw_parts = []
3940
serialize = ["dep:serde", "oxc_estree/serialize"]

crates/oxc_allocator/src/address.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::ptr;
2-
31
use crate::Box;
42

53
/// Memory address of an AST node in arena.
@@ -40,7 +38,7 @@ impl<T> GetAddress for Box<'_, T> {
4038
/// so this address acts as a unique identifier for the duration of the arena's existence.
4139
#[inline]
4240
fn address(&self) -> Address {
43-
Address::from_ptr(ptr::addr_of!(**self))
41+
Address::from_ptr(&raw const (**self))
4442
}
4543
}
4644

crates/oxc_allocator/src/allocator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl Allocator {
262262
#[expect(clippy::inline_always)]
263263
#[inline(always)]
264264
pub fn alloc<T>(&self, val: T) -> &mut T {
265-
const { assert!(!std::mem::needs_drop::<T>(), "Cannot allocate Drop type in arena") };
265+
const { assert!(!core::mem::needs_drop::<T>(), "Cannot allocate Drop type in arena") };
266266

267267
self.bump.alloc(val)
268268
}

crates/oxc_allocator/src/allocator_api2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// All have same safety preconditions of `bumpalo` methods of the same name.
33
#![expect(clippy::inline_always, clippy::undocumented_unsafe_blocks)]
44

5-
use std::{alloc::Layout, ptr::NonNull};
5+
use core::{alloc::Layout, ptr::NonNull};
66

77
use allocator_api2::alloc::{AllocError, Allocator};
88

crates/oxc_allocator/src/boxed.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
//!
33
//! Originally based on [jsparagus](https://github.com/mozilla-spidermonkey/jsparagus/blob/24004745a8ed4939fc0dc7332bfd1268ac52285f/crates/ast/src/arena.rs)
44
5-
use std::{
6-
self,
5+
use core::{
76
fmt::{self, Debug, Formatter},
87
hash::{Hash, Hasher},
98
marker::PhantomData,
109
ops::{self, Deref},
1110
ptr::{self, NonNull},
1211
};
1312

14-
#[cfg(any(feature = "serialize", test))]
13+
#[cfg(all(feature = "serialize", test))]
1514
use oxc_estree::{ESTree, Serializer as ESTreeSerializer};
1615
#[cfg(any(feature = "serialize", test))]
1716
use serde::{Serialize, Serializer as SerdeSerializer};
@@ -36,7 +35,7 @@ impl<T: ?Sized> Box<'_, T> {
3635
/// Const assertion that `T` is not `Drop`.
3736
/// Must be referenced in all methods which create a `Box`.
3837
const ASSERT_T_IS_NOT_DROP: () =
39-
assert!(!std::mem::needs_drop::<T>(), "Cannot create a Box<T> where T is a Drop type");
38+
assert!(!core::mem::needs_drop::<T>(), "Cannot create a Box<T> where T is a Drop type");
4039
}
4140

4241
impl<T> Box<'_, T> {
@@ -206,10 +205,11 @@ impl<T: Hash> Hash for Box<'_, T> {
206205

207206
#[cfg(test)]
208207
mod test {
209-
use std::hash::{DefaultHasher, Hash, Hasher};
210-
211208
use super::Box;
212209
use crate::Allocator;
210+
use alloc::format;
211+
use core::hash::{BuildHasher, Hash};
212+
use rustc_hash::FxBuildHasher;
213213

214214
#[test]
215215
fn box_deref_mut() {
@@ -231,9 +231,8 @@ mod test {
231231
#[test]
232232
fn box_hash() {
233233
fn hash(val: &impl Hash) -> u64 {
234-
let mut hasher = DefaultHasher::default();
235-
val.hash(&mut hasher);
236-
hasher.finish()
234+
let hasher = FxBuildHasher::default();
235+
hasher.hash_one(val)
237236
}
238237

239238
let allocator = Allocator::default();

crates/oxc_allocator/src/clone_in.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::cell::Cell;
1+
use core::cell::Cell;
22

33
use crate::{Allocator, Box, Vec};
44

crates/oxc_allocator/src/convert.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![expect(clippy::inline_always)]
22

3+
use alloc::string::String;
4+
35
use crate::{Allocator, Box};
46

57
/// This trait works similarly to the standard library [`From`] trait.

crates/oxc_allocator/src/from_raw_parts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Define additional [`Allocator::from_raw_parts`] method, used only by raw transfer.
22
3-
use std::{
3+
use core::{
44
alloc::Layout,
55
cell::Cell,
66
ptr::{self, NonNull},

crates/oxc_allocator/src/hash_map.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// All methods which just delegate to `hashbrown::HashMap` methods marked `#[inline(always)]`
88
#![expect(clippy::inline_always)]
99

10-
use std::{
10+
use core::{
1111
hash::Hash,
1212
mem::ManuallyDrop,
1313
ops::{Deref, DerefMut},
@@ -63,11 +63,11 @@ impl<'alloc, K, V> HashMap<'alloc, K, V> {
6363
/// Must be referenced in all methods which create a `HashMap`.
6464
const ASSERT_K_AND_V_ARE_NOT_DROP: () = {
6565
assert!(
66-
!std::mem::needs_drop::<K>(),
66+
!core::mem::needs_drop::<K>(),
6767
"Cannot create a HashMap<K, V> where K is a Drop type"
6868
);
6969
assert!(
70-
!std::mem::needs_drop::<V>(),
70+
!core::mem::needs_drop::<V>(),
7171
"Cannot create a HashMap<K, V> where V is a Drop type"
7272
);
7373
};

crates/oxc_allocator/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
//! * [`HashMap`]
1212
//!
1313
//! See [`Allocator`] docs for information on efficient use of [`Allocator`].
14-
14+
#![warn(clippy::alloc_instead_of_core, clippy::std_instead_of_alloc, clippy::std_instead_of_core)]
15+
#![cfg_attr(not(feature = "std"), no_std)]
1516
#![warn(missing_docs)]
1617

18+
extern crate alloc;
19+
1720
mod address;
1821
mod allocator;
1922
mod allocator_api2;

crates/oxc_allocator/src/string.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// All methods which just delegate to `bumpalo::collections::String` methods marked `#[inline(always)]`
66
#![expect(clippy::inline_always)]
77

8-
use std::{
8+
use core::{
99
fmt::{self, Debug, Display},
1010
hash::{Hash, Hasher},
1111
mem::ManuallyDrop,
@@ -336,8 +336,8 @@ macro_rules! impl_eq {
336336

337337
impl_eq! { String<'alloc>, str }
338338
impl_eq! { String<'alloc>, &'a str }
339-
impl_eq! { std::borrow::Cow<'a, str>, String<'alloc> }
340-
impl_eq! { std::string::String, String<'alloc> }
339+
impl_eq! { alloc::borrow::Cow<'a, str>, String<'alloc> }
340+
impl_eq! { alloc::string::String, String<'alloc> }
341341

342342
impl Display for String<'_> {
343343
#[inline]
@@ -381,7 +381,7 @@ mod test {
381381
#[test]
382382
fn string_from_array_len_3() {
383383
let hello = "hello";
384-
let world = std::string::String::from("world");
384+
let world = alloc::string::String::from("world");
385385
let allocator = Allocator::default();
386386
let string = String::from_strs_array_in([hello, &world, "!"], &allocator);
387387
assert_eq!(string, "helloworld!");

crates/oxc_allocator/src/vec.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
// All methods which just delegate to `allocator_api2::vec::Vec` methods marked `#[inline(always)]`
66
#![expect(clippy::inline_always)]
77

8-
use std::{
9-
self,
8+
use core::{
109
fmt::{self, Debug},
1110
hash::{Hash, Hasher},
1211
mem::ManuallyDrop,
@@ -17,7 +16,7 @@ use std::{
1716

1817
use allocator_api2::vec::Vec as InnerVec;
1918
use bumpalo::Bump;
20-
#[cfg(any(feature = "serialize", test))]
19+
#[cfg(all(feature = "serialize", test))]
2120
use oxc_estree::{ESTree, Serializer as ESTreeSerializer};
2221
#[cfg(any(feature = "serialize", test))]
2322
use serde::{Serialize, Serializer as SerdeSerializer};
@@ -48,7 +47,7 @@ impl<'alloc, T> Vec<'alloc, T> {
4847
/// Const assertion that `T` is not `Drop`.
4948
/// Must be referenced in all methods which create a `Vec`.
5049
const ASSERT_T_IS_NOT_DROP: () =
51-
assert!(!std::mem::needs_drop::<T>(), "Cannot create a Vec<T> where T is a Drop type");
50+
assert!(!core::mem::needs_drop::<T>(), "Cannot create a Vec<T> where T is a Drop type");
5251

5352
/// Constructs a new, empty `Vec<T>`.
5453
///
@@ -233,7 +232,7 @@ impl<'alloc, T> IntoIterator for Vec<'alloc, T> {
233232
}
234233

235234
impl<'i, T> IntoIterator for &'i Vec<'_, T> {
236-
type IntoIter = std::slice::Iter<'i, T>;
235+
type IntoIter = core::slice::Iter<'i, T>;
237236
type Item = &'i T;
238237

239238
#[inline(always)]
@@ -243,7 +242,7 @@ impl<'i, T> IntoIterator for &'i Vec<'_, T> {
243242
}
244243

245244
impl<'i, T> IntoIterator for &'i mut Vec<'_, T> {
246-
type IntoIter = std::slice::IterMut<'i, T>;
245+
type IntoIter = core::slice::IterMut<'i, T>;
247246
type Item = &'i mut T;
248247

249248
#[inline(always)]
@@ -304,6 +303,8 @@ impl<T: Debug> Debug for Vec<'_, T> {
304303

305304
#[cfg(test)]
306305
mod test {
306+
use alloc::format;
307+
307308
use super::Vec;
308309
use crate::{Allocator, Box};
309310

crates/oxc_linter/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ schemars = { workspace = true, features = ["indexmap2"] }
6060
self_cell = { workspace = true }
6161
serde = { workspace = true, features = ["derive"] }
6262
serde_json = { workspace = true }
63-
simdutf8 = { workspace = true }
63+
simdutf8 = { workspace = true, features = ["std"] }
6464
smallvec = { workspace = true }
6565

6666
[dev-dependencies]

0 commit comments

Comments
 (0)