Skip to content

Commit d5630c9

Browse files
gmorenzsagudev
andauthored
Remove Copy/Clone from MutableHandle (#559)
* Replace Copy/Clone with Reborrow on gc::MutableHandle Signed-off-by: Greg Morenz <[email protected]> * Remove jsapi_wrappers module Signed-off-by: Greg Morenz <[email protected]> * Fix typo Co-authored-by: Samson <[email protected]> Signed-off-by: Greg Morenz <[email protected]> --------- Signed-off-by: Greg Morenz <[email protected]> Signed-off-by: Greg Morenz <[email protected]> Co-authored-by: Samson <[email protected]>
1 parent 02aa85b commit d5630c9

File tree

3 files changed

+31
-167
lines changed

3 files changed

+31
-167
lines changed

mozjs/examples/wasm.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
//! and do some setup on both of these. You also need to enter a "realm"
77
//! (environment within one global object) before you can execute code.
88
9-
use ::std::ffi::c_char;
109
use ::std::ptr;
1110
use ::std::ptr::null_mut;
1211

1312
use mozjs::jsapi::*;
1413
use mozjs::jsval::ObjectValue;
1514
use mozjs::jsval::UndefinedValue;
1615
use mozjs::rooted;
17-
use mozjs::rust::jsapi_wrapped::{Construct1, JS_GetProperty, JS_SetProperty};
16+
use mozjs::rust::wrappers::{Construct1, JS_GetProperty, JS_SetProperty};
1817
use mozjs::rust::SIMPLE_GLOBAL_CLASS;
1918
use mozjs::rust::{IntoHandle, JSEngine, RealmOptions, Runtime};
2019
use mozjs_sys::jsgc::ValueArray;
@@ -64,20 +63,20 @@ fn run(rt: Runtime) {
6463
rt.cx(),
6564
global.handle(),
6665
c"WebAssembly".as_ptr(),
67-
&mut wasm.handle_mut()
66+
wasm.handle_mut()
6867
));
6968
rooted!(in(rt.cx()) let mut wasm_obj = wasm.to_object());
7069
assert!(JS_GetProperty(
7170
rt.cx(),
7271
wasm_obj.handle(),
7372
c"Module".as_ptr(),
74-
&mut wasm_module.handle_mut()
73+
wasm_module.handle_mut()
7574
));
7675
assert!(JS_GetProperty(
7776
rt.cx(),
7877
wasm_obj.handle(),
7978
c"Instance".as_ptr(),
80-
&mut wasm_instance.handle_mut()
79+
wasm_instance.handle_mut()
8180
));
8281

8382
// ptr needs to be aligned to 8
@@ -100,7 +99,7 @@ fn run(rt: Runtime) {
10099
rt.cx(),
101100
wasm_module.handle(),
102101
&args,
103-
&mut module.handle_mut()
102+
module.handle_mut()
104103
))
105104
}
106105

@@ -136,7 +135,7 @@ fn run(rt: Runtime) {
136135
rt.cx(),
137136
wasm_instance.handle(),
138137
&HandleValueArray::from(&args),
139-
&mut instance.handle_mut()
138+
instance.handle_mut()
140139
));
141140
}
142141

@@ -147,7 +146,7 @@ fn run(rt: Runtime) {
147146
rt.cx(),
148147
instance.handle(),
149148
c"exports".as_ptr(),
150-
&mut exports.handle_mut()
149+
exports.handle_mut()
151150
));
152151

153152
rooted!(in(rt.cx()) let mut exports_obj = exports.to_object());
@@ -156,7 +155,7 @@ fn run(rt: Runtime) {
156155
rt.cx(),
157156
exports_obj.handle(),
158157
c"foo".as_ptr(),
159-
&mut foo.handle_mut()
158+
foo.handle_mut()
160159
));
161160

162161
// call foo and get its result

mozjs/src/gc/root.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ pub struct Handle<'a, T: 'a> {
100100
pub(crate) ptr: &'a T,
101101
}
102102

103-
#[derive(Copy, Clone)]
104103
pub struct MutableHandle<'a, T: 'a> {
105104
pub(crate) ptr: *mut T,
106105
anchor: PhantomData<&'a mut T>,
@@ -205,6 +204,26 @@ impl<'a, T> MutableHandle<'a, T> {
205204
unsafe { *self.ptr = v }
206205
}
207206

207+
/// Creates a copy of this object, with a shorter lifetime, that holds a
208+
/// mutable borrow on the original object. When you write code that wants
209+
/// to use a `MutableHandle` more than once, you will typically need to
210+
/// call `reborrow` on all but the last usage. The same way that you might
211+
/// naively clone a type to allow it to be passed to multiple functions.
212+
///
213+
/// This is the same thing that happens with regular mutable references,
214+
/// except there the compiler implicitly inserts the reborrow calls. Until
215+
/// rust gains a feature to implicitly reborrow other types, we have to do
216+
/// it by hand.
217+
pub fn reborrow<'b>(&'b mut self) -> MutableHandle<'b, T>
218+
where
219+
'a: 'b,
220+
{
221+
MutableHandle {
222+
ptr: self.ptr,
223+
anchor: PhantomData,
224+
}
225+
}
226+
208227
pub(crate) fn raw(&mut self) -> RawMutableHandle<T> {
209228
unsafe { RawMutableHandle::from_marked_location(self.ptr) }
210229
}

mozjs/src/rust.rs

+3-157
Original file line numberDiff line numberDiff line change
@@ -893,9 +893,9 @@ pub unsafe fn try_to_outerize_object(mut rval: MutableHandleObject) {
893893
}
894894

895895
#[inline]
896-
pub unsafe fn maybe_wrap_object(cx: *mut JSContext, obj: MutableHandleObject) {
896+
pub unsafe fn maybe_wrap_object(cx: *mut JSContext, mut obj: MutableHandleObject) {
897897
if get_object_realm(*obj) != get_context_realm(cx) {
898-
assert!(JS_WrapObject(cx, obj.into()));
898+
assert!(JS_WrapObject(cx, obj.reborrow().into()));
899899
}
900900
try_to_outerize_object(obj);
901901
}
@@ -1065,13 +1065,7 @@ macro_rules! capture_stack {
10651065
}
10661066
}
10671067

1068-
/** Wrappers for JSAPI methods that should NOT be used.
1069-
*
1070-
* The wrapped methods are identical except that they accept Handle and MutableHandle arguments
1071-
* that include lifetimes instead.
1072-
*
1073-
* They require MutableHandles to implement Copy. All code should migrate to jsapi_wrapped instead.
1074-
* */
1068+
/// Wrappers for JSAPI methods that accept lifetimed Handle and MutableHandle arguments
10751069
pub mod wrappers {
10761070
macro_rules! wrap {
10771071
// The invocation of @inner has the following form:
@@ -1212,151 +1206,3 @@ pub mod wrappers {
12121206
include!("jsapi_wrappers.in.rs");
12131207
include!("glue_wrappers.in.rs");
12141208
}
1215-
1216-
/** Wrappers for JSAPI methods that accept lifetimed Handle and MutableHandle arguments.
1217-
*
1218-
* The wrapped methods are identical except that they accept Handle and MutableHandle arguments
1219-
* that include lifetimes instead. Besides, they mutably borrow the mutable handles
1220-
* instead of consuming/copying them.
1221-
*
1222-
* These wrappers are preferred, js::rust::wrappers should NOT be used.
1223-
* */
1224-
pub mod jsapi_wrapped {
1225-
macro_rules! wrap {
1226-
// The invocation of @inner has the following form:
1227-
// @inner (input args) <> (argument accumulator) <> (invocation accumulator) <> unparsed tokens
1228-
// when `unparsed tokens == \eps`, accumulator contains the final result
1229-
1230-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: Handle<$gentype:ty>, $($rest:tt)*) => {
1231-
wrap!(@inner $saved <> ($($declargs)* $arg: Handle<$gentype> , ) <> ($($acc,)* $arg.into(),) <> $($rest)*);
1232-
};
1233-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: MutableHandle<$gentype:ty>, $($rest:tt)*) => {
1234-
wrap!(@inner $saved <> ($($declargs)* $arg: &mut MutableHandle<$gentype> , ) <> ($($acc,)* (*$arg).into(),) <> $($rest)*);
1235-
};
1236-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: Handle, $($rest:tt)*) => {
1237-
wrap!(@inner $saved <> ($($declargs)* $arg: Handle , ) <> ($($acc,)* $arg.into(),) <> $($rest)*);
1238-
};
1239-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: MutableHandle, $($rest:tt)*) => {
1240-
wrap!(@inner $saved <> ($($declargs)* $arg: &mut MutableHandle , ) <> ($($acc,)* (*$arg).into(),) <> $($rest)*);
1241-
};
1242-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: HandleFunction , $($rest:tt)*) => {
1243-
wrap!(@inner $saved <> ($($declargs)* $arg: HandleFunction , ) <> ($($acc,)* $arg.into(),) <> $($rest)*);
1244-
};
1245-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: HandleId , $($rest:tt)*) => {
1246-
wrap!(@inner $saved <> ($($declargs)* $arg: HandleId , ) <> ($($acc,)* $arg.into(),) <> $($rest)*);
1247-
};
1248-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: HandleObject , $($rest:tt)*) => {
1249-
wrap!(@inner $saved <> ($($declargs)* $arg: HandleObject , ) <> ($($acc,)* $arg.into(),) <> $($rest)*);
1250-
};
1251-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: HandleScript , $($rest:tt)*) => {
1252-
wrap!(@inner $saved <> ($($declargs)* $arg: HandleScript , ) <> ($($acc,)* $arg.into(),) <> $($rest)*);
1253-
};
1254-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: HandleString , $($rest:tt)*) => {
1255-
wrap!(@inner $saved <> ($($declargs)* $arg: HandleString , ) <> ($($acc,)* $arg.into(),) <> $($rest)*);
1256-
};
1257-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: HandleSymbol , $($rest:tt)*) => {
1258-
wrap!(@inner $saved <> ($($declargs)* $arg: HandleSymbol , ) <> ($($acc,)* $arg.into(),) <> $($rest)*);
1259-
};
1260-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: HandleValue , $($rest:tt)*) => {
1261-
wrap!(@inner $saved <> ($($declargs)* $arg: HandleValue , ) <> ($($acc,)* $arg.into(),) <> $($rest)*);
1262-
};
1263-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: MutableHandleFunction , $($rest:tt)*) => {
1264-
wrap!(@inner $saved <> ($($declargs)* $arg: &mut MutableHandleFunction , ) <> ($($acc,)* (*$arg).into(),) <> $($rest)*);
1265-
};
1266-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: MutableHandleId , $($rest:tt)*) => {
1267-
wrap!(@inner $saved <> ($($declargs)* $arg: &mut MutableHandleId , ) <> ($($acc,)* (*$arg).into(),) <> $($rest)*);
1268-
};
1269-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: MutableHandleObject , $($rest:tt)*) => {
1270-
wrap!(@inner $saved <> ($($declargs)* $arg: &mut MutableHandleObject , ) <> ($($acc,)* (*$arg).into(),) <> $($rest)*);
1271-
};
1272-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: MutableHandleScript , $($rest:tt)*) => {
1273-
wrap!(@inner $saved <> ($($declargs)* $arg: &mut MutableHandleScript , ) <> ($($acc,)* (*$arg).into(),) <> $($rest)*);
1274-
};
1275-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: MutableHandleString , $($rest:tt)*) => {
1276-
wrap!(@inner $saved <> ($($declargs)* $arg: &mut MutableHandleString , ) <> ($($acc,)* (*$arg).into(),) <> $($rest)*);
1277-
};
1278-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: MutableHandleSymbol , $($rest:tt)*) => {
1279-
wrap!(@inner $saved <> ($($declargs)* $arg: &mut MutableHandleSymbol , ) <> ($($acc,)* (*$arg).into(),) <> $($rest)*);
1280-
};
1281-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: MutableHandleValue , $($rest:tt)*) => {
1282-
wrap!(@inner $saved <> ($($declargs)* $arg: &mut MutableHandleValue , ) <> ($($acc,)* (*$arg).into(),) <> $($rest)*);
1283-
};
1284-
(@inner $saved:tt <> ($($declargs:tt)*) <> ($($acc:expr,)*) <> $arg:ident: $type:ty, $($rest:tt)*) => {
1285-
wrap!(@inner $saved <> ($($declargs)* $arg: $type,) <> ($($acc,)* $arg,) <> $($rest)*);
1286-
};
1287-
(@inner ($module:tt: $func_name:ident ($($args:tt)*) -> $outtype:ty) <> ($($declargs:tt)*) <> ($($argexprs:expr,)*) <> ) => {
1288-
#[inline]
1289-
pub unsafe fn $func_name($($declargs)*) -> $outtype {
1290-
$module::$func_name($($argexprs),*)
1291-
}
1292-
};
1293-
($module:tt: pub fn $func_name:ident($($args:tt)*) -> $outtype:ty) => {
1294-
wrap!(@inner ($module: $func_name ($($args)*) -> $outtype) <> () <> () <> $($args)* ,);
1295-
};
1296-
($module:tt: pub fn $func_name:ident($($args:tt)*)) => {
1297-
wrap!($module: pub fn $func_name($($args)*) -> ());
1298-
}
1299-
}
1300-
1301-
use super::*;
1302-
use crate::glue;
1303-
use crate::glue::EncodedStringCallback;
1304-
use crate::jsapi;
1305-
use crate::jsapi::mozilla::Utf8Unit;
1306-
use crate::jsapi::BigInt;
1307-
use crate::jsapi::CallArgs;
1308-
use crate::jsapi::CloneDataPolicy;
1309-
use crate::jsapi::ColumnNumberOneOrigin;
1310-
use crate::jsapi::CompartmentTransplantCallback;
1311-
use crate::jsapi::ESClass;
1312-
use crate::jsapi::ExceptionStackBehavior;
1313-
use crate::jsapi::ForOfIterator;
1314-
use crate::jsapi::ForOfIterator_NonIterableBehavior;
1315-
use crate::jsapi::HandleObjectVector;
1316-
use crate::jsapi::InstantiateOptions;
1317-
use crate::jsapi::JSClass;
1318-
use crate::jsapi::JSErrorReport;
1319-
use crate::jsapi::JSExnType;
1320-
use crate::jsapi::JSFunctionSpec;
1321-
use crate::jsapi::JSFunctionSpecWithHelp;
1322-
use crate::jsapi::JSJitInfo;
1323-
use crate::jsapi::JSONParseHandler;
1324-
use crate::jsapi::JSONWriteCallback;
1325-
use crate::jsapi::JSPrincipals;
1326-
use crate::jsapi::JSPropertySpec;
1327-
use crate::jsapi::JSPropertySpec_Name;
1328-
use crate::jsapi::JSProtoKey;
1329-
use crate::jsapi::JSScript;
1330-
use crate::jsapi::JSStructuredCloneData;
1331-
use crate::jsapi::JSType;
1332-
use crate::jsapi::Latin1Char;
1333-
use crate::jsapi::ModuleErrorBehaviour;
1334-
use crate::jsapi::MutableHandleIdVector;
1335-
use crate::jsapi::PromiseState;
1336-
use crate::jsapi::PromiseUserInputEventHandlingState;
1337-
use crate::jsapi::PropertyKey;
1338-
use crate::jsapi::ReadOnlyCompileOptions;
1339-
use crate::jsapi::Realm;
1340-
use crate::jsapi::RefPtr;
1341-
use crate::jsapi::RegExpFlags;
1342-
use crate::jsapi::ScriptEnvironmentPreparer_Closure;
1343-
use crate::jsapi::SourceText;
1344-
use crate::jsapi::StackCapture;
1345-
use crate::jsapi::StructuredCloneScope;
1346-
use crate::jsapi::Symbol;
1347-
use crate::jsapi::SymbolCode;
1348-
use crate::jsapi::TaggedColumnNumberOneOrigin;
1349-
use crate::jsapi::TwoByteChars;
1350-
use crate::jsapi::UniqueChars;
1351-
use crate::jsapi::Value;
1352-
use crate::jsapi::WasmModule;
1353-
use crate::jsapi::{ElementAdder, IsArrayAnswer, PropertyDescriptor};
1354-
use crate::jsapi::{JSContext, JSFunction, JSNative, JSObject, JSString};
1355-
use crate::jsapi::{
1356-
JSStructuredCloneCallbacks, JSStructuredCloneReader, JSStructuredCloneWriter,
1357-
};
1358-
use crate::jsapi::{MallocSizeOf, ObjectOpResult, ObjectPrivateVisitor, TabSizes};
1359-
use crate::jsapi::{SavedFrameResult, SavedFrameSelfHosted};
1360-
include!("jsapi_wrappers.in.rs");
1361-
include!("glue_wrappers.in.rs");
1362-
}

0 commit comments

Comments
 (0)