1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00
denoland-deno/ext/node/ops/v8.rs
2024-01-01 19:58:21 +00:00

80 lines
2.3 KiB
Rust

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use deno_core::error::AnyError;
use deno_core::op2;
use deno_core::v8;
#[op2(fast)]
pub fn op_v8_cached_data_version_tag() -> u32 {
v8::script_compiler::cached_data_version_tag()
}
#[op2]
pub fn op_v8_get_heap_statistics(
scope: &mut v8::HandleScope,
#[buffer] buffer: &mut [f64],
) {
let mut stats = v8::HeapStatistics::default();
scope.get_heap_statistics(&mut stats);
buffer[0] = stats.total_heap_size() as f64;
buffer[1] = stats.total_heap_size_executable() as f64;
buffer[2] = stats.total_physical_size() as f64;
buffer[3] = stats.total_available_size() as f64;
buffer[4] = stats.used_heap_size() as f64;
buffer[5] = stats.heap_size_limit() as f64;
buffer[6] = stats.malloced_memory() as f64;
buffer[7] = stats.peak_malloced_memory() as f64;
buffer[8] = stats.does_zap_garbage() as f64;
buffer[9] = stats.number_of_native_contexts() as f64;
buffer[10] = stats.number_of_detached_contexts() as f64;
buffer[11] = stats.total_global_handles_size() as f64;
buffer[12] = stats.used_global_handles_size() as f64;
buffer[13] = stats.external_memory() as f64;
}
pub const VM_CONTEXT_INDEX: usize = 0;
fn make_context<'a>(
scope: &mut v8::HandleScope<'a>,
) -> v8::Local<'a, v8::Context> {
let scope = &mut v8::EscapableHandleScope::new(scope);
let context = v8::Context::from_snapshot(scope, VM_CONTEXT_INDEX).unwrap();
scope.escape(context)
}
#[op2]
pub fn op_vm_run_in_new_context<'a>(
scope: &mut v8::HandleScope<'a>,
script: v8::Local<v8::String>,
ctx_val: v8::Local<v8::Value>,
) -> Result<v8::Local<'a, v8::Value>, AnyError> {
let _ctx_obj = if ctx_val.is_undefined() || ctx_val.is_null() {
v8::Object::new(scope)
} else {
ctx_val.try_into()?
};
let ctx = make_context(scope);
let scope = &mut v8::ContextScope::new(scope, ctx);
let tc_scope = &mut v8::TryCatch::new(scope);
let script = match v8::Script::compile(tc_scope, script, None) {
Some(s) => s,
None => {
assert!(tc_scope.has_caught());
tc_scope.rethrow();
return Ok(v8::undefined(tc_scope).into());
}
};
Ok(match script.run(tc_scope) {
Some(result) => result,
None => {
assert!(tc_scope.has_caught());
tc_scope.rethrow();
v8::undefined(tc_scope).into()
}
})
}