From 90e019442d0eab7e660dfb174b3e30343e3127e8 Mon Sep 17 00:00:00 2001 From: Arseniy Lyashenko Date: Fri, 22 Sep 2023 22:20:37 +0300 Subject: [PATCH] Check env can be built --- core-backend/src/env.rs | 7 ++++++- core-backend/src/funcs.rs | 43 +++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/core-backend/src/env.rs b/core-backend/src/env.rs index e232bcd0711..099a4fe50cf 100644 --- a/core-backend/src/env.rs +++ b/core-backend/src/env.rs @@ -58,7 +58,7 @@ use gear_wasm_instrument::{ }; #[derive(Clone, Copy)] -pub struct SandboxValue(Value); +pub struct SandboxValue(pub Value); impl From for SandboxValue { fn from(value: i32) -> Self { @@ -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) + }); } } diff --git a/core-backend/src/funcs.rs b/core-backend/src/funcs.rs index c00d9023367..62383a1dc19 100644 --- a/core-backend/src/funcs.rs +++ b/core-backend/src/funcs.rs @@ -107,7 +107,7 @@ impl From 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 { @@ -121,7 +121,7 @@ pub trait SysCall { } pub trait SysCallFabric { - fn create(self, args: &[SandboxValue]) -> Result; + fn create(self, args: &[Value]) -> Result; } impl SysCallFabric for H @@ -129,8 +129,8 @@ where H: Fn() -> S, S: SysCall, { - fn create(self, args: &[SandboxValue]) -> Result { - let _: [SandboxValue; 0] = args.try_into().map_err(|_| HostError)?; + fn create(self, args: &[Value]) -> Result { + let _: [Value; 0] = args.try_into().map_err(|_| HostError)?; Ok((self)()) } } @@ -140,9 +140,9 @@ where H: Fn(u32) -> S, S: SysCall, { - fn create(self, args: &[SandboxValue]) -> Result { - let [a]: [SandboxValue; 1] = args.try_into().map_err(|_| HostError)?; - let a = a.try_into()?; + fn create(self, args: &[Value]) -> Result { + let [a]: [Value; 1] = args.try_into().map_err(|_| HostError)?; + let a = SandboxValue(a).try_into()?; Ok((self)(a)) } } @@ -152,14 +152,14 @@ where H: Fn(u32, u32, u32, u32, u32, u32) -> S, S: SysCall, { - fn create(self, args: &[SandboxValue]) -> Result { - 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 { + 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)) } } @@ -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)) } } @@ -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)) } } @@ -248,7 +248,7 @@ where { pub fn execute( caller: &mut CallerWrap, - args: &[SandboxValue], + args: &[Value], handler: H, ) -> Result where @@ -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();