StringConstructor.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/Utf32View.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/Array.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/StringConstructor.h>
  13. #include <LibJS/Runtime/StringObject.h>
  14. namespace JS {
  15. StringConstructor::StringConstructor(GlobalObject& global_object)
  16. : NativeFunction(vm().names.String.as_string(), *global_object.function_prototype())
  17. {
  18. }
  19. void StringConstructor::initialize(GlobalObject& global_object)
  20. {
  21. auto& vm = this->vm();
  22. NativeFunction::initialize(global_object);
  23. // 22.1.2.3 String.prototype, https://tc39.es/ecma262/#sec-string.prototype
  24. define_property(vm.names.prototype, global_object.string_prototype(), 0);
  25. define_property(vm.names.length, Value(1), Attribute::Configurable);
  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. }
  31. StringConstructor::~StringConstructor()
  32. {
  33. }
  34. // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
  35. Value StringConstructor::call()
  36. {
  37. if (!vm().argument_count())
  38. return js_string(heap(), "");
  39. if (vm().argument(0).is_symbol())
  40. return js_string(heap(), vm().argument(0).as_symbol().to_string());
  41. auto* string = vm().argument(0).to_primitive_string(global_object());
  42. if (vm().exception())
  43. return {};
  44. return string;
  45. }
  46. // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
  47. Value StringConstructor::construct(FunctionObject&)
  48. {
  49. PrimitiveString* primitive_string = nullptr;
  50. if (!vm().argument_count())
  51. primitive_string = js_string(vm(), "");
  52. else
  53. primitive_string = vm().argument(0).to_primitive_string(global_object());
  54. if (!primitive_string)
  55. return {};
  56. return StringObject::create(global_object(), *primitive_string);
  57. }
  58. // 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw
  59. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
  60. {
  61. auto* cooked = vm.argument(0).to_object(global_object);
  62. if (vm.exception())
  63. return {};
  64. auto raw_value = cooked->get(vm.names.raw).value_or(js_undefined());
  65. if (vm.exception())
  66. return {};
  67. auto* raw = raw_value.to_object(global_object);
  68. if (vm.exception())
  69. return {};
  70. auto literal_segments = length_of_array_like(global_object, *raw);
  71. if (vm.exception())
  72. return {};
  73. if (literal_segments == 0)
  74. return js_string(vm, "");
  75. const auto number_of_substituions = vm.argument_count() - 1;
  76. StringBuilder builder;
  77. for (size_t i = 0; i < literal_segments; ++i) {
  78. auto next_key = String::number(i);
  79. auto next_segment_value = raw->get(next_key).value_or(js_undefined());
  80. if (vm.exception())
  81. return {};
  82. auto next_segment = next_segment_value.to_string(global_object);
  83. if (vm.exception())
  84. return {};
  85. builder.append(next_segment);
  86. if (i + 1 == literal_segments)
  87. break;
  88. if (i < number_of_substituions) {
  89. auto next = vm.argument(i + 1);
  90. auto next_sub = next.to_string(global_object);
  91. if (vm.exception())
  92. return {};
  93. builder.append(next_sub);
  94. }
  95. }
  96. return js_string(vm, builder.build());
  97. }
  98. // 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode
  99. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code)
  100. {
  101. StringBuilder builder;
  102. for (size_t i = 0; i < vm.argument_count(); ++i) {
  103. auto char_code = vm.argument(i).to_i32(global_object);
  104. if (vm.exception())
  105. return {};
  106. auto truncated = char_code & 0xffff;
  107. // FIXME: We need an Utf16View :^)
  108. builder.append(Utf32View((u32*)&truncated, 1));
  109. }
  110. return js_string(vm, builder.build());
  111. }
  112. // 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint
  113. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point)
  114. {
  115. StringBuilder builder;
  116. for (size_t i = 0; i < vm.argument_count(); ++i) {
  117. auto next_code_point = vm.argument(i).to_number(global_object);
  118. if (vm.exception())
  119. return {};
  120. if (!next_code_point.is_integral_number()) {
  121. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
  122. return {};
  123. }
  124. auto code_point = next_code_point.to_i32(global_object);
  125. if (code_point < 0 || code_point > 0x10FFFF) {
  126. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
  127. return {};
  128. }
  129. builder.append_code_point(code_point);
  130. }
  131. return js_string(vm, builder.build());
  132. }
  133. }