Shifting the unsafety guarantees around

This commit is contained in:
Pauan 2019-07-18 04:47:10 +02:00
parent c3676bc6f9
commit 2405fad319
3 changed files with 10 additions and 10 deletions

8
src/cache/intern.rs vendored
View File

@ -8,6 +8,7 @@ cfg_if! {
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::cell::RefCell; use std::cell::RefCell;
use crate::JsValue; use crate::JsValue;
use crate::convert::IntoWasmAbi;
use uluru::{LRUCache, Entry}; use uluru::{LRUCache, Entry};
@ -33,13 +34,14 @@ cfg_if! {
cache.find(|p| p.key == key).map(|x| &x.value) cache.find(|p| p.key == key).map(|x| &x.value)
} }
pub(crate) fn get_str(s: &str) -> Option<JsValue> { /// This returns the raw index of the cached JsValue, so you must take care
/// so that you don't use it after it is freed.
pub(crate) fn unsafe_get_str(s: &str) -> Option<<JsValue as IntoWasmAbi>::Abi> {
CACHE.with(|cache| { CACHE.with(|cache| {
let mut cache = cache.entries.borrow_mut(); let mut cache = cache.entries.borrow_mut();
if let Some(value) = get_js_string(&mut cache, s) { if let Some(value) = get_js_string(&mut cache, s) {
// This is safe because the cache values are never removed Some(value.into_abi())
Some(value._unsafe_clone())
} else { } else {
None None

View File

@ -129,8 +129,11 @@ cfg_if! {
if #[cfg(feature = "enable-interning")] { if #[cfg(feature = "enable-interning")] {
#[inline] #[inline]
fn get_cached_str(x: &str) -> Option<WasmSlice> { fn get_cached_str(x: &str) -> Option<WasmSlice> {
// This uses 0 for the ptr as an indication that it is a JsValue and not a str // This is safe because the JsValue is immediately converted into a JS string,
crate::cache::intern::get_str(x).map(|x| WasmSlice { ptr: 0, len: x.into_abi() }) // so use-after-free cannot occur.
//
// This uses 0 for the ptr as an indication that it is a JsValue and not a str.
crate::cache::intern::unsafe_get_str(x).map(|x| WasmSlice { ptr: 0, len: x })
} }
} else { } else {

View File

@ -125,11 +125,6 @@ impl JsValue {
} }
} }
#[inline]
fn _unsafe_clone(&self) -> JsValue {
Self::_new(self.idx)
}
/// Creates a new JS value which is a string. /// Creates a new JS value which is a string.
/// ///
/// The utf-8 string provided is copied to the JS heap and the string will /// The utf-8 string provided is copied to the JS heap and the string will