forked from lune-org/lune
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Data:copyFrom, tests and annotation (lune-org#243)
- Loading branch information
Showing
12 changed files
with
514 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use mlua::prelude::*; | ||
|
||
use super::{FfiData, GetFfiData}; | ||
|
||
pub mod method_provider { | ||
|
||
use super::*; | ||
|
||
pub fn provide_copy_from<'lua, Target, M>(methods: &mut M) | ||
where | ||
Target: FfiData, | ||
M: LuaUserDataMethods<'lua, Target>, | ||
{ | ||
methods.add_method( | ||
"copyFrom", | ||
|_lua, | ||
this, | ||
(src, length, dst_offset, src_offset): ( | ||
LuaAnyUserData, | ||
usize, | ||
Option<isize>, | ||
Option<isize>, | ||
)| unsafe { | ||
let dst_offset = dst_offset.unwrap_or(0); | ||
let src_offset = src_offset.unwrap_or(0); | ||
let src = src.get_ffi_data()?; | ||
|
||
if !src.check_inner_boundary(src_offset, length) { | ||
return Err(LuaError::external("Source boundary check failed")); | ||
} | ||
if !this.check_inner_boundary(dst_offset, length) { | ||
return Err(LuaError::external("Self boundary check failed")); | ||
} | ||
|
||
this.copy_from(&src, length, dst_offset, src_offset); | ||
|
||
Ok(()) | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
local ffi = require("@lune/ffi") | ||
|
||
local ok | ||
|
||
-- Case1: Success | ||
ok = pcall(function() | ||
local box = ffi.u8:box(1) | ||
ffi.u8:readData(box) | ||
end) | ||
assert(ok, "assersion failed, Case1 should success") | ||
|
||
-- Case2: Fail | ||
ok = pcall(function() | ||
local box = ffi.u8:box(1) | ||
ffi.u16:readData(box) | ||
end) | ||
assert(not ok, "assersion failed, Case2 should fail") | ||
|
||
-- Case3: Success | ||
ok = pcall(function() | ||
local box = ffi.box(ffi.u8.size * 2) | ||
ffi.u16:readData(box) | ||
end) | ||
assert(ok, "assersion failed, Case3 should success") | ||
|
||
-- Case4: Success | ||
|
||
ok = pcall(function() | ||
local box = ffi.box(ffi.u8.size * 2) | ||
ffi.u8:readData(box, ffi.u8.size) | ||
end) | ||
assert(ok, "assersion failed, Case4 should success") | ||
|
||
-- Case5: Fail | ||
ok = pcall(function() | ||
local box = ffi.u8:box(1):ref() | ||
ffi.u16:readData(box) | ||
end) | ||
assert(not ok, "assersion failed, Case5 should fail") | ||
|
||
-- Case6: Success | ||
ok = pcall(function() | ||
local box = ffi.box(ffi.u8.size * 2):ref() | ||
ffi.u16:readData(box) | ||
end) | ||
assert(ok, "assersion failed, Case6 should success") | ||
|
||
-- Case7: Fail | ||
ok = pcall(function() | ||
local box = ffi.box(ffi.u8.size * 2):ref(ffi.u16.size) | ||
ffi.u16:readData(box) | ||
end) | ||
assert(ok, "assersion failed, Case7 should fail") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
local ffi = require("@lune/ffi") | ||
local c = ffi.c | ||
|
||
local ok | ||
|
||
-- Case1: Fail | ||
ok = pcall(function() | ||
local box = ffi.box(c.int.size - 1) | ||
c.int:writeData(box, 10) | ||
end) | ||
assert(not ok, "assersion failed, Case1 should fail") | ||
|
||
-- Case2: Success | ||
ok = pcall(function() | ||
local box = ffi.box(c.int.size) | ||
c.int:writeData(box, 10) | ||
end) | ||
assert(ok, "assersion failed, Case2 should success") | ||
|
||
-- Case3: Success | ||
ok = pcall(function() | ||
local box = ffi.box(c.int.size * 2) | ||
c.int:writeData(box, 10, c.int.size) | ||
end) | ||
assert(ok, "assersion failed, Case3 should success") | ||
|
||
-- Case4: Fail | ||
ok = pcall(function() | ||
local box = ffi.box(c.int.size * 2) | ||
c.int:writeData(box, 10, c.int.size * 2) | ||
end) | ||
assert(not ok, "assersion failed, Case4 should fail") | ||
|
||
-- Case5: Success | ||
ok = pcall(function() | ||
local box = ffi.box(c.int.size * 2):ref() | ||
c.int:writeData(box, 10, c.int.size) | ||
end) | ||
assert(ok, "assersion failed, Case5 should success") | ||
|
||
-- Case6: Fail | ||
ok = pcall(function() | ||
local box = ffi.box(c.int.size * 2):ref() | ||
c.int:writeData(box, 10, c.int.size * 2) | ||
end) | ||
assert(not ok, "assersion failed, Case6 should fail") |
Oops, something went wrong.