File tree Expand file tree Collapse file tree 3 files changed +10
-10
lines changed Expand file tree Collapse file tree 3 files changed +10
-10
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ cfg_if! {
8
8
use std:: borrow:: ToOwned ;
9
9
use std:: cell:: RefCell ;
10
10
use crate :: JsValue ;
11
+ use crate :: convert:: IntoWasmAbi ;
11
12
use uluru:: { LRUCache , Entry } ;
12
13
13
14
@@ -33,13 +34,14 @@ cfg_if! {
33
34
cache. find( |p| p. key == key) . map( |x| & x. value)
34
35
}
35
36
36
- pub ( crate ) fn get_str( s: & str ) -> Option <JsValue > {
37
+ /// This returns the raw index of the cached JsValue, so you must take care
38
+ /// so that you don't use it after it is freed.
39
+ pub ( crate ) fn unsafe_get_str( s: & str ) -> Option <<JsValue as IntoWasmAbi >:: Abi > {
37
40
CACHE . with( |cache| {
38
41
let mut cache = cache. entries. borrow_mut( ) ;
39
42
40
43
if let Some ( value) = get_js_string( & mut cache, s) {
41
- // This is safe because the cache values are never removed
42
- Some ( value. _unsafe_clone( ) )
44
+ Some ( value. into_abi( ) )
43
45
44
46
} else {
45
47
None
Original file line number Diff line number Diff line change @@ -129,8 +129,11 @@ cfg_if! {
129
129
if #[ cfg( feature = "enable-interning" ) ] {
130
130
#[ inline]
131
131
fn get_cached_str( x: & str ) -> Option <WasmSlice > {
132
- // This uses 0 for the ptr as an indication that it is a JsValue and not a str
133
- crate :: cache:: intern:: get_str( x) . map( |x| WasmSlice { ptr: 0 , len: x. into_abi( ) } )
132
+ // This is safe because the JsValue is immediately converted into a JS string,
133
+ // so use-after-free cannot occur.
134
+ //
135
+ // This uses 0 for the ptr as an indication that it is a JsValue and not a str.
136
+ crate :: cache:: intern:: unsafe_get_str( x) . map( |x| WasmSlice { ptr: 0 , len: x } )
134
137
}
135
138
136
139
} else {
Original file line number Diff line number Diff line change @@ -125,11 +125,6 @@ impl JsValue {
125
125
}
126
126
}
127
127
128
- #[ inline]
129
- fn _unsafe_clone ( & self ) -> JsValue {
130
- Self :: _new ( self . idx )
131
- }
132
-
133
128
/// Creates a new JS value which is a string.
134
129
///
135
130
/// The utf-8 string provided is copied to the JS heap and the string will
You can’t perform that action at this time.
0 commit comments