summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock48
-rw-r--r--Cargo.toml6
-rw-r--r--LICENSE-APACHE176
-rw-r--r--LICENSE-MIT23
-rw-r--r--README.md7
-rw-r--r--crates/core/Cargo.toml21
-rw-r--r--crates/core/helloworld.wasmbin0 -> 115 bytes
-rw-r--r--crates/core/helloworld.wat15
-rw-r--r--crates/core/src/bin.rs9
-rw-r--r--crates/core/src/instructions.rs106
-rw-r--r--crates/core/src/lib.rs178
12 files changed, 590 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ea8c4bf
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..4a3e0e4
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,48 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "equivalent"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
+
+[[package]]
+name = "hashbrown"
+version = "0.14.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156"
+
+[[package]]
+name = "indexmap"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
+dependencies = [
+ "equivalent",
+ "hashbrown",
+]
+
+[[package]]
+name = "semver"
+version = "1.0.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
+
+[[package]]
+name = "wasmcore"
+version = "0.1.0"
+dependencies = [
+ "wasmparser",
+]
+
+[[package]]
+name = "wasmparser"
+version = "0.118.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ebbb91574de0011ded32b14db12777e7dd5e9ea2f9d7317a1ab51a9495c75924"
+dependencies = [
+ "indexmap",
+ "semver",
+]
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..0cfbbe1
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,6 @@
+[workspace]
+members = [
+ "crates/core",
+]
+
+resolver = "2" \ No newline at end of file
diff --git a/LICENSE-APACHE b/LICENSE-APACHE
new file mode 100644
index 0000000..a7e77cb
--- /dev/null
+++ b/LICENSE-APACHE
@@ -0,0 +1,176 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS \ No newline at end of file
diff --git a/LICENSE-MIT b/LICENSE-MIT
new file mode 100644
index 0000000..468cd79
--- /dev/null
+++ b/LICENSE-MIT
@@ -0,0 +1,23 @@
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE. \ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..aca28d6
--- /dev/null
+++ b/README.md
@@ -0,0 +1,7 @@
+# TinyWasm
+
+# License
+
+Licensed under either of [Apache License, Version 2.0](./LICENSE-APACHE) or [MIT license](./LICENSE-MIT) at your option.
+
+Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in OKV by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. \ No newline at end of file
diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml
new file mode 100644
index 0000000..e8c52b9
--- /dev/null
+++ b/crates/core/Cargo.toml
@@ -0,0 +1,21 @@
+[package]
+name="wasmcore"
+version="0.1.0"
+edition="2021"
+
+[lib]
+name="wasmcore"
+path="src/lib.rs"
+
+[[bin]]
+name="wasmcore"
+path="src/bin.rs"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+wasmparser={version="0.118.0", default-features=false}
+
+[features]
+default=["std"]
+std=[]
diff --git a/crates/core/helloworld.wasm b/crates/core/helloworld.wasm
new file mode 100644
index 0000000..a5c95d0
--- /dev/null
+++ b/crates/core/helloworld.wasm
Binary files differ
diff --git a/crates/core/helloworld.wat b/crates/core/helloworld.wat
new file mode 100644
index 0000000..b74c98f
--- /dev/null
+++ b/crates/core/helloworld.wat
@@ -0,0 +1,15 @@
+(module
+ ;; Imports from JavaScript namespace
+ (import "console" "log" (func $log (param i32 i32))) ;; Import log function
+ (import "js" "mem" (memory 1)) ;; Import 1 page of memory (54kb)
+
+ ;; Data section of our module
+ (data (i32.const 0) "Hello World from WebAssembly!")
+
+ ;; Function declaration: Exported as helloWorld(), no arguments
+ (func (export "helloWorld")
+ i32.const 0 ;; pass offset 0 to log
+ i32.const 29 ;; pass length 29 to log (strlen of sample text)
+ call $log
+ )
+) \ No newline at end of file
diff --git a/crates/core/src/bin.rs b/crates/core/src/bin.rs
new file mode 100644
index 0000000..c4257e8
--- /dev/null
+++ b/crates/core/src/bin.rs
@@ -0,0 +1,9 @@
+use wasmcore::{self, Module};
+
+pub static WASM: &'static [u8] = include_bytes!("../helloworld.wasm");
+
+fn main() {
+ let module = Module::new(WASM);
+
+ println!("{:#?}", module);
+}
diff --git a/crates/core/src/instructions.rs b/crates/core/src/instructions.rs
new file mode 100644
index 0000000..e3e6a08
--- /dev/null
+++ b/crates/core/src/instructions.rs
@@ -0,0 +1,106 @@
+// https://webassembly.github.io/spec/core/binary/instructions.html
+
+// Controll Instructions
+pub mod control {
+ pub const WASM_UNREACHABLE: u8 = 0x00;
+ pub const WASM_NOP: u8 = 0x01; // do nothing
+
+ // stuctured instructions
+ pub const WASM_BLOCK: u8 = 0x02;
+ pub const WASM_LOOP: u8 = 0x03;
+ pub const WASM_IF: u8 = 0x04;
+
+ pub const WASM_ELSE: u8 = 0x05;
+ pub const WASM_END: u8 = 0x0B;
+ pub const WASM_BR: u8 = 0x0C;
+ pub const WASM_BR_IF: u8 = 0x0D;
+ pub const WASM_BR_TABLE: u8 = 0x0E;
+ pub const WASM_RETURN: u8 = 0x0F;
+ pub const WASM_CALL: u8 = 0x10;
+ pub const WASM_CALL_INDIRECT: u8 = 0x11;
+ pub const WASM_DROP: u8 = 0x1A;
+}
+
+// Reference Instructions
+pub mod reference {
+ pub const WASM_REF_NULL: u8 = 0xD0;
+ pub const WASM_REF_IS_NULL: u8 = 0xD1;
+ pub const WASM_REF_FUNC: u8 = 0xD2;
+}
+
+// Parametric Instructions
+pub mod parametric {
+ pub const WASM_DROP: u8 = 0x1A;
+ pub const WASM_SELECT: u8 = 0x1B;
+ pub const WASM_SELECT_T: u8 = 0x1C;
+}
+
+// Variable Instructions
+pub mod variable {
+ pub const WASM_LOCAL_GET: u8 = 0x20;
+ pub const WASM_LOCAL_SET: u8 = 0x21;
+ pub const WASM_LOCAL_TEE: u8 = 0x22;
+ pub const WASM_GLOBAL_GET: u8 = 0x23;
+ pub const WASM_GLOBAL_SET: u8 = 0x24;
+}
+
+// Table Instructions
+pub mod table {
+ pub const WASM_TABLE_GET: u8 = 0x25;
+ pub const WASM_TABLE_SET: u8 = 0x26;
+ pub const WASM_TABLE_INIT: u8 = 0xFC;
+ pub const WASM_ELEM_DROP: u8 = 0xFC;
+ pub const WASM_TABLE_COPY: u8 = 0xFC;
+ pub const WASM_TABLE_GROW: u8 = 0xFC;
+ pub const WASM_TABLE_SIZE: u8 = 0xFC;
+ pub const WASM_TABLE_FILL: u8 = 0xFC;
+}
+
+// Memory Instructions
+pub mod memory {
+ pub const WASM_I32_LOAD: u8 = 0x28;
+ pub const WASM_I64_LOAD: u8 = 0x29;
+ pub const WASM_F32_LOAD: u8 = 0x2A;
+ pub const WASM_F64_LOAD: u8 = 0x2B;
+ pub const WASM_I32_LOAD8_S: u8 = 0x2C;
+ pub const WASM_I32_LOAD8_U: u8 = 0x2D;
+ pub const WASM_I32_LOAD16_S: u8 = 0x2E;
+ pub const WASM_I32_LOAD16_U: u8 = 0x2F;
+ pub const WASM_I64_LOAD8_S: u8 = 0x30;
+ pub const WASM_I64_LOAD8_U: u8 = 0x31;
+ pub const WASM_I64_LOAD16_S: u8 = 0x32;
+ pub const WASM_I64_LOAD16_U: u8 = 0x33;
+ pub const WASM_I64_LOAD32_S: u8 = 0x34;
+ pub const WASM_I64_LOAD32_U: u8 = 0x35;
+ pub const WASM_I32_STORE: u8 = 0x36;
+ pub const WASM_I64_STORE: u8 = 0x37;
+ pub const WASM_F32_STORE: u8 = 0x38;
+ pub const WASM_F64_STORE: u8 = 0x39;
+ pub const WASM_I32_STORE8: u8 = 0x3A;
+ pub const WASM_I32_STORE16: u8 = 0x3B;
+ pub const WASM_I64_STORE8: u8 = 0x3C;
+ pub const WASM_I64_STORE16: u8 = 0x3D;
+ pub const WASM_I64_STORE32: u8 = 0x3E;
+ pub const WASM_MEMORY_SIZE: u8 = 0x3F;
+ pub const WASM_MEMORY_GROW: u8 = 0x40;
+ pub const WASM_MEMORY_INIT: u8 = 0xFC;
+ pub const WASM_DATA_DROP: u8 = 0xFC;
+ pub const WASM_MEMORY_COPY: u8 = 0xFC;
+ pub const WASM_MEMORY_FILL: u8 = 0xFC;
+}
+
+// Numeric Instructions
+pub mod numeric {
+ // Constants
+ pub const WASM_I32_CONST: u8 = 0x41;
+ pub const WASM_I64_CONST: u8 = 0x42;
+ pub const WASM_F32_CONST: u8 = 0x43;
+ pub const WASM_F64_CONST: u8 = 0x44;
+
+ // Operations
+ pub const START_NUMERIC: u8 = 0x45;
+ pub const END_NUMERIC: u8 = 0xC4;
+ pub const WASM_SATURATING_TRUNC: u8 = 0xFC;
+}
+
+pub const WASM_VEC: u8 = 0xFD;
diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs
new file mode 100644
index 0000000..ee8a3be
--- /dev/null
+++ b/crates/core/src/lib.rs
@@ -0,0 +1,178 @@
+#![no_std]
+#![forbid(unsafe_code)]
+
+#[cfg(feature = "std")]
+extern crate std;
+use std::println;
+
+extern crate alloc;
+use alloc::vec::Vec;
+
+use wasmparser::{
+ DataSectionReader, ElementSectionReader, ExportSectionReader, FunctionBody,
+ FunctionSectionReader, GlobalSectionReader, ImportSectionReader, MemorySectionReader, Payload,
+ TableSectionReader, TypeSectionReader, Validator,
+};
+mod instructions;
+
+struct Store {}
+
+pub struct Module<'a> {
+ reader: ModuleReader<'a>,
+}
+
+impl<'a> core::fmt::Debug for Module<'a> {
+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+ f.debug_struct("Module")
+ .field("version", &self.reader.version)
+ .field("type_section", &self.reader.type_section)
+ .field("function_section", &self.reader.function_section)
+ .field("table_section", &self.reader.table_section)
+ .field("memory_section", &self.reader.memory_section)
+ .field("global_section", &self.reader.global_section)
+ .field("element_section", &self.reader.element_section)
+ .field("data_section", &self.reader.data_section)
+ .field("code_section", &self.reader.code_section)
+ .field("import_section", &self.reader.import_section)
+ .field("export_section", &self.reader.export_section)
+ .finish()
+ }
+}
+
+#[derive(Default)]
+pub struct ModuleReader<'a> {
+ pub version: Option<u16>,
+ pub type_section: Option<TypeSectionReader<'a>>,
+ pub function_section: Option<FunctionSectionReader<'a>>,
+ pub table_section: Option<TableSectionReader<'a>>,
+ pub memory_section: Option<MemorySectionReader<'a>>,
+ pub global_section: Option<GlobalSectionReader<'a>>,
+ pub element_section: Option<ElementSectionReader<'a>>,
+ pub data_section: Option<DataSectionReader<'a>>,
+ pub code_section: Option<CodeSection<'a>>,
+ pub import_section: Option<ImportSectionReader<'a>>,
+ pub export_section: Option<ExportSectionReader<'a>>,
+}
+
+#[derive(Debug)]
+pub struct CodeSection<'a> {
+ pub(crate) functions: Vec<FunctionBody<'a>>,
+}
+
+impl<'a> CodeSection<'a> {
+ fn new() -> Self {
+ Self {
+ functions: Vec::new(),
+ }
+ }
+}
+
+impl<'a> Module<'a> {
+ pub fn new(wasm: &'a [u8]) -> Result<Module, ()> {
+ let mut validator = Validator::new();
+ let mut reader = ModuleReader::new();
+
+ for payload in wasmparser::Parser::new(0).parse_all(wasm) {
+ reader.process_payload(payload.unwrap(), &mut validator)?;
+ }
+
+ Ok(Self { reader })
+ }
+}
+
+impl<'a> ModuleReader<'a> {
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ pub fn process_payload(
+ &mut self,
+ payload: Payload<'a>,
+ validator: &mut Validator,
+ ) -> Result<bool, ()> {
+ use wasmparser::Payload::*;
+ match payload {
+ Version {
+ num,
+ encoding,
+ range,
+ } => {
+ validator.version(num, encoding, &range).map_err(|_| ())?;
+ self.version = Some(num);
+ match encoding {
+ wasmparser::Encoding::Module => {}
+ wasmparser::Encoding::Component => return Err(()),
+ }
+ }
+ TypeSection(reader) => {
+ validator.type_section(&reader).map_err(|_| ())?;
+ self.type_section = Some(reader);
+ }
+ FunctionSection(reader) => {
+ validator.function_section(&reader).map_err(|_| ())?;
+ self.function_section = Some(reader);
+ }
+ TableSection(reader) => {
+ validator.table_section(&reader).map_err(|_| ())?;
+ self.table_section = Some(reader);
+ }
+ MemorySection(reader) => {
+ validator.memory_section(&reader).map_err(|_| ())?;
+ self.memory_section = Some(reader);
+ }
+ GlobalSection(reader) => {
+ validator.global_section(&reader).map_err(|_| ())?;
+ self.global_section = Some(reader);
+ }
+ ElementSection(reader) => {
+ validator.element_section(&reader).map_err(|_| ())?;
+ self.element_section = Some(reader);
+ }
+ DataSection(reader) => {
+ validator.data_section(&reader).map_err(|_| ())?;
+ self.data_section = Some(reader);
+ }
+ CodeSectionStart { count, range, .. } => {
+ validator
+ .code_section_start(count, &range)
+ .map_err(|_| ())?;
+
+ self.code_section = Some(CodeSection::new());
+ }
+ CodeSectionEntry(function) => {
+ validator.code_section_entry(&function).map_err(|_| ())?;
+
+ if let Some(code_section) = &mut self.code_section {
+ code_section.functions.push(function);
+ } else {
+ return Err(());
+ }
+ }
+ ImportSection(reader) => {
+ validator.import_section(&reader).map_err(|_| ())?;
+ self.import_section = Some(reader);
+ }
+ ExportSection(reader) => {
+ validator.export_section(&reader).map_err(|_| ())?;
+ self.export_section = Some(reader);
+ }
+
+ End(offset) => {
+ validator.end(offset).map_err(|_| ())?;
+ return Ok(true);
+ }
+ x => println!("Unknown payload: {:?}", x),
+ };
+
+ Ok(false)
+ }
+}
+struct Instance {}
+
+pub fn parse(wasm: &[u8]) -> Result<Payload<'_>, ()> {
+ for payload in wasmparser::Parser::new(0).parse_all(wasm) {
+ return Ok(payload.unwrap());
+ }
+
+ return Err(());
+}