StringConstructor.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <AK/Utf16View.h>
  8. #include <AK/Utf32View.h>
  9. #include <LibJS/Runtime/AbstractOperations.h>
  10. #include <LibJS/Runtime/Array.h>
  11. #include <LibJS/Runtime/Error.h>
  12. #include <LibJS/Runtime/GlobalObject.h>
  13. #include <LibJS/Runtime/StringConstructor.h>
  14. #include <LibJS/Runtime/StringObject.h>
  15. namespace JS {
  16. StringConstructor::StringConstructor(GlobalObject& global_object)
  17. : NativeFunction(vm().names.String.as_string(), *global_object.function_prototype())
  18. {
  19. }
  20. void StringConstructor::initialize(GlobalObject& global_object)
  21. {
  22. auto& vm = this->vm();
  23. NativeFunction::initialize(global_object);
  24. // 22.1.2.3 String.prototype, https://tc39.es/ecma262/#sec-string.prototype
  25. define_direct_property(vm.names.prototype, global_object.string_prototype(), 0);
  26. u8 attr = Attribute::Writable | Attribute::Configurable;
  27. define_native_function(vm.names.raw, raw, 1, attr);
  28. define_native_function(vm.names.fromCharCode, from_char_code, 1, attr);
  29. define_native_function(vm.names.fromCodePoint, from_code_point, 1, attr);
  30. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  31. }
  32. StringConstructor::~StringConstructor()
  33. {
  34. }
  35. // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
  36. Value StringConstructor::call()
  37. {
  38. if (!vm().argument_count())
  39. return js_string(heap(), "");
  40. if (vm().argument(0).is_symbol())
  41. return js_string(heap(), vm().argument(0).as_symbol().to_string());
  42. auto* string = vm().argument(0).to_primitive_string(global_object());
  43. if (vm().exception())
  44. return {};
  45. return string;
  46. }
  47. // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
  48. Value StringConstructor::construct(FunctionObject& new_target)
  49. {
  50. auto& vm = global_object().vm();
  51. PrimitiveString* primitive_string;
  52. if (!vm.argument_count())
  53. primitive_string = js_string(vm, "");
  54. else
  55. primitive_string = vm.argument(0).to_primitive_string(global_object());
  56. if (!primitive_string)
  57. return {};
  58. auto* prototype = get_prototype_from_constructor(global_object(), new_target, &GlobalObject::string_prototype);
  59. if (vm.exception())
  60. return {};
  61. return StringObject::create(global_object(), *primitive_string, *prototype);
  62. }
  63. // 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw
  64. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
  65. {
  66. auto* cooked = vm.argument(0).to_object(global_object);
  67. if (vm.exception())
  68. return {};
  69. auto raw_value = cooked->get(vm.names.raw);
  70. if (vm.exception())
  71. return {};
  72. auto* raw = raw_value.to_object(global_object);
  73. if (vm.exception())
  74. return {};
  75. auto literal_segments = length_of_array_like(global_object, *raw);
  76. if (vm.exception())
  77. return {};
  78. if (literal_segments == 0)
  79. return js_string(vm, "");
  80. const auto number_of_substituions = vm.argument_count() - 1;
  81. StringBuilder builder;
  82. for (size_t i = 0; i < literal_segments; ++i) {
  83. auto next_key = String::number(i);
  84. auto next_segment_value = raw->get(next_key);
  85. if (vm.exception())
  86. return {};
  87. auto next_segment = next_segment_value.to_string(global_object);
  88. if (vm.exception())
  89. return {};
  90. builder.append(next_segment);
  91. if (i + 1 == literal_segments)
  92. break;
  93. if (i < number_of_substituions) {
  94. auto next = vm.argument(i + 1);
  95. auto next_sub = next.to_string(global_object);
  96. if (vm.exception())
  97. return {};
  98. builder.append(next_sub);
  99. }
  100. }
  101. return js_string(vm, builder.build());
  102. }
  103. // 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode
  104. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code)
  105. {
  106. Vector<u16> string;
  107. string.ensure_capacity(vm.argument_count());
  108. for (size_t i = 0; i < vm.argument_count(); ++i) {
  109. auto code_unit = vm.argument(i).to_u16(global_object);
  110. if (vm.exception())
  111. return {};
  112. string.append(code_unit);
  113. }
  114. return js_string(vm, move(string));
  115. }
  116. // 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint
  117. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point)
  118. {
  119. Vector<u16> string;
  120. string.ensure_capacity(vm.argument_count()); // This will be an under-estimate if any code point is > 0xffff.
  121. for (size_t i = 0; i < vm.argument_count(); ++i) {
  122. auto next_code_point = vm.argument(i).to_number(global_object);
  123. if (vm.exception())
  124. return {};
  125. if (!next_code_point.is_integral_number()) {
  126. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
  127. return {};
  128. }
  129. auto code_point = next_code_point.to_i32(global_object);
  130. if (code_point < 0 || code_point > 0x10FFFF) {
  131. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
  132. return {};
  133. }
  134. AK::code_point_to_utf16(string, static_cast<u32>(code_point));
  135. }
  136. return js_string(vm, move(string));
  137. }
  138. }