StringConstructor.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include <LibJS/Runtime/Utf16String.h>
  16. #include <LibJS/Runtime/ValueInlines.h>
  17. namespace JS {
  18. StringConstructor::StringConstructor(Realm& realm)
  19. : NativeFunction(realm.vm().names.String.as_string(), realm.intrinsics().function_prototype())
  20. {
  21. }
  22. void StringConstructor::initialize(Realm& realm)
  23. {
  24. auto& vm = this->vm();
  25. Base::initialize(realm);
  26. // 22.1.2.3 String.prototype, https://tc39.es/ecma262/#sec-string.prototype
  27. define_direct_property(vm.names.prototype, realm.intrinsics().string_prototype(), 0);
  28. u8 attr = Attribute::Writable | Attribute::Configurable;
  29. define_native_function(realm, vm.names.raw, raw, 1, attr);
  30. define_native_function(realm, vm.names.fromCharCode, from_char_code, 1, attr);
  31. define_native_function(realm, vm.names.fromCodePoint, from_code_point, 1, attr);
  32. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  33. }
  34. // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
  35. ThrowCompletionOr<Value> StringConstructor::call()
  36. {
  37. auto& vm = this->vm();
  38. auto value = vm.argument(0);
  39. // 1. If value is not present, let s be the empty String.
  40. if (!vm.argument_count())
  41. return PrimitiveString::create(vm, String {});
  42. // 2. Else,
  43. // a. If NewTarget is undefined and value is a Symbol, return SymbolDescriptiveString(value).
  44. if (value.is_symbol())
  45. return PrimitiveString::create(vm, MUST(value.as_symbol().descriptive_string()));
  46. // b. Let s be ? ToString(value).
  47. // 3. If NewTarget is undefined, return s.
  48. return TRY(value.to_primitive_string(vm));
  49. }
  50. // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
  51. ThrowCompletionOr<NonnullGCPtr<Object>> StringConstructor::construct(FunctionObject& new_target)
  52. {
  53. auto& vm = this->vm();
  54. auto& realm = *vm.current_realm();
  55. auto value = vm.argument(0);
  56. PrimitiveString* primitive_string;
  57. // 1. If value is not present, let s be the empty String.
  58. if (!vm.argument_count()) {
  59. primitive_string = PrimitiveString::create(vm, String {});
  60. }
  61. // 2. Else,
  62. else {
  63. // b. Let s be ? ToString(value).
  64. primitive_string = TRY(value.to_primitive_string(vm));
  65. }
  66. // 4. Return StringCreate(s, ? GetPrototypeFromConstructor(NewTarget, "%String.prototype%")).
  67. auto* prototype = TRY(get_prototype_from_constructor(vm, new_target, &Intrinsics::string_prototype));
  68. return StringObject::create(realm, *primitive_string, *prototype);
  69. }
  70. // 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode
  71. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code)
  72. {
  73. // 1. Let result be the empty String.
  74. Utf16Data string;
  75. string.ensure_capacity(vm.argument_count());
  76. // 2. For each element next of codeUnits, do
  77. for (size_t i = 0; i < vm.argument_count(); ++i) {
  78. // a. Let nextCU be the code unit whose numeric value is ℝ(? ToUint16(next)).
  79. auto next_code_unit = TRY(vm.argument(i).to_u16(vm));
  80. // b. Set result to the string-concatenation of result and nextCU.
  81. string.append(next_code_unit);
  82. }
  83. // 3. Return result.
  84. return PrimitiveString::create(vm, Utf16String::create(move(string)));
  85. }
  86. // 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint
  87. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point)
  88. {
  89. // 1. Let result be the empty String.
  90. Utf16Data string;
  91. // This will be an under-estimate if any code point is > 0xffff.
  92. string.ensure_capacity(vm.argument_count());
  93. // 2. For each element next of codePoints, do
  94. for (size_t i = 0; i < vm.argument_count(); ++i) {
  95. // a. Let nextCP be ? ToNumber(next).
  96. auto next_code_point = TRY(vm.argument(i).to_number(vm));
  97. // b. If IsIntegralNumber(nextCP) is false, throw a RangeError exception.
  98. if (!next_code_point.is_integral_number())
  99. return vm.throw_completion<RangeError>(ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
  100. auto code_point = MUST(next_code_point.to_i32(vm));
  101. // c. If ℝ(nextCP) < 0 or ℝ(nextCP) > 0x10FFFF, throw a RangeError exception.
  102. if (code_point < 0 || code_point > 0x10FFFF)
  103. return vm.throw_completion<RangeError>(ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
  104. // d. Set result to the string-concatenation of result and UTF16EncodeCodePoint(ℝ(nextCP)).
  105. MUST(code_point_to_utf16(string, static_cast<u32>(code_point)));
  106. }
  107. // 3. Assert: If codePoints is empty, then result is the empty String.
  108. if (!vm.argument_count())
  109. VERIFY(string.is_empty());
  110. // 4. Return result.
  111. return PrimitiveString::create(vm, Utf16String::create(move(string)));
  112. }
  113. // 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw
  114. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
  115. {
  116. auto template_ = vm.argument(0);
  117. // 1. Let substitutionCount be the number of elements in substitutions.
  118. auto substitution_count = vm.argument_count() > 0 ? vm.argument_count() - 1 : 0;
  119. // 2. Let cooked be ? ToObject(template).
  120. auto cooked = TRY(template_.to_object(vm));
  121. // 3. Let literals be ? ToObject(? Get(cooked, "raw")).
  122. auto literals = TRY(TRY(cooked->get(vm.names.raw)).to_object(vm));
  123. // 4. Let literalCount be ? LengthOfArrayLike(literals).
  124. auto literal_count = TRY(length_of_array_like(vm, literals));
  125. // 5. If literalCount ≤ 0, return the empty String.
  126. if (literal_count == 0)
  127. return PrimitiveString::create(vm, String {});
  128. // 6. Let R be the empty String.
  129. StringBuilder builder;
  130. // 7. Let nextIndex be 0.
  131. // 8. Repeat,
  132. for (size_t i = 0; i < literal_count; ++i) {
  133. auto next_key = DeprecatedString::number(i);
  134. // a. Let nextLiteralVal be ? Get(literals, ! ToString(𝔽(nextIndex))).
  135. auto next_literal_value = TRY(literals->get(next_key));
  136. // b. Let nextLiteral be ? ToString(nextLiteralVal).
  137. auto next_literal = TRY(next_literal_value.to_deprecated_string(vm));
  138. // c. Set R to the string-concatenation of R and nextLiteral.
  139. builder.append(next_literal);
  140. // d. If nextIndex + 1 = literalCount, return R.
  141. if (i + 1 == literal_count)
  142. break;
  143. // e. If nextIndex < substitutionCount, then
  144. if (i < substitution_count) {
  145. // i. Let nextSubVal be substitutions[nextIndex].
  146. auto next_substitution_value = vm.argument(i + 1);
  147. // ii. Let nextSub be ? ToString(nextSubVal).
  148. auto next_substitution = TRY(next_substitution_value.to_deprecated_string(vm));
  149. // iii. Set R to the string-concatenation of R and nextSub.
  150. builder.append(next_substitution);
  151. }
  152. // f. Set nextIndex to nextIndex + 1.
  153. }
  154. return PrimitiveString::create(vm, builder.to_deprecated_string());
  155. }
  156. }