From 474252324ef26e53b22958a69307c9bb2645793e Mon Sep 17 00:00:00 2001 From: Ryan Fickenscher Date: Mon, 15 Jun 2020 14:47:08 -0700 Subject: [PATCH] Added `globalThis` property (#495) * added builtin globalThis * forgot to initialize globalThis in the builtin mod.rs file * changed to to match naming conventions and fixed issue with suggested edits to globalThis' initial value * updated the test for the property as suggested --- boa/src/builtins/global_this/mod.rs | 11 +++++++++++ boa/src/builtins/global_this/tests.rs | 10 ++++++++++ boa/src/builtins/mod.rs | 2 ++ 3 files changed, 23 insertions(+) create mode 100644 boa/src/builtins/global_this/mod.rs create mode 100644 boa/src/builtins/global_this/tests.rs diff --git a/boa/src/builtins/global_this/mod.rs b/boa/src/builtins/global_this/mod.rs new file mode 100644 index 0000000000..c668313332 --- /dev/null +++ b/boa/src/builtins/global_this/mod.rs @@ -0,0 +1,11 @@ +#[cfg(test)] +mod tests; + +use crate::{builtins::value::Value, BoaProfiler}; + +/// Initialize the `globalThis` property on the global object. +#[inline] +pub fn init(global: &Value) { + let _timer = BoaProfiler::global().start_event("globalThis", "init"); + global.set_field("globalThis", global.clone()); +} diff --git a/boa/src/builtins/global_this/tests.rs b/boa/src/builtins/global_this/tests.rs new file mode 100644 index 0000000000..7dbac74d0c --- /dev/null +++ b/boa/src/builtins/global_this/tests.rs @@ -0,0 +1,10 @@ +use crate::exec; + +#[test] +fn global_this_exists_on_global_object_and_evaluates_to_an_object() { + let scenario = r#" + typeof globalThis; + "#; + + assert_eq!(&exec(scenario), "object"); +} diff --git a/boa/src/builtins/mod.rs b/boa/src/builtins/mod.rs index 4e43b5268f..d61d9c6909 100644 --- a/boa/src/builtins/mod.rs +++ b/boa/src/builtins/mod.rs @@ -6,6 +6,7 @@ pub mod boolean; pub mod console; pub mod error; pub mod function; +pub mod global_this; pub mod json; pub mod math; pub mod nan; @@ -35,6 +36,7 @@ pub fn init(global: &Value) { Array::init(global); BigInt::init(global); Boolean::init(global); + global_this::init(global); json::init(global); math::init(global); nan::init(global);