StringConstructor.cpp 3.6 KB

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