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

Commit 93e59ef

Browse files
author
bors-servo
authored
Auto merge of #339 - servo:conversion-heap, r=nox
Implement conversion traits for Heap. <!-- 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/339) <!-- Reviewable:end -->
2 parents b391ec6 + 701d809 commit 93e59ef

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/conversions.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use core::nonzero::NonZero;
3131
use error::throw_type_error;
3232
use glue::RUST_JS_NumberValue;
3333
use jsapi::{ForOfIterator, ForOfIterator_NonIterableBehavior, HandleValue};
34-
use jsapi::{JS_DefineElement, JS_GetLatin1StringCharsAndLength};
34+
use jsapi::{Heap, JS_DefineElement, JS_GetLatin1StringCharsAndLength};
3535
use jsapi::{JS_GetTwoByteStringCharsAndLength, JS_NewArrayObject1};
3636
use jsapi::{JS_NewUCStringCopyN, JSPROP_ENUMERATE, JS_StringHasLatin1Chars};
3737
use jsapi::{JSContext, JSObject, JSString, MutableHandleValue, RootedObject};
@@ -184,14 +184,6 @@ impl ToJSValConvertible for () {
184184
}
185185
}
186186

187-
impl ToJSValConvertible for JSVal {
188-
#[inline]
189-
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
190-
rval.set(*self);
191-
maybe_wrap_value(cx, rval);
192-
}
193-
}
194-
195187
impl FromJSValConvertible for JSVal {
196188
type Config = ();
197189
unsafe fn from_jsval(_cx: *mut JSContext,
@@ -202,6 +194,24 @@ impl FromJSValConvertible for JSVal {
202194
}
203195
}
204196

197+
impl FromJSValConvertible for Heap<JSVal> {
198+
type Config = ();
199+
unsafe fn from_jsval(_cx: *mut JSContext,
200+
value: HandleValue,
201+
_option: ())
202+
-> Result<ConversionResult<Self>, ()> {
203+
Ok(ConversionResult::Success(Heap::new(value.get())))
204+
}
205+
}
206+
207+
impl ToJSValConvertible for JSVal {
208+
#[inline]
209+
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
210+
rval.set(*self);
211+
maybe_wrap_value(cx, rval);
212+
}
213+
}
214+
205215
impl ToJSValConvertible for HandleValue {
206216
#[inline]
207217
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
@@ -210,6 +220,14 @@ impl ToJSValConvertible for HandleValue {
210220
}
211221
}
212222

223+
impl ToJSValConvertible for Heap<JSVal> {
224+
#[inline]
225+
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
226+
rval.set(self.get());
227+
maybe_wrap_value(cx, rval);
228+
}
229+
}
230+
213231
#[inline]
214232
unsafe fn convert_int_from_jsval<T, M>(cx: *mut JSContext, value: HandleValue,
215233
option: ConversionBehavior,
@@ -630,3 +648,12 @@ impl ToJSValConvertible for NonZero<*mut JSObject> {
630648
maybe_wrap_object_value(cx, rval);
631649
}
632650
}
651+
652+
// https://heycam.github.io/webidl/#es-object
653+
impl ToJSValConvertible for Heap<*mut JSObject> {
654+
#[inline]
655+
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
656+
rval.set(ObjectOrNullValue(self.get()));
657+
maybe_wrap_object_or_null_value(cx, rval);
658+
}
659+
}

0 commit comments

Comments
 (0)