Skip to main content

quickjs_jit_stdlib/llrt/llrt_utils/
clone.rs

1// Vendored from LLRT d9fd873e793c (libs/llrt_utils/src/clone.rs); module paths and
2// feature names adapted by scripts/import-stdlib.py.
3// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4// SPDX-License-Identifier: Apache-2.0
5use rquickjs::{class::JsClass, Class, Ctx, Object, Result, Value};
6
7pub trait StructuredClone<'js>: JsClass<'js> {
8    fn structured_clone(&self, ctx: &Ctx<'js>) -> Result<Value<'js>>;
9}
10
11pub fn clone_platform_object<'js, T>(
12    ctx: &Ctx<'js>,
13    object: &Object<'js>,
14) -> Result<Option<Value<'js>>>
15where
16    T: StructuredClone<'js>,
17{
18    if let Some(class) = Class::<T>::from_object(object) {
19        return Ok(Some(class.borrow().structured_clone(ctx)?));
20    }
21    Ok(None)
22}