-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[move] Enable serialization of source maps into json #18727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,19 @@ | |
use crate::source_map::SourceMap; | ||
use anyhow::{format_err, Result}; | ||
use move_ir_types::location::Loc; | ||
use std::{fs::File, io::Read, path::Path}; | ||
use std::{ | ||
fs::File, | ||
io::{Read, Write}, | ||
path::Path, | ||
}; | ||
|
||
pub type Error = (Loc, String); | ||
pub type Errors = Vec<Error>; | ||
|
||
pub fn source_map_from_file(file_path: &Path) -> Result<SourceMap> { | ||
if file_path.extension().is_some_and(|ext| ext == "json") { | ||
return deserialize_from_json(file_path); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we enforce a file type otherwise? Makes me nervous to just have ".json" or whatever else you want |
||
let mut bytes = Vec::new(); | ||
File::open(file_path) | ||
.ok() | ||
|
@@ -19,3 +26,31 @@ pub fn source_map_from_file(file_path: &Path) -> Result<SourceMap> { | |
bcs::from_bytes::<SourceMap>(&bytes) | ||
.map_err(|_| format_err!("Error deserializing into source map")) | ||
} | ||
|
||
pub fn serialize_to_json(map: &SourceMap) -> Result<Vec<u8>> { | ||
serde_json::to_vec(map).map_err(|e| format_err!("Error serializing to json: {}", e)) | ||
} | ||
|
||
pub fn serialize_to_json_file(map: &SourceMap, file_path: &Path) -> Result<()> { | ||
let json = serde_json::to_string_pretty(map) | ||
.map_err(|e| format_err!("Error serializing to json: {}", e))?; | ||
let mut f = | ||
std::fs::File::create(file_path).map_err(|e| format_err!("Error creating file: {}", e))?; | ||
f.write_all(json.as_bytes()) | ||
.map_err(|e| format_err!("Error writing to file: {}", e))?; | ||
Ok(()) | ||
} | ||
|
||
pub fn deserialize_from_json(file_path: &Path) -> Result<SourceMap> { | ||
let mut file = File::open(file_path).map_err(|e| format_err!("Error opening file: {}", e))?; | ||
let mut json = String::new(); | ||
file.read_to_string(&mut json) | ||
.map_err(|e| format_err!("Error reading file: {}", e))?; | ||
serde_json::from_str(&json).map_err(|e| format_err!("Error deserializing from json: {}", e)) | ||
} | ||
|
||
pub fn convert_to_json(file_path: &Path) -> Result<()> { | ||
let map = source_map_from_file(file_path)?; | ||
let json_file_path = file_path.with_extension("json"); | ||
serialize_to_json_file(&map, &json_file_path) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "tracing_unit_tests" | ||
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move | ||
|
||
[dependencies] | ||
MoveStdlib = { local = "../../../../move-stdlib" } | ||
|
||
[addresses] | ||
std = "0x1" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Command `test -t 1 --trace-execution new_traces`: | ||
INCLUDING DEPENDENCY MoveStdlib | ||
BUILDING tracing_unit_tests | ||
Running Move unit tests | ||
[ PASS ] 0x1::calls::test_call_order | ||
[ PASS ] 0x1::calls::test_call_return_order | ||
[ PASS ] 0x1::calls::test_complex_nested_calls | ||
[ PASS ] 0x1::calls::test_return_order | ||
[ PASS ] 0x1::errors::aborter | ||
[ PASS ] 0x1::errors::bad_cast | ||
[ PASS ] 0x1::errors::div_0 | ||
[ PASS ] 0x1::errors::fail_during_abort | ||
[ PASS ] 0x1::errors::overshift_l | ||
[ PASS ] 0x1::errors::overshift_r | ||
[ PASS ] 0x1::errors::underflow | ||
[ PASS ] 0x1::natives::get_orig_type_name_test | ||
[ PASS ] 0x1::natives::get_type_name_test | ||
[ PASS ] 0x1::packs::test_gen_pack_order | ||
[ PASS ] 0x1::packs::test_gen_unpack_order | ||
[ PASS ] 0x1::packs::test_pack_order | ||
[ PASS ] 0x1::packs::test_unpack_order | ||
[ PASS ] 0x1::references::nested_struct_reference_mutation | ||
[ PASS ] 0x1::references::pass_mut_assign_in_other_fn | ||
[ PASS ] 0x1::references::test_struct_borrow | ||
[ PASS ] 0x1::references::test_vector_mut_borrow | ||
[ PASS ] 0x1::references::test_vector_mut_borrow_pop | ||
Test result: OK. Total tests: 22; passed: 22; failed: 0 | ||
External Command `diff -qr new_traces saved_traces`: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test -t 1 --trace-execution new_traces | ||
> diff -qr new_traces saved_traces |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"events":[{"OpenFrame":{"frame":{"binary_member_index":0,"frame_id":0,"function_name":"test_call_order","is_native":false,"locals_types":[],"module":{"address":"0000000000000000000000000000000000000000000000000000000000000001","name":"calls"},"parameters":[],"return_types":[],"type_instantiation":[]},"gas_left":1000000000}},{"Instruction":{"gas_left":999999998,"instruction":"LD_U64","pc":0,"type_parameters":[]}},{"Effect":{"Push":{"RuntimeValue":{"value":1}}}},{"Instruction":{"gas_left":999999996,"instruction":"LD_TRUE","pc":1,"type_parameters":[]}},{"Effect":{"Push":{"RuntimeValue":{"value":true}}}},{"Instruction":{"gas_left":999999994,"instruction":"LD_U8","pc":2,"type_parameters":[]}},{"Effect":{"Push":{"RuntimeValue":{"value":1}}}},{"Instruction":{"gas_left":999999994,"instruction":"CALL","pc":3,"type_parameters":[]}},{"OpenFrame":{"frame":{"binary_member_index":1,"frame_id":8,"function_name":"f_test_call_order","is_native":false,"locals_types":[{"ref_type":null,"type_":"u64"},{"ref_type":null,"type_":"bool"},{"ref_type":null,"type_":"u8"}],"module":{"address":"0000000000000000000000000000000000000000000000000000000000000001","name":"calls"},"parameters":[{"RuntimeValue":{"value":1}},{"RuntimeValue":{"value":true}},{"RuntimeValue":{"value":1}}],"return_types":[],"type_instantiation":[]},"gas_left":999999994}},{"Instruction":{"gas_left":999994823,"instruction":"RET","pc":0,"type_parameters":[]}},{"CloseFrame":{"frame_id":8,"gas_left":999994823,"return_":[]}},{"Instruction":{"gas_left":999994184,"instruction":"RET","pc":4,"type_parameters":[]}},{"CloseFrame":{"frame_id":0,"gas_left":999994184,"return_":[]}}],"version":1} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"events":[{"OpenFrame":{"frame":{"binary_member_index":4,"frame_id":0,"function_name":"test_call_return_order","is_native":false,"locals_types":[],"module":{"address":"0000000000000000000000000000000000000000000000000000000000000001","name":"calls"},"parameters":[],"return_types":[],"type_instantiation":[]},"gas_left":1000000000}},{"Instruction":{"gas_left":1000000000,"instruction":"CALL","pc":0,"type_parameters":[]}},{"OpenFrame":{"frame":{"binary_member_index":3,"frame_id":2,"function_name":"f_test_return_order","is_native":false,"locals_types":[],"module":{"address":"0000000000000000000000000000000000000000000000000000000000000001","name":"calls"},"parameters":[],"return_types":[{"ref_type":null,"type_":"u64"},{"ref_type":null,"type_":"bool"},{"ref_type":null,"type_":"u8"}],"type_instantiation":[]},"gas_left":1000000000}},{"Instruction":{"gas_left":999998865,"instruction":"LD_U64","pc":0,"type_parameters":[]}},{"Effect":{"Push":{"RuntimeValue":{"value":1}}}},{"Instruction":{"gas_left":999998863,"instruction":"LD_TRUE","pc":1,"type_parameters":[]}},{"Effect":{"Push":{"RuntimeValue":{"value":true}}}},{"Instruction":{"gas_left":999998861,"instruction":"LD_U8","pc":2,"type_parameters":[]}},{"Effect":{"Push":{"RuntimeValue":{"value":0}}}},{"Instruction":{"gas_left":999998222,"instruction":"RET","pc":3,"type_parameters":[]}},{"CloseFrame":{"frame_id":2,"gas_left":999998222,"return_":[{"RuntimeValue":{"value":1}},{"RuntimeValue":{"value":true}},{"RuntimeValue":{"value":0}}]}},{"Instruction":{"gas_left":999998222,"instruction":"CALL","pc":1,"type_parameters":[]}},{"OpenFrame":{"frame":{"binary_member_index":1,"frame_id":12,"function_name":"f_test_call_order","is_native":false,"locals_types":[{"ref_type":null,"type_":"u64"},{"ref_type":null,"type_":"bool"},{"ref_type":null,"type_":"u8"}],"module":{"address":"0000000000000000000000000000000000000000000000000000000000000001","name":"calls"},"parameters":[{"RuntimeValue":{"value":1}},{"RuntimeValue":{"value":true}},{"RuntimeValue":{"value":0}}],"return_types":[],"type_instantiation":[]},"gas_left":999998222}},{"Instruction":{"gas_left":999993051,"instruction":"RET","pc":0,"type_parameters":[]}},{"CloseFrame":{"frame_id":12,"gas_left":999993051,"return_":[]}},{"Instruction":{"gas_left":999992412,"instruction":"RET","pc":2,"type_parameters":[]}},{"CloseFrame":{"frame_id":0,"gas_left":999992412,"return_":[]}}],"version":1} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit ,constant maybe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constants are used elsewhere for extension though I understand that
json
is not like the others.But it is a fair comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My only request is that if we extract this into a constant, that it's the
SOURCE_MAP_EXT
, and notJSON_EXT
.