Skip to content

Commit 9c01797

Browse files
authored
Implement JSAutoStructuredCloneBufferWrapper (#599)
* Implement JSAutoStructuredCloneBufferWrapper Implement a safe wrapper of `*mut JSAutoStructuredCloneBuffer` that Implements the Drop trait to prevent leakage. Signed-off-by: Kingsley Yung <[email protected]> * Remove unnecessary import Signed-off-by: Kingsley Yung <[email protected]> * Make sure JSAutoStructuredCloneBufferWrapper::new don't have null pointer Signed-off-by: Kingsley Yung <[email protected]> * Wrap raw pointer in NonNull Signed-off-by: Kingsley Yung <[email protected]> --------- Signed-off-by: Kingsley Yung <[email protected]>
1 parent ace07c8 commit 9c01797

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

mozjs/src/rust.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::glue::{CreateRootedIdVector, CreateRootedObjectVector};
2828
use crate::glue::{
2929
DeleteCompileOptions, DeleteRootedObjectVector, DescribeScriptedCaller, DestroyRootedIdVector,
3030
};
31+
use crate::glue::{DeleteJSAutoStructuredCloneBuffer, NewJSAutoStructuredCloneBuffer};
3132
use crate::glue::{
3233
GetIdVectorAddress, GetObjectVectorAddress, NewCompileOptions, SliceRootedIdVector,
3334
};
@@ -47,6 +48,7 @@ use crate::jsapi::{InitSelfHostedCode, IsWindowSlow};
4748
use crate::jsapi::{
4849
JSAutoRealm, JS_SetGCParameter, JS_SetNativeStackQuota, JS_WrapObject, JS_WrapValue,
4950
};
51+
use crate::jsapi::{JSAutoStructuredCloneBuffer, JSStructuredCloneCallbacks, StructuredCloneScope};
5052
use crate::jsapi::{JSClass, JSClassOps, JSContext, Realm, JSCLASS_RESERVED_SLOTS_SHIFT};
5153
use crate::jsapi::{JSErrorReport, JSFunctionSpec, JSGCParamKey};
5254
use crate::jsapi::{JSObject, JSPropertySpec, JSRuntime};
@@ -532,6 +534,34 @@ impl Drop for CompileOptionsWrapper {
532534
}
533535
}
534536

537+
pub struct JSAutoStructuredCloneBufferWrapper {
538+
ptr: NonNull<JSAutoStructuredCloneBuffer>,
539+
}
540+
541+
impl JSAutoStructuredCloneBufferWrapper {
542+
pub unsafe fn new(
543+
scope: StructuredCloneScope,
544+
callbacks: *const JSStructuredCloneCallbacks,
545+
) -> Self {
546+
let raw_ptr = NewJSAutoStructuredCloneBuffer(scope, callbacks);
547+
Self {
548+
ptr: NonNull::new(raw_ptr).unwrap(),
549+
}
550+
}
551+
552+
pub fn as_raw_ptr(&self) -> *mut JSAutoStructuredCloneBuffer {
553+
self.ptr.as_ptr()
554+
}
555+
}
556+
557+
impl Drop for JSAutoStructuredCloneBufferWrapper {
558+
fn drop(&mut self) {
559+
unsafe {
560+
DeleteJSAutoStructuredCloneBuffer(self.ptr.as_ptr());
561+
}
562+
}
563+
}
564+
535565
pub struct Stencil {
536566
inner: already_AddRefed<CompilationStencil>,
537567
}

0 commit comments

Comments
 (0)