mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 10:01:13 +09:00
LibJS+js: Support getting last value from "_" variable
The interpreter now has an "underscore is last value" flag, which makes Interpreter::get_variable() return the last value if: - The m_underscore_is_last_value flag is enabled - The name of the variable lookup is "_" - The result of that lookup is an empty value That means "_" can still be used as a regular variable and will stop doing its magic once anything is assigned to it. Example REPL session: > 1 1 > _ + _ 2 > _ + _ 4 > _ = "foo" "foo" > 1 1 > _ "foo" > delete _ true > 1 1 > _ 1 >
This commit is contained in:
parent
89004a3a40
commit
5072d4e02d
Notes:
sideshowbarker
2024-07-19 05:44:54 +09:00
Author: https://github.com/linusg
Commit: 5072d4e02d
Pull-request: https://github.com/SerenityOS/serenity/pull/2529
3 changed files with 13 additions and 2 deletions
|
@ -70,7 +70,9 @@ Value Interpreter::run(const Statement& statement, ArgumentVector arguments, Sco
|
|||
auto& block = static_cast<const ScopeNode&>(statement);
|
||||
enter_scope(block, move(arguments), scope_type);
|
||||
|
||||
m_last_value = js_undefined();
|
||||
if (block.children().is_empty())
|
||||
m_last_value = js_undefined();
|
||||
|
||||
for (auto& node : block.children()) {
|
||||
m_last_value = node.execute(*this);
|
||||
if (should_unwind()) {
|
||||
|
@ -176,7 +178,10 @@ Value Interpreter::get_variable(const FlyString& name)
|
|||
return possible_match.value().value;
|
||||
}
|
||||
}
|
||||
return global_object().get(name);
|
||||
auto value = global_object().get(name);
|
||||
if (m_underscore_is_last_value && name == "_" && value.is_empty())
|
||||
return m_last_value;
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference Interpreter::get_reference(const FlyString& name)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue