summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-06-25 20:53:17 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-25 21:15:04 +0200
commit667bba2410edde2314a030a1dc1758cf08373246 (patch)
tree67e5f69d81f2e5d15cabc9ceb335c999b73d0c0c /Userland
parentb650d11dd323c602a22ef64acd4c8f55dac6804a (diff)
downloadserenity-667bba2410edde2314a030a1dc1758cf08373246.zip
LibJS: Add the Function.[[ThisMode]] field
This is not a behavioral change in itself, just prep work for future spec-compliance changes.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Function.h11
-rw-r--r--Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp7
2 files changed, 18 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Function.h b/Userland/Libraries/LibJS/Runtime/Function.h
index f8298515bb..997e5e2bde 100644
--- a/Userland/Libraries/LibJS/Runtime/Function.h
+++ b/Userland/Libraries/LibJS/Runtime/Function.h
@@ -47,6 +47,16 @@ public:
// Used as the outer environment when evaluating the code of the function.
virtual EnvironmentRecord* environment() { return nullptr; }
+ enum class ThisMode : u8 {
+ Lexical,
+ Strict,
+ Global,
+ };
+
+ // [[ThisMode]]
+ ThisMode this_mode() const { return m_this_mode; }
+ void set_this_mode(ThisMode this_mode) { m_this_mode = this_mode; }
+
protected:
virtual void visit_edges(Visitor&) override;
@@ -59,6 +69,7 @@ private:
Vector<Value> m_bound_arguments;
Value m_home_object;
ConstructorKind m_constructor_kind = ConstructorKind::Base;
+ ThisMode m_this_mode { ThisMode::Global };
};
}
diff --git a/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp
index 523b6cdbd3..633609d87b 100644
--- a/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp
@@ -61,6 +61,13 @@ ScriptFunction::ScriptFunction(GlobalObject& global_object, const FlyString& nam
, m_is_strict(is_strict)
, m_is_arrow_function(is_arrow_function)
{
+ // NOTE: This logic is from OrdinaryFunctionCreate, https://tc39.es/ecma262/#sec-ordinaryfunctioncreate
+ if (m_is_arrow_function)
+ set_this_mode(ThisMode::Lexical);
+ else if (m_is_strict)
+ set_this_mode(ThisMode::Strict);
+ else
+ set_this_mode(ThisMode::Global);
}
void ScriptFunction::initialize(GlobalObject& global_object)