ArrayPrototype.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  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 <AK/StringBuilder.h>
  28. #include <LibJS/Heap/Heap.h>
  29. #include <LibJS/Interpreter.h>
  30. #include <LibJS/Runtime/Array.h>
  31. #include <LibJS/Runtime/ArrayPrototype.h>
  32. #include <LibJS/Runtime/Error.h>
  33. #include <LibJS/Runtime/Value.h>
  34. namespace JS {
  35. ArrayPrototype::ArrayPrototype()
  36. {
  37. put_native_function("shift", shift);
  38. put_native_function("pop", pop);
  39. put_native_function("push", push, 1);
  40. put_native_function("toString", to_string, 0);
  41. put("length", Value(0));
  42. }
  43. ArrayPrototype::~ArrayPrototype()
  44. {
  45. }
  46. static Array* array_from(Interpreter& interpreter)
  47. {
  48. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  49. if (!this_object)
  50. return {};
  51. if (!this_object->is_array()) {
  52. interpreter.throw_exception<TypeError>("Not an Array");
  53. return nullptr;
  54. }
  55. return static_cast<Array*>(this_object);
  56. }
  57. Value ArrayPrototype::push(Interpreter& interpreter)
  58. {
  59. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  60. if (!this_object)
  61. return {};
  62. ASSERT(this_object->is_array());
  63. if (!interpreter.argument_count())
  64. return js_undefined();
  65. static_cast<Array*>(this_object)->push(interpreter.argument(0));
  66. return Value(static_cast<const Array*>(this_object)->length());
  67. }
  68. Value ArrayPrototype::pop(Interpreter& interpreter)
  69. {
  70. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  71. if (!this_object)
  72. return {};
  73. ASSERT(this_object->is_array());
  74. return static_cast<Array*>(this_object)->pop();
  75. }
  76. Value ArrayPrototype::shift(Interpreter& interpreter)
  77. {
  78. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  79. if (!this_object)
  80. return {};
  81. ASSERT(this_object->is_array());
  82. return static_cast<Array*>(this_object)->shift();
  83. }
  84. Value ArrayPrototype::to_string(Interpreter& interpreter)
  85. {
  86. auto* array = array_from(interpreter);
  87. if (!array)
  88. return {};
  89. StringBuilder builder;
  90. for (size_t i = 0; i < array->elements().size(); ++i) {
  91. if (i != 0)
  92. builder.append(',');
  93. if (!array->elements()[i].is_empty())
  94. builder.append(array->elements()[i].to_string());
  95. }
  96. return js_string(interpreter, builder.to_string());
  97. }
  98. }