1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:47:44 +00: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:
Linus Groh 2020-06-08 12:08:35 +01:00 committed by Andreas Kling
parent 89004a3a40
commit 5072d4e02d
3 changed files with 13 additions and 2 deletions

View file

@ -177,6 +177,9 @@ public:
Value last_value() const { return m_last_value; }
bool underscore_is_last_value() const { return m_underscore_is_last_value; }
void set_underscore_is_last_value(bool b) { m_underscore_is_last_value = b; }
Console& console() { return m_console; }
const Console& console() const { return m_console; }
@ -199,6 +202,8 @@ private:
ScopeType m_unwind_until { ScopeType::None };
FlyString m_unwind_until_label;
bool m_underscore_is_last_value { false };
Console m_console;
};