Simplifying the output

This commit is contained in:
Pauan 2019-07-16 23:03:18 +02:00
parent 4e504654d1
commit 1e4cac9c95
3 changed files with 27 additions and 47 deletions

View File

@ -1088,6 +1088,28 @@ impl<'a> Context<'a> {
Ok(()) Ok(())
} }
fn expose_get_cached_string_from_wasm(&mut self) -> Result<(), Error> {
if !self.should_write_global("get_cached_string_from_wasm") {
return Ok(());
}
self.expose_get_object();
self.expose_get_string_from_wasm()?;
self.global("
function getCachedStringFromWasm(ptr, len) {
if (ptr === 0) {
if (len !== 0) {
return getObject(len);
}
} else {
return getStringFromWasm(ptr, len);
}
}
");
Ok(())
}
fn expose_get_array_js_value_from_wasm(&mut self) -> Result<(), Error> { fn expose_get_array_js_value_from_wasm(&mut self) -> Result<(), Error> {
if !self.should_write_global("get_array_js_value_from_wasm") { if !self.should_write_global("get_array_js_value_from_wasm") {
return Ok(()); return Ok(());

View File

@ -139,16 +139,10 @@ impl<'a, 'b> Outgoing<'a, 'b> {
let len = self.arg(*length); let len = self.arg(*length);
let tmp = self.js.tmp(); let tmp = self.js.tmp();
self.js.typescript_required("string"); self.js.typescript_optional("string");
self.cx.expose_get_object(); self.cx.expose_get_cached_string_from_wasm()?;
self.cx.expose_get_string_from_wasm()?;
self.js.prelude(&format!( self.js.prelude(&format!("const v{} = getCachedStringFromWasm({}, {});", tmp, ptr, len));
"const v{tmp} = {ptr} === 0 ? getObject({len}) : getStringFromWasm({ptr}, {len});",
tmp = tmp,
ptr = ptr,
len = len,
));
if *owned { if *owned {
self.prelude_free_cached_string(&ptr, &len)?; self.prelude_free_cached_string(&ptr, &len)?;
@ -332,35 +326,6 @@ impl<'a, 'b> Outgoing<'a, 'b> {
self.js.prelude("}"); self.js.prelude("}");
Ok(format!("v{}", i)) Ok(format!("v{}", i))
} }
NonstandardOutgoing::OptionCachedString {
offset,
length,
owned,
} => {
let ptr = self.arg(*offset);
let len = self.arg(*length);
let tmp = self.js.tmp();
self.js.typescript_optional("string");
self.cx.expose_get_object();
self.cx.expose_get_string_from_wasm()?;
self.js.prelude(&format!("let v{};", tmp));
self.js.prelude(&format!(
"if ({ptr} === 0) {{ if ({len} !== 0) {{ v{tmp} = getObject({len}); }} }} else {{ v{tmp} = getStringFromWasm({ptr}, {len}); }}",
tmp = tmp,
ptr = ptr,
len = len,
));
if *owned {
self.prelude_free_cached_string(&ptr, &len)?;
}
Ok(format!("v{}", tmp))
}
} }
} }

View File

@ -88,13 +88,6 @@ pub enum NonstandardOutgoing {
kind: VectorKind, kind: VectorKind,
}, },
///
OptionCachedString {
offset: u32,
length: u32,
owned: bool,
},
/// An optional slice of data is being passed into JS. /// An optional slice of data is being passed into JS.
/// ///
/// TODO: with some cleverness this could probably use `AllocCopy`. /// TODO: with some cleverness this could probably use `AllocCopy`.
@ -462,7 +455,7 @@ impl OutgoingBuilder<'_> {
let offset = self.push_wasm(ValType::I32); let offset = self.push_wasm(ValType::I32);
let length = self.push_wasm(ValType::I32); let length = self.push_wasm(ValType::I32);
self.webidl.push(ast::WebidlScalarType::DomString); self.webidl.push(ast::WebidlScalarType::DomString);
self.bindings.push(NonstandardOutgoing::OptionCachedString { self.bindings.push(NonstandardOutgoing::CachedString {
offset, offset,
length, length,
owned: true, owned: true,
@ -506,7 +499,7 @@ impl OutgoingBuilder<'_> {
let offset = self.push_wasm(ValType::I32); let offset = self.push_wasm(ValType::I32);
let length = self.push_wasm(ValType::I32); let length = self.push_wasm(ValType::I32);
self.webidl.push(ast::WebidlScalarType::DomString); self.webidl.push(ast::WebidlScalarType::DomString);
self.bindings.push(NonstandardOutgoing::OptionCachedString { self.bindings.push(NonstandardOutgoing::CachedString {
offset, offset,
length, length,
owned: false, owned: false,