[WASM] mem_cmp added to the Wasm runtime (#7539)

* mem_cmp added to the Wasm runtime

* schedule.wasm.mem_copy to schedule.wasm.mem_cmp for mem_cmp
This commit is contained in:
Alexey
2018-01-15 17:24:24 +03:00
committed by Nikolay Volf
parent ad14e656f6
commit d927320719
6 changed files with 37 additions and 0 deletions

View File

@@ -19,6 +19,7 @@
use std::sync::Arc;
use byteorder::{LittleEndian, ByteOrder};
use libc::{memcmp, c_void};
use vm;
use panic_payload;
@@ -567,6 +568,28 @@ impl<'a, 'b> Runtime<'a, 'b> {
&*self.memory
}
fn mem_cmp(&mut self, context: InterpreterCallerContext)
-> Result<Option<interpreter::RuntimeValue>, InterpreterError>
{
//
// method signature:
// fn memcmp(cx: *const u8, ct: *const u8, n: usize) -> i32;
//
let len = context.value_stack.pop_as::<i32>()? as u32;
let cx = context.value_stack.pop_as::<i32>()? as u32;
let ct = context.value_stack.pop_as::<i32>()? as u32;
self.charge(|schedule| schedule.wasm.mem_cmp as u64 * len as u64)?;
let cx = self.memory.get(cx, len as usize)?;
let ct = self.memory.get(ct, len as usize)?;
let result = unsafe {
memcmp(cx.as_ptr() as *const c_void, ct.as_ptr() as *const c_void, len as usize)
};
Ok(Some(Into::into(result)))
}
fn mem_copy(&mut self, context: InterpreterCallerContext)
-> Result<Option<interpreter::RuntimeValue>, InterpreterError>
{
@@ -890,6 +913,9 @@ impl<'a, 'b> interpreter::UserFunctionExecutor<UserTrap> for Runtime<'a, 'b> {
"_emscripten_memcpy_big" => {
self.mem_copy(context)
},
"_ext_memcmp" => {
self.mem_cmp(context)
},
"_ext_memcpy" => {
self.mem_copy(context)
},