Find static descriptions inside data nodes

Right now this library only works if the static description is the entire data
node, but with upcoming LLD support everything will be in one data node. This
updates the logic for finding/parsing the program to search through the entire
data node and also know how big a program description is when it finds it.
This commit is contained in:
Alex Crichton
2018-02-10 10:05:43 -08:00
parent 7f94c662b9
commit 75837925e9
2 changed files with 44 additions and 46 deletions

View File

@ -134,31 +134,20 @@ fn extract_programs(module: &mut Module) -> Vec<shared::Program> {
None => return ret, None => return ret,
}; };
'outer: for entry in data.entries() {
for i in (0..data.entries().len()).rev() { let mut value = bytes_to_u32(entry.value());
{ loop {
let mut value = bytes_to_u32(data.entries()[i].value()); match value.iter().position(|i| i.0 == 0x30d97887) {
loop { Some(i) => value = &value[i + 1..],
match value.iter().position(|i| i.0 == (b'w' as u32)) { None => break,
Some(i) => value = &value[i + 1..],
None => continue 'outer,
}
match value.iter().position(|i| i.0 == (b'b' as u32)) {
Some(i) => value = &value[i + 1..],
None => continue 'outer,
}
match value.iter().position(|i| i.0 == (b'g' as u32)) {
Some(i) => value = &value[i + 1..],
None => continue 'outer,
}
match value.iter().position(|i| i.0 == (b':' as u32)) {
Some(i) => value = &value[i + 1..],
None => continue 'outer,
}
break
} }
// TODO: shouldn't take the rest of the value if value.get(0).map(|c| c.0) != Some(0xd4182f61) {
let json = value.iter() continue
}
let cnt = value[1].0 as usize;
let (a, b) = value[2..].split_at(cnt);
value = b;
let json = a.iter()
.map(|i| char::from_u32(i.0).unwrap()) .map(|i| char::from_u32(i.0).unwrap())
.collect::<String>(); .collect::<String>();
let p = match serde_json::from_str(&json) { let p = match serde_json::from_str(&json) {
@ -169,7 +158,6 @@ fn extract_programs(module: &mut Module) -> Vec<shared::Program> {
}; };
ret.push(p); ret.push(p);
} }
data.entries_mut().remove(i);
} }
return ret return ret
} }

View File

@ -277,28 +277,38 @@ impl Program {
} }
pub fn wbg_literal(&self, dst: &mut Tokens) -> usize { pub fn wbg_literal(&self, dst: &mut Tokens) -> usize {
let mut a = LiteralBuilder { let mut tmp = Tokens::new();
dst, let cnt = {
cnt: 0, let mut a = LiteralBuilder {
dst: &mut tmp,
cnt: 0,
};
a.fields(&[
("exports", &|a| a.list(&self.exports, Export::wbg_literal)),
("imports", &|a| a.list(&self.imports, Import::wbg_literal)),
("custom_type_names", &|a| {
let names = self.exports.iter()
.filter_map(|e| e.class)
.collect::<BTreeSet<_>>();
a.list(&names, |s, a| {
let val = shared::name_to_descriptor(s.as_ref());
a.fields(&[
("descriptor", &|a| a.char(val)),
("name", &|a| a.str(s.as_ref()))
]);
})
}),
]);
a.cnt
}; };
a.append("wbg:"); let cnt = cnt as u32;
a.fields(&[ (quote! {
("exports", &|a| a.list(&self.exports, Export::wbg_literal)), 0x30d97887,
("imports", &|a| a.list(&self.imports, Import::wbg_literal)), 0xd4182f61,
("custom_type_names", &|a| { #cnt,
let names = self.exports.iter() }).to_tokens(dst);
.filter_map(|e| e.class) tmp.to_tokens(dst);
.collect::<BTreeSet<_>>(); (cnt as usize) + 3
a.list(&names, |s, a| {
let val = shared::name_to_descriptor(s.as_ref());
a.fields(&[
("descriptor", &|a| a.char(val)),
("name", &|a| a.str(s.as_ref()))
]);
})
}),
]);
return a.cnt
} }
} }