Changing to use WasmSlice for the caching

This commit is contained in:
Pauan
2019-07-16 22:38:20 +02:00
parent f7e8e70684
commit 2ee4c54f00
7 changed files with 181 additions and 67 deletions

View File

@ -24,6 +24,7 @@ tys! {
BOOLEAN
FUNCTION
CLOSURE
CACHED_STRING
STRING
REF
REFMUT
@ -58,6 +59,7 @@ pub enum Descriptor {
RefMut(Box<Descriptor>),
Slice(Box<Descriptor>),
Vector(Box<Descriptor>),
CachedString,
String,
Anyref,
Enum { hole: u32 },
@ -127,6 +129,7 @@ impl Descriptor {
SLICE => Descriptor::Slice(Box::new(Descriptor::_decode(data, clamped))),
VECTOR => Descriptor::Vector(Box::new(Descriptor::_decode(data, clamped))),
OPTIONAL => Descriptor::Option(Box::new(Descriptor::_decode(data, clamped))),
CACHED_STRING => Descriptor::CachedString,
STRING => Descriptor::String,
ANYREF => Descriptor::Anyref,
ENUM => Descriptor::Enum { hole: get(data) },

View File

@ -130,6 +130,33 @@ impl<'a, 'b> Outgoing<'a, 'b> {
Ok(format!("v{}", i))
}
NonstandardOutgoing::CachedString {
offset,
length,
owned,
} => {
let ptr = self.arg(*offset);
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.prelude(&format!(
"const v{tmp} = {ptr} === 0 ? getObject({len}) : getStringFromWasm({ptr}, {len});",
tmp = tmp,
ptr = ptr,
len = len,
));
if *owned {
self.prelude_free_cached_string(&ptr, &len)?;
}
Ok(format!("v{}", tmp))
}
NonstandardOutgoing::StackClosure {
a,
b,
@ -305,6 +332,35 @@ 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))
}
}
}
@ -408,4 +464,15 @@ impl<'a, 'b> Outgoing<'a, 'b> {
));
self.cx.require_internal_export("__wbindgen_free")
}
fn prelude_free_cached_string(&mut self, ptr: &str, len: &str) -> Result<(), Error> {
self.js.prelude(&format!(
"if ({ptr} !== 0) {{ wasm.__wbindgen_free({ptr}, {len} * {size}); }}",
ptr = ptr,
len = len,
size = VectorKind::String.size(),
));
self.cx.require_internal_export("__wbindgen_free")
}
}

View File

@ -60,6 +60,13 @@ pub enum NonstandardOutgoing {
kind: VectorKind,
},
///
CachedString {
offset: u32,
length: u32,
owned: bool,
},
/// A `&[u64]` or `&[i64]` is being passed to JS, and the 64-bit sizes here
/// aren't supported by WebIDL bindings yet.
View64 {
@ -81,6 +88,13 @@ 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`.
@ -240,6 +254,17 @@ impl OutgoingBuilder<'_> {
Descriptor::Ref(d) => self.process_ref(false, d)?,
Descriptor::RefMut(d) => self.process_ref(true, d)?,
Descriptor::CachedString => {
let offset = self.push_wasm(ValType::I32);
let length = self.push_wasm(ValType::I32);
self.webidl.push(ast::WebidlScalarType::Any);
self.bindings.push(NonstandardOutgoing::CachedString {
offset,
length,
owned: true,
})
}
Descriptor::Vector(_) | Descriptor::String => {
let kind = arg.vector_kind().ok_or_else(|| {
format_err!(
@ -281,6 +306,16 @@ impl OutgoingBuilder<'_> {
self.bindings
.push(NonstandardOutgoing::BorrowedAnyref { idx });
}
Descriptor::CachedString => {
let offset = self.push_wasm(ValType::I32);
let length = self.push_wasm(ValType::I32);
self.webidl.push(ast::WebidlScalarType::DomString);
self.bindings.push(NonstandardOutgoing::CachedString {
offset,
length,
owned: false,
})
}
Descriptor::Slice(_) | Descriptor::String => {
use wasm_webidl_bindings::ast::WebidlScalarType::*;
@ -422,6 +457,18 @@ impl OutgoingBuilder<'_> {
}
Descriptor::Ref(d) => self.process_option_ref(false, d)?,
Descriptor::RefMut(d) => self.process_option_ref(true, d)?,
Descriptor::CachedString => {
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 {
offset,
length,
owned: true,
})
}
Descriptor::String | Descriptor::Vector(_) => {
let kind = arg.vector_kind().ok_or_else(|| {
format_err!(
@ -455,6 +502,16 @@ impl OutgoingBuilder<'_> {
self.bindings
.push(NonstandardOutgoing::BorrowedAnyref { idx });
}
Descriptor::CachedString => {
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 {
offset,
length,
owned: false,
})
}
Descriptor::String | Descriptor::Slice(_) => {
let kind = arg.vector_kind().ok_or_else(|| {
format_err!(