mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-29 13:06:06 +03:00
add ptr validation
This commit is contained in:
parent
639ccd53ce
commit
749ac6502f
@ -2,6 +2,7 @@ use proc_macro2::{Ident, Span, TokenStream, TokenTree};
|
||||
use quote::ToTokens;
|
||||
use shared;
|
||||
use syn;
|
||||
use syn::AttrStyle;
|
||||
|
||||
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
||||
#[derive(Default)]
|
||||
|
@ -68,6 +68,9 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
/// passed should be `this.ptr`.
|
||||
pub fn method(&mut self, method: bool) -> &mut Self {
|
||||
if method {
|
||||
self.prelude("if (this.ptr === 0) {
|
||||
throw new Error('Attempt to use a moved value');
|
||||
}");
|
||||
self.rust_arguments.insert(0, "this.ptr".to_string());
|
||||
}
|
||||
self
|
||||
@ -149,6 +152,9 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
} else {
|
||||
self.prelude(&format!("\
|
||||
const ptr{i} = {arg}.ptr;\n\
|
||||
if (ptr{i} === 0) {{
|
||||
throw new Error('Attempt to use a moved value');
|
||||
}}
|
||||
{arg}.ptr = 0;\n\
|
||||
", i = i, arg = name));
|
||||
self.rust_arguments.push(format!("ptr{}", i));
|
||||
@ -361,4 +367,4 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
let ts = format!("{} {}({}): {};\n", prefix, self.js_name, ts_args, self.ret_ty);
|
||||
(js, ts)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user