mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
js: Add a print() function to the environment
It's really annoying to write `console.log(JSON.stringify(something))` in scripts, and the output is less than easily readable. This exposes the existing `print(Value)` function into the JS world, and allows us to write `print(something)` and get a neat representation in the console.
This commit is contained in:
parent
3b0943d24c
commit
3b04693d7e
Notes:
sideshowbarker
2024-07-18 03:20:18 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/3b04693d7e Pull-request: https://github.com/SerenityOS/serenity/pull/12511
1 changed files with 16 additions and 0 deletions
|
@ -94,6 +94,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(save_to_file);
|
||||
JS_DECLARE_NATIVE_FUNCTION(load_json);
|
||||
JS_DECLARE_NATIVE_FUNCTION(last_value_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(print);
|
||||
};
|
||||
|
||||
class ScriptObject final : public JS::GlobalObject {
|
||||
|
@ -107,6 +108,7 @@ public:
|
|||
private:
|
||||
JS_DECLARE_NATIVE_FUNCTION(load_file);
|
||||
JS_DECLARE_NATIVE_FUNCTION(load_json);
|
||||
JS_DECLARE_NATIVE_FUNCTION(print);
|
||||
};
|
||||
|
||||
static bool s_dump_ast = false;
|
||||
|
@ -1171,6 +1173,7 @@ void ReplObject::initialize_global_object()
|
|||
define_native_function("load", load_file, 1, attr);
|
||||
define_native_function("save", save_to_file, 1, attr);
|
||||
define_native_function("loadJSON", load_json, 1, attr);
|
||||
define_native_function("print", print, 1, attr);
|
||||
|
||||
define_native_accessor(
|
||||
"_",
|
||||
|
@ -1230,6 +1233,12 @@ JS_DEFINE_NATIVE_FUNCTION(ReplObject::load_json)
|
|||
return load_json_impl(vm, global_object);
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(ReplObject::print)
|
||||
{
|
||||
::print(vm.argument(0));
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
void ScriptObject::initialize_global_object()
|
||||
{
|
||||
Base::initialize_global_object();
|
||||
|
@ -1237,6 +1246,7 @@ void ScriptObject::initialize_global_object()
|
|||
u8 attr = JS::Attribute::Configurable | JS::Attribute::Writable | JS::Attribute::Enumerable;
|
||||
define_native_function("load", load_file, 1, attr);
|
||||
define_native_function("loadJSON", load_json, 1, attr);
|
||||
define_native_function("print", print, 1, attr);
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(ScriptObject::load_file)
|
||||
|
@ -1249,6 +1259,12 @@ JS_DEFINE_NATIVE_FUNCTION(ScriptObject::load_json)
|
|||
return load_json_impl(vm, global_object);
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(ScriptObject::print)
|
||||
{
|
||||
::print(vm.argument(0));
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
static void repl(JS::Interpreter& interpreter)
|
||||
{
|
||||
while (!s_fail_repl) {
|
||||
|
|
Loading…
Add table
Reference in a new issue