8000 feat(err): add JSONExtract to the rust hogvm by oliverb123 · Pull Request #33607 · PostHog/posthog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(err): add JSONExtract to the rust hogvm #33607

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

Merged
merged 8 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions posthog/models/error_tracking/hogvm_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
"indexOf",
"notEmpty",
"match",
"arrayExists",
"JSONExtract",
"arrayReduce",
"arrayCount",
"arrayFilter",
"arrayExists",
"arrayMap",
"arrayCount",
"arrayReduce",
]
2 changes: 1 addition & 1 deletion rust/common/hogvm/src/bin/stl_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn main() {
"RUST_HOGVM_STL = [\n {}\n]",
hogvm::stl()
.iter()
.map(|(name, _)| *name)
.map(|(name, _)| name.as_str())
.chain(hog_stl().functions().iter().map(|(name, _)| name.as_str()))
.map(|n| format!("\"{}\"", n))
.collect::<Vec<_>>()
Expand Down
74 changes: 58 additions & 16 deletions rust/common/hogvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,8 @@ impl ExecutionContext {
let Some(native_fn) = self.native_fns.get(name) else {
return Err(VmError::UnknownFunction(name.to_string()));
};
let result = native_fn(vm, args);
match result {
Ok(HogValue::Ref(ptr)) => vm.push_stack(ptr),
Ok(HogValue::Lit(lit)) => match lit {
// Object types returned from native functions get heap allocated, just like ones declared
// in the bytecode, whereas other types are pushed directly onto the stack. The purity of
// native functions means we don't need to worry about memory management for these values,
// beyond what the heap internally manages.
HogLiteral::Array(_) | HogLiteral::Object(_) => {
let ptr = vm.heap.emplace(lit)?;
vm.push_stack(ptr)
}
_ => vm.push_stack(lit),
},
Err(e) => Err(e),
}
let emplaced = walk_emplacing(vm, native_fn(vm, args)?)?;
vm.push_stack(emplaced)
}

pub fn get_bytecode(&self, ip: usize, symbol: &Option<Symbol>) -> Result<&JsonValue, VmError> {
Expand Down Expand Up @@ -187,3 +173,59 @@ impl std::fmt::Display for Symbol {
write!(f, "{}/{}", self.module, self.name)
}
}

/// Walk a HogValue and its children recursively to ensure all indexable types (arrays and objects) are heap allocated,
/// and then return the now-properly-allocated value. This is useful if, for example, you've constructed a HogValue
/// from a JSON object without mutable access to a VM's heap, and now need to push it into the VM's memory space for the
/// program to use.
///
/// This is exposed as a utility, but generally ExecutionContext::execute_native_function_call should do what you need.
fn walk_emplacing(vm: &mut HogVM, value: HogValue) -> Result<HogValue, VmError> {
// Chase the pointer, if this is one, and clone out of it. We hold on to the original pointer
// so we can swap the walked value back into it after we're done.
let (literal, existing_location) = match value {
HogValue::Lit(lit) => (lit, None),
HogValue::Ref(ptr) => {
let val = vm.heap.get(ptr)?.clone();
(val, Some(ptr))
}
};

match literal {
HogLiteral::Array(arr) => {
let emplaced_arr: Result<Vec<HogValue>, _> =
arr.into_iter().map(|i| walk_emplacing(vm, i)).collect();
let emplaced_arr = HogLiteral::Array(emplaced_arr?);

if let Some(ptr) = existing_location {
// If this was already a heap-allocated array, replace it with the new one
*vm.heap.get_mut(ptr)? = emplaced_arr;
Ok(ptr.into())
} else {
// Otherwise heap allocate it and return the pointer
vm.heap.emplace(emplaced_arr).map(|ptr| ptr.into())
}
}
HogLiteral::Object(obj) => {
let emplaced_obj: Result<HashMap<String, HogValue>, _> = obj
.into_iter()
.map(|(k, v)| Ok((k, walk_emplacing(vm, v)?)))
.collect();
let emplaced_obj = HogLiteral::Object(emplaced_obj?);

if let Some(ptr) = existing_location {
// As above, if this was already heap allocated, replace it with the new one
*vm.heap.get_mut(ptr)? = emplaced_obj;
Ok(ptr.into())
} else {
// Otherwise heap allocate it and return the pointer
vm.heap.emplace(emplaced_obj).map(|ptr| ptr.into())
}
}
// If we're looking at a non-indexable type, just return it, or the reference to it,
// if it was already heap allocated.
_ => Ok(existing_location
.map(|ptr| ptr.into())
.unwrap_or(literal.into())),
}
}
1 change: 1 addition & 0 deletions rust/common/hogvm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use thiserror::Error;

// TBH this is probably need to be broken up somehow
#[derive(Debug, Error, Clone)]
#[non_exhaustive]
pub enum VmError {
#[error("Expected operation, got {0:?}")]
NotAnOperation(Value),
Expand Down
2 changes: 2 additions & 0 deletions rust/common/hogvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ pub use vm::VmFailure;

// STL - again, we expose a lot, because we want to make it easy to extend this
pub use stl::hog_stl;
pub use stl::native_func;
pub use stl::stl;
pub use stl::stl_map;
pub use stl::NativeFunction;

// Values - We expose almost everything here for the sake of native function extension authors
pub use values::construct_free_standing;
pub use values::Callable;
pub use values::Closure;
pub use values::FromHogLiteral;
Expand Down
Loading
Loading
0