1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-12 02:27:46 -05:00
denoland-deno/serde_v8/magic/any_value.rs
Bartek Iwańczuk dda0f1c343
refactor(serde_v8): split ZeroCopyBuf into JsBuffer and ToJsBuffer (#19566)
`ZeroCopyBuf` was convenient to use, but sometimes it did hide details
that some copies were necessary in certain cases. Also it made it way to easy
for the caller to pass around and convert into different values. This commit
splits `ZeroCopyBuf` into `JsBuffer` (an array buffer coming from V8) and
`ToJsBuffer` (a Rust buffer that will be converted into a V8 array buffer).

As a result some magical conversions were removed (they were never used)
limiting the API surface and preparing for changes in #19534.
2023-06-22 23:37:56 +02:00

69 lines
1.9 KiB
Rust

use num_bigint::BigInt;
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use super::buffer::JsBuffer;
use super::transl8::FromV8;
use super::transl8::ToV8;
use crate::magic::transl8::impl_magic;
use crate::Error;
use crate::ToJsBuffer;
/// An untagged enum type that can be any of number, string, bool, bigint, or
/// buffer.
#[derive(Debug)]
pub enum AnyValue {
RustBuffer(ToJsBuffer),
V8Buffer(JsBuffer),
String(String),
Number(f64),
BigInt(BigInt),
Bool(bool),
}
impl_magic!(AnyValue);
impl ToV8 for AnyValue {
fn to_v8<'a>(
&mut self,
scope: &mut v8::HandleScope<'a>,
) -> Result<v8::Local<'a, v8::Value>, crate::Error> {
match self {
Self::RustBuffer(buf) => crate::to_v8(scope, buf),
Self::V8Buffer(_) => unreachable!(),
Self::String(s) => crate::to_v8(scope, s),
Self::Number(num) => crate::to_v8(scope, num),
Self::BigInt(bigint) => {
crate::to_v8(scope, crate::BigInt::from(bigint.clone()))
}
Self::Bool(b) => crate::to_v8(scope, b),
}
}
}
impl FromV8 for AnyValue {
fn from_v8(
scope: &mut v8::HandleScope,
value: v8::Local<v8::Value>,
) -> Result<Self, crate::Error> {
if value.is_string() {
let string = crate::from_v8(scope, value)?;
Ok(AnyValue::String(string))
} else if value.is_number() {
let string = crate::from_v8(scope, value)?;
Ok(AnyValue::Number(string))
} else if value.is_big_int() {
let bigint = crate::BigInt::from_v8(scope, value)?;
Ok(AnyValue::BigInt(bigint.into()))
} else if value.is_array_buffer_view() {
let buf = JsBuffer::from_v8(scope, value)?;
Ok(AnyValue::V8Buffer(buf))
} else if value.is_boolean() {
let string = crate::from_v8(scope, value)?;
Ok(AnyValue::Bool(string))
} else {
Err(Error::Message(
"expected string, number, bigint, ArrayBufferView, boolean".into(),
))
}
}
}