StringConstructor.cpp 7.4 KB

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