mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 17:57:35 +00:00
LibJS: Add length property to ScriptFunction
This commit is contained in:
parent
cd3e2690eb
commit
4d931b524d
3 changed files with 37 additions and 0 deletions
|
@ -24,8 +24,10 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Function.h>
|
||||||
#include <LibJS/AST.h>
|
#include <LibJS/AST.h>
|
||||||
#include <LibJS/Interpreter.h>
|
#include <LibJS/Interpreter.h>
|
||||||
|
#include <LibJS/Runtime/Error.h>
|
||||||
#include <LibJS/Runtime/ScriptFunction.h>
|
#include <LibJS/Runtime/ScriptFunction.h>
|
||||||
#include <LibJS/Runtime/Value.h>
|
#include <LibJS/Runtime/Value.h>
|
||||||
|
|
||||||
|
@ -35,6 +37,7 @@ ScriptFunction::ScriptFunction(const ScopeNode& body, Vector<FlyString> paramete
|
||||||
: m_body(body)
|
: m_body(body)
|
||||||
, m_parameters(move(parameters))
|
, m_parameters(move(parameters))
|
||||||
{
|
{
|
||||||
|
put_native_property("length", length_getter, length_setter);
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptFunction::~ScriptFunction()
|
ScriptFunction::~ScriptFunction()
|
||||||
|
@ -60,4 +63,18 @@ Value ScriptFunction::construct(Interpreter& interpreter)
|
||||||
return call(interpreter);
|
return call(interpreter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value ScriptFunction::length_getter(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
|
||||||
|
if (!this_object)
|
||||||
|
return {};
|
||||||
|
if (!this_object->is_function())
|
||||||
|
return interpreter.throw_exception<Error>("TypeError", "Not a function");
|
||||||
|
return Value(static_cast<i32>(static_cast<const ScriptFunction*>(this_object)->parameters().size()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptFunction::length_setter(Interpreter&, Value)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,9 @@ private:
|
||||||
virtual bool is_script_function() const final { return true; }
|
virtual bool is_script_function() const final { return true; }
|
||||||
virtual const char* class_name() const override { return "ScriptFunction"; }
|
virtual const char* class_name() const override { return "ScriptFunction"; }
|
||||||
|
|
||||||
|
static Value length_getter(Interpreter&);
|
||||||
|
static void length_setter(Interpreter&, Value);
|
||||||
|
|
||||||
NonnullRefPtr<ScopeNode> m_body;
|
NonnullRefPtr<ScopeNode> m_body;
|
||||||
const Vector<FlyString> m_parameters;
|
const Vector<FlyString> m_parameters;
|
||||||
};
|
};
|
||||||
|
|
17
Libraries/LibJS/Tests/function-length.js
Normal file
17
Libraries/LibJS/Tests/function-length.js
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
function assert(x) { if (!x) throw 1; }
|
||||||
|
|
||||||
|
try {
|
||||||
|
function foo() { }
|
||||||
|
assert(foo.length === 0);
|
||||||
|
assert((foo.length = 5) === 5);
|
||||||
|
assert(foo.length === 0);
|
||||||
|
|
||||||
|
function bar(a, b, c) {}
|
||||||
|
assert(bar.length === 3);
|
||||||
|
assert((bar.length = 5) === 5);
|
||||||
|
assert(bar.length === 3);
|
||||||
|
|
||||||
|
console.log("PASS");
|
||||||
|
} catch (e) {
|
||||||
|
console.log("FAIL: " + e);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue