Table.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibJS/Runtime/VM.h>
  9. #include <LibWasm/Types.h>
  10. #include <LibWeb/Bindings/Intrinsics.h>
  11. #include <LibWeb/Bindings/TablePrototype.h>
  12. #include <LibWeb/WebAssembly/Table.h>
  13. #include <LibWeb/WebAssembly/WebAssembly.h>
  14. namespace Web::WebAssembly {
  15. JS_DEFINE_ALLOCATOR(Table);
  16. static Wasm::ValueType table_kind_to_value_type(Bindings::TableKind kind)
  17. {
  18. switch (kind) {
  19. case Bindings::TableKind::Externref:
  20. return Wasm::ValueType { Wasm::ValueType::ExternReference };
  21. case Bindings::TableKind::Anyfunc:
  22. return Wasm::ValueType { Wasm::ValueType::FunctionReference };
  23. }
  24. VERIFY_NOT_REACHED();
  25. }
  26. static JS::ThrowCompletionOr<Wasm::Value> value_to_reference(JS::VM& vm, JS::Value value, Wasm::ValueType const& reference_type)
  27. {
  28. if (value.is_undefined())
  29. return Wasm::Value(reference_type, 0ull);
  30. return Detail::to_webassembly_value(vm, value, reference_type);
  31. }
  32. WebIDL::ExceptionOr<JS::NonnullGCPtr<Table>> Table::construct_impl(JS::Realm& realm, TableDescriptor& descriptor, JS::Value value)
  33. {
  34. auto& vm = realm.vm();
  35. auto reference_type = table_kind_to_value_type(descriptor.element);
  36. auto reference_value = TRY(value_to_reference(vm, value, reference_type));
  37. Wasm::Limits limits { descriptor.initial, move(descriptor.maximum) };
  38. Wasm::TableType table_type { reference_type, move(limits) };
  39. auto& cache = Detail::get_cache(realm);
  40. auto address = cache.abstract_machine().store().allocate(table_type);
  41. if (!address.has_value())
  42. return vm.throw_completion<JS::TypeError>("Wasm Table allocation failed"sv);
  43. auto const& reference = reference_value.value().get<Wasm::Reference>();
  44. auto& table = *cache.abstract_machine().store().get(*address);
  45. for (auto& element : table.elements())
  46. element = reference;
  47. return vm.heap().allocate<Table>(realm, realm, *address);
  48. }
  49. Table::Table(JS::Realm& realm, Wasm::TableAddress address)
  50. : Bindings::PlatformObject(realm)
  51. , m_address(address)
  52. {
  53. }
  54. void Table::initialize(JS::Realm& realm)
  55. {
  56. Base::initialize(realm);
  57. WEB_SET_PROTOTYPE_FOR_INTERFACE_WITH_CUSTOM_NAME(Table, WebAssembly.Table);
  58. }
  59. // https://webassembly.github.io/spec/js-api/#dom-table-grow
  60. WebIDL::ExceptionOr<u32> Table::grow(u32 delta, JS::Value value)
  61. {
  62. auto& vm = this->vm();
  63. auto& cache = Detail::get_cache(realm());
  64. auto* table = cache.abstract_machine().store().get(address());
  65. if (!table)
  66. return vm.throw_completion<JS::RangeError>("Could not find the memory table to grow"sv);
  67. auto initial_size = table->elements().size();
  68. auto reference_value = TRY(value_to_reference(vm, value, table->type().element_type()));
  69. auto const& reference = reference_value.value().get<Wasm::Reference>();
  70. if (!table->grow(delta, reference))
  71. return vm.throw_completion<JS::RangeError>("Failed to grow table"sv);
  72. return initial_size;
  73. }
  74. // https://webassembly.github.io/spec/js-api/#dom-table-get
  75. WebIDL::ExceptionOr<JS::Value> Table::get(u32 index) const
  76. {
  77. auto& vm = this->vm();
  78. auto& cache = Detail::get_cache(realm());
  79. auto* table = cache.abstract_machine().store().get(address());
  80. if (!table)
  81. return vm.throw_completion<JS::RangeError>("Could not find the memory table"sv);
  82. if (table->elements().size() <= index)
  83. return vm.throw_completion<JS::RangeError>("Table element index out of range"sv);
  84. auto& ref = table->elements()[index];
  85. if (!ref.ref().has<Wasm::Reference::Null>())
  86. return JS::js_undefined();
  87. Wasm::Value wasm_value { ref };
  88. return Detail::to_js_value(vm, wasm_value);
  89. }
  90. // https://webassembly.github.io/spec/js-api/#dom-table-set
  91. WebIDL::ExceptionOr<void> Table::set(u32 index, JS::Value value)
  92. {
  93. auto& vm = this->vm();
  94. auto& cache = Detail::get_cache(realm());
  95. auto* table = cache.abstract_machine().store().get(address());
  96. if (!table)
  97. return vm.throw_completion<JS::RangeError>("Could not find the memory table"sv);
  98. if (table->elements().size() <= index)
  99. return vm.throw_completion<JS::RangeError>("Table element index out of range"sv);
  100. auto reference_value = TRY(value_to_reference(vm, value, table->type().element_type()));
  101. auto const& reference = reference_value.value().get<Wasm::Reference>();
  102. table->elements()[index] = reference;
  103. return {};
  104. }
  105. // https://webassembly.github.io/spec/js-api/#dom-table-length
  106. WebIDL::ExceptionOr<u32> Table::length() const
  107. {
  108. auto& vm = this->vm();
  109. auto& cache = Detail::get_cache(realm());
  110. auto* table = cache.abstract_machine().store().get(address());
  111. if (!table)
  112. return vm.throw_completion<JS::RangeError>("Could not find the memory table"sv);
  113. return table->elements().size();
  114. }
  115. }