#[js(value)]

Use #[js(value)] to represent arbitrary JavaScript values as types in Rust's type system.

use ferrosaur::js;
#[js(value)]
struct Lorem;

The derived types are not intended to be instantiated directly. Instead, you can return them from APIs that you declare on js(global_this), a js(module), or another js(value). To declare APIs, use js(interface).

Illustrative example: The To-do List
use ferrosaur::js;

#[js(module("../examples/js/mod.js"))]
struct Module;

#[js(interface)]
impl Module {
    #[js(prop)]
    fn todos(&self) -> TodoList {}
}

#[js(value)]
struct TodoList;

#[js(interface)]
impl TodoList {
    #[js(func)]
    fn create(&self) -> Todo {}
}

#[js(value)]
struct Todo;

#[js(interface)]
impl Todo {
    #[js(prop(with_setter))]
    fn done(&self) -> bool {}
}

tip

Types derived with js(value), js(module), and js(global_this) are essentially newtypes around V8 types.

Sections

Option of_type(T)

By default, js(value) generates a struct that is:

use deno_core::v8;
struct Lorem(v8::Global<v8::Value>);
//                          ^ inner type

By using the of_type option, you can use some other V8 data types for the inner type. For example:

use ferrosaur::js;
#[js(value(of_type(v8::Promise)))]
struct Response;
// struct Response(v8::Global<v8::Promise>);

It should make sense for the data type T to be placed in a v8::Global. In particular, this means v8::Local<v8::Value> implements TryInto<v8::Local<T>>.

This could be useful if you want to have simple runtime type checking for your types. For example, given the Response type above, if a JS function is supposed to return a Response, i.e. a Promise, but it returns undefined, then the corresponding Rust function returns Err(...) instead of Ok(Response).

Note that this is "type checking" only in so far as v8 can try-convert between different V8 types; this is not TypeScript-style structural typing.

note

See Specifying types for more info on how you can specify types when using this crate.

Derived APIs

In the signatures below,

  • Type is the type that you apply js(value) to;
  • <T> is the one of the v8::* data types. By default, this is v8::Value, but you can control it using the of_type option.

Trait implementations

impl AsRef<v8::Global<T>> for Type

impl From<v8::Global<T>> for Type

impl From<Type> for v8::Global<T>

impl<'a> FromV8<'a> for Type

impl<'a> ToV8<'a> for Type

impl<'a> ToV8<'a> for &'_ Type