AbstractMachine.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/HashTable.h>
  10. #include <AK/OwnPtr.h>
  11. #include <AK/Result.h>
  12. #include <AK/StackInfo.h>
  13. #include <AK/UFixedBigInt.h>
  14. #include <LibWasm/Types.h>
  15. // NOTE: Special case for Wasm::Result.
  16. #include <LibJS/Runtime/Completion.h>
  17. namespace Wasm {
  18. class Configuration;
  19. struct Interpreter;
  20. struct InstantiationError {
  21. ByteString error { "Unknown error" };
  22. };
  23. struct LinkError {
  24. enum OtherErrors {
  25. InvalidImportedModule,
  26. };
  27. Vector<ByteString> missing_imports;
  28. Vector<OtherErrors> other_errors;
  29. };
  30. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, FunctionAddress, Arithmetic, Comparison, Increment);
  31. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, ExternAddress, Arithmetic, Comparison, Increment);
  32. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, TableAddress, Arithmetic, Comparison, Increment);
  33. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, GlobalAddress, Arithmetic, Comparison, Increment);
  34. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, ElementAddress, Arithmetic, Comparison, Increment);
  35. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, DataAddress, Arithmetic, Comparison, Increment);
  36. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, MemoryAddress, Arithmetic, Comparison, Increment);
  37. // FIXME: These should probably be made generic/virtual if/when we decide to do something more
  38. // fancy than just a dumb interpreter.
  39. class Reference {
  40. public:
  41. struct Null {
  42. ValueType type;
  43. };
  44. struct Func {
  45. FunctionAddress address;
  46. RefPtr<Module> source_module; // null if host function.
  47. };
  48. struct Extern {
  49. ExternAddress address;
  50. };
  51. using RefType = Variant<Null, Func, Extern>;
  52. explicit Reference(RefType ref)
  53. : m_ref(move(ref))
  54. {
  55. }
  56. explicit Reference()
  57. : m_ref(Reference::Null { ValueType(ValueType::Kind::FunctionReference) })
  58. {
  59. }
  60. auto& ref() const { return m_ref; }
  61. private:
  62. RefType m_ref;
  63. };
  64. class Value {
  65. public:
  66. explicit Value(ValueType type)
  67. : m_value(u128())
  68. {
  69. switch (type.kind()) {
  70. case ValueType::I32:
  71. case ValueType::I64:
  72. case ValueType::F32:
  73. case ValueType::F64:
  74. case ValueType::V128:
  75. break;
  76. case ValueType::FunctionReference:
  77. // ref.null funcref
  78. m_value = u128(0, 2);
  79. break;
  80. case ValueType::ExternReference:
  81. // ref.null externref
  82. m_value = u128(0, 3);
  83. break;
  84. }
  85. }
  86. template<typename T>
  87. requires(sizeof(T) == sizeof(u64)) explicit Value(T raw_value)
  88. : m_value(u128(bit_cast<i64>(raw_value), 0))
  89. {
  90. }
  91. template<typename T>
  92. requires(sizeof(T) == sizeof(u32)) explicit Value(T raw_value)
  93. : m_value(u128(static_cast<i64>(bit_cast<i32>(raw_value)), 0))
  94. {
  95. }
  96. template<typename T>
  97. requires(sizeof(T) == sizeof(u8) && Signed<T>) explicit Value(T raw_value)
  98. : m_value(u128(static_cast<i64>(bit_cast<i8>(raw_value)), 0))
  99. {
  100. }
  101. template<typename T>
  102. requires(sizeof(T) == sizeof(u8) && Unsigned<T>) explicit Value(T raw_value)
  103. : m_value(u128(static_cast<u64>(bit_cast<u8>(raw_value)), 0))
  104. {
  105. }
  106. template<typename T>
  107. requires(sizeof(T) == sizeof(u16) && Signed<T>) explicit Value(T raw_value)
  108. : m_value(u128(static_cast<i64>(bit_cast<i16>(raw_value)), 0))
  109. {
  110. }
  111. template<typename T>
  112. requires(sizeof(T) == sizeof(u16) && Unsigned<T>) explicit Value(T raw_value)
  113. : m_value(u128(static_cast<u64>(bit_cast<u16>(raw_value)), 0))
  114. {
  115. }
  116. explicit Value(Reference ref)
  117. {
  118. // Reference variant is encoded in the high storage of the u128:
  119. // 0: funcref
  120. // 1: externref
  121. // 2: null funcref
  122. // 3: null externref
  123. ref.ref().visit(
  124. [&](Reference::Func const& func) { m_value = u128(bit_cast<u64>(func.address), bit_cast<u64>(func.source_module.ptr())); },
  125. [&](Reference::Extern const& func) { m_value = u128(bit_cast<u64>(func.address), 1); },
  126. [&](Reference::Null const& null) { m_value = u128(0, null.type.kind() == ValueType::Kind::FunctionReference ? 2 : 3); });
  127. }
  128. template<SameAs<u128> T>
  129. explicit Value(T raw_value)
  130. : m_value(raw_value)
  131. {
  132. }
  133. ALWAYS_INLINE Value(Value const& value) = default;
  134. ALWAYS_INLINE Value(Value&& value) = default;
  135. ALWAYS_INLINE Value& operator=(Value&& value) = default;
  136. ALWAYS_INLINE Value& operator=(Value const& value) = default;
  137. template<typename T>
  138. ALWAYS_INLINE T to() const
  139. {
  140. static_assert(IsOneOf<T, u128, u64, i64, f32, f64, Reference> || IsIntegral<T>, "Unsupported type for Value::to()");
  141. if constexpr (IsSame<T, u128>) {
  142. return m_value;
  143. }
  144. if constexpr (IsOneOf<T, u64, i64>) {
  145. return bit_cast<T>(m_value.low());
  146. }
  147. if constexpr (IsIntegral<T> && sizeof(T) < 8) {
  148. return bit_cast<T>(static_cast<MakeUnsigned<T>>(m_value.low() & NumericLimits<MakeUnsigned<T>>::max()));
  149. }
  150. if constexpr (IsSame<T, f32>) {
  151. u32 low = m_value.low() & 0xFFFFFFFF;
  152. return bit_cast<f32>(low);
  153. }
  154. if constexpr (IsSame<T, f64>) {
  155. return bit_cast<f64>(m_value.low());
  156. }
  157. if constexpr (IsSame<T, Reference>) {
  158. switch (m_value.high() & 3) {
  159. case 0:
  160. return Reference { Reference::Func { bit_cast<FunctionAddress>(m_value.low()), bit_cast<Wasm::Module*>(m_value.high()) } };
  161. case 1:
  162. return Reference { Reference::Extern { bit_cast<ExternAddress>(m_value.low()) } };
  163. case 2:
  164. return Reference { Reference::Null { ValueType(ValueType::Kind::FunctionReference) } };
  165. case 3:
  166. return Reference { Reference::Null { ValueType(ValueType::Kind::ExternReference) } };
  167. }
  168. }
  169. VERIFY_NOT_REACHED();
  170. }
  171. auto& value() const { return m_value; }
  172. private:
  173. u128 m_value;
  174. };
  175. struct Trap {
  176. ByteString reason;
  177. };
  178. // A variant of Result that does not include external reasons for error (JS::Completion, for now).
  179. class PureResult {
  180. public:
  181. explicit PureResult(Vector<Value> values)
  182. : m_result(move(values))
  183. {
  184. }
  185. PureResult(Trap trap)
  186. : m_result(move(trap))
  187. {
  188. }
  189. auto is_trap() const { return m_result.has<Trap>(); }
  190. auto& values() const { return m_result.get<Vector<Value>>(); }
  191. auto& values() { return m_result.get<Vector<Value>>(); }
  192. auto& trap() const { return m_result.get<Trap>(); }
  193. auto& trap() { return m_result.get<Trap>(); }
  194. private:
  195. friend class Result;
  196. explicit PureResult(Variant<Vector<Value>, Trap>&& result)
  197. : m_result(move(result))
  198. {
  199. }
  200. Variant<Vector<Value>, Trap> m_result;
  201. };
  202. class Result {
  203. public:
  204. explicit Result(Vector<Value> values)
  205. : m_result(move(values))
  206. {
  207. }
  208. Result(Trap trap)
  209. : m_result(move(trap))
  210. {
  211. }
  212. Result(JS::Completion completion)
  213. : m_result(move(completion))
  214. {
  215. VERIFY(m_result.get<JS::Completion>().is_abrupt());
  216. }
  217. Result(PureResult&& result)
  218. : m_result(result.m_result.downcast<decltype(m_result)>())
  219. {
  220. }
  221. auto is_trap() const { return m_result.has<Trap>(); }
  222. auto is_completion() const { return m_result.has<JS::Completion>(); }
  223. auto& values() const { return m_result.get<Vector<Value>>(); }
  224. auto& values() { return m_result.get<Vector<Value>>(); }
  225. auto& trap() const { return m_result.get<Trap>(); }
  226. auto& trap() { return m_result.get<Trap>(); }
  227. auto& completion() { return m_result.get<JS::Completion>(); }
  228. auto& completion() const { return m_result.get<JS::Completion>(); }
  229. PureResult assert_wasm_result() &&
  230. {
  231. VERIFY(!is_completion());
  232. return PureResult(move(m_result).downcast<Vector<Value>, Trap>());
  233. }
  234. private:
  235. Variant<Vector<Value>, Trap, JS::Completion> m_result;
  236. };
  237. using ExternValue = Variant<FunctionAddress, TableAddress, MemoryAddress, GlobalAddress>;
  238. class ExportInstance {
  239. public:
  240. explicit ExportInstance(ByteString name, ExternValue value)
  241. : m_name(move(name))
  242. , m_value(move(value))
  243. {
  244. }
  245. auto& name() const { return m_name; }
  246. auto& value() const { return m_value; }
  247. private:
  248. ByteString m_name;
  249. ExternValue m_value;
  250. };
  251. class ModuleInstance {
  252. public:
  253. explicit ModuleInstance(
  254. Vector<FunctionType> types, Vector<FunctionAddress> function_addresses, Vector<TableAddress> table_addresses,
  255. Vector<MemoryAddress> memory_addresses, Vector<GlobalAddress> global_addresses, Vector<DataAddress> data_addresses,
  256. Vector<ExportInstance> exports)
  257. : m_types(move(types))
  258. , m_functions(move(function_addresses))
  259. , m_tables(move(table_addresses))
  260. , m_memories(move(memory_addresses))
  261. , m_globals(move(global_addresses))
  262. , m_datas(move(data_addresses))
  263. , m_exports(move(exports))
  264. {
  265. }
  266. ModuleInstance() = default;
  267. auto& types() const { return m_types; }
  268. auto& functions() const { return m_functions; }
  269. auto& tables() const { return m_tables; }
  270. auto& memories() const { return m_memories; }
  271. auto& globals() const { return m_globals; }
  272. auto& elements() const { return m_elements; }
  273. auto& datas() const { return m_datas; }
  274. auto& exports() const { return m_exports; }
  275. auto& types() { return m_types; }
  276. auto& functions() { return m_functions; }
  277. auto& tables() { return m_tables; }
  278. auto& memories() { return m_memories; }
  279. auto& globals() { return m_globals; }
  280. auto& elements() { return m_elements; }
  281. auto& datas() { return m_datas; }
  282. auto& exports() { return m_exports; }
  283. private:
  284. Vector<FunctionType> m_types;
  285. Vector<FunctionAddress> m_functions;
  286. Vector<TableAddress> m_tables;
  287. Vector<MemoryAddress> m_memories;
  288. Vector<GlobalAddress> m_globals;
  289. Vector<ElementAddress> m_elements;
  290. Vector<DataAddress> m_datas;
  291. Vector<ExportInstance> m_exports;
  292. };
  293. class WasmFunction {
  294. public:
  295. explicit WasmFunction(FunctionType const& type, ModuleInstance const& instance, Module const& module, CodeSection::Code const& code)
  296. : m_type(type)
  297. , m_module(module.make_weak_ptr())
  298. , m_module_instance(instance)
  299. , m_code(code)
  300. {
  301. }
  302. auto& type() const { return m_type; }
  303. auto& module() const { return m_module_instance; }
  304. auto& code() const { return m_code; }
  305. RefPtr<Module const> module_ref() const { return m_module.strong_ref(); }
  306. private:
  307. FunctionType m_type;
  308. WeakPtr<Module const> m_module;
  309. ModuleInstance const& m_module_instance;
  310. CodeSection::Code const& m_code;
  311. };
  312. class HostFunction {
  313. public:
  314. explicit HostFunction(AK::Function<Result(Configuration&, Vector<Value>&)> function, FunctionType const& type, ByteString name)
  315. : m_function(move(function))
  316. , m_type(type)
  317. , m_name(move(name))
  318. {
  319. }
  320. auto& function() { return m_function; }
  321. auto& type() const { return m_type; }
  322. auto& name() const { return m_name; }
  323. private:
  324. AK::Function<Result(Configuration&, Vector<Value>&)> m_function;
  325. FunctionType m_type;
  326. ByteString m_name;
  327. };
  328. using FunctionInstance = Variant<WasmFunction, HostFunction>;
  329. class TableInstance {
  330. public:
  331. explicit TableInstance(TableType const& type, Vector<Reference> elements)
  332. : m_elements(move(elements))
  333. , m_type(type)
  334. {
  335. }
  336. auto& elements() const { return m_elements; }
  337. auto& elements() { return m_elements; }
  338. auto& type() const { return m_type; }
  339. bool grow(u32 size_to_grow, Reference const& fill_value)
  340. {
  341. if (size_to_grow == 0)
  342. return true;
  343. size_t new_size = m_elements.size() + size_to_grow;
  344. if (auto max = m_type.limits().max(); max.has_value()) {
  345. if (max.value() < new_size)
  346. return false;
  347. }
  348. if (new_size >= NumericLimits<u32>::max()) {
  349. return false;
  350. }
  351. auto previous_size = m_elements.size();
  352. if (m_elements.try_resize(new_size).is_error())
  353. return false;
  354. for (size_t i = previous_size; i < m_elements.size(); ++i)
  355. m_elements[i] = fill_value;
  356. m_type = TableType { m_type.element_type(), Limits(m_type.limits().min() + size_to_grow, m_type.limits().max()) };
  357. return true;
  358. }
  359. private:
  360. Vector<Reference> m_elements;
  361. TableType m_type;
  362. };
  363. class MemoryInstance {
  364. public:
  365. static ErrorOr<MemoryInstance> create(MemoryType const& type)
  366. {
  367. MemoryInstance instance { type };
  368. if (!instance.grow(type.limits().min() * Constants::page_size, GrowType::No))
  369. return Error::from_string_literal("Failed to grow to requested size");
  370. return { move(instance) };
  371. }
  372. auto& type() const { return m_type; }
  373. auto size() const { return m_size; }
  374. auto& data() const { return m_data; }
  375. auto& data() { return m_data; }
  376. enum class InhibitGrowCallback {
  377. No,
  378. Yes,
  379. };
  380. enum class GrowType {
  381. No,
  382. Yes,
  383. };
  384. bool grow(size_t size_to_grow, GrowType grow_type = GrowType::Yes, InhibitGrowCallback inhibit_callback = InhibitGrowCallback::No)
  385. {
  386. if (size_to_grow == 0)
  387. return true;
  388. u64 new_size = m_data.size() + size_to_grow;
  389. // Can't grow past 2^16 pages.
  390. if (new_size >= Constants::page_size * 65536)
  391. return false;
  392. if (auto max = m_type.limits().max(); max.has_value()) {
  393. if (max.value() * Constants::page_size < new_size)
  394. return false;
  395. }
  396. auto previous_size = m_size;
  397. if (m_data.try_resize(new_size).is_error())
  398. return false;
  399. m_size = new_size;
  400. // The spec requires that we zero out everything on grow
  401. __builtin_memset(m_data.offset_pointer(previous_size), 0, size_to_grow);
  402. // NOTE: This exists because wasm-js-api wants to execute code after a successful grow,
  403. // See [this issue](https://github.com/WebAssembly/spec/issues/1635) for more details.
  404. if (inhibit_callback == InhibitGrowCallback::No && successful_grow_hook)
  405. successful_grow_hook();
  406. if (grow_type == GrowType::Yes) {
  407. // Grow the memory's type. We do this when encountering a `memory.grow`.
  408. //
  409. // See relevant spec link:
  410. // https://www.w3.org/TR/wasm-core-2/#growing-memories%E2%91%A0
  411. m_type = MemoryType { Limits(m_type.limits().min() + size_to_grow / Constants::page_size, m_type.limits().max()) };
  412. }
  413. return true;
  414. }
  415. Function<void()> successful_grow_hook;
  416. private:
  417. explicit MemoryInstance(MemoryType const& type)
  418. : m_type(type)
  419. {
  420. }
  421. MemoryType m_type;
  422. size_t m_size { 0 };
  423. ByteBuffer m_data;
  424. };
  425. class GlobalInstance {
  426. public:
  427. explicit GlobalInstance(Value value, bool is_mutable, ValueType type)
  428. : m_mutable(is_mutable)
  429. , m_value(value)
  430. , m_type(type)
  431. {
  432. }
  433. auto is_mutable() const { return m_mutable; }
  434. auto& value() const { return m_value; }
  435. GlobalType type() const { return { m_type, is_mutable() }; }
  436. void set_value(Value value)
  437. {
  438. VERIFY(is_mutable());
  439. m_value = move(value);
  440. }
  441. private:
  442. bool m_mutable { false };
  443. Value m_value;
  444. ValueType m_type;
  445. };
  446. class DataInstance {
  447. public:
  448. explicit DataInstance(Vector<u8> data)
  449. : m_data(move(data))
  450. {
  451. }
  452. size_t size() const { return m_data.size(); }
  453. Vector<u8>& data() { return m_data; }
  454. Vector<u8> const& data() const { return m_data; }
  455. private:
  456. Vector<u8> m_data;
  457. };
  458. class ElementInstance {
  459. public:
  460. explicit ElementInstance(ValueType type, Vector<Reference> references)
  461. : m_type(move(type))
  462. , m_references(move(references))
  463. {
  464. }
  465. auto& type() const { return m_type; }
  466. auto& references() const { return m_references; }
  467. private:
  468. ValueType m_type;
  469. Vector<Reference> m_references;
  470. };
  471. class Store {
  472. public:
  473. Store() = default;
  474. Optional<FunctionAddress> allocate(ModuleInstance&, Module const&, CodeSection::Code const&, TypeIndex);
  475. Optional<FunctionAddress> allocate(HostFunction&&);
  476. Optional<TableAddress> allocate(TableType const&);
  477. Optional<MemoryAddress> allocate(MemoryType const&);
  478. Optional<DataAddress> allocate_data(Vector<u8>);
  479. Optional<GlobalAddress> allocate(GlobalType const&, Value);
  480. Optional<ElementAddress> allocate(ValueType const&, Vector<Reference>);
  481. Module const* get_module_for(FunctionAddress);
  482. FunctionInstance* get(FunctionAddress);
  483. TableInstance* get(TableAddress);
  484. MemoryInstance* get(MemoryAddress);
  485. GlobalInstance* get(GlobalAddress);
  486. DataInstance* get(DataAddress);
  487. ElementInstance* get(ElementAddress);
  488. private:
  489. Vector<FunctionInstance> m_functions;
  490. Vector<TableInstance> m_tables;
  491. Vector<MemoryInstance> m_memories;
  492. Vector<GlobalInstance> m_globals;
  493. Vector<ElementInstance> m_elements;
  494. Vector<DataInstance> m_datas;
  495. };
  496. class Label {
  497. public:
  498. explicit Label(size_t arity, InstructionPointer continuation, size_t stack_height)
  499. : m_arity(arity)
  500. , m_stack_height(stack_height)
  501. , m_continuation(continuation)
  502. {
  503. }
  504. auto continuation() const { return m_continuation; }
  505. auto arity() const { return m_arity; }
  506. auto stack_height() const { return m_stack_height; }
  507. private:
  508. size_t m_arity { 0 };
  509. size_t m_stack_height { 0 };
  510. InstructionPointer m_continuation { 0 };
  511. };
  512. class Frame {
  513. public:
  514. explicit Frame(ModuleInstance const& module, Vector<Value> locals, Expression const& expression, size_t arity)
  515. : m_module(module)
  516. , m_locals(move(locals))
  517. , m_expression(expression)
  518. , m_arity(arity)
  519. {
  520. }
  521. auto& module() const { return m_module; }
  522. auto& locals() const { return m_locals; }
  523. auto& locals() { return m_locals; }
  524. auto& expression() const { return m_expression; }
  525. auto arity() const { return m_arity; }
  526. auto label_index() const { return m_label_index; }
  527. auto& label_index() { return m_label_index; }
  528. private:
  529. ModuleInstance const& m_module;
  530. Vector<Value> m_locals;
  531. Expression const& m_expression;
  532. size_t m_arity { 0 };
  533. size_t m_label_index { 0 };
  534. };
  535. using InstantiationResult = AK::ErrorOr<NonnullOwnPtr<ModuleInstance>, InstantiationError>;
  536. class AbstractMachine {
  537. public:
  538. explicit AbstractMachine() = default;
  539. // Validate a module; permanently sets the module's validity status.
  540. ErrorOr<void, ValidationError> validate(Module&);
  541. // Load and instantiate a module, and link it into this interpreter.
  542. InstantiationResult instantiate(Module const&, Vector<ExternValue>);
  543. Result invoke(FunctionAddress, Vector<Value>);
  544. Result invoke(Interpreter&, FunctionAddress, Vector<Value>);
  545. auto& store() const { return m_store; }
  546. auto& store() { return m_store; }
  547. void enable_instruction_count_limit() { m_should_limit_instruction_count = true; }
  548. private:
  549. Optional<InstantiationError> allocate_all_initial_phase(Module const&, ModuleInstance&, Vector<ExternValue>&, Vector<Value>& global_values, Vector<FunctionAddress>& own_functions);
  550. Optional<InstantiationError> allocate_all_final_phase(Module const&, ModuleInstance&, Vector<Vector<Reference>>& elements);
  551. Store m_store;
  552. StackInfo m_stack_info;
  553. bool m_should_limit_instruction_count { false };
  554. };
  555. class Linker {
  556. public:
  557. struct Name {
  558. ByteString module;
  559. ByteString name;
  560. ImportSection::Import::ImportDesc type;
  561. };
  562. explicit Linker(Module const& module)
  563. : m_module(module)
  564. {
  565. }
  566. // Link a module, the import 'module name' is ignored with this.
  567. void link(ModuleInstance const&);
  568. // Link a bunch of qualified values, also matches 'module name'.
  569. void link(HashMap<Name, ExternValue> const&);
  570. auto& unresolved_imports()
  571. {
  572. populate();
  573. return m_unresolved_imports;
  574. }
  575. AK::ErrorOr<Vector<ExternValue>, LinkError> finish();
  576. private:
  577. void populate();
  578. Module const& m_module;
  579. HashMap<Name, ExternValue> m_resolved_imports;
  580. HashTable<Name> m_unresolved_imports;
  581. Vector<Name> m_ordered_imports;
  582. Optional<LinkError> m_error;
  583. };
  584. }
  585. template<>
  586. struct AK::Traits<Wasm::Linker::Name> : public AK::DefaultTraits<Wasm::Linker::Name> {
  587. static constexpr bool is_trivial() { return false; }
  588. static unsigned hash(Wasm::Linker::Name const& entry) { return pair_int_hash(entry.module.hash(), entry.name.hash()); }
  589. static bool equals(Wasm::Linker::Name const& a, Wasm::Linker::Name const& b) { return a.name == b.name && a.module == b.module; }
  590. };