mirror of
https://github.com/fluencelabs/marine-rs-sdk
synced 2025-03-15 22:30:50 +00:00
Merge pull request #3 from fluencelabs/call_parameters
Introduce call parameters
This commit is contained in:
commit
d0db9a365f
@ -19,6 +19,8 @@ crate-type = ["rlib"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = { version = "0.4.8", features = ["std"] }
|
log = { version = "0.4.8", features = ["std"] }
|
||||||
|
fluence-sdk-macro = { path = "../macro" }
|
||||||
|
serde = "1.0.115"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
simple_logger = "1.6.0" # used in doc test
|
simple_logger = "1.6.0" # used in doc test
|
||||||
@ -30,3 +32,6 @@ debug = []
|
|||||||
|
|
||||||
# Enable logger (this will cause log_utf8_string to appear in imports)
|
# Enable logger (this will cause log_utf8_string to appear in imports)
|
||||||
logger = []
|
logger = []
|
||||||
|
|
||||||
|
# Indicates that this crate is used inside FCE, it allow to generate impl block for CallParameters
|
||||||
|
used_in_fce = []
|
||||||
|
61
crates/main/src/call_parameters.rs
Normal file
61
crates/main/src/call_parameters.rs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 Fluence Labs Limited
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use fluence_sdk_macro::fce;
|
||||||
|
|
||||||
|
/// This struct contains parameters that would be accessible by Wasm modules.
|
||||||
|
#[fce]
|
||||||
|
#[derive(Clone, PartialEq, Default, Eq, Debug, serde::Serialize, serde::Deserialize)]
|
||||||
|
pub struct CallParameters {
|
||||||
|
pub call_id: String,
|
||||||
|
pub user_name: String,
|
||||||
|
pub application_id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CallParameters {
|
||||||
|
pub fn new<C, U, A>(call_id: C, user_name: U, application_id: A) -> Self
|
||||||
|
where
|
||||||
|
C: Into<String>,
|
||||||
|
U: Into<String>,
|
||||||
|
A: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
call_id: call_id.into(),
|
||||||
|
user_name: user_name.into(),
|
||||||
|
application_id: application_id.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This functions takes from host current call parameters.
|
||||||
|
/// Beware that this implies import function call which takes some time.
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
pub fn get_call_parameters() -> CallParameters {
|
||||||
|
// it's safe until it is executed on standard Fluence node with appropriate import function
|
||||||
|
unsafe {
|
||||||
|
let raw_call_parameters = get_call_raw_parameters();
|
||||||
|
CallParameters::__fce_generated_deserialize(raw_call_parameters)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
#[link(wasm_import_module = "host")]
|
||||||
|
#[allow(improper_ctypes)]
|
||||||
|
extern "C" {
|
||||||
|
// returns serialized current call parameters
|
||||||
|
#[link_name = "get_call_parameters"]
|
||||||
|
fn get_call_raw_parameters() -> *const u8;
|
||||||
|
}
|
@ -31,11 +31,15 @@
|
|||||||
)]
|
)]
|
||||||
#![warn(rust_2018_idioms)]
|
#![warn(rust_2018_idioms)]
|
||||||
|
|
||||||
|
mod call_parameters;
|
||||||
mod export_allocator;
|
mod export_allocator;
|
||||||
#[cfg(any(feature = "debug", feature = "logger"))]
|
#[cfg(any(feature = "debug", feature = "logger"))]
|
||||||
mod logger;
|
mod logger;
|
||||||
mod result;
|
mod result;
|
||||||
|
|
||||||
|
pub use call_parameters::CallParameters;
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
pub use call_parameters::get_call_parameters;
|
||||||
pub use export_allocator::allocate;
|
pub use export_allocator::allocate;
|
||||||
pub use export_allocator::deallocate;
|
pub use export_allocator::deallocate;
|
||||||
#[cfg(feature = "logger")]
|
#[cfg(feature = "logger")]
|
||||||
@ -45,19 +49,6 @@ pub use result::get_result_size;
|
|||||||
pub use result::set_result_ptr;
|
pub use result::set_result_ptr;
|
||||||
pub use result::set_result_size;
|
pub use result::set_result_size;
|
||||||
|
|
||||||
/// This trait is used to convert structs to a form compatible with
|
|
||||||
/// record.lift_memory and record.lower_memory instructions.
|
|
||||||
/// Normally, this trait shouldn't be used directly.
|
|
||||||
pub trait FCEStructSerializable {
|
|
||||||
// Serialize the provided record to a Vec<u8>, returns pointer to it in a form compatible with
|
|
||||||
// record.lift_memory.
|
|
||||||
// The caller should manage the lifetime of returned pointer.
|
|
||||||
fn __fce_generated_serialize(self) -> *const u8;
|
|
||||||
|
|
||||||
// Deserialize record from a pointer (normally, come from record.lower_memory).
|
|
||||||
unsafe fn __fce_generated_deserialize(record_ptr: *const u8) -> Self;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
pub(crate) fn log<S: AsRef<str>>(msg: S) {
|
pub(crate) fn log<S: AsRef<str>>(msg: S) {
|
||||||
// logs will be printed only if debug feature is enabled
|
// logs will be printed only if debug feature is enabled
|
||||||
|
@ -60,6 +60,7 @@ impl ForeignModEpilogGlueCodeGenerator for Option<ParsedType> {
|
|||||||
},
|
},
|
||||||
Some(ParsedType::Record(record_name)) => {
|
Some(ParsedType::Record(record_name)) => {
|
||||||
let record_ident = new_ident!(record_name);
|
let record_ident = new_ident!(record_name);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#record_ident::__fce_generated_deserialize(fluence::internal::get_result_ptr() as _)
|
#record_ident::__fce_generated_deserialize(fluence::internal::get_result_ptr() as _)
|
||||||
}
|
}
|
||||||
|
@ -69,10 +69,6 @@ impl quote::ToTokens for fce_ast_types::AstFunctionItem {
|
|||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[allow(clippy::all)]
|
#[allow(clippy::all)]
|
||||||
pub unsafe fn #func_name(#(#raw_arg_names: #raw_arg_types),*) #fn_return_type {
|
pub unsafe fn #func_name(#(#raw_arg_names: #raw_arg_types),*) #fn_return_type {
|
||||||
// brings serialize/deserialize methods for records
|
|
||||||
#[allow(dead_code)]
|
|
||||||
use fluence::internal::FCEStructSerializable;
|
|
||||||
|
|
||||||
// arguments conversation from Wasm types to Rust types
|
// arguments conversation from Wasm types to Rust types
|
||||||
#prolog
|
#prolog
|
||||||
|
|
||||||
|
@ -16,10 +16,10 @@
|
|||||||
|
|
||||||
use crate::fce_ast_types;
|
use crate::fce_ast_types;
|
||||||
use crate::new_ident;
|
use crate::new_ident;
|
||||||
|
use crate::parsed_type::*;
|
||||||
|
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use crate::parsed_type::*;
|
|
||||||
|
|
||||||
impl quote::ToTokens for fce_ast_types::AstExternModItem {
|
impl quote::ToTokens for fce_ast_types::AstExternModItem {
|
||||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||||
@ -121,10 +121,6 @@ fn generate_wrapper_functions(extern_item: &fce_ast_types::AstExternModItem) ->
|
|||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[allow(clippy::all)]
|
#[allow(clippy::all)]
|
||||||
#visibility unsafe fn #func_name(#(#arg_names: #arg_types), *) #return_type {
|
#visibility unsafe fn #func_name(#(#arg_names: #arg_types), *) #return_type {
|
||||||
// brings serialize/deserialize methods for records
|
|
||||||
#[allow(dead_code)]
|
|
||||||
use fluence::internal::FCEStructSerializable;
|
|
||||||
|
|
||||||
// calling the original function with converted args
|
// calling the original function with converted args
|
||||||
#return_expression #import_func_name(#(#raw_args), *);
|
#return_expression #import_func_name(#(#raw_args), *);
|
||||||
|
|
||||||
|
@ -43,10 +43,12 @@ impl quote::ToTokens for fce_ast_types::AstRecordItem {
|
|||||||
let glue_code = quote::quote! {
|
let glue_code = quote::quote! {
|
||||||
#original
|
#original
|
||||||
|
|
||||||
|
// used_id_fce is a special feature that indicates that this struct will be used inside
|
||||||
|
// FCE for some internal needs
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[allow(clippy::all)]
|
#[allow(clippy::all)]
|
||||||
impl fluence::internal::FCEStructSerializable for #record_name {
|
impl #record_name {
|
||||||
#serializer_fn
|
#serializer_fn
|
||||||
|
|
||||||
#deserializer_fn
|
#deserializer_fn
|
||||||
@ -67,8 +69,8 @@ fn generate_serializer_fn(record: &fce_ast_types::AstRecordItem) -> proc_macro2:
|
|||||||
let serializer = record.generate_serializer();
|
let serializer = record.generate_serializer();
|
||||||
|
|
||||||
quote::quote! {
|
quote::quote! {
|
||||||
fn __fce_generated_serialize(self) -> *const u8 {
|
pub fn __fce_generated_serialize(self) -> *const u8 {
|
||||||
let mut raw_record = Vec::new();
|
let mut raw_record: Vec<u64> = Vec::new();
|
||||||
|
|
||||||
#serializer
|
#serializer
|
||||||
|
|
||||||
@ -90,7 +92,7 @@ fn generate_deserializer_fn(record: &fce_ast_types::AstRecordItem) -> proc_macro
|
|||||||
crate::utils::get_record_size(record.fields.iter().map(|ast_field| &ast_field.ty));
|
crate::utils::get_record_size(record.fields.iter().map(|ast_field| &ast_field.ty));
|
||||||
|
|
||||||
quote::quote! {
|
quote::quote! {
|
||||||
unsafe fn __fce_generated_deserialize(record_ptr: *const u8) -> Self {
|
pub unsafe fn __fce_generated_deserialize(record_ptr: *const u8) -> Self {
|
||||||
let raw_record: Vec<u64> = Vec::from_raw_parts(record_ptr as _, #record_size, #record_size);
|
let raw_record: Vec<u64> = Vec::from_raw_parts(record_ptr as _, #record_size, #record_size);
|
||||||
|
|
||||||
#deserializer
|
#deserializer
|
||||||
|
@ -41,6 +41,7 @@ impl RecordSerializerGlueCodeGenerator for fce_ast_types::AstRecordItem {
|
|||||||
quote! {
|
quote! {
|
||||||
raw_record.push(#field_ident.as_ptr() as _);
|
raw_record.push(#field_ident.as_ptr() as _);
|
||||||
raw_record.push(#field_ident.len() as _);
|
raw_record.push(#field_ident.len() as _);
|
||||||
|
std::mem::forget(#field_ident);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ParsedType::Record(_) => {
|
ParsedType::Record(_) => {
|
||||||
|
@ -71,6 +71,9 @@ extern crate fluence_sdk_macro;
|
|||||||
extern crate fluence_sdk_main;
|
extern crate fluence_sdk_main;
|
||||||
|
|
||||||
pub use fluence_sdk_macro::fce;
|
pub use fluence_sdk_macro::fce;
|
||||||
|
pub use fluence_sdk_main::CallParameters;
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
pub use fluence_sdk_main::get_call_parameters;
|
||||||
#[cfg(feature = "logger")]
|
#[cfg(feature = "logger")]
|
||||||
pub use fluence_sdk_main::WasmLogger;
|
pub use fluence_sdk_main::WasmLogger;
|
||||||
|
|
||||||
@ -81,6 +84,4 @@ pub mod internal {
|
|||||||
pub use fluence_sdk_main::get_result_size;
|
pub use fluence_sdk_main::get_result_size;
|
||||||
pub use fluence_sdk_main::set_result_ptr;
|
pub use fluence_sdk_main::set_result_ptr;
|
||||||
pub use fluence_sdk_main::set_result_size;
|
pub use fluence_sdk_main::set_result_size;
|
||||||
|
|
||||||
pub use fluence_sdk_main::FCEStructSerializable;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user