@@ -12,7 +12,7 @@ impl JSTypedArray {
12
12
///
13
13
/// # Safety
14
14
///
15
- /// Ensure `raw` is valid.
15
+ /// Ensure `raw` is valid, and represents a typed array .
16
16
pub ( crate ) unsafe fn from_raw ( ctx : sys:: JSContextRef , raw : sys:: JSObjectRef ) -> Self {
17
17
Self { ctx, raw }
18
18
}
@@ -24,13 +24,7 @@ impl JSTypedArray {
24
24
/// ```rust
25
25
/// # use javascriptcore::*;
26
26
/// 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)
34
28
/// .unwrap()
35
29
/// .as_typed_array()
36
30
/// .unwrap();
@@ -52,13 +46,7 @@ impl JSTypedArray {
52
46
/// ```rust
53
47
/// # use javascriptcore::*;
54
48
/// 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)
62
50
/// .unwrap()
63
51
/// .as_typed_array()
64
52
/// .unwrap();
@@ -78,16 +66,14 @@ impl JSTypedArray {
78
66
79
67
/// Returns the byte offset of the Typed Array.
80
68
///
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
+ ///
81
73
/// ```rust
82
74
/// # use javascriptcore::*;
83
75
/// 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)
91
77
/// .unwrap()
92
78
/// .as_typed_array()
93
79
/// .unwrap();
@@ -106,16 +92,15 @@ impl JSTypedArray {
106
92
}
107
93
108
94
/// 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
+ ///
109
100
/// ```rust
110
101
/// # use javascriptcore::*;
111
102
/// 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)
119
104
/// .unwrap()
120
105
/// .as_typed_array()
121
106
/// .unwrap();
@@ -140,6 +125,47 @@ impl JSTypedArray {
140
125
///
141
126
/// The pointer of the slice returned by this function is temporary and is not
142
127
/// 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
+ /// ```
143
169
pub unsafe fn as_mut_slice ( & mut self ) -> Result < & mut [ u8 ] , JSException > {
144
170
self . as_mut_slice_impl ( )
145
171
}
@@ -165,6 +191,18 @@ impl JSTypedArray {
165
191
166
192
/// Returns a `Vec` (so a copy) of the underlying buffer represented by the
167
193
/// 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
+ /// ```
168
206
pub fn to_vec ( & self ) -> Result < Vec < u8 > , JSException > {
169
207
Ok ( unsafe { self . as_mut_slice_impl ( ) } ?. to_vec ( ) )
170
208
}
0 commit comments