mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-14 20:11:37 +03:00
Simplifying the output
This commit is contained in:
parent
4e504654d1
commit
1e4cac9c95
@ -1088,6 +1088,28 @@ impl<'a> Context<'a> {
|
||||
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> {
|
||||
if !self.should_write_global("get_array_js_value_from_wasm") {
|
||||
return Ok(());
|
||||
|
@ -139,16 +139,10 @@ impl<'a, 'b> Outgoing<'a, 'b> {
|
||||
let len = self.arg(*length);
|
||||
let tmp = self.js.tmp();
|
||||
|
||||
self.js.typescript_required("string");
|
||||
self.cx.expose_get_object();
|
||||
self.cx.expose_get_string_from_wasm()?;
|
||||
self.js.typescript_optional("string");
|
||||
self.cx.expose_get_cached_string_from_wasm()?;
|
||||
|
||||
self.js.prelude(&format!(
|
||||
"const v{tmp} = {ptr} === 0 ? getObject({len}) : getStringFromWasm({ptr}, {len});",
|
||||
tmp = tmp,
|
||||
ptr = ptr,
|
||||
len = len,
|
||||
));
|
||||
self.js.prelude(&format!("const v{} = getCachedStringFromWasm({}, {});", tmp, ptr, len));
|
||||
|
||||
if *owned {
|
||||
self.prelude_free_cached_string(&ptr, &len)?;
|
||||
@ -332,35 +326,6 @@ impl<'a, 'b> Outgoing<'a, 'b> {
|
||||
self.js.prelude("}");
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -88,13 +88,6 @@ pub enum NonstandardOutgoing {
|
||||
kind: VectorKind,
|
||||
},
|
||||
|
||||
///
|
||||
OptionCachedString {
|
||||
offset: u32,
|
||||
length: u32,
|
||||
owned: bool,
|
||||
},
|
||||
|
||||
/// An optional slice of data is being passed into JS.
|
||||
///
|
||||
/// TODO: with some cleverness this could probably use `AllocCopy`.
|
||||
@ -462,7 +455,7 @@ impl OutgoingBuilder<'_> {
|
||||
let offset = self.push_wasm(ValType::I32);
|
||||
let length = self.push_wasm(ValType::I32);
|
||||
self.webidl.push(ast::WebidlScalarType::DomString);
|
||||
self.bindings.push(NonstandardOutgoing::OptionCachedString {
|
||||
self.bindings.push(NonstandardOutgoing::CachedString {
|
||||
offset,
|
||||
length,
|
||||
owned: true,
|
||||
@ -506,7 +499,7 @@ impl OutgoingBuilder<'_> {
|
||||
let offset = self.push_wasm(ValType::I32);
|
||||
let length = self.push_wasm(ValType::I32);
|
||||
self.webidl.push(ast::WebidlScalarType::DomString);
|
||||
self.bindings.push(NonstandardOutgoing::OptionCachedString {
|
||||
self.bindings.push(NonstandardOutgoing::CachedString {
|
||||
offset,
|
||||
length,
|
||||
owned: false,
|
||||
|
Loading…
Reference in New Issue
Block a user