summaryrefslogtreecommitdiff
path: root/core/ops.rs
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2021-04-12 23:38:26 +0200
committerGitHub <noreply@github.com>2021-04-12 17:38:26 -0400
commit2eafbf2b98e56ef992ba0ea47886e97c9567886e (patch)
tree6a4ed3781d40cbdbc100d967fa7ef55dace0322a /core/ops.rs
parent73b7bd92e5c9b7f03aee11808fdcb72f95348d72 (diff)
perf(core/ops): avoid allocs when returning primitives (#10149)
Diffstat (limited to 'core/ops.rs')
-rw-r--r--core/ops.rs27
1 files changed, 19 insertions, 8 deletions
diff --git a/core/ops.rs b/core/ops.rs
index 3cc24c8cc..0c9e19292 100644
--- a/core/ops.rs
+++ b/core/ops.rs
@@ -4,6 +4,7 @@ use crate::error::bad_resource_id;
use crate::error::type_error;
use crate::error::AnyError;
use crate::gotham_state::GothamState;
+use crate::minvalue::SerializablePkg;
use crate::resources::ResourceId;
use crate::resources::ResourceTable;
use crate::runtime::GetErrorClassFn;
@@ -61,7 +62,7 @@ impl<'a, 'b, 'c> OpPayload<'a, 'b, 'c> {
}
pub enum OpResponse {
- Value(Box<dyn serde_v8::Serializable>),
+ Value(OpResult),
Buffer(Box<[u8]>),
}
@@ -74,13 +75,23 @@ pub enum Op {
NotFound,
}
-#[derive(Serialize)]
-#[serde(untagged)]
-pub enum OpResult<R> {
- Ok(R),
+pub enum OpResult {
+ Ok(SerializablePkg),
Err(OpError),
}
+impl OpResult {
+ pub fn to_v8<'a>(
+ &self,
+ scope: &mut v8::HandleScope<'a>,
+ ) -> Result<v8::Local<'a, v8::Value>, serde_v8::Error> {
+ match self {
+ Self::Ok(x) => x.to_v8(scope),
+ Self::Err(err) => serde_v8::to_v8(scope, err),
+ }
+ }
+}
+
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OpError {
@@ -93,13 +104,13 @@ pub fn serialize_op_result<R: Serialize + 'static>(
result: Result<R, AnyError>,
state: Rc<RefCell<OpState>>,
) -> OpResponse {
- OpResponse::Value(Box::new(match result {
- Ok(v) => OpResult::Ok(v),
+ OpResponse::Value(match result {
+ Ok(v) => OpResult::Ok(v.into()),
Err(err) => OpResult::Err(OpError {
class_name: (state.borrow().get_error_class_fn)(&err),
message: err.to_string(),
}),
- }))
+ })
}
/// Maintains the resources and ops inside a JS runtime.