WebAssemblyTableConstructor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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() = default;
  19. JS::ThrowCompletionOr<JS::Value> WebAssemblyTableConstructor::call()
  20. {
  21. return vm().throw_completion<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Table");
  22. }
  23. JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(FunctionObject&)
  24. {
  25. auto& vm = this->vm();
  26. auto& global_object = this->global_object();
  27. auto descriptor = TRY(vm.argument(0).to_object(global_object));
  28. auto element_value = TRY(descriptor->get("element"));
  29. if (!element_value.is_string())
  30. return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::InvalidHint, element_value.to_string_without_side_effects());
  31. auto& element = element_value.as_string().string();
  32. Optional<Wasm::ValueType> reference_type;
  33. if (element == "anyfunc"sv)
  34. reference_type = Wasm::ValueType(Wasm::ValueType::FunctionReference);
  35. else if (element == "externref"sv)
  36. reference_type = Wasm::ValueType(Wasm::ValueType::ExternReference);
  37. if (!reference_type.has_value())
  38. return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::InvalidHint, element);
  39. auto initial_value = TRY(descriptor->get("initial"));
  40. auto maximum_value = TRY(descriptor->get("maximum"));
  41. auto initial = TRY(initial_value.to_u32(global_object));
  42. Optional<u32> maximum;
  43. if (!maximum_value.is_undefined())
  44. maximum = TRY(maximum_value.to_u32(global_object));
  45. if (maximum.has_value() && maximum.value() < initial)
  46. return vm.throw_completion<JS::RangeError>(global_object, "maximum should be larger than or equal to initial");
  47. auto value_value = TRY(descriptor->get("value"));
  48. auto reference_value = TRY([&]() -> JS::ThrowCompletionOr<Wasm::Value> {
  49. if (value_value.is_undefined())
  50. return Wasm::Value(*reference_type, 0ull);
  51. return to_webassembly_value(global_object, value_value, *reference_type);
  52. }());
  53. auto& reference = reference_value.value().get<Wasm::Reference>();
  54. auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::TableType { *reference_type, Wasm::Limits { initial, maximum } });
  55. if (!address.has_value())
  56. return vm.throw_completion<JS::TypeError>(global_object, "Wasm Table allocation failed");
  57. auto& table = *WebAssemblyObject::s_abstract_machine.store().get(*address);
  58. for (auto& element : table.elements())
  59. element = reference;
  60. return vm.heap().allocate<WebAssemblyTableObject>(global_object, global_object, *address);
  61. }
  62. void WebAssemblyTableConstructor::initialize(JS::GlobalObject& global_object)
  63. {
  64. auto& vm = this->vm();
  65. auto& window = static_cast<WindowObject&>(global_object);
  66. NativeFunction::initialize(global_object);
  67. define_direct_property(vm.names.prototype, &window.ensure_web_prototype<WebAssemblyTablePrototype>("WebAssemblyTablePrototype"), 0);
  68. define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
  69. }
  70. }