rusty_v8
rusty_v8 copied to clipboard
Mismatched behavior for json stringnify
Summary
I don't know if it is by design.
While testing the performance of JSON stringnify, I found there's a mismatched behavior between using v8::json::stringnify
and direct evaluation. The code is as follows.
Code
use v8;
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);
// using built-in json stringify
let object_value = v8::String::new(scope, r#"\uD834\uDF06"#).unwrap();
let res = v8::json::stringify(scope, object_value.into())
.unwrap()
.to_rust_string_lossy(scope);
// using direct evaluation
let code = v8::String::new(scope, r#"JSON.stringify("\uD834\uDF06")"#).unwrap();
println!("javascript code: {}", code.to_rust_string_lossy(scope));
let script = v8::Script::compile(scope, code, None).unwrap();
let result = script.run(scope).unwrap();
let result = result.to_string(scope).unwrap();
println!("javascript result: {}", result.to_rust_string_lossy(scope));
}
Expected behavior
Both results should equal to "𝌆"
Actual behavior
The result from direct evaluation is correct of "𝌆"
. However the result from v8::json::stringnify
is not:
"\\uD834\\uDF06"
Haven't dug into the implementation. Test is copied from test262-value-string-escape-unicode
It might be a bug in to_rust_string_lossy
implementation (or a limitation of it). Do you get same results using C++ API?