1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
use crate::{
collections::{
string_interner::{InternHint, Sym as Symbol},
StringInterner,
},
func::{FuncEntity, HostFuncEntity, HostFuncTrampolineEntity},
module::{ImportName, ImportType},
AsContext,
AsContextMut,
Caller,
Engine,
Error,
Extern,
ExternType,
Func,
FuncType,
GlobalType,
InstancePre,
IntoFunc,
MemoryType,
Module,
TableType,
Val,
};
use core::{
fmt::{self, Debug, Display},
marker::PhantomData,
};
use std::{
collections::{btree_map::Entry, BTreeMap},
sync::Arc,
vec::Vec,
};
/// An error that may occur upon operating with [`Linker`] instances.
#[derive(Debug)]
pub enum LinkerError {
/// Encountered duplicate definitions for the same name.
DuplicateDefinition {
/// The duplicate import name of the definition.
import_name: ImportName,
},
/// Encountered when no definition for an import is found.
MissingDefinition {
/// The name of the import for which no definition was found.
name: ImportName,
/// The type of the import for which no definition has been found.
ty: ExternType,
},
/// Encountered when a definition with invalid type is found.
InvalidTypeDefinition {
/// The name of the import for which no definition was found.
name: ImportName,
/// The expected import type.
expected: ExternType,
/// The found definition type.
found: ExternType,
},
/// Encountered when a [`FuncType`] does not match the expected [`FuncType`].
FuncTypeMismatch {
/// The name of the import with the mismatched type.
name: ImportName,
/// The expected [`FuncType`].
expected: FuncType,
/// The mismatching [`FuncType`] found.
found: FuncType,
},
/// Encountered when a [`TableType`] does not match the expected [`TableType`].
InvalidTableSubtype {
/// The name of the import with the invalid [`TableType`].
name: ImportName,
/// The [`TableType`] that is supposed to be a subtype of `other`.
ty: TableType,
/// The [`TableType`] this is supposed to be a supertype of `ty`.
other: TableType,
},
/// Encountered when a [`MemoryType`] does not match the expected [`MemoryType`].
InvalidMemorySubtype {
/// The name of the import with the invalid [`MemoryType`].
name: ImportName,
/// The [`MemoryType`] that is supposed to be a subtype of `other`.
ty: MemoryType,
/// The [`MemoryType`] this is supposed to be a supertype of `ty`.
other: MemoryType,
},
/// Encountered when a [`GlobalType`] does not match the expected [`GlobalType`].
GlobalTypeMismatch {
/// The name of the import with the mismatched type.
name: ImportName,
/// The expected [`GlobalType`].
expected: GlobalType,
/// The mismatching [`GlobalType`] found.
found: GlobalType,
},
}
impl LinkerError {
/// Creates a new [`LinkerError`] for when an imported definition was not found.
fn missing_definition(import: &ImportType) -> Self {
Self::MissingDefinition {
name: import.import_name().clone(),
ty: import.ty().clone(),
}
}
/// Creates a new [`LinkerError`] for when an imported definition has an invalid type.
fn invalid_type_definition(import: &ImportType, found: &ExternType) -> Self {
Self::InvalidTypeDefinition {
name: import.import_name().clone(),
expected: import.ty().clone(),
found: found.clone(),
}
}
/// Create a new [`LinkerError`] for when a [`FuncType`] mismatched.
fn func_type_mismatch(name: &ImportName, expected: &FuncType, found: &FuncType) -> Self {
Self::FuncTypeMismatch {
name: name.clone(),
expected: expected.clone(),
found: found.clone(),
}
}
/// Create a new [`LinkerError`] for when a [`TableType`] `ty` unexpectedly is not a subtype of `other`.
fn table_type_mismatch(name: &ImportName, ty: &TableType, other: &TableType) -> Self {
Self::InvalidTableSubtype {
name: name.clone(),
ty: *ty,
other: *other,
}
}
/// Create a new [`LinkerError`] for when a [`MemoryType`] `ty` unexpectedly is not a subtype of `other`.
fn invalid_memory_subtype(name: &ImportName, ty: &MemoryType, other: &MemoryType) -> Self {
Self::InvalidMemorySubtype {
name: name.clone(),
ty: *ty,
other: *other,
}
}
/// Create a new [`LinkerError`] for when a [`GlobalType`] mismatched.
fn global_type_mismatch(name: &ImportName, expected: &GlobalType, found: &GlobalType) -> Self {
Self::GlobalTypeMismatch {
name: name.clone(),
expected: *expected,
found: *found,
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for LinkerError {}
impl Display for LinkerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::DuplicateDefinition { import_name } => {
write!(
f,
"encountered duplicate definition with name `{import_name}`",
)
}
Self::MissingDefinition { name, ty } => {
write!(
f,
"cannot find definition for import {name} with type {ty:?}",
)
}
Self::InvalidTypeDefinition {
name,
expected,
found,
} => {
write!(f, "found definition for import {name} with type {expected:?} but found type {found:?}")
}
Self::FuncTypeMismatch {
name,
expected,
found,
} => {
write!(
f,
"function type mismatch for import {name}: \
expected {expected:?} but found {found:?}",
)
}
Self::InvalidTableSubtype { name, ty, other } => {
write!(
f,
"import {name}: table type {ty:?} is not a subtype of {other:?}"
)
}
Self::InvalidMemorySubtype { name, ty, other } => {
write!(
f,
"import {name}: memory type {ty:?} is not a subtype of {other:?}"
)
}
Self::GlobalTypeMismatch {
name,
expected,
found,
} => {
write!(
f,
"global variable type mismatch for import {name}: \
expected {expected:?} but found {found:?}",
)
}
}
}
}
/// Wasm import keys.
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
#[repr(transparent)]
struct ImportKey {
/// Merged module and name symbols.
///
/// Merging allows for a faster `Ord` implementation.
module_and_name: u64,
}
impl Debug for ImportKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ImportKey")
.field("module", &self.module())
.field("name", &self.name())
.finish()
}
}
impl ImportKey {
/// Creates a new [`ImportKey`] from the given `module` and `name` symbols.
#[inline]
pub fn new(module: Symbol, name: Symbol) -> Self {
let module_and_name = u64::from(module.into_u32()) << 32 | u64::from(name.into_u32());
Self { module_and_name }
}
/// Returns the `module` [`Symbol`] of the [`ImportKey`].
#[inline]
pub fn module(&self) -> Symbol {
Symbol::from_u32((self.module_and_name >> 32) as u32)
}
/// Returns the `name` [`Symbol`] of the [`ImportKey`].
#[inline]
pub fn name(&self) -> Symbol {
Symbol::from_u32(self.module_and_name as u32)
}
}
/// A [`Linker`] definition.
#[derive(Debug)]
enum Definition<T> {
/// An external item from an [`Instance`](crate::Instance).
Extern(Extern),
/// A [`Linker`] internal host function.
HostFunc(HostFuncTrampolineEntity<T>),
}
impl<T> Clone for Definition<T> {
fn clone(&self) -> Self {
match self {
Self::Extern(definition) => Self::Extern(*definition),
Self::HostFunc(host_func) => Self::HostFunc(host_func.clone()),
}
}
}
impl<T> Definition<T> {
/// Returns the [`Extern`] item if this [`Definition`] is [`Definition::Extern`].
///
/// Otherwise returns `None`.
fn as_extern(&self) -> Option<&Extern> {
match self {
Definition::Extern(item) => Some(item),
Definition::HostFunc(_) => None,
}
}
/// Returns the [`ExternType`] of the [`Definition`].
pub fn ty(&self, ctx: impl AsContext) -> ExternType {
match self {
Definition::Extern(item) => item.ty(ctx),
Definition::HostFunc(host_func) => ExternType::Func(host_func.func_type().clone()),
}
}
/// Returns the [`Func`] of the [`Definition`] if it is a function.
///
/// Returns `None` otherwise.
///
/// # Note
///
/// - This allocates a new [`Func`] on the `ctx` if it is a [`Linker`]
/// defined host function.
/// - This unifies handling of [`Definition::Extern(Extern::Func)`] and
/// [`Definition::HostFunc`].
pub fn as_func(&self, mut ctx: impl AsContextMut<Data = T>) -> Option<Func> {
match self {
Definition::Extern(Extern::Func(func)) => Some(*func),
Definition::HostFunc(host_func) => {
let trampoline = ctx
.as_context_mut()
.store
.alloc_trampoline(host_func.trampoline().clone());
let ty = host_func.func_type();
let entity = HostFuncEntity::new(ctx.as_context().engine(), ty, trampoline);
let func = ctx
.as_context_mut()
.store
.inner
.alloc_func(FuncEntity::Host(entity));
Some(func)
}
_ => None,
}
}
}
/// A linker used to define module imports and instantiate module instances.
#[derive(Debug)]
pub struct Linker<T> {
/// The underlying [`Engine`] for the [`Linker`].
///
/// # Note
///
/// Primarily required to define [`Linker`] owned host functions
// using [`Linker::func_wrap`] and [`Linker::func_new`]. TODO: implement methods
engine: Engine,
/// Definitions shared with other [`Linker`] instances created by the same [`LinkerBuilder`].
///
/// `None` if no [`LinkerBuilder`] was used for creation of the [`Linker`].
shared: Option<Arc<LinkerInner<T>>>,
/// Inner linker implementation details.
inner: LinkerInner<T>,
}
impl<T> Clone for Linker<T> {
fn clone(&self) -> Linker<T> {
Self {
engine: self.engine.clone(),
shared: self.shared.clone(),
inner: self.inner.clone(),
}
}
}
impl<T> Default for Linker<T> {
fn default() -> Self {
Self::new(&Engine::default())
}
}
impl<T> Linker<T> {
/// Creates a new [`Linker`].
pub fn new(engine: &Engine) -> Self {
Self {
engine: engine.clone(),
shared: None,
inner: LinkerInner::default(),
}
}
/// Creates a new [`LinkerBuilder`] to construct a [`Linker`].
pub fn build() -> LinkerBuilder<state::Constructing, T> {
LinkerBuilder {
inner: Arc::new(LinkerInner::default()),
marker: PhantomData,
}
}
/// Returns the underlying [`Engine`] of the [`Linker`].
pub fn engine(&self) -> &Engine {
&self.engine
}
/// Ensures that the `name` in `module` is undefined in the shared definitions.
///
/// Returns `Ok` if no shared definition exists.
///
/// # Errors
///
/// If there exists a shared definition for `name` in `module`.
fn ensure_undefined(&self, module: &str, name: &str) -> Result<(), LinkerError> {
if let Some(shared) = &self.shared {
if shared.has_definition(module, name) {
return Err(LinkerError::DuplicateDefinition {
import_name: ImportName::new(module, name),
});
}
}
Ok(())
}
/// Define a new item in this [`Linker`].
///
/// # Errors
///
/// If there already is a definition under the same name for this [`Linker`].
pub fn define(
&mut self,
module: &str,
name: &str,
item: impl Into<Extern>,
) -> Result<&mut Self, LinkerError> {
self.ensure_undefined(module, name)?;
let key = self.inner.new_import_key(module, name);
self.inner.insert(key, Definition::Extern(item.into()))?;
Ok(self)
}
/// Creates a new named [`Func::new`]-style host [`Func`] for this [`Linker`].
///
/// For more information see [`Linker::func_wrap`].
///
/// # Errors
///
/// If there already is a definition under the same name for this [`Linker`].
pub fn func_new(
&mut self,
module: &str,
name: &str,
ty: FuncType,
func: impl Fn(Caller<'_, T>, &[Val], &mut [Val]) -> Result<(), Error> + Send + Sync + 'static,
) -> Result<&mut Self, LinkerError> {
self.ensure_undefined(module, name)?;
let func = HostFuncTrampolineEntity::new(ty, func);
let key = self.inner.new_import_key(module, name);
self.inner.insert(key, Definition::HostFunc(func))?;
Ok(self)
}
/// Creates a new named [`Func::new`]-style host [`Func`] for this [`Linker`].
///
/// For information how to use this API see [`Func::wrap`].
///
/// This method creates a host function for this [`Linker`] under the given name.
/// It is distinct in its ability to create a [`Store`] independent
/// host function. Host functions defined this way can be used to instantiate
/// instances in multiple different [`Store`] entities.
///
/// The same applies to other [`Linker`] methods to define new [`Func`] instances
/// such as [`Linker::func_new`].
///
/// In a concurrently running program, this means that these host functions
/// could be called concurrently if different [`Store`] entities are executing on
/// different threads.
///
/// # Errors
///
/// If there already is a definition under the same name for this [`Linker`].
///
/// [`Store`]: crate::Store
pub fn func_wrap<Params, Args>(
&mut self,
module: &str,
name: &str,
func: impl IntoFunc<T, Params, Args>,
) -> Result<&mut Self, LinkerError> {
self.ensure_undefined(module, name)?;
let func = HostFuncTrampolineEntity::wrap(func);
let key = self.inner.new_import_key(module, name);
self.inner.insert(key, Definition::HostFunc(func))?;
Ok(self)
}
/// Looks up a defined [`Extern`] by name in this [`Linker`].
///
/// - Returns `None` if this name was not previously defined in this [`Linker`].
/// - Returns `None` if the definition is a [`Linker`] defined host function.
///
/// # Panics
///
/// If the [`Engine`] of this [`Linker`] and the [`Engine`] of `context` are not the same.
pub fn get(
&self,
context: impl AsContext<Data = T>,
module: &str,
name: &str,
) -> Option<Extern> {
match self.get_definition(context, module, name) {
Some(Definition::Extern(item)) => Some(*item),
_ => None,
}
}
/// Looks up a [`Definition`] by name in this [`Linker`].
///
/// Returns `None` if this name was not previously defined in this [`Linker`].
///
/// # Panics
///
/// If the [`Engine`] of this [`Linker`] and the [`Engine`] of `context` are not the same.
fn get_definition(
&self,
context: impl AsContext<Data = T>,
module: &str,
name: &str,
) -> Option<&Definition<T>> {
assert!(Engine::same(
context.as_context().store.engine(),
self.engine()
));
if let Some(shared) = &self.shared {
if let Some(item) = shared.get_definition(module, name) {
return Some(item);
}
}
self.inner.get_definition(module, name)
}
/// Instantiates the given [`Module`] using the definitions in the [`Linker`].
///
/// # Panics
///
/// If the [`Engine`] of the [`Linker`] and `context` are not the same.
///
/// # Errors
///
/// - If the linker does not define imports of the instantiated [`Module`].
/// - If any imported item does not satisfy its type requirements.
pub fn instantiate(
&self,
mut context: impl AsContextMut<Data = T>,
module: &Module,
) -> Result<InstancePre, Error> {
assert!(Engine::same(self.engine(), context.as_context().engine()));
// TODO: possibly add further resource limtation here on number of externals.
// Not clear that user can't import the same external lots of times to inflate this.
let externals = module
.imports()
.map(|import| self.process_import(&mut context, import))
.collect::<Result<Vec<Extern>, Error>>()?;
module.instantiate(context, externals)
}
/// Processes a single [`Module`] import.
///
/// # Panics
///
/// If the [`Engine`] of the [`Linker`] and `context` are not the same.
///
/// # Errors
///
/// If the imported item does not satisfy constraints set by the [`Module`].
fn process_import(
&self,
mut context: impl AsContextMut<Data = T>,
import: ImportType,
) -> Result<Extern, Error> {
assert!(Engine::same(self.engine(), context.as_context().engine()));
let import_name = import.import_name();
let module_name = import.module();
let field_name = import.name();
let resolved = self
.get_definition(context.as_context(), module_name, field_name)
.ok_or_else(|| LinkerError::missing_definition(&import))?;
let invalid_type = || LinkerError::invalid_type_definition(&import, &resolved.ty(&context));
match import.ty() {
ExternType::Func(expected_type) => {
let found_type = resolved
.ty(&context)
.func()
.cloned()
.ok_or_else(invalid_type)?;
if &found_type != expected_type {
return Err(Error::from(LinkerError::func_type_mismatch(
import_name,
expected_type,
&found_type,
)));
}
let func = resolved
.as_func(&mut context)
.expect("already asserted that `resolved` is a function");
Ok(Extern::Func(func))
}
ExternType::Table(expected_type) => {
let table = resolved
.as_extern()
.copied()
.and_then(Extern::into_table)
.ok_or_else(invalid_type)?;
let found_type = table.dynamic_ty(context);
found_type.is_subtype_or_err(expected_type).map_err(|_| {
LinkerError::table_type_mismatch(import_name, expected_type, &found_type)
})?;
Ok(Extern::Table(table))
}
ExternType::Memory(expected_type) => {
let memory = resolved
.as_extern()
.copied()
.and_then(Extern::into_memory)
.ok_or_else(invalid_type)?;
let found_type = memory.dynamic_ty(context);
found_type.is_subtype_or_err(expected_type).map_err(|_| {
LinkerError::invalid_memory_subtype(import_name, expected_type, &found_type)
})?;
Ok(Extern::Memory(memory))
}
ExternType::Global(expected_type) => {
let global = resolved
.as_extern()
.copied()
.and_then(Extern::into_global)
.ok_or_else(invalid_type)?;
let found_type = global.ty(context);
if &found_type != expected_type {
return Err(Error::from(LinkerError::global_type_mismatch(
import_name,
expected_type,
&found_type,
)));
}
Ok(Extern::Global(global))
}
}
}
}
/// Contains type states for the [`LinkerBuilder`] construction process.
pub mod state {
/// Signals that the [`LinkerBuilder`] is itself under construction.
///
/// [`LinkerBuilder`]: super::LinkerBuilder
pub enum Constructing {}
/// Signals that the [`LinkerBuilder`] is ready to create new [`Linker`] instances.
///
/// [`Linker`]: super::Linker
/// [`LinkerBuilder`]: super::LinkerBuilder
pub enum Ready {}
}
/// A linker used to define module imports and instantiate module instances.
///
/// Create this type via the [`Linker::build`] method.
#[derive(Debug)]
pub struct LinkerBuilder<State, T> {
/// Internal linker implementation details.
inner: Arc<LinkerInner<T>>,
/// The [`LinkerBuilder`] type state.
marker: PhantomData<fn() -> State>,
}
impl<T> Clone for LinkerBuilder<state::Ready, T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
marker: PhantomData,
}
}
}
impl<T> LinkerBuilder<state::Ready, T> {
/// Finishes construction of the [`Linker`] by attaching an [`Engine`].
pub fn create(&self, engine: &Engine) -> Linker<T> {
Linker {
engine: engine.clone(),
shared: self.inner.clone().into(),
inner: <LinkerInner<T>>::default(),
}
}
}
impl<T> LinkerBuilder<state::Constructing, T> {
/// Signals that the [`LinkerBuilder`] is now ready to create new [`Linker`] instances.
pub fn finish(self) -> LinkerBuilder<state::Ready, T> {
LinkerBuilder {
inner: self.inner,
marker: PhantomData,
}
}
/// Returns an exclusive reference to the underlying [`Linker`] internals if no [`Linker`] has been built, yet.
///
/// # Panics
///
/// If the [`LinkerBuilder`] has already created a [`Linker`] using [`LinkerBuilder::finish`].
fn inner_mut(&mut self) -> &mut LinkerInner<T> {
Arc::get_mut(&mut self.inner).unwrap_or_else(|| {
unreachable!("tried to define host function in LinkerBuilder after Linker creation")
})
}
/// Creates a new named [`Func::new`]-style host [`Func`] for this [`Linker`].
///
/// For more information see [`Linker::func_wrap`].
///
/// # Errors
///
/// If there already is a definition under the same name for this [`Linker`].
///
/// # Panics
///
/// If the [`LinkerBuilder`] has already created a [`Linker`] using [`LinkerBuilder::finish`].
pub fn func_new(
&mut self,
module: &str,
name: &str,
ty: FuncType,
func: impl Fn(Caller<'_, T>, &[Val], &mut [Val]) -> Result<(), Error> + Send + Sync + 'static,
) -> Result<&mut Self, LinkerError> {
self.inner_mut().func_new(module, name, ty, func)?;
Ok(self)
}
/// Creates a new named [`Func::new`]-style host [`Func`] for this [`Linker`].
///
/// For information how to use this API see [`Func::wrap`].
///
/// This method creates a host function for this [`Linker`] under the given name.
/// It is distinct in its ability to create a [`Store`] independent
/// host function. Host functions defined this way can be used to instantiate
/// instances in multiple different [`Store`] entities.
///
/// The same applies to other [`Linker`] methods to define new [`Func`] instances
/// such as [`Linker::func_new`].
///
/// In a concurrently running program, this means that these host functions
/// could be called concurrently if different [`Store`] entities are executing on
/// different threads.
///
/// # Errors
///
/// If there already is a definition under the same name for this [`Linker`].
///
/// [`Store`]: crate::Store
///
/// # Panics
///
/// If the [`LinkerBuilder`] has already created a [`Linker`] using [`LinkerBuilder::finish`].
pub fn func_wrap<Params, Args>(
&mut self,
module: &str,
name: &str,
func: impl IntoFunc<T, Params, Args>,
) -> Result<&mut Self, LinkerError> {
self.inner_mut().func_wrap(module, name, func)?;
Ok(self)
}
}
/// Internal [`Linker`] implementation.
#[derive(Debug)]
pub struct LinkerInner<T> {
/// Allows to efficiently store strings and deduplicate them..
strings: StringInterner,
/// Stores the definitions given their names.
///
/// # Dev. Note
///
/// Benchmarks show that [`BTreeMap`] performs better than [`HashMap`]
/// which is why we do not use [`wasmi_collections::Map`] here.
///
/// [`HashMap`]: std::collections::HashMap
definitions: BTreeMap<ImportKey, Definition<T>>,
}
impl<T> Default for LinkerInner<T> {
fn default() -> Self {
Self {
strings: StringInterner::default(),
definitions: BTreeMap::default(),
}
}
}
impl<T> Clone for LinkerInner<T> {
fn clone(&self) -> Self {
Self {
strings: self.strings.clone(),
definitions: self.definitions.clone(),
}
}
}
impl<T> LinkerInner<T> {
/// Returns the import key for the module name and item name.
fn new_import_key(&mut self, module: &str, name: &str) -> ImportKey {
ImportKey::new(
self.strings
.get_or_intern_with_hint(module, InternHint::LikelyExists),
self.strings
.get_or_intern_with_hint(name, InternHint::LikelyNew),
)
}
/// Returns the import key for the module name and item name.
fn get_import_key(&self, module: &str, name: &str) -> Option<ImportKey> {
Some(ImportKey::new(
self.strings.get(module)?,
self.strings.get(name)?,
))
}
/// Resolves the module and item name of the import key if any.
fn resolve_import_key(&self, key: ImportKey) -> Option<(&str, &str)> {
let module_name = self.strings.resolve(key.module())?;
let item_name = self.strings.resolve(key.name())?;
Some((module_name, item_name))
}
/// Inserts the extern item under the import key.
///
/// # Errors
///
/// If there already is a definition for the import key for this [`Linker`].
fn insert(&mut self, key: ImportKey, item: Definition<T>) -> Result<(), LinkerError> {
match self.definitions.entry(key) {
Entry::Occupied(_) => {
let (module_name, field_name) = self
.resolve_import_key(key)
.unwrap_or_else(|| panic!("encountered missing import names for key {key:?}"));
let import_name = ImportName::new(module_name, field_name);
return Err(LinkerError::DuplicateDefinition { import_name });
}
Entry::Vacant(v) => {
v.insert(item);
}
}
Ok(())
}
/// Creates a new named [`Func::new`]-style host [`Func`] for this [`Linker`].
///
/// For more information see [`Linker::func_wrap`].
///
/// # Errors
///
/// If there already is a definition under the same name for this [`Linker`].
pub fn func_new(
&mut self,
module: &str,
name: &str,
ty: FuncType,
func: impl Fn(Caller<'_, T>, &[Val], &mut [Val]) -> Result<(), Error> + Send + Sync + 'static,
) -> Result<&mut Self, LinkerError> {
let func = HostFuncTrampolineEntity::new(ty, func);
let key = self.new_import_key(module, name);
self.insert(key, Definition::HostFunc(func))?;
Ok(self)
}
/// Creates a new named [`Func::new`]-style host [`Func`] for this [`Linker`].
///
/// For information how to use this API see [`Func::wrap`].
///
/// This method creates a host function for this [`Linker`] under the given name.
/// It is distinct in its ability to create a [`Store`] independent
/// host function. Host functions defined this way can be used to instantiate
/// instances in multiple different [`Store`] entities.
///
/// The same applies to other [`Linker`] methods to define new [`Func`] instances
/// such as [`Linker::func_new`].
///
/// In a concurrently running program, this means that these host functions
/// could be called concurrently if different [`Store`] entities are executing on
/// different threads.
///
/// # Errors
///
/// If there already is a definition under the same name for this [`Linker`].
///
/// [`Store`]: crate::Store
pub fn func_wrap<Params, Args>(
&mut self,
module: &str,
name: &str,
func: impl IntoFunc<T, Params, Args>,
) -> Result<&mut Self, LinkerError> {
let func = HostFuncTrampolineEntity::wrap(func);
let key = self.new_import_key(module, name);
self.insert(key, Definition::HostFunc(func))?;
Ok(self)
}
/// Looks up a [`Definition`] by name in this [`Linker`].
///
/// Returns `None` if this name was not previously defined in this [`Linker`].
///
/// # Panics
///
/// If the [`Engine`] of this [`Linker`] and the [`Engine`] of `context` are not the same.
fn get_definition(&self, module: &str, name: &str) -> Option<&Definition<T>> {
let key = self.get_import_key(module, name)?;
self.definitions.get(&key)
}
/// Returns `true` if [`LinkerInner`] contains a [`Definition`] for `name` in `module`.
fn has_definition(&self, module: &str, name: &str) -> bool {
let Some(key) = self.get_import_key(module, name) else {
return false;
};
self.definitions.contains_key(&key)
}
}
#[cfg(test)]
mod tests {
use crate::core::ValType;
use super::*;
use crate::Store;
struct HostState {
a: i32,
b: i64,
}
#[test]
fn linker_funcs_work() {
let engine = Engine::default();
let mut linker = <Linker<HostState>>::new(&engine);
linker
.func_new(
"host",
"get_a",
FuncType::new([], [ValType::I32]),
|ctx: Caller<HostState>, _params: &[Val], results: &mut [Val]| {
results[0] = Val::from(ctx.data().a);
Ok(())
},
)
.unwrap();
linker
.func_new(
"host",
"set_a",
FuncType::new([ValType::I32], []),
|mut ctx: Caller<HostState>, params: &[Val], _results: &mut [Val]| {
ctx.data_mut().a = params[0].i32().unwrap();
Ok(())
},
)
.unwrap();
linker
.func_wrap("host", "get_b", |ctx: Caller<HostState>| ctx.data().b)
.unwrap();
linker
.func_wrap("host", "set_b", |mut ctx: Caller<HostState>, value: i64| {
ctx.data_mut().b = value
})
.unwrap();
let a_init = 42;
let b_init = 77;
let mut store = <Store<HostState>>::new(
&engine,
HostState {
a: a_init,
b: b_init,
},
);
let wat = r#"
(module
(import "host" "get_a" (func $host_get_a (result i32)))
(import "host" "set_a" (func $host_set_a (param i32)))
(import "host" "get_b" (func $host_get_b (result i64)))
(import "host" "set_b" (func $host_set_b (param i64)))
(func (export "wasm_get_a") (result i32)
(call $host_get_a)
)
(func (export "wasm_set_a") (param $param i32)
(call $host_set_a (local.get $param))
)
(func (export "wasm_get_b") (result i64)
(call $host_get_b)
)
(func (export "wasm_set_b") (param $param i64)
(call $host_set_b (local.get $param))
)
)
"#;
let wasm = wat::parse_str(wat).unwrap();
let module = Module::new(&engine, &wasm[..]).unwrap();
let instance = linker
.instantiate(&mut store, &module)
.unwrap()
.start(&mut store)
.unwrap();
let wasm_get_a = instance
.get_typed_func::<(), i32>(&store, "wasm_get_a")
.unwrap();
let wasm_set_a = instance
.get_typed_func::<i32, ()>(&store, "wasm_set_a")
.unwrap();
let wasm_get_b = instance
.get_typed_func::<(), i64>(&store, "wasm_get_b")
.unwrap();
let wasm_set_b = instance
.get_typed_func::<i64, ()>(&store, "wasm_set_b")
.unwrap();
assert_eq!(wasm_get_a.call(&mut store, ()).unwrap(), a_init);
wasm_set_a.call(&mut store, 100).unwrap();
assert_eq!(wasm_get_a.call(&mut store, ()).unwrap(), 100);
assert_eq!(wasm_get_b.call(&mut store, ()).unwrap(), b_init);
wasm_set_b.call(&mut store, 200).unwrap();
assert_eq!(wasm_get_b.call(&mut store, ()).unwrap(), 200);
}
#[test]
fn build_linker() {
let mut builder = <Linker<()>>::build();
builder
.func_wrap("env", "foo", || std::println!("called foo"))
.unwrap();
builder
.func_new(
"env",
"bar",
FuncType::new([], []),
|_caller, _params, _results| {
std::println!("called bar");
Ok(())
},
)
.unwrap();
let builder = builder.finish();
for _ in 0..3 {
let engine = Engine::default();
let _ = builder.create(&engine);
}
}
#[test]
fn linker_builder_uses() {
use crate::{Engine, Linker, Module, Store};
let wasm = wat::parse_str(
r#"
(module
(import "host" "func.0" (func $host_func.0))
(import "host" "func.1" (func $host_func.1))
(func (export "hello")
(call $host_func.0)
(call $host_func.1)
)
)"#,
)
.unwrap();
let engine = Engine::default();
let mut builder = <Linker<()>>::build();
builder
.func_wrap("host", "func.0", |_caller: Caller<()>| ())
.unwrap();
builder
.func_wrap("host", "func.1", |_caller: Caller<()>| ())
.unwrap();
let linker = builder.finish().create(&engine);
let mut store = Store::new(&engine, ());
let module = Module::new(&engine, &wasm[..]).unwrap();
linker.instantiate(&mut store, &module).unwrap();
}
#[test]
fn linker_builder_and_linker_uses() {
use crate::{Engine, Linker, Module, Store};
let wasm = wat::parse_str(
r#"
(module
(import "host" "func.0" (func $host_func.0))
(import "host" "func.1" (func $host_func.1))
(func (export "hello")
(call $host_func.0)
(call $host_func.1)
)
)"#,
)
.unwrap();
let engine = Engine::default();
let mut builder = <Linker<()>>::build();
builder
.func_wrap("host", "func.0", |_caller: Caller<()>| ())
.unwrap();
let mut linker = builder.finish().create(&engine);
linker
.func_wrap("host", "func.1", |_caller: Caller<()>| ())
.unwrap();
let mut store = Store::new(&engine, ());
let module = Module::new(&engine, &wasm[..]).unwrap();
linker.instantiate(&mut store, &module).unwrap();
}
#[test]
fn linker_builder_no_overwrite() {
use crate::{Engine, Linker};
let engine = Engine::default();
let mut builder = <Linker<()>>::build();
builder
.func_wrap("host", "func.0", |_caller: Caller<()>| ())
.unwrap();
let mut linker = builder.finish().create(&engine);
linker
.func_wrap("host", "func.1", |_caller: Caller<()>| ())
.unwrap();
// The following definition won't shadow the previous 'host/func.0' func and errors instead:
linker
.func_wrap("host", "func.0", |_caller: Caller<()>| ())
.unwrap_err();
}
#[test]
fn populate_via_imports() {
use crate::{Engine, Func, Linker, Memory, MemoryType, Module, Store};
let wasm = wat::parse_str(
r#"
(module
(import "host" "hello" (func $host_hello (param i32) (result i32)))
(import "env" "memory" (memory $mem 0 4096))
(func (export "hello") (result i32)
(call $host_hello (i32.const 3))
(i32.const 2)
i32.add
)
)"#,
)
.unwrap();
let engine = Engine::default();
let mut linker = <Linker<()>>::new(&engine);
let mut store = Store::new(&engine, ());
let memory = Memory::new(&mut store, MemoryType::new(1, Some(4096)).unwrap()).unwrap();
let module = Module::new(&engine, &wasm[..]).unwrap();
linker.define("env", "memory", memory).unwrap();
let func = Func::new(
&mut store,
FuncType::new([ValType::I32], [ValType::I32]),
|_caller, _params, _results| todo!(),
);
linker.define("host", "hello", func).unwrap();
linker.instantiate(&mut store, &module).unwrap();
}
}