Skip to content

Commit

Permalink
Check env can be built
Browse files Browse the repository at this point in the history
  • Loading branch information
ark0f committed Sep 22, 2023
1 parent 1693a11 commit 90e0194
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
7 changes: 6 additions & 1 deletion core-backend/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use gear_wasm_instrument::{
};

#[derive(Clone, Copy)]
pub struct SandboxValue(Value);
pub struct SandboxValue(pub Value);

impl From<i32> for SandboxValue {
fn from(value: i32) -> Self {
Expand Down Expand Up @@ -330,6 +330,11 @@ where

builder.add_func(Alloc, wrap_common_func!(FuncsHandler::alloc, (2) -> (1)));
builder.add_func(Free, wrap_common_func!(FuncsHandler::free, (2) -> (1)));

builder.add_func(CreateProgram, |caller, args| {
let mut caller = CallerWrap::prepare(caller)?;
FuncsHandler::execute(&mut caller, args, FuncsHandler::create_program2)
});
}
}

Expand Down
43 changes: 21 additions & 22 deletions core-backend/src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl From<u32> for SysCallReturnValue {
}

pub trait SysCallContext: Sized {
fn from_args(args: &[SandboxValue]) -> Result<(Self, &[SandboxValue]), HostError>;
fn from_args(args: &[Value]) -> Result<(Self, &[Value]), HostError>;
}

pub trait SysCall<Ext, T = ()> {
Expand All @@ -121,16 +121,16 @@ pub trait SysCall<Ext, T = ()> {
}

pub trait SysCallFabric<Ext, Args, R, S> {
fn create(self, args: &[SandboxValue]) -> Result<S, HostError>;
fn create(self, args: &[Value]) -> Result<S, HostError>;
}

impl<Ext, R, S, H> SysCallFabric<Ext, (), R, S> for H
where
H: Fn() -> S,
S: SysCall<Ext, R>,
{
fn create(self, args: &[SandboxValue]) -> Result<S, HostError> {
let _: [SandboxValue; 0] = args.try_into().map_err(|_| HostError)?;
fn create(self, args: &[Value]) -> Result<S, HostError> {
let _: [Value; 0] = args.try_into().map_err(|_| HostError)?;
Ok((self)())
}
}
Expand All @@ -140,9 +140,9 @@ where
H: Fn(u32) -> S,
S: SysCall<Ext, R>,
{
fn create(self, args: &[SandboxValue]) -> Result<S, HostError> {
let [a]: [SandboxValue; 1] = args.try_into().map_err(|_| HostError)?;
let a = a.try_into()?;
fn create(self, args: &[Value]) -> Result<S, HostError> {
let [a]: [Value; 1] = args.try_into().map_err(|_| HostError)?;
let a = SandboxValue(a).try_into()?;
Ok((self)(a))
}
}
Expand All @@ -152,14 +152,14 @@ where
H: Fn(u32, u32, u32, u32, u32, u32) -> S,
S: SysCall<Ext, R>,
{
fn create(self, args: &[SandboxValue]) -> Result<S, HostError> {
let [a, b, c, d, e, f]: [SandboxValue; 6] = args.try_into().map_err(|_| HostError)?;
let a = a.try_into()?;
let b = b.try_into()?;
let c = c.try_into()?;
let d = d.try_into()?;
let e = e.try_into()?;
let f = f.try_into()?;
fn create(self, args: &[Value]) -> Result<S, HostError> {
let [a, b, c, d, e, f]: [Value; 6] = args.try_into().map_err(|_| HostError)?;
let a = SandboxValue(a).try_into()?;
let b = SandboxValue(b).try_into()?;
let c = SandboxValue(c).try_into()?;
let d = SandboxValue(d).try_into()?;
let e = SandboxValue(e).try_into()?;
let f = SandboxValue(f).try_into()?;
Ok((self)(a, b, c, d, e, f))
}
}
Expand All @@ -170,11 +170,11 @@ struct FallibleSysCallContext {
}

impl SysCallContext for FallibleSysCallContext {
fn from_args(args: &[SandboxValue]) -> Result<(Self, &[SandboxValue]), HostError> {
fn from_args(args: &[Value]) -> Result<(Self, &[Value]), HostError> {
let (gas, args) = args.split_first().ok_or(HostError)?;
let gas: u64 = (*gas).try_into()?;
let gas: u64 = SandboxValue(*gas).try_into()?;
let (res_ptr, args) = args.split_last().ok_or(HostError)?;
let res_ptr: u32 = (*res_ptr).try_into()?;
let res_ptr: u32 = SandboxValue(*res_ptr).try_into()?;
Ok((FallibleSysCallContext { gas, res_ptr }, args))
}
}
Expand Down Expand Up @@ -208,9 +208,9 @@ pub struct InfallibleSysCallContext {
}

impl SysCallContext for InfallibleSysCallContext {
fn from_args(args: &[SandboxValue]) -> Result<(Self, &[SandboxValue]), HostError> {
fn from_args(args: &[Value]) -> Result<(Self, &[Value]), HostError> {
let (gas, args) = args.split_first().ok_or(HostError)?;
let gas: u64 = (*gas).try_into()?;
let gas: u64 = SandboxValue(*gas).try_into()?;
Ok((Self { gas }, args))
}
}
Expand Down Expand Up @@ -248,7 +248,7 @@ where
{
pub fn execute<H, Args, R, S>(
caller: &mut CallerWrap<Ext>,
args: &[SandboxValue],
args: &[Value],
handler: H,
) -> Result<WasmReturnValue, HostError>
where
Expand All @@ -258,7 +258,6 @@ where
{
let (ctx, args) = S::Context::from_args(args)?;
let sys_call = SysCallFabric::create(handler, args)?;
// FIXME: return value is not ignored
let (gas, value) = sys_call.execute(caller, ctx)?;
let value = value.into();

Expand Down

0 comments on commit 90e0194

Please sign in to comment.