0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-12 17:09:28 -05:00

add v8::JSON (#54)

This commit is contained in:
Bartek Iwańczuk 2019-12-09 23:11:31 +01:00 committed by Ry Dahl
parent e6e35a57e9
commit 41e1f14ab5
5 changed files with 88 additions and 0 deletions

View file

@ -12,6 +12,7 @@ v8_static_library("rusty_v8") {
"src/inspector/client.cc",
"src/inspector/string_buffer.cc",
"src/isolate.cc",
"src/json.cc",
"src/locker.cc",
"src/number.cc",
"src/platform/mod.cc",

14
src/json.cc Normal file
View file

@ -0,0 +1,14 @@
#include "support.h"
#include "v8/include/v8.h"
using namespace support;
extern "C" {
v8::Value *v8__JSON__Parse(v8::Local<v8::Context> context, v8::Local<v8::String> json_string) {
return maybe_local_to_ptr(v8::JSON::Parse(context, json_string));
}
v8::String *v8__JSON__Stringify(v8::Local<v8::Context> context, v8::Local<v8::Value> json_object) {
return maybe_local_to_ptr(v8::JSON::Stringify(context, json_object));
}
}

44
src/json.rs Normal file
View file

@ -0,0 +1,44 @@
#![allow(non_snake_case)]
use crate::Context;
use crate::Local;
use crate::String;
use crate::Value;
extern "C" {
fn v8__JSON__Parse(
context: *mut Context,
json_string: *mut String,
) -> *mut Value;
fn v8__JSON__Stringify(
context: *mut Context,
json_object: *mut Value,
) -> *mut String;
}
/// A JSON Parser and Stringifier
pub mod JSON {
use super::*;
/// Tries to parse the string `json_string` and returns it as value if
/// successful.
pub fn Parse<'sc>(
mut context: Local<'sc, Context>,
mut json_string: Local<'sc, String>,
) -> Option<Local<'sc, Value>> {
unsafe {
Local::from_raw(v8__JSON__Parse(&mut *context, &mut *json_string))
}
}
/// Tries to stringify the JSON-serializable object `json_object` and returns
/// it as string if successful.
pub fn Stringify<'sc>(
mut context: Local<'sc, Context>,
mut json_object: Local<'sc, Value>,
) -> Option<Local<'sc, String>> {
unsafe {
Local::from_raw(v8__JSON__Stringify(&mut *context, &mut *json_object))
}
}
}

View file

@ -15,6 +15,7 @@ mod context;
mod exception;
mod handle_scope;
mod isolate;
mod json;
mod local;
mod locker;
mod number;
@ -36,6 +37,7 @@ pub use context::Context;
pub use exception::Exception;
pub use handle_scope::HandleScope;
pub use isolate::Isolate;
pub use json::JSON;
pub use local::Local;
pub use locker::Locker;
pub use number::{Integer, Number};

View file

@ -242,3 +242,30 @@ fn exception() {
});
isolate.exit();
}
#[test]
fn json() {
setup();
let mut params = v8::Isolate::create_params();
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
v8::HandleScope::enter(&mut locker, |s| {
let mut context = v8::Context::new(s);
context.enter();
let json_string =
v8::String::new(s, "{\"a\": 1, \"b\": 2}", Default::default())
.unwrap();
let maybe_value = v8::JSON::Parse(context, json_string);
assert!(maybe_value.is_some());
let value = maybe_value.unwrap();
let maybe_stringified = v8::JSON::Stringify(context, value);
assert!(maybe_stringified.is_some());
let stringified = maybe_stringified.unwrap();
let rust_str = stringified.to_rust_string_lossy(s);
assert_eq!("{\"a\":1,\"b\":2}".to_string(), rust_str);
context.exit();
});
}