Skip to content
This repository was archived by the owner on Nov 12, 2022. It is now read-only.

Commit 03db1ae

Browse files
author
bors-servo
authored
Auto merge of #393 - marmistrz:master, r=jdm
Add lifetimes for Handle and MutableHandle Servo compiled fine for me with the following patch set. Since the `From` trait consumes the object, I created a `raw` method for `MutableHandle`. This is not ideal, since `into()` would be more idiomatic. Is there anything better I could do or should I just leave it as it is? There are two questions marked with `// REVIEW` since I'm not sure if the explicit lifetimes are the same what compiler inferred. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-mozjs/393) <!-- Reviewable:end -->
2 parents 0ff4db7 + 709357f commit 03db1ae

13 files changed

+711
-91
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "mozjs"
33
description = "Rust bindings to the Mozilla SpiderMonkey JavaScript engine."
44
repository = "https://github.com/servo/rust-mozjs"
5-
version = "0.5.0"
5+
version = "0.6.0"
66
authors = ["The Servo Project Developers"]
77
build = "build.rs"
88
license = "MPL-2.0"

src/conversions.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@
3030
use error::throw_type_error;
3131
use glue::RUST_JS_NumberValue;
3232
use jsapi::AssertSameCompartment;
33-
use jsapi::{ForOfIterator, ForOfIterator_NonIterableBehavior, HandleValue};
33+
use jsapi::{ForOfIterator, ForOfIterator_NonIterableBehavior};
3434
use jsapi::{Heap, JS_DefineElement, JS_GetLatin1StringCharsAndLength};
3535
use jsapi::{JS_GetTwoByteStringCharsAndLength, JS_NewArrayObject1};
3636
use jsapi::{JS_NewUCStringCopyN, JSPROP_ENUMERATE, JS_StringHasLatin1Chars};
37-
use jsapi::{JSContext, JSObject, JSString, MutableHandleValue, RootedObject};
37+
use jsapi::{JSContext, JSObject, JSString, RootedObject};
3838
use jsval::{BooleanValue, Int32Value, NullValue, UInt32Value, UndefinedValue};
3939
use jsval::{JSVal, ObjectValue, ObjectOrNullValue, StringValue};
4040
use rust::{ToBoolean, ToInt32, ToInt64, ToNumber, ToUint16, ToUint32, ToUint64};
4141
use rust::{ToString, maybe_wrap_object_or_null_value, maybe_wrap_object_value};
42+
use rust::{HandleValue, MutableHandleValue};
4243
use rust::maybe_wrap_value;
4344
use libc;
4445
use num_traits::{Bounded, Zero};
@@ -180,7 +181,7 @@ fn clamp_to<D>(d: f64) -> D
180181
// https://heycam.github.io/webidl/#es-void
181182
impl ToJSValConvertible for () {
182183
#[inline]
183-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
184+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
184185
rval.set(UndefinedValue());
185186
}
186187
}
@@ -197,23 +198,23 @@ impl FromJSValConvertible for JSVal {
197198

198199
impl ToJSValConvertible for JSVal {
199200
#[inline]
200-
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
201+
unsafe fn to_jsval(&self, cx: *mut JSContext, mut rval: MutableHandleValue) {
201202
rval.set(*self);
202203
maybe_wrap_value(cx, rval);
203204
}
204205
}
205206

206-
impl ToJSValConvertible for HandleValue {
207+
impl<'a> ToJSValConvertible for HandleValue<'a> {
207208
#[inline]
208-
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
209+
unsafe fn to_jsval(&self, cx: *mut JSContext, mut rval: MutableHandleValue) {
209210
rval.set(self.get());
210211
maybe_wrap_value(cx, rval);
211212
}
212213
}
213214

214215
impl ToJSValConvertible for Heap<JSVal> {
215216
#[inline]
216-
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
217+
unsafe fn to_jsval(&self, cx: *mut JSContext, mut rval: MutableHandleValue) {
217218
rval.set(self.get());
218219
maybe_wrap_value(cx, rval);
219220
}
@@ -238,7 +239,7 @@ unsafe fn convert_int_from_jsval<T, M>(cx: *mut JSContext, value: HandleValue,
238239
// https://heycam.github.io/webidl/#es-boolean
239240
impl ToJSValConvertible for bool {
240241
#[inline]
241-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
242+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
242243
rval.set(BooleanValue(*self));
243244
}
244245
}
@@ -254,7 +255,7 @@ impl FromJSValConvertible for bool {
254255
// https://heycam.github.io/webidl/#es-byte
255256
impl ToJSValConvertible for i8 {
256257
#[inline]
257-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
258+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
258259
rval.set(Int32Value(*self as i32));
259260
}
260261
}
@@ -273,7 +274,7 @@ impl FromJSValConvertible for i8 {
273274
// https://heycam.github.io/webidl/#es-octet
274275
impl ToJSValConvertible for u8 {
275276
#[inline]
276-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
277+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
277278
rval.set(Int32Value(*self as i32));
278279
}
279280
}
@@ -292,7 +293,7 @@ impl FromJSValConvertible for u8 {
292293
// https://heycam.github.io/webidl/#es-short
293294
impl ToJSValConvertible for i16 {
294295
#[inline]
295-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
296+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
296297
rval.set(Int32Value(*self as i32));
297298
}
298299
}
@@ -311,7 +312,7 @@ impl FromJSValConvertible for i16 {
311312
// https://heycam.github.io/webidl/#es-unsigned-short
312313
impl ToJSValConvertible for u16 {
313314
#[inline]
314-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
315+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
315316
rval.set(Int32Value(*self as i32));
316317
}
317318
}
@@ -330,7 +331,7 @@ impl FromJSValConvertible for u16 {
330331
// https://heycam.github.io/webidl/#es-long
331332
impl ToJSValConvertible for i32 {
332333
#[inline]
333-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
334+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
334335
rval.set(Int32Value(*self));
335336
}
336337
}
@@ -349,7 +350,7 @@ impl FromJSValConvertible for i32 {
349350
// https://heycam.github.io/webidl/#es-unsigned-long
350351
impl ToJSValConvertible for u32 {
351352
#[inline]
352-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
353+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
353354
rval.set(UInt32Value(*self));
354355
}
355356
}
@@ -368,7 +369,7 @@ impl FromJSValConvertible for u32 {
368369
// https://heycam.github.io/webidl/#es-long-long
369370
impl ToJSValConvertible for i64 {
370371
#[inline]
371-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
372+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
372373
rval.set(RUST_JS_NumberValue(*self as f64));
373374
}
374375
}
@@ -387,7 +388,7 @@ impl FromJSValConvertible for i64 {
387388
// https://heycam.github.io/webidl/#es-unsigned-long-long
388389
impl ToJSValConvertible for u64 {
389390
#[inline]
390-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
391+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
391392
rval.set(RUST_JS_NumberValue(*self as f64));
392393
}
393394
}
@@ -406,7 +407,7 @@ impl FromJSValConvertible for u64 {
406407
// https://heycam.github.io/webidl/#es-float
407408
impl ToJSValConvertible for f32 {
408409
#[inline]
409-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
410+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
410411
rval.set(RUST_JS_NumberValue(*self as f64));
411412
}
412413
}
@@ -423,7 +424,7 @@ impl FromJSValConvertible for f32 {
423424
// https://heycam.github.io/webidl/#es-double
424425
impl ToJSValConvertible for f64 {
425426
#[inline]
426-
unsafe fn to_jsval(&self, _cx: *mut JSContext, rval: MutableHandleValue) {
427+
unsafe fn to_jsval(&self, _cx: *mut JSContext, mut rval: MutableHandleValue) {
427428
rval.set(RUST_JS_NumberValue(*self));
428429
}
429430
}
@@ -467,7 +468,7 @@ pub unsafe fn jsstr_to_string(cx: *mut JSContext, jsstr: *mut JSString) -> Strin
467468
// https://heycam.github.io/webidl/#es-USVString
468469
impl ToJSValConvertible for str {
469470
#[inline]
470-
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
471+
unsafe fn to_jsval(&self, cx: *mut JSContext, mut rval: MutableHandleValue) {
471472
let mut string_utf16: Vec<u16> = Vec::with_capacity(self.len());
472473
string_utf16.extend(self.encode_utf16());
473474
let jsstr = JS_NewUCStringCopyN(cx,
@@ -503,7 +504,7 @@ impl FromJSValConvertible for String {
503504

504505
impl<T: ToJSValConvertible> ToJSValConvertible for Option<T> {
505506
#[inline]
506-
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
507+
unsafe fn to_jsval(&self, cx: *mut JSContext, mut rval: MutableHandleValue) {
507508
match self {
508509
&Some(ref value) => value.to_jsval(cx, rval),
509510
&None => rval.set(NullValue()),
@@ -538,16 +539,16 @@ impl<T: FromJSValConvertible> FromJSValConvertible for Option<T> {
538539
// https://heycam.github.io/webidl/#es-sequence
539540
impl<T: ToJSValConvertible> ToJSValConvertible for [T] {
540541
#[inline]
541-
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
542+
unsafe fn to_jsval(&self, cx: *mut JSContext, mut rval: MutableHandleValue) {
542543
rooted!(in(cx) let js_array = JS_NewArrayObject1(cx, self.len() as libc::size_t));
543544
assert!(!js_array.handle().is_null());
544545

545546
rooted!(in(cx) let mut val = UndefinedValue());
546547
for (index, obj) in self.iter().enumerate() {
547548
obj.to_jsval(cx, val.handle_mut());
548549

549-
assert!(JS_DefineElement(cx, js_array.handle(),
550-
index as u32, val.handle(), JSPROP_ENUMERATE, None, None));
550+
assert!(JS_DefineElement(cx, js_array.handle().into(),
551+
index as u32, val.handle().into(), JSPROP_ENUMERATE, None, None));
551552
}
552553

553554
rval.set(ObjectValue(js_array.handle().get()));
@@ -604,7 +605,7 @@ impl<C: Clone, T: FromJSValConvertible<Config=C>> FromJSValConvertible for Vec<T
604605
let iterator = ForOfIteratorGuard::new(cx, &mut iterator);
605606
let iterator = &mut *iterator.root;
606607

607-
if !iterator.init(value, ForOfIterator_NonIterableBehavior::AllowNonIterable) {
608+
if !iterator.init(value.into(), ForOfIterator_NonIterableBehavior::AllowNonIterable) {
608609
return Err(())
609610
}
610611

@@ -617,7 +618,7 @@ impl<C: Clone, T: FromJSValConvertible<Config=C>> FromJSValConvertible for Vec<T
617618
loop {
618619
let mut done = false;
619620
rooted!(in(cx) let mut val = UndefinedValue());
620-
if !iterator.next(val.handle_mut(), &mut done) {
621+
if !iterator.next(val.handle_mut().into(), &mut done) {
621622
return Err(())
622623
}
623624

@@ -638,7 +639,7 @@ impl<C: Clone, T: FromJSValConvertible<Config=C>> FromJSValConvertible for Vec<T
638639
// https://heycam.github.io/webidl/#es-object
639640
impl ToJSValConvertible for *mut JSObject {
640641
#[inline]
641-
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
642+
unsafe fn to_jsval(&self, cx: *mut JSContext, mut rval: MutableHandleValue) {
642643
rval.set(ObjectOrNullValue(*self));
643644
maybe_wrap_object_or_null_value(cx, rval);
644645
}
@@ -647,7 +648,7 @@ impl ToJSValConvertible for *mut JSObject {
647648
// https://heycam.github.io/webidl/#es-object
648649
impl ToJSValConvertible for ptr::NonNull<JSObject> {
649650
#[inline]
650-
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
651+
unsafe fn to_jsval(&self, cx: *mut JSContext, mut rval: MutableHandleValue) {
651652
rval.set(ObjectValue(self.as_ptr()));
652653
maybe_wrap_object_value(cx, rval);
653654
}
@@ -656,7 +657,7 @@ impl ToJSValConvertible for ptr::NonNull<JSObject> {
656657
// https://heycam.github.io/webidl/#es-object
657658
impl ToJSValConvertible for Heap<*mut JSObject> {
658659
#[inline]
659-
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
660+
unsafe fn to_jsval(&self, cx: *mut JSContext, mut rval: MutableHandleValue) {
660661
rval.set(ObjectOrNullValue(self.get()));
661662
maybe_wrap_object_or_null_value(cx, rval);
662663
}

src/generate_wrappers.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
# This is one big heuristic but seems to work well enough
3+
grep_heur() {
4+
grep -v "link_name" "$1" | \
5+
grep -v '"\]' | \
6+
grep -F -v '/\*\*' | \
7+
sed -z 's/,\n */, /g' | \
8+
sed -z 's/:\n */: /g' | \
9+
sed -z 's/\n *->/ ->/g' | \
10+
grep -v '^\}$' | \
11+
sed 's/^ *pub/pub/' | \
12+
sed -z 's/\;\n/\n/g' | \
13+
grep 'pub fn' | \
14+
grep Handle | \
15+
grep -v roxyHandler | \
16+
grep -v 'pub fn Unbox' | # this function seems to be platform specific \
17+
sed 's/Handle<\*mut JSObject>/HandleObject/g'
18+
}
19+
20+
grep_heur jsapi_linux_64.rs | sed 's/\(.*\)/wrap!(jsapi: \1);/g' > jsapi_wrappers.in
21+
grep_heur glue.rs | sed 's/\(.*\)/wrap!(glue: \1);/g' > glue_wrappers.in

src/glue_wrappers.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
wrap!(glue: pub fn InvokeGetOwnPropertyDescriptor(handler: *const ::libc::c_void, cx: *mut JSContext, proxy: HandleObject, id: HandleId, desc: MutableHandle<PropertyDescriptor>) -> bool);
2+
wrap!(glue: pub fn InvokeHasOwn(handler: *const ::libc::c_void, cx: *mut JSContext, proxy: HandleObject, id: HandleId, bp: *mut bool) -> bool);
3+
wrap!(glue: pub fn CallJitGetterOp(info: *const JSJitInfo, cx: *mut JSContext, thisObj: HandleObject, specializedThis: *mut ::libc::c_void, argc: u32, vp: *mut Value) -> bool);
4+
wrap!(glue: pub fn CallJitSetterOp(info: *const JSJitInfo, cx: *mut JSContext, thisObj: HandleObject, specializedThis: *mut ::libc::c_void, argc: u32, vp: *mut Value) -> bool);
5+
wrap!(glue: pub fn CallJitMethodOp(info: *const JSJitInfo, cx: *mut JSContext, thisObj: HandleObject, specializedThis: *mut ::libc::c_void, argc: u32, vp: *mut Value) -> bool);
6+
wrap!(glue: pub fn NewProxyObject(aCx: *mut JSContext, aHandler: *const ::libc::c_void, aPriv: HandleValue, proto: *mut JSObject, parent: *mut JSObject, call: *mut JSObject, construct: *mut JSObject) -> *mut JSObject);
7+
wrap!(glue: pub fn WrapperNew(aCx: *mut JSContext, aObj: HandleObject, aHandler: *const ::libc::c_void, aClass: *const JSClass, aSingleton: bool) -> *mut JSObject);
8+
wrap!(glue: pub fn NewWindowProxy(aCx: *mut JSContext, aObj: HandleObject, aHandler: *const ::libc::c_void) -> *mut JSObject);
9+
wrap!(glue: pub fn RUST_JSID_IS_INT(id: HandleId) -> bool);
10+
wrap!(glue: pub fn RUST_JSID_TO_INT(id: HandleId) -> i32);
11+
wrap!(glue: pub fn RUST_JSID_IS_STRING(id: HandleId) -> bool);
12+
wrap!(glue: pub fn RUST_JSID_TO_STRING(id: HandleId) -> *mut JSString);

0 commit comments

Comments
 (0)