mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-31 01:11:06 +00:00
Remove unsafe
usage in backend/src/encode.rs
Using `unsafe` was just a little too eager there so let's use an off-the-shelf solution for solving the actual problem we have, which is to allocate strings with a lifetime of `Interner` rather than deduplicating strings.
This commit is contained in:
parent
93a1301c9f
commit
f3f3ebee3a
@ -15,6 +15,7 @@ spans = []
|
|||||||
extra-traits = ["syn/extra-traits"]
|
extra-traits = ["syn/extra-traits"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bumpalo = "2.1"
|
||||||
lazy_static = "1.0.0"
|
lazy_static = "1.0.0"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
proc-macro2 = "0.4.8"
|
proc-macro2 = "0.4.8"
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use proc_macro2::{Ident, Span};
|
use proc_macro2::{Ident, Span};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::HashMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@ -24,8 +24,7 @@ pub fn encode(program: &ast::Program) -> Result<EncodeResult, Diagnostic> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Interner {
|
struct Interner {
|
||||||
map: RefCell<HashMap<Ident, String>>,
|
bump: bumpalo::Bump,
|
||||||
strings: RefCell<HashSet<String>>,
|
|
||||||
files: RefCell<HashMap<String, LocalFile>>,
|
files: RefCell<HashMap<String, LocalFile>>,
|
||||||
root: PathBuf,
|
root: PathBuf,
|
||||||
crate_name: String,
|
crate_name: String,
|
||||||
@ -40,8 +39,7 @@ struct LocalFile {
|
|||||||
impl Interner {
|
impl Interner {
|
||||||
fn new() -> Interner {
|
fn new() -> Interner {
|
||||||
Interner {
|
Interner {
|
||||||
map: RefCell::new(HashMap::new()),
|
bump: bumpalo::Bump::new(),
|
||||||
strings: RefCell::new(HashSet::new()),
|
|
||||||
files: RefCell::new(HashMap::new()),
|
files: RefCell::new(HashMap::new()),
|
||||||
root: env::var_os("CARGO_MANIFEST_DIR").unwrap().into(),
|
root: env::var_os("CARGO_MANIFEST_DIR").unwrap().into(),
|
||||||
crate_name: env::var("CARGO_PKG_NAME").unwrap(),
|
crate_name: env::var("CARGO_PKG_NAME").unwrap(),
|
||||||
@ -49,22 +47,14 @@ impl Interner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn intern(&self, s: &Ident) -> &str {
|
fn intern(&self, s: &Ident) -> &str {
|
||||||
let mut map = self.map.borrow_mut();
|
self.intern_str(&s.to_string())
|
||||||
if let Some(s) = map.get(s) {
|
|
||||||
return unsafe { &*(&**s as *const str) };
|
|
||||||
}
|
|
||||||
map.insert(s.clone(), s.to_string());
|
|
||||||
unsafe { &*(&*map[s] as *const str) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn intern_str(&self, s: &str) -> &str {
|
fn intern_str(&self, s: &str) -> &str {
|
||||||
let mut strings = self.strings.borrow_mut();
|
// NB: eventually this could be used to intern `s` to only allocate one
|
||||||
if let Some(s) = strings.get(s) {
|
// copy, but for now let's just "transmute" `s` to have the same
|
||||||
return unsafe { &*(&**s as *const str) };
|
// lifetmie as this struct itself (which is our main goal here)
|
||||||
}
|
bumpalo::collections::String::from_str_in(s, &self.bump).into_bump_str()
|
||||||
strings.insert(s.to_string());
|
|
||||||
drop(strings);
|
|
||||||
self.intern_str(s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given an import to a local module `id` this generates a unique module id
|
/// Given an import to a local module `id` this generates a unique module id
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#![cfg_attr(feature = "extra-traits", deny(missing_debug_implementations))]
|
#![cfg_attr(feature = "extra-traits", deny(missing_debug_implementations))]
|
||||||
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-backend/0.2")]
|
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-backend/0.2")]
|
||||||
|
|
||||||
|
extern crate bumpalo;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
extern crate proc_macro2;
|
extern crate proc_macro2;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user