1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::{error::WasmEdgeError, ffi, utils::check, Config, Module, WasmEdgeResult};
#[derive(Debug)]
pub struct Validator {
pub(crate) inner: InnerValidator,
pub(crate) registered: bool,
}
impl Validator {
pub fn create(config: Option<&Config>) -> WasmEdgeResult<Self> {
let ctx = match config {
Some(config) => unsafe { ffi::WasmEdge_ValidatorCreate(config.inner.0) },
None => unsafe { ffi::WasmEdge_ValidatorCreate(std::ptr::null_mut()) },
};
match ctx.is_null() {
true => Err(Box::new(WasmEdgeError::CompilerCreate)),
false => Ok(Self {
inner: InnerValidator(ctx),
registered: false,
}),
}
}
pub fn validate(&self, module: &Module) -> WasmEdgeResult<()> {
unsafe {
check(ffi::WasmEdge_ValidatorValidate(
self.inner.0,
module.inner.0,
))
}
}
#[cfg(feature = "ffi")]
pub fn as_ptr(&self) -> *const ffi::WasmEdge_ValidatorContext {
self.inner.0 as *const _
}
}
impl Drop for Validator {
fn drop(&mut self) {
if !self.registered && !self.inner.0.is_null() {
unsafe { ffi::WasmEdge_ValidatorDelete(self.inner.0) }
}
}
}
#[derive(Debug)]
pub(crate) struct InnerValidator(pub(crate) *mut ffi::WasmEdge_ValidatorContext);
unsafe impl Send for InnerValidator {}
unsafe impl Sync for InnerValidator {}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Config, Loader};
use std::{
sync::{Arc, Mutex},
thread,
};
#[test]
#[allow(clippy::assertions_on_result_states)]
fn test_validator() {
let result = Validator::create(None);
assert!(result.is_ok());
let result = Config::create();
assert!(result.is_ok());
let mut config = result.unwrap();
config.reference_types(true);
let result = Loader::create(Some(&config));
assert!(result.is_ok());
let loader = result.unwrap();
let path =
std::path::PathBuf::from(env!("WASMEDGE_DIR")).join("test/api/apiTestData/test.wasm");
let result = loader.from_file(path);
assert!(result.is_ok());
let module = result.unwrap();
assert!(!module.inner.0.is_null());
let result = Validator::create(None);
assert!(result.is_ok());
let validator = result.unwrap();
let result = validator.validate(&module);
assert!(result.is_ok());
}
#[test]
#[allow(clippy::assertions_on_result_states)]
fn test_validator_send() {
let result = Validator::create(None);
assert!(result.is_ok());
let validator = result.unwrap();
let handle = thread::spawn(move || {
let result = Loader::create(None);
assert!(result.is_ok());
let loader = result.unwrap();
let path = std::path::PathBuf::from(env!("WASMEDGE_DIR"))
.join("test/api/apiTestData/test.wasm");
let result = loader.from_file(path);
assert!(result.is_ok());
let module = result.unwrap();
assert!(!module.inner.0.is_null());
let result = validator.validate(&module);
assert!(result.is_ok());
});
handle.join().unwrap();
}
#[test]
#[allow(clippy::assertions_on_result_states)]
fn test_validator_sync() {
let result = Validator::create(None);
assert!(result.is_ok());
let validator = Arc::new(Mutex::new(result.unwrap()));
let validator_cloned = Arc::clone(&validator);
let handle = thread::spawn(move || {
let result = Loader::create(None);
assert!(result.is_ok());
let loader = result.unwrap();
let path = std::path::PathBuf::from(env!("WASMEDGE_DIR"))
.join("test/api/apiTestData/test.wasm");
let result = loader.from_file(path);
assert!(result.is_ok());
let module = result.unwrap();
assert!(!module.inner.0.is_null());
let result = validator_cloned.lock();
assert!(result.is_ok());
let validator = result.unwrap();
let result = validator.validate(&module);
assert!(result.is_ok());
});
handle.join().unwrap();
}
}