mirror of
https://github.com/fluencelabs/marine.git
synced 2025-06-17 08:51:30 +00:00
74 lines
2.6 KiB
Rust
74 lines
2.6 KiB
Rust
![]() |
/*
|
||
|
* 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 crate::instructions_generator::WITGenerator;
|
||
|
use crate::default_export_api_config::*;
|
||
|
|
||
|
pub use fluence_sdk_wit::FCEAst;
|
||
|
use wasmer_wit::ast::Interfaces;
|
||
|
|
||
|
// TODO: add error handling
|
||
|
/// Parse generated by rust-sdk AST types, generate instructions and embed them to Wasm file.
|
||
|
pub fn embed_wit(path: std::path::PathBuf) {
|
||
|
let wasm_module = walrus::ModuleConfig::new()
|
||
|
.parse_file(path.clone())
|
||
|
.unwrap();
|
||
|
let ast_set = wasm_ast_extractor(&wasm_module);
|
||
|
let interfaces = generate_interfaces(&ast_set);
|
||
|
|
||
|
let mut wasm_module = wit_parser::embed_wit(wasm_module, &interfaces);
|
||
|
wasm_module.emit_wasm_file(path).unwrap();
|
||
|
}
|
||
|
|
||
|
/// Extract all custom AST types previously embedded by rust-sdk from compiled binary.
|
||
|
fn wasm_ast_extractor(wasm_module: &walrus::Module) -> Vec<fluence_sdk_wit::FCEAst> {
|
||
|
let mut extracted_ast = Vec::new();
|
||
|
|
||
|
// consider only sections name of that starts with GENERATED_SECTION_PREFIX
|
||
|
for custom_module in wasm_module.customs.iter().filter(|(_, section)| {
|
||
|
section
|
||
|
.name()
|
||
|
.starts_with(fluence_sdk_wit::GENERATED_SECTION_PREFIX)
|
||
|
}) {
|
||
|
let default_ids = walrus::IdsToIndices::default();
|
||
|
let raw_data = custom_module.1.data(&default_ids);
|
||
|
let decoded_json: fluence_sdk_wit::FCEAst = serde_json::from_slice(&raw_data).unwrap();
|
||
|
extracted_ast.push(decoded_json);
|
||
|
}
|
||
|
|
||
|
extracted_ast
|
||
|
}
|
||
|
|
||
|
fn generate_interfaces(ast_set: &[FCEAst]) -> Interfaces<'_> {
|
||
|
let mut interfaces = Interfaces::default();
|
||
|
generate_default_export_api(&mut interfaces);
|
||
|
|
||
|
for ast in ast_set {
|
||
|
ast.generate_wit(&mut interfaces);
|
||
|
}
|
||
|
|
||
|
interfaces
|
||
|
}
|
||
|
|
||
|
fn generate_default_export_api(interfaces: &mut Interfaces) {
|
||
|
ALLOCATE_FUNC.update_interfaces(interfaces);
|
||
|
DEALLOCATE_FUNC.update_interfaces(interfaces);
|
||
|
SET_RESULT_SIZE_FUNC.update_interfaces(interfaces);
|
||
|
SET_RESULT_PTR_FUNC.update_interfaces(interfaces);
|
||
|
GET_RESULT_SIZE_FUNC.update_interfaces(interfaces);
|
||
|
GET_RESULT_PTR_FUNC.update_interfaces(interfaces);
|
||
|
}
|