StringConstructor.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. NativeFunction::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. if (!vm.argument_count())
  38. return js_string(heap(), "");
  39. if (vm.argument(0).is_symbol())
  40. return js_string(vm, vm.argument(0).as_symbol().to_string());
  41. return TRY(vm.argument(0).to_primitive_string(vm));
  42. }
  43. // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
  44. ThrowCompletionOr<Object*> StringConstructor::construct(FunctionObject& new_target)
  45. {
  46. auto& vm = this->vm();
  47. auto& realm = *vm.current_realm();
  48. PrimitiveString* primitive_string;
  49. if (!vm.argument_count())
  50. primitive_string = js_string(vm, "");
  51. else
  52. primitive_string = TRY(vm.argument(0).to_primitive_string(vm));
  53. auto* prototype = TRY(get_prototype_from_constructor(vm, new_target, &Intrinsics::string_prototype));
  54. return StringObject::create(realm, *primitive_string, *prototype);
  55. }
  56. // 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw
  57. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
  58. {
  59. auto* cooked = TRY(vm.argument(0).to_object(vm));
  60. auto raw_value = TRY(cooked->get(vm.names.raw));
  61. auto* raw = TRY(raw_value.to_object(vm));
  62. auto literal_segments = TRY(length_of_array_like(vm, *raw));
  63. if (literal_segments == 0)
  64. return js_string(vm, "");
  65. auto const number_of_substituions = vm.argument_count() - 1;
  66. StringBuilder builder;
  67. for (size_t i = 0; i < literal_segments; ++i) {
  68. auto next_key = String::number(i);
  69. auto next_segment_value = TRY(raw->get(next_key));
  70. auto next_segment = TRY(next_segment_value.to_string(vm));
  71. builder.append(next_segment);
  72. if (i + 1 == literal_segments)
  73. break;
  74. if (i < number_of_substituions) {
  75. auto next = vm.argument(i + 1);
  76. auto next_sub = TRY(next.to_string(vm));
  77. builder.append(next_sub);
  78. }
  79. }
  80. return js_string(vm, builder.build());
  81. }
  82. // 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode
  83. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code)
  84. {
  85. Vector<u16, 1> string;
  86. string.ensure_capacity(vm.argument_count());
  87. for (size_t i = 0; i < vm.argument_count(); ++i)
  88. string.append(TRY(vm.argument(i).to_u16(vm)));
  89. return js_string(vm, Utf16String(move(string)));
  90. }
  91. // 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint
  92. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point)
  93. {
  94. Vector<u16, 1> string;
  95. string.ensure_capacity(vm.argument_count()); // This will be an under-estimate if any code point is > 0xffff.
  96. for (size_t i = 0; i < vm.argument_count(); ++i) {
  97. auto next_code_point = TRY(vm.argument(i).to_number(vm));
  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 = TRY(next_code_point.to_i32(vm));
  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. AK::code_point_to_utf16(string, static_cast<u32>(code_point));
  104. }
  105. return js_string(vm, Utf16String(move(string)));
  106. }
  107. }