diff options
author | Jack Karamanian <karamanian.jack@gmail.com> | 2020-05-30 01:07:02 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-30 10:33:24 +0200 |
commit | f4129ac422a2ed7795d1b3f7a368277aed4b4754 (patch) | |
tree | 744b3559b6e31e37f49a1ce077bbc012da9200eb /Libraries | |
parent | 3ffb0a4e87366b128e6d7ad749bd7083f7016975 (diff) | |
download | serenity-f4129ac422a2ed7795d1b3f7a368277aed4b4754.zip |
LibJS: Use the function's bound |this| and bound arguments in
Interpreter::call()
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Interpreter.cpp | 5 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Function.prototype.bind.js | 11 |
2 files changed, 14 insertions, 2 deletions
diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index 28b52900f6..d7b3a51fa5 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -207,9 +207,10 @@ Value Interpreter::call(Function& function, Value this_value, Optional<MarkedVal { auto& call_frame = push_call_frame(); call_frame.function_name = function.name(); - call_frame.this_value = this_value; + call_frame.this_value = function.bound_this().value_or(this_value); + call_frame.arguments = function.bound_arguments(); if (arguments.has_value()) - call_frame.arguments = arguments.value().values(); + call_frame.arguments.append(arguments.value().values()); call_frame.environment = function.create_environment(); auto result = function.call(*this); pop_call_frame(); diff --git a/Libraries/LibJS/Tests/Function.prototype.bind.js b/Libraries/LibJS/Tests/Function.prototype.bind.js index c6df364c15..34df409e21 100644 --- a/Libraries/LibJS/Tests/Function.prototype.bind.js +++ b/Libraries/LibJS/Tests/Function.prototype.bind.js @@ -11,6 +11,17 @@ try { } assert(getB.bind("bar")() === "B"); + // Bound functions should work with array functions + var Make3 = Number.bind(null, 3); + assert([55].map(Make3)[0] === 3); + + var MakeTrue = Boolean.bind(null, true); + assert([1, 2, 3].filter(MakeTrue).length === 3); + + assert([1, 2, 3].reduce((function (acc, x) { return acc + x }).bind(null, 4, 5)) === 9); + + assert([1, 2, 3].reduce((function (acc, x) { return acc + x + this; }).bind(3)) === 12); + function sum(a, b, c) { return a + b + c; } |