GlobalObject.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/BuiltinWrappers.h>
  8. #include <AK/CharacterTypes.h>
  9. #include <AK/FloatingPointStringConversions.h>
  10. #include <AK/Hex.h>
  11. #include <AK/UnicodeUtils.h>
  12. #include <AK/Utf16View.h>
  13. #include <AK/Utf8View.h>
  14. #include <LibJS/Heap/DeferGC.h>
  15. #include <LibJS/Interpreter.h>
  16. #include <LibJS/Runtime/AbstractOperations.h>
  17. #include <LibJS/Runtime/AggregateErrorConstructor.h>
  18. #include <LibJS/Runtime/ArrayBufferConstructor.h>
  19. #include <LibJS/Runtime/ArrayConstructor.h>
  20. #include <LibJS/Runtime/ArrayPrototype.h>
  21. #include <LibJS/Runtime/AsyncFunctionConstructor.h>
  22. #include <LibJS/Runtime/AsyncGeneratorFunctionConstructor.h>
  23. #include <LibJS/Runtime/AsyncGeneratorPrototype.h>
  24. #include <LibJS/Runtime/AtomicsObject.h>
  25. #include <LibJS/Runtime/BigIntConstructor.h>
  26. #include <LibJS/Runtime/BooleanConstructor.h>
  27. #include <LibJS/Runtime/ConsoleObject.h>
  28. #include <LibJS/Runtime/DataViewConstructor.h>
  29. #include <LibJS/Runtime/DateConstructor.h>
  30. #include <LibJS/Runtime/ErrorConstructor.h>
  31. #include <LibJS/Runtime/FinalizationRegistryConstructor.h>
  32. #include <LibJS/Runtime/FinalizationRegistryPrototype.h>
  33. #include <LibJS/Runtime/FunctionConstructor.h>
  34. #include <LibJS/Runtime/GeneratorFunctionConstructor.h>
  35. #include <LibJS/Runtime/GeneratorPrototype.h>
  36. #include <LibJS/Runtime/GlobalEnvironment.h>
  37. #include <LibJS/Runtime/GlobalObject.h>
  38. #include <LibJS/Runtime/Intl/CollatorConstructor.h>
  39. #include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
  40. #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
  41. #include <LibJS/Runtime/Intl/DurationFormatConstructor.h>
  42. #include <LibJS/Runtime/Intl/Intl.h>
  43. #include <LibJS/Runtime/Intl/ListFormatConstructor.h>
  44. #include <LibJS/Runtime/Intl/LocaleConstructor.h>
  45. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  46. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  47. #include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
  48. #include <LibJS/Runtime/Intl/SegmenterConstructor.h>
  49. #include <LibJS/Runtime/JSONObject.h>
  50. #include <LibJS/Runtime/MapConstructor.h>
  51. #include <LibJS/Runtime/MathObject.h>
  52. #include <LibJS/Runtime/NumberConstructor.h>
  53. #include <LibJS/Runtime/Object.h>
  54. #include <LibJS/Runtime/ObjectConstructor.h>
  55. #include <LibJS/Runtime/PromiseConstructor.h>
  56. #include <LibJS/Runtime/ProxyConstructor.h>
  57. #include <LibJS/Runtime/Realm.h>
  58. #include <LibJS/Runtime/ReflectObject.h>
  59. #include <LibJS/Runtime/RegExpConstructor.h>
  60. #include <LibJS/Runtime/SetConstructor.h>
  61. #include <LibJS/Runtime/ShadowRealmConstructor.h>
  62. #include <LibJS/Runtime/Shape.h>
  63. #include <LibJS/Runtime/StringConstructor.h>
  64. #include <LibJS/Runtime/StringPrototype.h>
  65. #include <LibJS/Runtime/SymbolConstructor.h>
  66. #include <LibJS/Runtime/Temporal/CalendarConstructor.h>
  67. #include <LibJS/Runtime/Temporal/DurationConstructor.h>
  68. #include <LibJS/Runtime/Temporal/InstantConstructor.h>
  69. #include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
  70. #include <LibJS/Runtime/Temporal/PlainDateTimeConstructor.h>
  71. #include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
  72. #include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
  73. #include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
  74. #include <LibJS/Runtime/Temporal/Temporal.h>
  75. #include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
  76. #include <LibJS/Runtime/Temporal/ZonedDateTimeConstructor.h>
  77. #include <LibJS/Runtime/TypedArray.h>
  78. #include <LibJS/Runtime/Value.h>
  79. #include <LibJS/Runtime/WeakMapConstructor.h>
  80. #include <LibJS/Runtime/WeakRefConstructor.h>
  81. #include <LibJS/Runtime/WeakSetConstructor.h>
  82. namespace JS {
  83. GlobalObject::GlobalObject(Realm& realm)
  84. : Object(GlobalObjectTag::Tag, realm)
  85. {
  86. ensure_shape_is_unique();
  87. Object::set_prototype(realm.intrinsics().object_prototype());
  88. }
  89. // 9.3.4 SetDefaultGlobalBindings ( realmRec ), https://tc39.es/ecma262/#sec-setdefaultglobalbindings
  90. Object& set_default_global_bindings(Realm& realm)
  91. {
  92. auto& vm = realm.vm();
  93. // 1. Let global be realmRec.[[GlobalObject]].
  94. auto& global = realm.global_object();
  95. // 2. For each property of the Global Object specified in clause 19, do
  96. // a. Let name be the String value of the property name.
  97. // b. Let desc be the fully populated data Property Descriptor for the property, containing the specified attributes for the property.
  98. // For properties listed in 19.2, 19.3, or 19.4 the value of the [[Value]] attribute is the corresponding intrinsic object from realmRec.
  99. // c. Perform ? DefinePropertyOrThrow(global, name, desc).
  100. // NOTE: This function is infallible as we set properties directly; property clashes in global object construction are not expected.
  101. u8 attr = Attribute::Writable | Attribute::Configurable;
  102. // 19.2 Function Properties of the Global Object, https://tc39.es/ecma262/#sec-function-properties-of-the-global-object
  103. global.define_direct_property(vm.names.eval, realm.intrinsics().eval_function(), attr);
  104. global.define_direct_property(vm.names.isFinite, realm.intrinsics().is_finite_function(), attr);
  105. global.define_direct_property(vm.names.isNaN, realm.intrinsics().is_nan_function(), attr);
  106. global.define_direct_property(vm.names.parseFloat, realm.intrinsics().parse_float_function(), attr);
  107. global.define_direct_property(vm.names.parseInt, realm.intrinsics().parse_int_function(), attr);
  108. global.define_direct_property(vm.names.decodeURI, realm.intrinsics().decode_uri_function(), attr);
  109. global.define_direct_property(vm.names.decodeURIComponent, realm.intrinsics().decode_uri_component_function(), attr);
  110. global.define_direct_property(vm.names.encodeURI, realm.intrinsics().encode_uri_function(), attr);
  111. global.define_direct_property(vm.names.encodeURIComponent, realm.intrinsics().encode_uri_component_function(), attr);
  112. // 19.1 Value Properties of the Global Object, https://tc39.es/ecma262/#sec-value-properties-of-the-global-object
  113. global.define_direct_property(vm.names.globalThis, &global, attr);
  114. global.define_direct_property(vm.names.Infinity, js_infinity(), 0);
  115. global.define_direct_property(vm.names.NaN, js_nan(), 0);
  116. global.define_direct_property(vm.names.undefined, js_undefined(), 0);
  117. // 19.3 Constructor Properties of the Global Object, https://tc39.es/ecma262/#sec-constructor-properties-of-the-global-object
  118. global.define_intrinsic_accessor(vm.names.AggregateError, attr, [](auto& realm) -> Value { return realm.intrinsics().aggregate_error_constructor(); });
  119. global.define_intrinsic_accessor(vm.names.Array, attr, [](auto& realm) -> Value { return realm.intrinsics().array_constructor(); });
  120. global.define_intrinsic_accessor(vm.names.ArrayBuffer, attr, [](auto& realm) -> Value { return realm.intrinsics().array_buffer_constructor(); });
  121. global.define_intrinsic_accessor(vm.names.BigInt, attr, [](auto& realm) -> Value { return realm.intrinsics().bigint_constructor(); });
  122. global.define_intrinsic_accessor(vm.names.BigInt64Array, attr, [](auto& realm) -> Value { return realm.intrinsics().big_int64_array_constructor(); });
  123. global.define_intrinsic_accessor(vm.names.BigUint64Array, attr, [](auto& realm) -> Value { return realm.intrinsics().big_uint64_array_constructor(); });
  124. global.define_intrinsic_accessor(vm.names.Boolean, attr, [](auto& realm) -> Value { return realm.intrinsics().boolean_constructor(); });
  125. global.define_intrinsic_accessor(vm.names.DataView, attr, [](auto& realm) -> Value { return realm.intrinsics().data_view_constructor(); });
  126. global.define_intrinsic_accessor(vm.names.Date, attr, [](auto& realm) -> Value { return realm.intrinsics().date_constructor(); });
  127. global.define_intrinsic_accessor(vm.names.Error, attr, [](auto& realm) -> Value { return realm.intrinsics().error_constructor(); });
  128. global.define_intrinsic_accessor(vm.names.EvalError, attr, [](auto& realm) -> Value { return realm.intrinsics().eval_error_constructor(); });
  129. global.define_intrinsic_accessor(vm.names.FinalizationRegistry, attr, [](auto& realm) -> Value { return realm.intrinsics().finalization_registry_constructor(); });
  130. global.define_intrinsic_accessor(vm.names.Float32Array, attr, [](auto& realm) -> Value { return realm.intrinsics().float32_array_constructor(); });
  131. global.define_intrinsic_accessor(vm.names.Float64Array, attr, [](auto& realm) -> Value { return realm.intrinsics().float64_array_constructor(); });
  132. global.define_intrinsic_accessor(vm.names.Function, attr, [](auto& realm) -> Value { return realm.intrinsics().function_constructor(); });
  133. global.define_intrinsic_accessor(vm.names.Int8Array, attr, [](auto& realm) -> Value { return realm.intrinsics().int8_array_constructor(); });
  134. global.define_intrinsic_accessor(vm.names.Int16Array, attr, [](auto& realm) -> Value { return realm.intrinsics().int16_array_constructor(); });
  135. global.define_intrinsic_accessor(vm.names.Int32Array, attr, [](auto& realm) -> Value { return realm.intrinsics().int32_array_constructor(); });
  136. global.define_intrinsic_accessor(vm.names.Map, attr, [](auto& realm) -> Value { return realm.intrinsics().map_constructor(); });
  137. global.define_intrinsic_accessor(vm.names.Number, attr, [](auto& realm) -> Value { return realm.intrinsics().number_constructor(); });
  138. global.define_intrinsic_accessor(vm.names.Object, attr, [](auto& realm) -> Value { return realm.intrinsics().object_constructor(); });
  139. global.define_intrinsic_accessor(vm.names.Promise, attr, [](auto& realm) -> Value { return realm.intrinsics().promise_constructor(); });
  140. global.define_intrinsic_accessor(vm.names.Proxy, attr, [](auto& realm) -> Value { return realm.intrinsics().proxy_constructor(); });
  141. global.define_intrinsic_accessor(vm.names.RangeError, attr, [](auto& realm) -> Value { return realm.intrinsics().range_error_constructor(); });
  142. global.define_intrinsic_accessor(vm.names.ReferenceError, attr, [](auto& realm) -> Value { return realm.intrinsics().reference_error_constructor(); });
  143. global.define_intrinsic_accessor(vm.names.RegExp, attr, [](auto& realm) -> Value { return realm.intrinsics().regexp_constructor(); });
  144. global.define_intrinsic_accessor(vm.names.Set, attr, [](auto& realm) -> Value { return realm.intrinsics().set_constructor(); });
  145. global.define_intrinsic_accessor(vm.names.ShadowRealm, attr, [](auto& realm) -> Value { return realm.intrinsics().shadow_realm_constructor(); });
  146. global.define_intrinsic_accessor(vm.names.String, attr, [](auto& realm) -> Value { return realm.intrinsics().string_constructor(); });
  147. global.define_intrinsic_accessor(vm.names.Symbol, attr, [](auto& realm) -> Value { return realm.intrinsics().symbol_constructor(); });
  148. global.define_intrinsic_accessor(vm.names.SyntaxError, attr, [](auto& realm) -> Value { return realm.intrinsics().syntax_error_constructor(); });
  149. global.define_intrinsic_accessor(vm.names.TypeError, attr, [](auto& realm) -> Value { return realm.intrinsics().type_error_constructor(); });
  150. global.define_intrinsic_accessor(vm.names.Uint8Array, attr, [](auto& realm) -> Value { return realm.intrinsics().uint8_array_constructor(); });
  151. global.define_intrinsic_accessor(vm.names.Uint8ClampedArray, attr, [](auto& realm) -> Value { return realm.intrinsics().uint8_clamped_array_constructor(); });
  152. global.define_intrinsic_accessor(vm.names.Uint16Array, attr, [](auto& realm) -> Value { return realm.intrinsics().uint16_array_constructor(); });
  153. global.define_intrinsic_accessor(vm.names.Uint32Array, attr, [](auto& realm) -> Value { return realm.intrinsics().uint32_array_constructor(); });
  154. global.define_intrinsic_accessor(vm.names.URIError, attr, [](auto& realm) -> Value { return realm.intrinsics().uri_error_constructor(); });
  155. global.define_intrinsic_accessor(vm.names.WeakMap, attr, [](auto& realm) -> Value { return realm.intrinsics().weak_map_constructor(); });
  156. global.define_intrinsic_accessor(vm.names.WeakRef, attr, [](auto& realm) -> Value { return realm.intrinsics().weak_ref_constructor(); });
  157. global.define_intrinsic_accessor(vm.names.WeakSet, attr, [](auto& realm) -> Value { return realm.intrinsics().weak_set_constructor(); });
  158. // 19.4 Other Properties of the Global Object, https://tc39.es/ecma262/#sec-other-properties-of-the-global-object
  159. global.define_intrinsic_accessor(vm.names.Atomics, attr, [](auto& realm) -> Value { return realm.intrinsics().atomics_object(); });
  160. global.define_intrinsic_accessor(vm.names.Intl, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_object(); });
  161. global.define_intrinsic_accessor(vm.names.JSON, attr, [](auto& realm) -> Value { return realm.intrinsics().json_object(); });
  162. global.define_intrinsic_accessor(vm.names.Math, attr, [](auto& realm) -> Value { return realm.intrinsics().math_object(); });
  163. global.define_intrinsic_accessor(vm.names.Reflect, attr, [](auto& realm) -> Value { return realm.intrinsics().reflect_object(); });
  164. global.define_intrinsic_accessor(vm.names.Temporal, attr, [](auto& realm) -> Value { return realm.intrinsics().temporal_object(); });
  165. // B.2.1 Additional Properties of the Global Object, https://tc39.es/ecma262/#sec-additional-properties-of-the-global-object
  166. global.define_direct_property(vm.names.escape, realm.intrinsics().escape_function(), attr);
  167. global.define_direct_property(vm.names.unescape, realm.intrinsics().unescape_function(), attr);
  168. // Non-standard
  169. global.define_direct_property(vm.names.InternalError, realm.intrinsics().internal_error_constructor(), attr);
  170. global.define_direct_property(vm.names.console, realm.intrinsics().console_object(), attr);
  171. // 3. Return global.
  172. return global;
  173. }
  174. void GlobalObject::initialize(Realm& realm)
  175. {
  176. Base::initialize(realm);
  177. auto& vm = this->vm();
  178. // Non-standard
  179. u8 attr = Attribute::Writable | Attribute::Configurable;
  180. define_native_function(realm, vm.names.gc, gc, 0, attr);
  181. }
  182. GlobalObject::~GlobalObject() = default;
  183. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::gc)
  184. {
  185. #ifdef AK_OS_SERENITY
  186. dbgln("Forced garbage collection requested!");
  187. #endif
  188. vm.heap().collect_garbage();
  189. return js_undefined();
  190. }
  191. // 19.2.3 isNaN ( number ), https://tc39.es/ecma262/#sec-isnan-number
  192. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_nan)
  193. {
  194. return Value(TRY(vm.argument(0).to_number(vm)).is_nan());
  195. }
  196. // 19.2.2 isFinite ( number ), https://tc39.es/ecma262/#sec-isfinite-number
  197. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_finite)
  198. {
  199. return Value(TRY(vm.argument(0).to_number(vm)).is_finite_number());
  200. }
  201. // 19.2.4 parseFloat ( string ), https://tc39.es/ecma262/#sec-parsefloat-string
  202. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::parse_float)
  203. {
  204. if (vm.argument(0).is_number())
  205. return vm.argument(0);
  206. auto input_string = TRY(vm.argument(0).to_string(vm));
  207. auto trimmed_string = MUST(trim_string(vm, PrimitiveString::create(vm, input_string), TrimMode::Left));
  208. if (trimmed_string.is_empty())
  209. return js_nan();
  210. auto result = parse_first_floating_point<double>(trimmed_string.characters(), trimmed_string.characters() + trimmed_string.length());
  211. if (result.parsed_value())
  212. return result.value;
  213. bool starts_with_sign = trimmed_string[0] == '-' || trimmed_string[0] == '+';
  214. auto signless_view = starts_with_sign ? trimmed_string.substring_view(1) : trimmed_string.view();
  215. if (signless_view.starts_with("Infinity"sv, AK::CaseSensitivity::CaseSensitive)) {
  216. // Only an immediate - means we should return negative infinity
  217. if (trimmed_string[0] == '-')
  218. return js_negative_infinity();
  219. return js_infinity();
  220. }
  221. return js_nan();
  222. }
  223. // 19.2.5 parseInt ( string, radix ), https://tc39.es/ecma262/#sec-parseint-string-radix
  224. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::parse_int)
  225. {
  226. // 1. Let inputString be ? ToString(string).
  227. auto input_string = TRY(vm.argument(0).to_string(vm));
  228. // 2. Let S be ! TrimString(inputString, start).
  229. auto string = MUST(trim_string(vm, PrimitiveString::create(vm, input_string), TrimMode::Left));
  230. // 3. Let sign be 1.
  231. auto sign = 1;
  232. // 4. If S is not empty and the first code unit of S is the code unit 0x002D (HYPHEN-MINUS), set sign to -1.
  233. if (!string.is_empty() && string[0] == 0x2D)
  234. sign = -1;
  235. // 5. If S is not empty and the first code unit of S is the code unit 0x002B (PLUS SIGN) or the code unit 0x002D (HYPHEN-MINUS), remove the first code unit from S.
  236. auto trimmed_view = string.view();
  237. if (!string.is_empty() && (string[0] == 0x2B || string[0] == 0x2D))
  238. trimmed_view = trimmed_view.substring_view(1);
  239. // 6. Let R be ℝ(? ToInt32(radix)).
  240. auto radix = TRY(vm.argument(1).to_i32(vm));
  241. // 7. Let stripPrefix be true.
  242. auto strip_prefix = true;
  243. // 8. If R ≠ 0, then
  244. if (radix != 0) {
  245. // a. If R < 2 or R > 36, return NaN.
  246. if (radix < 2 || radix > 36)
  247. return js_nan();
  248. // b. If R ≠ 16, set stripPrefix to false.
  249. if (radix != 16)
  250. strip_prefix = false;
  251. }
  252. // 9. Else,
  253. else {
  254. // a. Set R to 10.
  255. radix = 10;
  256. }
  257. // 10. If stripPrefix is true, then
  258. if (strip_prefix) {
  259. // a. If the length of S is at least 2 and the first two code units of S are either "0x" or "0X", then
  260. if (trimmed_view.length() >= 2 && trimmed_view.substring_view(0, 2).equals_ignoring_case("0x"sv)) {
  261. // i. Remove the first two code units from S.
  262. trimmed_view = trimmed_view.substring_view(2);
  263. // ii. Set R to 16.
  264. radix = 16;
  265. }
  266. }
  267. // 11. If S contains a code unit that is not a radix-R digit, let end be the index within S of the first such code unit; otherwise, let end be the length of S.
  268. // 12. Let Z be the substring of S from 0 to end.
  269. // 13. If Z is empty, return NaN.
  270. // 14. Let mathInt be the integer value that is represented by Z in radix-R notation, using the letters A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; and if R is not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-approximated integer representing the integer value denoted by Z in radix-R notation.)
  271. auto parse_digit = [&](u32 code_point) -> Optional<u32> {
  272. if (!is_ascii_alphanumeric(code_point))
  273. return {};
  274. auto digit = parse_ascii_base36_digit(code_point);
  275. if (digit >= (u32)radix)
  276. return {};
  277. return digit;
  278. };
  279. bool had_digits = false;
  280. double number = 0;
  281. for (auto code_point : Utf8View(trimmed_view)) {
  282. auto digit = parse_digit(code_point);
  283. if (!digit.has_value())
  284. break;
  285. had_digits = true;
  286. number *= radix;
  287. number += digit.value();
  288. }
  289. if (!had_digits)
  290. return js_nan();
  291. // 15. If mathInt = 0, then
  292. // a. If sign = -1, return -0𝔽.
  293. // b. Return +0𝔽.
  294. // 16. Return 𝔽(sign × mathInt).
  295. return Value(sign * number);
  296. }
  297. // 19.2.1 eval ( x ), https://tc39.es/ecma262/#sec-eval-x
  298. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::eval)
  299. {
  300. return perform_eval(vm, vm.argument(0), CallerMode::NonStrict, EvalMode::Indirect);
  301. }
  302. // 19.2.6.1.1 Encode ( string, unescapedSet ), https://tc39.es/ecma262/#sec-encode
  303. static ThrowCompletionOr<DeprecatedString> encode(VM& vm, DeprecatedString const& string, StringView unescaped_set)
  304. {
  305. auto utf16_string = TRY(Utf16String::create(vm, string));
  306. // 1. Let strLen be the length of string.
  307. auto string_length = utf16_string.length_in_code_units();
  308. // 2. Let R be the empty String.
  309. StringBuilder encoded_builder;
  310. // 3. Let k be 0.
  311. auto k = 0u;
  312. // 4. Repeat,
  313. while (k < string_length) {
  314. // a. If k = strLen, return R.
  315. // Handled below
  316. // b. Let C be the code unit at index k within string.
  317. auto code_unit = utf16_string.code_unit_at(k);
  318. // c. If C is in unescapedSet, then
  319. // NOTE: We assume the unescaped set only contains ascii characters as unescaped_set is a StringView.
  320. if (code_unit < 0x80 && unescaped_set.contains(static_cast<char>(code_unit))) {
  321. // i. Set k to k + 1.
  322. k++;
  323. // ii. Set R to the string-concatenation of R and C.
  324. encoded_builder.append(code_unit);
  325. }
  326. // d. Else,
  327. else {
  328. // i. Let cp be CodePointAt(string, k).
  329. auto code_point = code_point_at(utf16_string.view(), k);
  330. // ii. If cp.[[IsUnpairedSurrogate]] is true, throw a URIError exception.
  331. if (code_point.is_unpaired_surrogate)
  332. return vm.throw_completion<URIError>(ErrorType::URIMalformed);
  333. // iii. Set k to k + cp.[[CodeUnitCount]].
  334. k += code_point.code_unit_count;
  335. // iv. Let Octets be the List of octets resulting by applying the UTF-8 transformation to cp.[[CodePoint]].
  336. // v. For each element octet of Octets, do
  337. auto nwritten = AK::UnicodeUtils::code_point_to_utf8(code_point.code_point, [&encoded_builder](u8 octet) {
  338. // 1. Set R to the string-concatenation of:
  339. // * R
  340. // * "%"
  341. // * the String representation of octet, formatted as a two-digit uppercase hexadecimal number, padded to the left with a zero if necessary
  342. encoded_builder.appendff("%{:02X}", octet);
  343. });
  344. VERIFY(nwritten > 0);
  345. }
  346. }
  347. return encoded_builder.build();
  348. }
  349. // 19.2.6.1.2 Decode ( string, reservedSet ), https://tc39.es/ecma262/#sec-decode
  350. static ThrowCompletionOr<DeprecatedString> decode(VM& vm, DeprecatedString const& string, StringView reserved_set)
  351. {
  352. StringBuilder decoded_builder;
  353. auto code_point_start_offset = 0u;
  354. auto expected_continuation_bytes = 0;
  355. for (size_t k = 0; k < string.length(); k++) {
  356. auto code_unit = string[k];
  357. if (code_unit != '%') {
  358. if (expected_continuation_bytes > 0)
  359. return vm.throw_completion<URIError>(ErrorType::URIMalformed);
  360. decoded_builder.append(code_unit);
  361. continue;
  362. }
  363. if (k + 2 >= string.length())
  364. return vm.throw_completion<URIError>(ErrorType::URIMalformed);
  365. auto first_digit = decode_hex_digit(string[k + 1]);
  366. if (first_digit >= 16)
  367. return vm.throw_completion<URIError>(ErrorType::URIMalformed);
  368. auto second_digit = decode_hex_digit(string[k + 2]);
  369. if (second_digit >= 16)
  370. return vm.throw_completion<URIError>(ErrorType::URIMalformed);
  371. u8 decoded_code_unit = (first_digit << 4) | second_digit;
  372. k += 2;
  373. if (expected_continuation_bytes > 0) {
  374. decoded_builder.append(decoded_code_unit);
  375. expected_continuation_bytes--;
  376. if (expected_continuation_bytes == 0 && !Utf8View(decoded_builder.string_view().substring_view(code_point_start_offset)).validate())
  377. return vm.throw_completion<URIError>(ErrorType::URIMalformed);
  378. continue;
  379. }
  380. if (decoded_code_unit < 0x80) {
  381. if (reserved_set.contains(static_cast<char>(decoded_code_unit)))
  382. decoded_builder.append(string.substring_view(k - 2, 3));
  383. else
  384. decoded_builder.append(decoded_code_unit);
  385. continue;
  386. }
  387. auto leading_ones = count_leading_zeroes_safe(static_cast<u8>(~decoded_code_unit));
  388. if (leading_ones == 1 || leading_ones > 4)
  389. return vm.throw_completion<URIError>(ErrorType::URIMalformed);
  390. code_point_start_offset = decoded_builder.length();
  391. decoded_builder.append(decoded_code_unit);
  392. expected_continuation_bytes = leading_ones - 1;
  393. }
  394. if (expected_continuation_bytes > 0)
  395. return vm.throw_completion<URIError>(ErrorType::URIMalformed);
  396. return decoded_builder.build();
  397. }
  398. // 19.2.6.4 encodeURI ( uri ), https://tc39.es/ecma262/#sec-encodeuri-uri
  399. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::encode_uri)
  400. {
  401. auto uri_string = TRY(vm.argument(0).to_string(vm));
  402. auto encoded = TRY(encode(vm, uri_string, ";/?:@&=+$,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()#"sv));
  403. return PrimitiveString::create(vm, move(encoded));
  404. }
  405. // 19.2.6.2 decodeURI ( encodedURI ), https://tc39.es/ecma262/#sec-decodeuri-encodeduri
  406. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::decode_uri)
  407. {
  408. auto uri_string = TRY(vm.argument(0).to_string(vm));
  409. auto decoded = TRY(decode(vm, uri_string, ";/?:@&=+$,#"sv));
  410. return PrimitiveString::create(vm, move(decoded));
  411. }
  412. // 19.2.6.5 encodeURIComponent ( uriComponent ), https://tc39.es/ecma262/#sec-encodeuricomponent-uricomponent
  413. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::encode_uri_component)
  414. {
  415. auto uri_string = TRY(vm.argument(0).to_string(vm));
  416. auto encoded = TRY(encode(vm, uri_string, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()"sv));
  417. return PrimitiveString::create(vm, move(encoded));
  418. }
  419. // 19.2.6.3 decodeURIComponent ( encodedURIComponent ), https://tc39.es/ecma262/#sec-decodeuricomponent-encodeduricomponent
  420. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::decode_uri_component)
  421. {
  422. auto uri_string = TRY(vm.argument(0).to_string(vm));
  423. auto decoded = TRY(decode(vm, uri_string, ""sv));
  424. return PrimitiveString::create(vm, move(decoded));
  425. }
  426. // B.2.1.1 escape ( string ), https://tc39.es/ecma262/#sec-escape-string
  427. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::escape)
  428. {
  429. auto string = TRY(vm.argument(0).to_string(vm));
  430. StringBuilder escaped;
  431. for (auto code_point : TRY_OR_THROW_OOM(vm, utf8_to_utf16(string))) {
  432. if (code_point < 256) {
  433. if ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./"sv.contains(static_cast<char>(code_point)))
  434. escaped.append(code_point);
  435. else
  436. escaped.appendff("%{:02X}", code_point);
  437. continue;
  438. }
  439. escaped.appendff("%u{:04X}", code_point);
  440. }
  441. return PrimitiveString::create(vm, escaped.build());
  442. }
  443. // B.2.1.2 unescape ( string ), https://tc39.es/ecma262/#sec-unescape-string
  444. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::unescape)
  445. {
  446. auto string = TRY(vm.argument(0).to_string(vm));
  447. ssize_t length = string.length();
  448. StringBuilder unescaped(length);
  449. for (auto k = 0; k < length; ++k) {
  450. u32 code_point = string[k];
  451. if (code_point == '%') {
  452. if (k <= length - 6 && string[k + 1] == 'u' && is_ascii_hex_digit(string[k + 2]) && is_ascii_hex_digit(string[k + 3]) && is_ascii_hex_digit(string[k + 4]) && is_ascii_hex_digit(string[k + 5])) {
  453. code_point = (parse_ascii_hex_digit(string[k + 2]) << 12) | (parse_ascii_hex_digit(string[k + 3]) << 8) | (parse_ascii_hex_digit(string[k + 4]) << 4) | parse_ascii_hex_digit(string[k + 5]);
  454. k += 5;
  455. } else if (k <= length - 3 && is_ascii_hex_digit(string[k + 1]) && is_ascii_hex_digit(string[k + 2])) {
  456. code_point = (parse_ascii_hex_digit(string[k + 1]) << 4) | parse_ascii_hex_digit(string[k + 2]);
  457. k += 2;
  458. }
  459. }
  460. unescaped.append_code_point(code_point);
  461. }
  462. return PrimitiveString::create(vm, unescaped.build());
  463. }
  464. }