summaryrefslogtreecommitdiff
path: root/ext/ffi/static.rs
diff options
context:
space:
mode:
authorhaturau <135221985+haturatu@users.noreply.github.com>2024-11-20 01:20:47 +0900
committerGitHub <noreply@github.com>2024-11-20 01:20:47 +0900
commit85719a67e59c7aa45bead26e4942d7df8b1b42d4 (patch)
treeface0aecaac53e93ce2f23b53c48859bcf1a36ec /ext/ffi/static.rs
parent67697bc2e4a62a9670699fd18ad0dd8efc5bd955 (diff)
parent186b52731c6bb326c4d32905c5e732d082e83465 (diff)
Merge branch 'denoland:main' into main
Diffstat (limited to 'ext/ffi/static.rs')
-rw-r--r--ext/ffi/static.rs31
1 files changed, 22 insertions, 9 deletions
diff --git a/ext/ffi/static.rs b/ext/ffi/static.rs
index f08605754..61b405933 100644
--- a/ext/ffi/static.rs
+++ b/ext/ffi/static.rs
@@ -2,14 +2,24 @@
use crate::dlfcn::DynamicLibraryResource;
use crate::symbol::NativeType;
-use deno_core::error::type_error;
-use deno_core::error::AnyError;
use deno_core::op2;
use deno_core::v8;
use deno_core::OpState;
use deno_core::ResourceId;
use std::ptr;
+#[derive(Debug, thiserror::Error)]
+pub enum StaticError {
+ #[error(transparent)]
+ Dlfcn(super::DlfcnError),
+ #[error("Invalid FFI static type 'void'")]
+ InvalidTypeVoid,
+ #[error("Invalid FFI static type 'struct'")]
+ InvalidTypeStruct,
+ #[error(transparent)]
+ Resource(deno_core::error::AnyError),
+}
+
#[op2]
pub fn op_ffi_get_static<'scope>(
scope: &mut v8::HandleScope<'scope>,
@@ -18,24 +28,27 @@ pub fn op_ffi_get_static<'scope>(
#[string] name: String,
#[serde] static_type: NativeType,
optional: bool,
-) -> Result<v8::Local<'scope, v8::Value>, AnyError> {
- let resource = state.resource_table.get::<DynamicLibraryResource>(rid)?;
+) -> Result<v8::Local<'scope, v8::Value>, StaticError> {
+ let resource = state
+ .resource_table
+ .get::<DynamicLibraryResource>(rid)
+ .map_err(StaticError::Resource)?;
let data_ptr = match resource.get_static(name) {
- Ok(data_ptr) => Ok(data_ptr),
+ Ok(data_ptr) => data_ptr,
Err(err) => {
if optional {
let null: v8::Local<v8::Value> = v8::null(scope).into();
return Ok(null);
} else {
- Err(err)
+ return Err(StaticError::Dlfcn(err));
}
}
- }?;
+ };
Ok(match static_type {
NativeType::Void => {
- return Err(type_error("Invalid FFI static type 'void'"));
+ return Err(StaticError::InvalidTypeVoid);
}
NativeType::Bool => {
// SAFETY: ptr is user provided
@@ -132,7 +145,7 @@ pub fn op_ffi_get_static<'scope>(
external
}
NativeType::Struct(_) => {
- return Err(type_error("Invalid FFI static type 'struct'"));
+ return Err(StaticError::InvalidTypeStruct);
}
})
}