1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:08:12 +00:00

LibJS: Rework how native functions are called to improve |this| value

Native functions now only get the Interpreter& as an argument. They can
then extract |this| along with any indexed arguments it wants from it.

This forces functions that want |this| to actually deal with calling
interpreter.this_value().to_object(), and dealing with the possibility
of a non-object |this|.

This is still not great but let's keep massaging it forward.
This commit is contained in:
Andreas Kling 2020-03-28 22:48:35 +01:00
parent c209ea1985
commit 7c4e53f31e
25 changed files with 145 additions and 102 deletions

View file

@ -27,6 +27,8 @@
#include <AK/FlyString.h>
#include <AK/String.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/StringObject.h>
@ -86,7 +88,7 @@ bool Value::to_boolean() const
}
}
Value Value::to_object(Heap& heap) const
Object* Value::to_object(Heap& heap) const
{
if (is_object())
return const_cast<Object*>(as_object());
@ -94,6 +96,11 @@ Value Value::to_object(Heap& heap) const
if (is_string())
return heap.allocate<StringObject>(m_value.as_string);
if (is_null() || is_undefined()) {
heap.interpreter().throw_exception<Error>("TypeError", "ToObject on null or undefined.");
return nullptr;
}
ASSERT_NOT_REACHED();
}