Skip to content

Commit cb6f392

Browse files
authored
Add Component::{text,address_map,functions} methods (#13073)
These are similar to the methods with the same names on `Module`. Also publicly export `ModuleFunction`, since it is returned by `Module::functions` (and now also `Component::functions`). Also publicly re-export `StaticModuleIndex` and `FuncIndex`, since they appear inside `ModuleFunction`.
1 parent 9f8e3c7 commit cb6f392

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

crates/wasmtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ pub use wasmtime_environ::OperatorCost;
513513
pub use wasmtime_environ::ToWasmtimeResult;
514514
#[doc(inline)]
515515
pub use wasmtime_environ::error;
516+
pub use wasmtime_environ::{FuncIndex, StaticModuleIndex};
516517

517518
// Only for use in `bindgen!`-generated code.
518519
#[doc(hidden)]

crates/wasmtime/src/runtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub use instantiate::CompiledModule;
9999
pub use limits::*;
100100
pub use linker::*;
101101
pub use memory::*;
102-
pub use module::{Module, ModuleExport};
102+
pub use module::{Module, ModuleExport, ModuleFunction};
103103
pub use resources::*;
104104
#[cfg(all(feature = "async", feature = "call-hook"))]
105105
pub use store::CallHookHandler;

crates/wasmtime/src/runtime/component/component.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,31 @@ impl Component {
558558
&self.inner.code
559559
}
560560

561+
/// Get this component's code object's `.text` section, containing its
562+
/// compiled executable code.
563+
pub fn text(&self) -> &[u8] {
564+
self.engine_code().text()
565+
}
566+
567+
/// Get information about functions in this component's `.text` section:
568+
/// their module index, function index, name, and offset+length.
569+
pub fn functions(&self) -> impl Iterator<Item = crate::ModuleFunction> + '_ {
570+
self.inner
571+
.static_modules
572+
.values()
573+
.flat_map(|m| m.functions())
574+
}
575+
576+
/// Get the address map for this component's `.text` section.
577+
///
578+
/// See [`Module::address_map`] for more details.
579+
pub fn address_map(&self) -> Option<impl Iterator<Item = (usize, Option<u32>)> + '_> {
580+
Some(
581+
wasmtime_environ::iterate_address_map(self.engine_code().address_map_data())?
582+
.map(|(offset, file_pos)| (offset as usize, file_pos.file_offset())),
583+
)
584+
}
585+
561586
/// Same as [`Module::serialize`], except for a component.
562587
///
563588
/// Note that the artifact produced here must be passed to

crates/wasmtime/src/runtime/module.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use wasmparser::{Parser, ValidPayload, Validator};
2222
use wasmtime_environ::FrameTable;
2323
use wasmtime_environ::{
2424
CompiledFunctionsTable, CompiledModuleInfo, EntityIndex, HostPtr, ModuleTypes, ObjectKind,
25-
TypeTrace, VMOffsets, VMSharedTypeIndex, WasmChecksum,
25+
StaticModuleIndex, TypeTrace, VMOffsets, VMSharedTypeIndex, WasmChecksum,
2626
};
2727
#[cfg(feature = "gc")]
2828
use wasmtime_unwinder::ExceptionTable;
@@ -1089,10 +1089,12 @@ impl Module {
10891089
/// Results are yielded in a ModuleFunction struct.
10901090
pub fn functions<'a>(&'a self) -> impl ExactSizeIterator<Item = ModuleFunction> + 'a {
10911091
let module = self.compiled_module();
1092-
self.env_module().defined_func_indices().map(|idx| {
1092+
let module_index = self.env_module().module_index;
1093+
self.env_module().defined_func_indices().map(move |idx| {
10931094
let loc = module.func_loc(idx);
10941095
let idx = module.module().func_index(idx);
10951096
ModuleFunction {
1097+
module: module_index,
10961098
index: idx,
10971099
name: module.func_name(idx).map(|n| n.to_string()),
10981100
offset: loc.start as usize,
@@ -1213,9 +1215,15 @@ impl Module {
12131215

12141216
/// Describes a function for a given module.
12151217
pub struct ModuleFunction {
1218+
/// The static module index this function belongs to.
1219+
pub module: StaticModuleIndex,
1220+
/// The function index within the module.
12161221
pub index: wasmtime_environ::FuncIndex,
1222+
/// The display name of the function, if available.
12171223
pub name: Option<String>,
1224+
/// The byte offset of this function in the text section.
12181225
pub offset: usize,
1226+
/// The byte length of this function in the text section.
12191227
pub len: usize,
12201228
}
12211229

0 commit comments

Comments
 (0)