add ptr validation

This commit is contained in:
Robert Masen 2018-06-17 20:13:56 -05:00
parent 639ccd53ce
commit 749ac6502f
2 changed files with 8 additions and 1 deletions

View File

@ -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)]

View File

@ -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)
}
}
}