diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index 73cfd2f921f..c864652c4fa 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -44,6 +44,7 @@ StringPrototype::StringPrototype() put_native_function("repeat", repeat, 1); put_native_function("startsWith", starts_with, 1); put_native_function("indexOf", index_of, 1); + put_native_function("toLowerCase", to_lowercase, 0); } StringPrototype::~StringPrototype() @@ -133,6 +134,26 @@ Value StringPrototype::index_of(Interpreter& interpreter) return Value((i32)(ptr - haystack.characters())); } +static StringObject* string_object_from(Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return nullptr; + if (!this_object->is_string_object()) { + interpreter.throw_exception("TypeError", "Not a String object"); + return nullptr; + } + return static_cast(this_object); +} + +Value StringPrototype::to_lowercase(Interpreter& interpreter) +{ + auto* string_object = string_object_from(interpreter); + if (!string_object) + return {}; + return js_string(interpreter, string_object->primitive_string()->string().to_lowercase()); +} + Value StringPrototype::length_getter(Interpreter& interpreter) { auto* this_object = interpreter.this_value().to_object(interpreter.heap()); diff --git a/Libraries/LibJS/Runtime/StringPrototype.h b/Libraries/LibJS/Runtime/StringPrototype.h index 3a2f749aa25..979530c5ddc 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.h +++ b/Libraries/LibJS/Runtime/StringPrototype.h @@ -42,6 +42,7 @@ private: static Value repeat(Interpreter&); static Value starts_with(Interpreter&); static Value index_of(Interpreter&); + static Value to_lowercase(Interpreter&); static Value length_getter(Interpreter&); }; diff --git a/Libraries/LibJS/Tests/String.prototype.toLowerCase.js b/Libraries/LibJS/Tests/String.prototype.toLowerCase.js new file mode 100644 index 00000000000..3ebc601e5ae --- /dev/null +++ b/Libraries/LibJS/Tests/String.prototype.toLowerCase.js @@ -0,0 +1,11 @@ +try { + assert("foo".toLowerCase() === "foo"); + assert("Foo".toLowerCase() === "foo"); + assert("FOO".toLowerCase() === "foo"); + + assert(('b' + 'a' + + 'a' + 'a').toLowerCase() === "banana"); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +}