Procházet zdrojové kódy

LibJS: Add globalThis

We already have "global" as a way to access the global object in js(1)
(both REPL and script mode). This replaces it with "globalThis", which
is available in all environments, not just js.
Linus Groh před 5 roky
rodič
revize
c06a6c67d5

+ 1 - 0
Libraries/LibJS/Runtime/GlobalObject.cpp

@@ -61,6 +61,7 @@ GlobalObject::GlobalObject()
     put("Infinity", js_infinity());
     put("Infinity", js_infinity());
     put("undefined", js_undefined());
     put("undefined", js_undefined());
 
 
+    put("globalThis", this);
     put("console", heap().allocate<ConsoleObject>());
     put("console", heap().allocate<ConsoleObject>());
     put("Math", heap().allocate<MathObject>());
     put("Math", heap().allocate<MathObject>());
 
 

+ 1 - 1
Libraries/LibJS/Tests/function-this-in-arguments.js

@@ -1,6 +1,6 @@
 try {
 try {
   assert(typeof this === "object");
   assert(typeof this === "object");
-  assert(this === global);
+  assert(this === globalThis);
 
 
   function Foo() {
   function Foo() {
     this.x = 5;
     this.x = 5;

+ 0 - 2
Userland/js.cpp

@@ -384,7 +384,6 @@ int main(int argc, char** argv)
     if (script_path == nullptr) {
     if (script_path == nullptr) {
         auto interpreter = JS::Interpreter::create<ReplObject>();
         auto interpreter = JS::Interpreter::create<ReplObject>();
         interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
         interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
-        interpreter->global_object().put("global", &interpreter->global_object());
         if (test_mode) {
         if (test_mode) {
             interpreter->global_object().put_native_function("assert", assert_impl);
             interpreter->global_object().put_native_function("assert", assert_impl);
         }
         }
@@ -515,7 +514,6 @@ int main(int argc, char** argv)
     } else {
     } else {
         auto interpreter = JS::Interpreter::create<JS::GlobalObject>();
         auto interpreter = JS::Interpreter::create<JS::GlobalObject>();
         interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
         interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
-        interpreter->global_object().put("global", &interpreter->global_object());
         if (test_mode) {
         if (test_mode) {
             interpreter->global_object().put_native_function("assert", assert_impl);
             interpreter->global_object().put_native_function("assert", assert_impl);
         }
         }