From ed0f45172bbce704d2cda3c5f7363249e31488bf Mon Sep 17 00:00:00 2001 From: yodalee Date: Sun, 15 Jul 2018 00:59:16 +0800 Subject: [PATCH] add pretty Debug implementation for CodeObject --- vm/src/bytecode.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/vm/src/bytecode.rs b/vm/src/bytecode.rs index cec2f496f..b95521be5 100644 --- a/vm/src/bytecode.rs +++ b/vm/src/bytecode.rs @@ -11,8 +11,9 @@ let call_function = 0x64; * Primitive instruction type, which can be encoded and decoded. */ use std::collections::HashMap; +use std::fmt; -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct CodeObject { pub instructions: Vec, pub label_map: HashMap, @@ -117,3 +118,15 @@ pub enum BlockType { Except, } */ + +impl fmt::Debug for CodeObject { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let inst_str = self.instructions.iter() + .enumerate() + .map(|(i, inst)| format!("Inst {}: {:?}", i, inst)) + .collect::>() + .join("\n"); + let labelmap_str = format!("label_map: {:?}", self.label_map); + write!(f, "Code Object {{ \n{}\n{} }}", inst_str, labelmap_str) + } +}