WebAssemblyTableConstructor.cpp 3.6 KB

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