Amy
Amy3mo ago

How to get the path to the file from a deno_core op2?

I want to implement a subset of fs in op2, but I want it to be relative to the dir the file is in. I can't find anything on OpState or HandleScope for that though. And right now, import.meta.path is undefined, not sure how to fix that either
1 Reply
Amy
AmyOP3mo ago
okay looks like its like
#[op2]
#[string]
pub fn op_where_am_i<'s>(
scope: &mut v8::HandleScope<'s>,
state: &OpState,
) -> Result<Option<String>, deno_error::JsErrorBox> {
let stack_trace = v8::StackTrace::current_stack_trace(scope, 1);

if let Some(trace) = stack_trace {
if trace.get_frame_count() > 0 {
let frame = trace.get_frame(scope, 0).unwrap();
let script_name = frame.get_script_name_or_source_url(scope);
if let Some(name) = script_name {
return Ok(Some(name.to_rust_string_lossy(scope)));
}
}
}
Ok(None)
}
#[op2]
#[string]
pub fn op_where_am_i<'s>(
scope: &mut v8::HandleScope<'s>,
state: &OpState,
) -> Result<Option<String>, deno_error::JsErrorBox> {
let stack_trace = v8::StackTrace::current_stack_trace(scope, 1);

if let Some(trace) = stack_trace {
if trace.get_frame_count() > 0 {
let frame = trace.get_frame(scope, 0).unwrap();
let script_name = frame.get_script_name_or_source_url(scope);
if let Some(name) = script_name {
return Ok(Some(name.to_rust_string_lossy(scope)));
}
}
}
Ok(None)
}
works

Did you find this page helpful?