Userland/js: Add global property to the global object + help fix

This adds a `global` property to the global object so that 
`global == this` when typed into the REPL.

This matches what Node.js does and helps with testing on existing 
JS code that uses `global` to detect its environment.
This commit is contained in:
Marcin Gasperowicz 2020-06-02 13:02:29 +02:00 committed by Andreas Kling
parent 59adccb987
commit a60a5bc70a
Notes: sideshowbarker 2024-07-19 05:53:55 +09:00

View File

@ -359,6 +359,7 @@ ReplObject::ReplObject()
void ReplObject::initialize()
{
GlobalObject::initialize();
define_property("global", this, JS::Attribute::Enumerable);
define_native_function("exit", exit_interpreter);
define_native_function("help", repl_help);
define_native_function("load", load_file, 1);
@ -396,7 +397,8 @@ JS::Value ReplObject::repl_help(JS::Interpreter&)
printf("REPL commands:\n");
printf(" exit(code): exit the REPL with specified code. Defaults to 0.\n");
printf(" help(): display this menu\n");
printf(" load(files): Accepts file names as params to load into running session. For example load(\"js/1.js\", \"js/2.js\", \"js/3.js\")\n");
printf(" load(files): accepts file names as params to load into running session. For example load(\"js/1.js\", \"js/2.js\", \"js/3.js\")\n");
printf(" save(file): accepts a file name, writes REPL input history to a file. For example: save(\"foo.txt\")\n");
return JS::js_undefined();
}