Skip to content

Commit c936c89

Browse files
committed
feat: add Reference values and some "raw" register type helpers
we are calling the directly constructed TypeRecord raw here, to preserve backward compatibility: the `_type` helpers accept a kind and a name for the simpler cases. We might change that API in the future, as there are possible improvements and reforms to type/values
1 parent a2cc701 commit c936c89

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "runtime_tracing"
3-
version = "0.8.0"
3+
version = "0.9.0"
44
edition = "2021"
55
authors = ["Metacraft Labs Ltd"]
66
description = "A library for the schema and tracing helpers for the CodeTracer db trace format"

src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,24 @@ mod tests {
113113
let rvalue_2 = tracer.compound_rvalue(&["variable2".to_string(), "variable3".to_string()]);
114114
tracer.assign("variable1", rvalue_2, PassBy::Value);
115115

116+
// example for reference types
117+
let reference_type = TypeRecord {
118+
kind: TypeKind::Pointer,
119+
lang_type: "MyReference<Int>".to_string(),
120+
specific_info: TypeSpecificInfo::Pointer {
121+
dereference_type_id: tracer.ensure_type_id(TypeKind::Int, "Int")
122+
}
123+
};
124+
let reference_type_id = tracer.ensure_raw_type_id(reference_type);
125+
let _reference_value = ValueRecord::Reference {
126+
dereferenced: Box::new(int_value_1.clone()),
127+
mutable: false,
128+
type_id: reference_type_id,
129+
};
130+
116131
tracer.drop_variables(&["variable1".to_string(), "variable2".to_string(), "variable3".to_string()]);
117132

118-
assert_eq!(tracer.events.len(), 46);
133+
assert_eq!(tracer.events.len(), 47);
119134
// visible with
120135
// cargo tets -- --nocapture
121136
// println!("{:#?}", tracer.events);

src/tracer.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@ impl Tracer {
8888
}
8989

9090
pub fn ensure_type_id(&mut self, kind: TypeKind, lang_type: &str) -> TypeId {
91-
if !self.types.contains_key(lang_type) {
92-
self.types.insert(lang_type.to_string(), TypeId(self.types.len()));
93-
self.register_type(kind, lang_type);
91+
let typ = self.to_raw_type(kind, lang_type);
92+
self.ensure_raw_type_id(typ)
93+
}
94+
95+
pub fn ensure_raw_type_id(&mut self, typ: TypeRecord) -> TypeId {
96+
if !self.types.contains_key(&typ.lang_type) {
97+
self.types.insert(typ.lang_type.clone(), TypeId(self.types.len()));
98+
self.register_raw_type(typ.clone());
9499
}
95-
*self.types.get(lang_type).unwrap()
100+
*self.types.get(&typ.lang_type).unwrap()
96101
}
97102

98103
pub fn ensure_variable_id(&mut self, variable_name: &str) -> VariableId {
@@ -157,12 +162,20 @@ impl Tracer {
157162
}));
158163
}
159164

160-
pub fn register_type(&mut self, kind: TypeKind, lang_type: &str) {
161-
let typ = TypeRecord {
165+
pub fn to_raw_type(&self, kind: TypeKind, lang_type: &str) -> TypeRecord {
166+
TypeRecord {
162167
kind,
163168
lang_type: lang_type.to_string(),
164169
specific_info: TypeSpecificInfo::None,
165-
};
170+
}
171+
}
172+
173+
pub fn register_type(&mut self, kind: TypeKind, lang_type: &str) {
174+
let typ = self.to_raw_type(kind, lang_type);
175+
self.events.push(TraceLowLevelEvent::Type(typ));
176+
}
177+
178+
pub fn register_raw_type(&mut self, typ: TypeRecord) {
166179
self.events.push(TraceLowLevelEvent::Type(typ));
167180
}
168181

src/types.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ pub struct VariableRecord {
280280
pub struct TypeRecord {
281281
pub kind: TypeKind,
282282
pub lang_type: String,
283-
// for now only for Struct: TODO eventually
283+
// for now only for Struct and Pointer: TODO eventually
284284
// replace with an enum for TypeRecord, or with more cases
285285
// in TypeSpecificInfo for collections, etc
286286
pub specific_info: TypeSpecificInfo,
@@ -297,6 +297,7 @@ pub struct FieldTypeRecord {
297297
pub enum TypeSpecificInfo {
298298
None,
299299
Struct { fields: Vec<FieldTypeRecord> },
300+
Pointer { dereference_type_id: TypeId },
300301
}
301302

302303
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -340,8 +341,8 @@ pub enum ValueRecord {
340341
},
341342
Sequence {
342343
elements: Vec<ValueRecord>,
343-
type_id: TypeId,
344344
is_slice: bool,
345+
type_id: TypeId,
345346
},
346347
Tuple {
347348
elements: Vec<ValueRecord>,
@@ -356,6 +357,13 @@ pub enum ValueRecord {
356357
contents: Box<ValueRecord>, // usually a Struct or a Tuple
357358
type_id: TypeId,
358359
},
360+
// TODO: eventually add more pointer-like variants
361+
// or more fields (address?)
362+
Reference {
363+
dereferenced: Box<ValueRecord>,
364+
mutable: bool,
365+
type_id: TypeId,
366+
},
359367
Raw {
360368
r: String,
361369
type_id: TypeId,

0 commit comments

Comments
 (0)