StringConstructor.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/StringBuilder.h>
  27. #include <AK/Utf32View.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/Array.h>
  30. #include <LibJS/Runtime/Error.h>
  31. #include <LibJS/Runtime/GlobalObject.h>
  32. #include <LibJS/Runtime/StringConstructor.h>
  33. #include <LibJS/Runtime/StringObject.h>
  34. namespace JS {
  35. StringConstructor::StringConstructor(GlobalObject& global_object)
  36. : NativeFunction("String", *global_object.function_prototype())
  37. {
  38. }
  39. void StringConstructor::initialize(GlobalObject& global_object)
  40. {
  41. NativeFunction::initialize(global_object);
  42. define_property("prototype", global_object.string_prototype(), 0);
  43. define_property("length", Value(1), Attribute::Configurable);
  44. u8 attr = Attribute::Writable | Attribute::Configurable;
  45. define_native_function("raw", raw, 1, attr);
  46. define_native_function("fromCharCode", from_char_code, 1, attr);
  47. }
  48. StringConstructor::~StringConstructor()
  49. {
  50. }
  51. Value StringConstructor::call(Interpreter& interpreter)
  52. {
  53. if (!interpreter.argument_count())
  54. return js_string(interpreter, "");
  55. if (interpreter.argument(0).is_symbol())
  56. return js_string(interpreter, interpreter.argument(0).as_symbol().to_string());
  57. auto* string = interpreter.argument(0).to_primitive_string(interpreter);
  58. if (interpreter.exception())
  59. return {};
  60. return string;
  61. }
  62. Value StringConstructor::construct(Interpreter& interpreter, Function&)
  63. {
  64. PrimitiveString* primitive_string = nullptr;
  65. if (!interpreter.argument_count())
  66. primitive_string = js_string(interpreter, "");
  67. else
  68. primitive_string = interpreter.argument(0).to_primitive_string(interpreter);
  69. if (!primitive_string)
  70. return {};
  71. return StringObject::create(global_object(), *primitive_string);
  72. }
  73. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
  74. {
  75. auto* template_object = interpreter.argument(0).to_object(interpreter, global_object);
  76. if (interpreter.exception())
  77. return {};
  78. auto raw = template_object->get("raw");
  79. if (interpreter.exception())
  80. return {};
  81. if (raw.is_empty() || raw.is_undefined() || raw.is_null()) {
  82. interpreter.throw_exception<TypeError>(ErrorType::StringRawCannotConvert, raw.is_null() ? "null" : "undefined");
  83. return {};
  84. }
  85. if (!raw.is_array())
  86. return js_string(interpreter, "");
  87. auto* array = static_cast<Array*>(raw.to_object(interpreter, global_object));
  88. auto& raw_array_elements = array->indexed_properties();
  89. StringBuilder builder;
  90. for (size_t i = 0; i < raw_array_elements.array_like_size(); ++i) {
  91. auto result = raw_array_elements.get(array, i);
  92. if (interpreter.exception())
  93. return {};
  94. if (!result.has_value())
  95. continue;
  96. builder.append(result.value().value.to_string(interpreter));
  97. if (interpreter.exception())
  98. return {};
  99. if (i + 1 < interpreter.argument_count() && i < raw_array_elements.array_like_size() - 1) {
  100. builder.append(interpreter.argument(i + 1).to_string(interpreter));
  101. if (interpreter.exception())
  102. return {};
  103. }
  104. }
  105. return js_string(interpreter, builder.build());
  106. }
  107. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code)
  108. {
  109. StringBuilder builder;
  110. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  111. auto char_code = interpreter.argument(i).to_i32(interpreter);
  112. if (interpreter.exception())
  113. return {};
  114. auto truncated = char_code & 0xffff;
  115. // FIXME: We need an Utf16View :^)
  116. builder.append(Utf32View((u32*)&truncated, 1));
  117. }
  118. return js_string(interpreter, builder.build());
  119. }
  120. }