Skip to content

Commit 5a46eb5

Browse files
Hywanwaywardmonkeys
authored andcommitted
doc: Improve doc and add examples.
1 parent dc3f41b commit 5a46eb5

File tree

2 files changed

+68
-30
lines changed

2 files changed

+68
-30
lines changed

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub struct JSString {
126126
/// A JavaScript Typed Array.
127127
///
128128
/// A Typed Array is a special JavaScript object that represent a family of
129-
/// buffer view. Learn more by [reading the documentation][doc].
129+
/// buffer views. Learn more by [reading the documentation][doc].
130130
///
131131
/// [doc]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#behavior_when_viewing_a_resizable_buffer
132132
pub struct JSTypedArray {

src/typed_array.rs

+67-29
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl JSTypedArray {
1212
///
1313
/// # Safety
1414
///
15-
/// Ensure `raw` is valid.
15+
/// Ensure `raw` is valid, and represents a typed array.
1616
pub(crate) unsafe fn from_raw(ctx: sys::JSContextRef, raw: sys::JSObjectRef) -> Self {
1717
Self { ctx, raw }
1818
}
@@ -24,13 +24,7 @@ impl JSTypedArray {
2424
/// ```rust
2525
/// # use javascriptcore::*;
2626
/// let ctx = JSContext::default();
27-
/// let array = evaluate_script(
28-
/// &ctx,
29-
/// "new Uint8Array([1, 2, 3, 4, 5])",
30-
/// None,
31-
/// "foo.js",
32-
/// 1,
33-
/// )
27+
/// let array = evaluate_script(&ctx, "new Uint8Array([1, 2, 3, 4, 5])", None, "foo.js", 1)
3428
/// .unwrap()
3529
/// .as_typed_array()
3630
/// .unwrap();
@@ -52,13 +46,7 @@ impl JSTypedArray {
5246
/// ```rust
5347
/// # use javascriptcore::*;
5448
/// let ctx = JSContext::default();
55-
/// let array = evaluate_script(
56-
/// &ctx,
57-
/// "new Uint8Array([1, 2, 3, 4, 5])",
58-
/// None,
59-
/// "foo.js",
60-
/// 1,
61-
/// )
49+
/// let array = evaluate_script(&ctx, "new Uint8Array([1, 2, 3, 4, 5])", None, "foo.js", 1)
6250
/// .unwrap()
6351
/// .as_typed_array()
6452
/// .unwrap();
@@ -78,16 +66,14 @@ impl JSTypedArray {
7866

7967
/// Returns the byte offset of the Typed Array.
8068
///
69+
/// The _byte offset_ is the offset used when a Typed Array is created from
70+
/// another Typed Array, it's a “subview” that can start from an offset, up to a
71+
/// certain length, which is the byte length.
72+
///
8173
/// ```rust
8274
/// # use javascriptcore::*;
8375
/// let ctx = JSContext::default();
84-
/// let array = evaluate_script(
85-
/// &ctx,
86-
/// "const array = new Uint8Array([1, 2, 3, 4, 5]); new Uint8Array(array.buffer, 3)",
87-
/// None,
88-
/// "foo.js",
89-
/// 1,
90-
/// )
76+
/// let array = evaluate_script(&ctx, "const array = new Uint8Array([1, 2, 3, 4, 5]); new Uint8Array(array.buffer, 3)", None, "foo.js", 1)
9177
/// .unwrap()
9278
/// .as_typed_array()
9379
/// .unwrap();
@@ -106,16 +92,15 @@ impl JSTypedArray {
10692
}
10793

10894
/// Returns the byte length of the Typed Array.
95+
///
96+
/// The _byte length_ is the length used when a Typed Array is created from
97+
/// another Typed Array, it's a “subview” that can start from an offset, up to a
98+
/// certain length, which is the byte length.
99+
///
109100
/// ```rust
110101
/// # use javascriptcore::*;
111102
/// let ctx = JSContext::default();
112-
/// let array = evaluate_script(
113-
/// &ctx,
114-
/// "const array = new Uint8Array([1, 2, 3, 4, 5]); new Uint8Array(array.buffer, 1, 2)",
115-
/// None,
116-
/// "foo.js",
117-
/// 1,
118-
/// )
103+
/// let array = evaluate_script(&ctx, "const array = new Uint8Array([1, 2, 3, 4, 5]); new Uint8Array(array.buffer, 1, 2)", None, "foo.js", 1)
119104
/// .unwrap()
120105
/// .as_typed_array()
121106
/// .unwrap();
@@ -140,6 +125,47 @@ impl JSTypedArray {
140125
///
141126
/// The pointer of the slice returned by this function is temporary and is not
142127
/// guaranteed to remain valid across JavaScriptCore API calls.
128+
///
129+
/// # Example
130+
///
131+
/// ```rust
132+
/// # use javascriptcore::*;
133+
/// let ctx = JSContext::default();
134+
///
135+
/// /// Create a Typed Array from the Rust API.
136+
/// let mut bytes = vec![1u8, 2, 3, 4, 5];
137+
/// let array_as_value =
138+
/// unsafe { JSValue::new_typed_array_with_bytes(&ctx, bytes.as_mut_slice()) }.unwrap();
139+
/// let mut array = array_as_value.as_typed_array().unwrap();
140+
///
141+
/// ctx.global_object().unwrap().set_property("array", array_as_value).unwrap();
142+
///
143+
/// /// Create a sub-Typed Array from `array` in JavaScript.
144+
/// let mut sub_array = evaluate_script(
145+
/// &ctx,
146+
/// "new Uint8Array(array.buffer, 1, 3)",
147+
/// None,
148+
/// "foo.js",
149+
/// 1,
150+
/// )
151+
/// .unwrap()
152+
/// .as_typed_array()
153+
/// .unwrap();
154+
///
155+
/// let sub_slice = unsafe { sub_array.as_mut_slice() }.unwrap();
156+
///
157+
/// // Items are untouched.
158+
/// assert_eq!(sub_slice, &[2, 3, 4]);
159+
/// assert_eq!(bytes, &[1, 2, 3, 4, 5]);
160+
///
161+
/// // Now let's mutate them.
162+
/// sub_slice[0] = 12;
163+
/// sub_slice[2] = 14;
164+
///
165+
/// // See, they are mutated.
166+
/// assert_eq!(sub_slice, &[12, 3, 14]);
167+
/// assert_eq!(bytes, &[1, 12, 3, 14, 5]);
168+
/// ```
143169
pub unsafe fn as_mut_slice(&mut self) -> Result<&mut [u8], JSException> {
144170
self.as_mut_slice_impl()
145171
}
@@ -165,6 +191,18 @@ impl JSTypedArray {
165191

166192
/// Returns a `Vec` (so a copy) of the underlying buffer represented by the
167193
/// Typed Array.
194+
///
195+
/// ```rust
196+
/// # use javascriptcore::*;
197+
/// let ctx = JSContext::default();
198+
///
199+
/// let mut array = evaluate_script(&ctx, "const array = new Uint8Array([1, 2, 3, 4, 5]); new Uint8Array(array.buffer, 1, 3)", None, "foo.js", 1)
200+
/// .unwrap()
201+
/// .as_typed_array()
202+
/// .unwrap();
203+
///
204+
/// assert_eq!(array.to_vec().unwrap(), &[2, 3, 4]);
205+
/// ```
168206
pub fn to_vec(&self) -> Result<Vec<u8>, JSException> {
169207
Ok(unsafe { self.as_mut_slice_impl() }?.to_vec())
170208
}

0 commit comments

Comments
 (0)