mirror of
https://github.com/RGBCube/serenity
synced 2025-05-23 09:25:11 +00:00
LibJS: Add String.prototype.toString()
This commit is contained in:
parent
6f3ea75569
commit
32c27afdf0
3 changed files with 22 additions and 5 deletions
|
@ -48,6 +48,7 @@ StringPrototype::StringPrototype()
|
||||||
put_native_function("indexOf", index_of, 1);
|
put_native_function("indexOf", index_of, 1);
|
||||||
put_native_function("toLowerCase", to_lowercase, 0);
|
put_native_function("toLowerCase", to_lowercase, 0);
|
||||||
put_native_function("toUpperCase", to_uppercase, 0);
|
put_native_function("toUpperCase", to_uppercase, 0);
|
||||||
|
put_native_function("toString", to_string, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
StringPrototype::~StringPrototype()
|
StringPrototype::~StringPrototype()
|
||||||
|
@ -167,12 +168,18 @@ Value StringPrototype::to_uppercase(Interpreter& interpreter)
|
||||||
|
|
||||||
Value StringPrototype::length_getter(Interpreter& interpreter)
|
Value StringPrototype::length_getter(Interpreter& interpreter)
|
||||||
{
|
{
|
||||||
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
|
auto* string_object = string_object_from(interpreter);
|
||||||
if (!this_object)
|
if (!string_object)
|
||||||
return {};
|
return {};
|
||||||
if (!this_object->is_string_object())
|
return Value((i32) string_object->primitive_string()->string().length());
|
||||||
return interpreter.throw_exception<TypeError>("Not a String object");
|
}
|
||||||
return Value((i32) static_cast<const StringObject*>(this_object)->primitive_string()->string().length());
|
|
||||||
|
Value StringPrototype::to_string(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
auto* string_object = string_object_from(interpreter);
|
||||||
|
if (!string_object)
|
||||||
|
return {};
|
||||||
|
return js_string(interpreter, string_object->primitive_string()->string());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,7 @@ private:
|
||||||
static Value index_of(Interpreter&);
|
static Value index_of(Interpreter&);
|
||||||
static Value to_lowercase(Interpreter&);
|
static Value to_lowercase(Interpreter&);
|
||||||
static Value to_uppercase(Interpreter&);
|
static Value to_uppercase(Interpreter&);
|
||||||
|
static Value to_string(Interpreter&);
|
||||||
|
|
||||||
static Value length_getter(Interpreter&);
|
static Value length_getter(Interpreter&);
|
||||||
};
|
};
|
||||||
|
|
9
Libraries/LibJS/Tests/String.prototype.toString.js
Normal file
9
Libraries/LibJS/Tests/String.prototype.toString.js
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
try {
|
||||||
|
assert(String.prototype.toString.length === 0)
|
||||||
|
assert("".toString() === "");
|
||||||
|
assert("hello friends".toString() === "hello friends");
|
||||||
|
|
||||||
|
console.log("PASS");
|
||||||
|
} catch (e) {
|
||||||
|
console.log("FAIL: " + e);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue