WebAssemblyTableConstructor.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/GlobalObject.h>
  7. #include <LibJS/Runtime/TypedArray.h>
  8. #include <LibWeb/Bindings/WindowObject.h>
  9. #include <LibWeb/WebAssembly/WebAssemblyObject.h>
  10. #include <LibWeb/WebAssembly/WebAssemblyTableConstructor.h>
  11. #include <LibWeb/WebAssembly/WebAssemblyTableObject.h>
  12. #include <LibWeb/WebAssembly/WebAssemblyTablePrototype.h>
  13. namespace Web::Bindings {
  14. WebAssemblyTableConstructor::WebAssemblyTableConstructor(JS::GlobalObject& global_object)
  15. : NativeFunction(*global_object.function_prototype())
  16. {
  17. }
  18. WebAssemblyTableConstructor::~WebAssemblyTableConstructor()
  19. {
  20. }
  21. JS::Value WebAssemblyTableConstructor::call()
  22. {
  23. vm().throw_exception<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Table");
  24. return {};
  25. }
  26. JS::Value WebAssemblyTableConstructor::construct(FunctionObject&)
  27. {
  28. auto& vm = this->vm();
  29. auto& global_object = this->global_object();
  30. auto descriptor = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
  31. auto element_value = TRY_OR_DISCARD(descriptor->get("element"));
  32. if (!element_value.is_string()) {
  33. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::InvalidHint, element_value.to_string_without_side_effects());
  34. return {};
  35. }
  36. auto& element = element_value.as_string().string();
  37. Optional<Wasm::ValueType> reference_type;
  38. if (element == "anyfunc"sv)
  39. reference_type = Wasm::ValueType(Wasm::ValueType::FunctionReference);
  40. else if (element == "externref"sv)
  41. reference_type = Wasm::ValueType(Wasm::ValueType::ExternReference);
  42. if (!reference_type.has_value()) {
  43. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::InvalidHint, element);
  44. return {};
  45. }
  46. auto initial_value = TRY_OR_DISCARD(descriptor->get("initial"));
  47. auto maximum_value = TRY_OR_DISCARD(descriptor->get("maximum"));
  48. auto initial = TRY_OR_DISCARD(initial_value.to_u32(global_object));
  49. Optional<u32> maximum;
  50. if (!maximum_value.is_undefined())
  51. maximum = TRY_OR_DISCARD(maximum_value.to_u32(global_object));
  52. if (maximum.has_value() && maximum.value() < initial) {
  53. vm.throw_exception<JS::RangeError>(global_object, "maximum should be larger than or equal to initial");
  54. return {};
  55. }
  56. auto value_value = TRY_OR_DISCARD(descriptor->get("value"));
  57. auto reference_value = [&]() -> Optional<Wasm::Value> {
  58. if (value_value.is_undefined())
  59. return Wasm::Value(*reference_type, 0ull);
  60. return to_webassembly_value(value_value, *reference_type, global_object);
  61. }();
  62. if (!reference_value.has_value())
  63. return {};
  64. auto& reference = reference_value->value().get<Wasm::Reference>();
  65. auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::TableType { *reference_type, Wasm::Limits { initial, maximum } });
  66. if (!address.has_value()) {
  67. vm.throw_exception<JS::TypeError>(global_object, "Wasm Table allocation failed");
  68. return {};
  69. }
  70. auto& table = *WebAssemblyObject::s_abstract_machine.store().get(*address);
  71. for (auto& element : table.elements())
  72. element = reference;
  73. return vm.heap().allocate<WebAssemblyTableObject>(global_object, global_object, *address);
  74. }
  75. void WebAssemblyTableConstructor::initialize(JS::GlobalObject& global_object)
  76. {
  77. auto& vm = this->vm();
  78. auto& window = static_cast<WindowObject&>(global_object);
  79. NativeFunction::initialize(global_object);
  80. define_direct_property(vm.names.prototype, &window.ensure_web_prototype<WebAssemblyTablePrototype>("WebAssemblyTablePrototype"), 0);
  81. define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
  82. }
  83. }