WebAssemblyTableConstructor.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 = vm.argument(0).to_object(global_object);
  31. if (vm.exception())
  32. return {};
  33. auto element_value = TRY_OR_DISCARD(descriptor->get("element"));
  34. if (!element_value.is_string()) {
  35. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::InvalidHint, element_value.to_string_without_side_effects());
  36. return {};
  37. }
  38. auto& element = element_value.as_string().string();
  39. Optional<Wasm::ValueType> reference_type;
  40. if (element == "anyfunc"sv)
  41. reference_type = Wasm::ValueType(Wasm::ValueType::FunctionReference);
  42. else if (element == "externref"sv)
  43. reference_type = Wasm::ValueType(Wasm::ValueType::ExternReference);
  44. if (!reference_type.has_value()) {
  45. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::InvalidHint, element);
  46. return {};
  47. }
  48. auto initial_value = TRY_OR_DISCARD(descriptor->get("initial"));
  49. auto maximum_value = TRY_OR_DISCARD(descriptor->get("maximum"));
  50. auto initial = initial_value.to_u32(global_object);
  51. if (vm.exception())
  52. return {};
  53. Optional<u32> maximum;
  54. if (!maximum_value.is_undefined()) {
  55. maximum = maximum_value.to_u32(global_object);
  56. if (vm.exception())
  57. return {};
  58. }
  59. if (maximum.has_value() && maximum.value() < initial) {
  60. vm.throw_exception<JS::RangeError>(global_object, "maximum should be larger than or equal to initial");
  61. return {};
  62. }
  63. auto value_value = TRY_OR_DISCARD(descriptor->get("value"));
  64. auto reference_value = [&]() -> Optional<Wasm::Value> {
  65. if (value_value.is_undefined())
  66. return Wasm::Value(*reference_type, 0ull);
  67. return to_webassembly_value(value_value, *reference_type, global_object);
  68. }();
  69. if (!reference_value.has_value())
  70. return {};
  71. auto& reference = reference_value->value().get<Wasm::Reference>();
  72. auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::TableType { *reference_type, Wasm::Limits { initial, maximum } });
  73. if (!address.has_value()) {
  74. vm.throw_exception<JS::TypeError>(global_object, "Wasm Table allocation failed");
  75. return {};
  76. }
  77. auto& table = *WebAssemblyObject::s_abstract_machine.store().get(*address);
  78. for (auto& element : table.elements())
  79. element = reference;
  80. return vm.heap().allocate<WebAssemblyTableObject>(global_object, global_object, *address);
  81. }
  82. void WebAssemblyTableConstructor::initialize(JS::GlobalObject& global_object)
  83. {
  84. auto& vm = this->vm();
  85. auto& window = static_cast<WindowObject&>(global_object);
  86. NativeFunction::initialize(global_object);
  87. define_direct_property(vm.names.prototype, &window.ensure_web_prototype<WebAssemblyTablePrototype>("WebAssemblyTablePrototype"), 0);
  88. define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
  89. }
  90. }