Giter Club home page Giter Club logo

serde_v8's Introduction

serde_v8

Author: Aaron O'Mullan [email protected]

Serde support for encoding/decoding (rusty_)v8 values.

Broadly serde_v8 aims to provide an expressive but ~maximally efficient encoding layer to biject rust & v8/js values. It's a core component of deno's op-layer and is used to encode/decode all non-buffer values.

Original issue: denoland/deno#9540

Quickstart

serde_v8 fits naturally into the serde ecosystem, so if you've already used serde or serde_json, serde_v8's API should be very familiar.

serde_v8 exposes two key-functions:

  • to_v8: maps rust->v8, similar to serde_json::to_string, ...
  • from_v8: maps v8->rust, similar to serde_json::from_str, ...

Best practices

Whilst serde_v8 is compatible with serde_json::Value it's important to keep in mind that serde_json::Value is essentially a loosely-typed value (think nested HashMaps), so when writing ops we recommend directly using rust structs/tuples or primitives, since mapping to serde_json::Value will add extra overhead and result in slower ops.

I also recommend avoiding unecessary "wrappers", if your op takes a single-keyed struct, consider unwrapping that as a plain value unless you plan to add fields in the near-future.

Instead of returning "nothing" via Ok(json!({})), change your return type to rust's unit type () and returning Ok(()), serde_v8 will efficiently encode that as a JS null.

Advanced features

If you need to mix rust & v8 values in structs/tuples, you can use the special serde_v8::Value type, which will passthrough the original v8 value untouched when encoding/decoding.

TODO

  • Experiment with KeyCache to optimize struct keys
  • Experiment with external v8 strings
  • Explore using json-stringifier.cc's fast-paths for arrays
  • Improve tests to test parity with serde_json (should be mostly interchangeable)
  • Consider a Payload type that's deserializable by itself (holds scope & value)
  • Ensure we return errors instead of panicking on .unwrap()s

serde_v8's People

Contributors

aarono avatar bartlomieju avatar dsherret avatar lucacasonato avatar magurotuna avatar maxxcs avatar ry avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

serde_v8's Issues

serde_v8 is vulnerable to prototype pollution

use rusty_v8 as v8;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct TestStruct {
    a: String,
    b: String,
}

fn run_script(scope: &mut v8::HandleScope, code: &str) {
    let code_string = v8::String::new(scope, code).unwrap();
    let script = v8::Script::compile(scope, code_string, None).unwrap();
    script.run(scope).unwrap();
}

fn main() {
    let platform = v8::new_default_platform(0, false).make_shared();
    v8::V8::initialize_platform(platform);
    v8::V8::initialize();

    let isolate = &mut v8::Isolate::new(Default::default());
    let scope = &mut v8::HandleScope::new(isolate);
    let context = v8::Context::new(scope);
    let scope = &mut v8::ContextScope::new(scope, context);

    run_script(
        scope,
        r#"
            Object.defineProperty(Object.prototype, "a", {
                set: (v) => {
                    throw new Error("WTF");
                }
            });
        "#,
    );

    let test = TestStruct {
        a: "a".into(),
        b: "b".into(),
    };
    let try_catch = &mut v8::TryCatch::new(scope);
    let _result = serde_v8::to_v8(try_catch, test);

    match try_catch.message() {
        Some(message) => println!(
            "Thrown exception: {}",
            message.get(try_catch).to_rust_string_lossy(try_catch)
        ),
        None => println!("No exception thrown."),
    }
}
Thrown exception: Uncaught Error: WTF

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.