ArrayBufferPrototype.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <LibJS/Runtime/ArrayBuffer.h>
  28. #include <LibJS/Runtime/ArrayBufferPrototype.h>
  29. #include <LibJS/Runtime/GlobalObject.h>
  30. namespace JS {
  31. ArrayBufferPrototype::ArrayBufferPrototype(GlobalObject& global_object)
  32. : Object(*global_object.object_prototype())
  33. {
  34. }
  35. void ArrayBufferPrototype::initialize(GlobalObject& global_object)
  36. {
  37. auto& vm = this->vm();
  38. Object::initialize(global_object);
  39. u8 attr = Attribute::Writable | Attribute::Configurable;
  40. define_native_function(vm.names.slice, slice, 2, attr);
  41. // FIXME: This should be an accessor property
  42. define_native_property(vm.names.byteLength, byte_length_getter, nullptr, Attribute::Configurable);
  43. define_property(vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "ArrayBuffer"), Attribute::Configurable);
  44. }
  45. ArrayBufferPrototype::~ArrayBufferPrototype()
  46. {
  47. }
  48. static ArrayBuffer* array_buffer_object_from(VM& vm, GlobalObject& global_object)
  49. {
  50. // ArrayBuffer.prototype.* deliberately don't coerce |this| value to object.
  51. auto this_value = vm.this_value(global_object);
  52. if (!this_value.is_object())
  53. return nullptr;
  54. auto& this_object = this_value.as_object();
  55. if (!is<ArrayBuffer>(this_object)) {
  56. vm.throw_exception<TypeError>(global_object, ErrorType::NotAn, "ArrayBuffer");
  57. return nullptr;
  58. }
  59. return static_cast<ArrayBuffer*>(&this_object);
  60. }
  61. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferPrototype::slice)
  62. {
  63. auto array_buffer_object = array_buffer_object_from(vm, global_object);
  64. if (!array_buffer_object)
  65. return {};
  66. TODO();
  67. }
  68. JS_DEFINE_NATIVE_GETTER(ArrayBufferPrototype::byte_length_getter)
  69. {
  70. auto array_buffer_object = array_buffer_object_from(vm, global_object);
  71. if (!array_buffer_object)
  72. return {};
  73. // FIXME: Check for shared buffer
  74. // FIXME: Check for detached buffer
  75. return Value((double)array_buffer_object->byte_length());
  76. }
  77. }