StringConstructor.cpp 7.4 KB

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