Repository: awslabs/llrt
File: modules/llrt_buffer/src/buffer.rs (Rust)

Code before:
pub fn btoa(ctx: Ctx<'_>, value: Coerced<String>) -> Result<String> {
    // Per WHATWG spec, btoa() treats input as a "binary string" where each character
    // must have a code point 0-255. Characters > 255 cause InvalidCharacterError.
    let bytes: Vec<u8> = value
        .chars()
        .map(|c| {
            let code_point = c as u32;
            if code_point > 255 {
                Err(Exception::throw_message(
                    &ctx,
                    "Invalid character: btoa() argument contains character with code point > 255",
                ))
            } else {
                Ok(code_point as u8)
            }
        })
        .collect::<Result<Vec<u8>>>()?;
    Ok(bytes_to_b64_string(&bytes))
}

#[cfg(test)]
mod tests {
    use llrt_test::{call_test, test_async_with, ModuleEvaluator};

    use crate::BufferModule;

    #[tokio::test]
    async fn test_atob() {
        test_async_with(|ctx| {
            Box::pin(async move {
                crate::init(&ctx).unwrap();
                ModuleEvaluator::eval_rust::<BufferModule>(ctx.clone(), "buffer")
                    .await
                    .unwrap();

                let data = "aGVsbG8gd29ybGQ=".to_string();
                let module = ModuleEvaluator::eval_js(
                    ctx.clone(),
                    "test",
                    r#"
                        import { atob } from 'buffer';

                        export async function test(data) {
                            return atob(data);
                        }
                    "#,
                )
                .await
                .unwrap();
                let result = call_test::<String, _>(&ctx, &module, (data,)).await;
                assert_eq!(result, "hello world");

Review comment (performance):
Can we do something like this? Fast path of ascii. That's SIMD optimized.

```rust
fn btoa(ctx: Ctx<'_>, value: Coerced<String>) -> Result<String> {
    let s: &str = value.as_str();

    let bytes = if s.is_ascii() {
        // Fast path: ASCII is a 1:1 mapping to bytes 0-127
          return Ok(bytes_to_b64_string(s))
    } else {
        // Slow path: Check for Latin-1 (0-255)
        ...
    };

    Ok(bytes_to_b64_string(&bytes))
}
```

Code after:
pub fn btoa(ctx: Ctx<'_>, value: Coerced<String>) -> Result<String> {
    // Per WHATWG spec, btoa() treats input as a "binary string" where each character
    // must have a code point 0-255. Characters > 255 cause InvalidCharacterError.
    let s: &str = value.as_str();

    // Fast path: ASCII is a 1:1 mapping to bytes 0-127 (SIMD optimized)
    if s.is_ascii() {
        return Ok(bytes_to_b64_string(s.as_bytes()));
    }

    // Slow path: Check for Latin-1 (0-255)
    let bytes: Vec<u8> = s
        .chars()
        .map(|c| {
            let code_point = c as u32;
            if code_point > 255 {
                Err(Exception::throw_message(
                    &ctx,
                    "Invalid character: btoa() argument contains character with code point > 255",
                ))
            } else {
                Ok(code_point as u8)
            }
        })
        .collect::<Result<Vec<u8>>>()?;
    Ok(bytes_to_b64_string(&bytes))
}

#[cfg(test)]
mod tests {
    use llrt_test::{call_test, test_async_with, ModuleEvaluator};

    use crate::BufferModule;

    #[tokio::test]
    async fn test_atob() {
        test_async_with(|ctx| {
            Box::pin(async move {
                crate::init(&ctx).unwrap();
                ModuleEvaluator::eval_rust::<BufferModule>(ctx.clone(), "buffer")
                    .await
                    .unwrap();

                let data = "aGVsbG8gd29ybGQ=".to_string();
                let module = ModuleEvaluator::eval_js(
                    ctx.clone(),
                    "test",
                    r#"
                        import { atob } from 'buffer';

                        export async function test(data) {