1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:17:35 +00:00

LibJS: Add String.prototype.split using the @@split methods on object

This commit is contained in:
davidot 2021-06-27 19:14:34 +02:00 committed by Linus Groh
parent 36668893a6
commit 7a3b057a20

View file

@ -575,23 +575,34 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::slice)
// 22.1.3.21 String.prototype.split ( separator, limit ), https://tc39.es/ecma262/#sec-string.prototype.split // 22.1.3.21 String.prototype.split ( separator, limit ), https://tc39.es/ecma262/#sec-string.prototype.split
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split) JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split)
{ {
// FIXME Implement the @@split part auto separator_argument = vm.argument(0);
auto limit_argument = vm.argument(1);
auto string = ak_string_from(vm, global_object); auto this_value = require_object_coercible(global_object, vm.this_value(global_object));
if (!string.has_value()) if (vm.exception())
return {}; return {};
if (!separator_argument.is_nullish()) {
auto splitter = separator_argument.get_method(global_object, *vm.well_known_symbol_split());
if (vm.exception())
return {};
if (splitter)
return vm.call(*splitter, separator_argument, this_value, limit_argument);
}
auto string = this_value.to_string(global_object);
auto* result = Array::create(global_object); auto* result = Array::create(global_object);
size_t result_len = 0; size_t result_len = 0;
auto limit = NumericLimits<u32>::max(); auto limit = NumericLimits<u32>::max();
if (!vm.argument(1).is_undefined()) { if (!vm.argument(1).is_undefined()) {
limit = vm.argument(1).to_u32(global_object); limit = limit_argument.to_u32(global_object);
if (vm.exception()) if (vm.exception())
return {}; return {};
} }
auto separator = vm.argument(0).to_string(global_object); auto separator = separator_argument.to_string(global_object);
if (vm.exception()) if (vm.exception())
return {}; return {};
@ -599,15 +610,15 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split)
return result; return result;
if (vm.argument(0).is_undefined()) { if (vm.argument(0).is_undefined()) {
result->define_property(0, js_string(vm, *string)); result->define_property(0, js_string(vm, string));
return result; return result;
} }
auto len = string->length(); auto len = string.length();
auto separator_len = separator.length(); auto separator_len = separator.length();
if (len == 0) { if (len == 0) {
if (separator_len > 0) if (separator_len > 0)
result->define_property(0, js_string(vm, *string)); result->define_property(0, js_string(vm, string));
return result; return result;
} }
@ -615,18 +626,18 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split)
auto pos = start; auto pos = start;
if (separator_len == 0) { if (separator_len == 0) {
for (pos = 0; pos < len; pos++) for (pos = 0; pos < len; pos++)
result->define_property(pos, js_string(vm, string->substring(pos, 1))); result->define_property(pos, js_string(vm, string.substring(pos, 1)));
return result; return result;
} }
while (pos != len) { while (pos != len) {
auto e = split_match(*string, pos, separator); auto e = split_match(string, pos, separator);
if (!e.has_value()) { if (!e.has_value()) {
pos += 1; pos += 1;
continue; continue;
} }
auto segment = string->substring_view(start, pos - start); auto segment = string.substring_view(start, pos - start);
result->define_property(result_len, js_string(vm, segment)); result->define_property(result_len, js_string(vm, segment));
result_len++; result_len++;
if (result_len == limit) if (result_len == limit)
@ -635,7 +646,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split)
pos = start; pos = start;
} }
auto rest = string->substring(start, len - start); auto rest = string.substring(start, len - start);
result->define_property(result_len, js_string(vm, rest)); result->define_property(result_len, js_string(vm, rest));
return result; return result;