WebAssemblyTableConstructor.cpp 4.0 KB

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