mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 14:38:11 +00:00
LibJS: Hoist function declarations
This patch adds function declaration hoisting. The mechanism is similar to var hoisting. Hoisted function declarations are to be put before the hoisted var declarations, hence they have to be treated separately.
This commit is contained in:
parent
e162b59a5e
commit
2579d0bf55
6 changed files with 73 additions and 15 deletions
37
Libraries/LibJS/Tests/function-hoisting.js
Normal file
37
Libraries/LibJS/Tests/function-hoisting.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var callHoisted = hoisted();
|
||||
function hoisted() {
|
||||
return true;
|
||||
}
|
||||
assert(hoisted() === true);
|
||||
assert(callHoisted === true);
|
||||
|
||||
{
|
||||
var callScopedHoisted = scopedHoisted();
|
||||
function scopedHoisted() {
|
||||
return "foo";
|
||||
}
|
||||
assert(scopedHoisted() === "foo");
|
||||
assert(callScopedHoisted === "foo");
|
||||
}
|
||||
assert(scopedHoisted() === "foo");
|
||||
assert(callScopedHoisted === "foo");
|
||||
|
||||
const test = () => {
|
||||
var iife = (function () {
|
||||
return declaredLater();
|
||||
})();
|
||||
function declaredLater() {
|
||||
return "yay";
|
||||
}
|
||||
return iife;
|
||||
};
|
||||
assert(typeof declaredLater === "undefined");
|
||||
assert(test() === "yay");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue