Valery Antopol a61ddfc404
feat(marine-js)!: replace old marine-js with common marine-runtime + backend traits impl for JS (#332)
* add js wasm backend crate + blank trait impls

* make wasmtime a default feature for runtime and core

* WIP: mock WASI, greeting almost works

* WIP: added @wasmer/wasi, moved some stuff to JsStore, implementing Caller

* finalize Caller

* remove old code

* changing js API + fmt

* update wasm-bindgen generated and patched code

* update call_module to throw error, fix non-logging tests

* add multi-module test + update js api

* fix last element getting

* refactor interface + pass envs

* get rid of make_*_result

* small refactor

* support passing log function

* get rid of some todos

* use String instead of Vec<u8> for wasi envs

* use Strings for wasi envs in marine js

* little fix

* self-review fixes, import ordering

* self-review fixes, import ordering

* make clippy happy + fmt

* self-review fixes

* self-review fixes

* self-review fixes

* revert example artifact change

* pr fixes

* add __wbg_adapter_N updating code

* add all-types test

* fix build

* update marine_js.js

* Fix I64 handling

* pr fixes

* fix import order

* add copyrights

* Add comments, slightly beautify code

* fmt

* make clippy happy

* update js interface

* split function interface, improve naming

* update Cargo.lock

* update to new wasm-backend traits

* wip

* js glue code update

* improve comment

* use typed index collection

* Add more comments

* Add more comments

* Fix warnings

* pr fixes

* pr fixes
2023-07-25 19:49:55 +03:00

87 lines
2.7 KiB
Rust

/*
* Copyright 2023 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::JsStore;
use crate::JsInstance;
use crate::JsImports;
use crate::JsWasmBackend;
use crate::module_info::ModuleInfo;
use marine_wasm_backend_traits::prelude::*;
use anyhow::anyhow;
use js_sys::WebAssembly;
use js_sys::Uint8Array;
use wasm_bindgen::JsValue;
pub struct JsModule {
inner: WebAssembly::Module,
module_info: ModuleInfo,
}
impl Module<JsWasmBackend> for JsModule {
fn new(_store: &mut JsStore, wasm: &[u8]) -> ModuleCreationResult<Self> {
let data = Uint8Array::new_with_length(wasm.len() as u32);
data.copy_from(wasm);
let data_obj: JsValue = data.into();
let module = WebAssembly::Module::new(&data_obj).map_err(|e| {
log::debug!("Module::new failed: {:?}", e);
ModuleCreationError::FailedToCompileWasm(anyhow!(format!(
"error compiling module: {:?}",
e
)))
})?;
// JS WebAssembly module does not provide info about export signatures,
// so this data is extracted from wasm in control module.
let module_info = ModuleInfo::from_bytes(wasm)?;
let module = Self {
inner: module,
module_info,
};
Ok(module)
}
fn custom_sections(&self, name: &str) -> &[Vec<u8>] {
match self.module_info.custom_sections.get_vec(name) {
None => &[],
Some(data) => data,
}
}
fn instantiate(
&self,
store: &mut JsStore,
imports: &JsImports,
) -> InstantiationResult<<JsWasmBackend as WasmBackend>::Instance> {
let imports_object = imports.build_import_object(store.as_context(), &self.inner);
let instance = WebAssembly::Instance::new(&self.inner, &imports_object)
.map_err(|e| InstantiationError::Other(anyhow!("failed to instantiate: {:?}", e)))?;
// adds memory to @wasmer/wasi object
imports.bind_to_instance(store.as_context(), &instance);
let stored_instance = JsInstance::new(
&mut store.as_context_mut(),
instance,
self.module_info.clone(),
);
Ok(stored_instance)
}
}