StringConstructor.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. }
  29. StringConstructor::~StringConstructor()
  30. {
  31. }
  32. // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
  33. Value StringConstructor::call()
  34. {
  35. if (!vm().argument_count())
  36. return js_string(heap(), "");
  37. if (vm().argument(0).is_symbol())
  38. return js_string(heap(), vm().argument(0).as_symbol().to_string());
  39. auto* string = vm().argument(0).to_primitive_string(global_object());
  40. if (vm().exception())
  41. return {};
  42. return string;
  43. }
  44. // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
  45. Value StringConstructor::construct(Function&)
  46. {
  47. PrimitiveString* primitive_string = nullptr;
  48. if (!vm().argument_count())
  49. primitive_string = js_string(vm(), "");
  50. else
  51. primitive_string = vm().argument(0).to_primitive_string(global_object());
  52. if (!primitive_string)
  53. return {};
  54. return StringObject::create(global_object(), *primitive_string);
  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* template_object = vm.argument(0).to_object(global_object);
  60. if (vm.exception())
  61. return {};
  62. auto raw = template_object->get(vm.names.raw);
  63. if (vm.exception())
  64. return {};
  65. if (raw.is_empty() || raw.is_nullish()) {
  66. vm.throw_exception<TypeError>(global_object, ErrorType::StringRawCannotConvert, raw.is_null() ? "null" : "undefined");
  67. return {};
  68. }
  69. // FIXME: This should use length_of_array_like() and work with any object
  70. if (!raw.is_object() || !raw.as_object().is_array())
  71. return js_string(vm, "");
  72. auto* array = static_cast<Array*>(raw.to_object(global_object));
  73. auto& raw_array_elements = array->indexed_properties();
  74. StringBuilder builder;
  75. for (size_t i = 0; i < raw_array_elements.array_like_size(); ++i) {
  76. auto result = raw_array_elements.get(array, i);
  77. if (vm.exception())
  78. return {};
  79. if (!result.has_value())
  80. continue;
  81. builder.append(result.value().value.to_string(global_object));
  82. if (vm.exception())
  83. return {};
  84. if (i + 1 < vm.argument_count() && i < raw_array_elements.array_like_size() - 1) {
  85. builder.append(vm.argument(i + 1).to_string(global_object));
  86. if (vm.exception())
  87. return {};
  88. }
  89. }
  90. return js_string(vm, builder.build());
  91. }
  92. // 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode
  93. JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code)
  94. {
  95. StringBuilder builder;
  96. for (size_t i = 0; i < vm.argument_count(); ++i) {
  97. auto char_code = vm.argument(i).to_i32(global_object);
  98. if (vm.exception())
  99. return {};
  100. auto truncated = char_code & 0xffff;
  101. // FIXME: We need an Utf16View :^)
  102. builder.append(Utf32View((u32*)&truncated, 1));
  103. }
  104. return js_string(vm, builder.build());
  105. }
  106. }