StringConstructor.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/Array.h>
  9. #include <LibJS/Runtime/Error.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/StringConstructor.h>
  12. #include <LibJS/Runtime/StringObject.h>
  13. namespace JS {
  14. StringConstructor::StringConstructor(GlobalObject& global_object)
  15. : NativeFunction(vm().names.String, *global_object.function_prototype())
  16. {
  17. }
  18. void StringConstructor::initialize(GlobalObject& global_object)
  19. {
  20. auto& vm = this->vm();
  21. NativeFunction::initialize(global_object);
  22. // 22.1.2.3 String.prototype, https://tc39.es/ecma262/#sec-string.prototype
  23. define_property(vm.names.prototype, global_object.string_prototype(), 0);
  24. define_property(vm.names.length, Value(1), Attribute::Configurable);
  25. u8 attr = Attribute::Writable | Attribute::Configurable;
  26. define_native_function(vm.names.raw, raw, 1, attr);
  27. define_native_function(vm.names.fromCharCode, from_char_code, 1, attr);
  28. define_native_function(vm.names.fromCodePoint, from_code_point, 1, attr);
  29. }
  30. StringConstructor::~StringConstructor()
  31. {
  32. }
  33. // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
  34. Value StringConstructor::call()
  35. {
  36. if (!vm().argument_count())
  37. return js_string(heap(), "");
  38. if (vm().argument(0).is_symbol())
  39. return js_string(heap(), vm().argument(0).as_symbol().to_string());
  40. auto* string = vm().argument(0).to_primitive_string(global_object());
  41. if (vm().exception())
  42. return {};
  43. return string;
  44. }
  45. // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
  46. Value StringConstructor::construct(Function&)
  47. {
  48. PrimitiveString* primitive_string = nullptr;
  49. if (!vm().argument_count())
  50. primitive_string = js_string(vm(), "");
  51. else
  52. primitive_string = vm().argument(0).to_primitive_string(global_object());
  53. if (!primitive_string)
  54. return {};
  55. return StringObject::create(global_object(), *primitive_string);
  56. }
  57. // 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw
  58. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
  59. {
  60. auto* template_object = vm.argument(0).to_object(global_object);
  61. if (vm.exception())
  62. return {};
  63. auto raw = template_object->get(vm.names.raw);
  64. if (vm.exception())
  65. return {};
  66. if (raw.is_empty() || raw.is_nullish()) {
  67. vm.throw_exception<TypeError>(global_object, ErrorType::StringRawCannotConvert, raw.is_null() ? "null" : "undefined");
  68. return {};
  69. }
  70. // FIXME: This should use length_of_array_like() and work with any object
  71. if (!raw.is_object() || !raw.as_object().is_array())
  72. return js_string(vm, "");
  73. auto* array = static_cast<Array*>(raw.to_object(global_object));
  74. auto& raw_array_elements = array->indexed_properties();
  75. StringBuilder builder;
  76. for (size_t i = 0; i < raw_array_elements.array_like_size(); ++i) {
  77. auto result = raw_array_elements.get(array, i);
  78. if (vm.exception())
  79. return {};
  80. if (!result.has_value())
  81. continue;
  82. builder.append(result.value().value.to_string(global_object));
  83. if (vm.exception())
  84. return {};
  85. if (i + 1 < vm.argument_count() && i < raw_array_elements.array_like_size() - 1) {
  86. builder.append(vm.argument(i + 1).to_string(global_object));
  87. if (vm.exception())
  88. return {};
  89. }
  90. }
  91. return js_string(vm, builder.build());
  92. }
  93. // 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode
  94. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code)
  95. {
  96. StringBuilder builder;
  97. for (size_t i = 0; i < vm.argument_count(); ++i) {
  98. auto char_code = vm.argument(i).to_i32(global_object);
  99. if (vm.exception())
  100. return {};
  101. auto truncated = char_code & 0xffff;
  102. // FIXME: We need an Utf16View :^)
  103. builder.append(Utf32View((u32*)&truncated, 1));
  104. }
  105. return js_string(vm, builder.build());
  106. }
  107. // 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint
  108. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point)
  109. {
  110. StringBuilder builder;
  111. for (size_t i = 0; i < vm.argument_count(); ++i) {
  112. auto next_code_point = vm.argument(i).to_number(global_object);
  113. if (vm.exception())
  114. return {};
  115. if (!next_code_point.is_integral_number()) {
  116. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
  117. return {};
  118. }
  119. auto code_point = next_code_point.to_i32(global_object);
  120. if (code_point < 0 || code_point > 0x10FFFF) {
  121. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
  122. return {};
  123. }
  124. builder.append_code_point(code_point);
  125. }
  126. return js_string(vm, builder.build());
  127. }
  128. }