Skip to content

Commit

Permalink
LibJS+LibWeb: Add a bunch of temporary execution context debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
networkException committed Sep 18, 2022
1 parent e486942 commit 1e8a758
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Userland/Libraries/LibJS/CyclicModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ ThrowCompletionOr<u32> CyclicModule::inner_module_linking(VM& vm, Vector<Module*
for (auto& required_string : m_requested_modules) {
ModuleRequest required { required_string };

dbgln("a. Let requiredModule be ? HostResolveImportedModule(module, required).");

// a. Let requiredModule be ? HostResolveImportedModule(module, required).
auto required_module = TRY(vm.host_resolve_imported_module(NonnullGCPtr<Module>(*this), required));

Expand Down Expand Up @@ -311,6 +313,7 @@ ThrowCompletionOr<u32> CyclicModule::inner_module_evaluation(VM& vm, Vector<Modu

// 11. For each String required of module.[[RequestedModules]], do
for (auto& required : m_requested_modules) {
dbgln("Let requiredModule be ! HostResolveImportedModule(module, required).");

// a. Let requiredModule be ! HostResolveImportedModule(module, required).
auto* required_module = MUST(vm.host_resolve_imported_module(NonnullGCPtr<Module>(*this), required)).ptr();
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibJS/Runtime/Realm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Realm* Realm::create(VM& vm)
// 1. Let realmRec be a new Realm Record.
auto* realm = vm.heap().allocate_without_realm<Realm>();

dbgln("CreateRealm {}", realm);
// VERIFY_NOT_REACHED();

// 2. Perform CreateIntrinsics(realmRec).
Intrinsics::create(*realm);

Expand Down
15 changes: 13 additions & 2 deletions Userland/Libraries/LibJS/Runtime/Realm.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,19 @@ class Realm final
m_intrinsics = &intrinsics;
}

HostDefined* host_defined() { return m_host_defined; }
void set_host_defined(OwnPtr<HostDefined> host_defined) { m_host_defined = move(host_defined); }
HostDefined* host_defined()
{
dbgln("(realm: {}).host_defined(): {}", this, m_host_defined.ptr());

return m_host_defined;
}

void set_host_defined(OwnPtr<HostDefined> host_defined)
{
dbgln("(realm: {}).set_host_defined(host_defined: {})", this, host_defined.ptr());

m_host_defined = move(host_defined);
}

private:
Realm() = default;
Expand Down
27 changes: 24 additions & 3 deletions Userland/Libraries/LibJS/Runtime/VM.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class VM : public RefCounted<VM> {

void push_execution_context(ExecutionContext& context)
{
dbgln("(vm: {}).push_execution_context(context: {})", this, &context);
dbgln("(vm: {}).push_execution_context(context.realm: {})", this, context.realm);

m_execution_context_stack.append(&context);
}

Expand All @@ -100,6 +103,9 @@ class VM : public RefCounted<VM> {

ThrowCompletionOr<void> push_execution_context(ExecutionContext& context, CheckStackSpaceLimitTag)
{
dbgln("(vm: {}).push_execution_context(context: {}, CheckStackSpaceLimitTag)", this, &context);
dbgln("(vm: {}).push_execution_context(context.realm: {}, CheckStackSpaceLimitTag)", this, context.realm);

// Ensure we got some stack space left, so the next function call doesn't kill us.
if (did_reach_stack_space_limit())
return throw_completion<InternalError>(ErrorType::CallStackSizeExceeded);
Expand All @@ -109,7 +115,11 @@ class VM : public RefCounted<VM> {

void pop_execution_context()
{
m_execution_context_stack.take_last();
auto* taken = m_execution_context_stack.take_last();

dbgln("pop_execution_context(taken: {})", taken);
dbgln("pop_execution_context(taken.realm: {})", taken->realm);

if (m_execution_context_stack.is_empty() && on_call_stack_emptied)
on_call_stack_emptied();
}
Expand All @@ -127,8 +137,19 @@ class VM : public RefCounted<VM> {

// https://tc39.es/ecma262/#current-realm
// The value of the Realm component of the running execution context is also called the current Realm Record.
Realm const* current_realm() const { return running_execution_context().realm; }
Realm* current_realm() { return running_execution_context().realm; }
Realm const* current_realm() const
{
dbgln("VM.h: (vm: {}).current_realm() const: running_execution_context({}).realm({})", this, &running_execution_context(), running_execution_context().realm);

return running_execution_context().realm;
}

Realm* current_realm()
{
dbgln("VM.h: (vm: {}).current_realm(): running_execution_context({}).realm({})", this, &running_execution_context(), running_execution_context().realm);

return running_execution_context().realm;
}

// https://tc39.es/ecma262/#active-function-object
// The value of the Function component of the running execution context is also called the active function object.
Expand Down
12 changes: 12 additions & 0 deletions Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,23 @@ NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM& vm, Fu
// 2. Let realm execution context be the running JavaScript execution context.
auto realm_execution_context = MUST(JS::Realm::initialize_host_defined_realm(vm, move(create_global_object), move(create_global_this_value)));

dbgln("About to run vm.execution_context_stack().remove_first_matching(realm_execution_context: {}), current stack looks like this:", realm_execution_context.ptr());

for (auto* context : vm.execution_context_stack()) {
dbgln("context: {}", context);
}

// 3. Remove realm execution context from the JavaScript execution context stack.
vm.execution_context_stack().remove_first_matching([&realm_execution_context](auto* execution_context) {
return execution_context == realm_execution_context.ptr();
});

dbgln("Ran vm.execution_context_stack().remove_first_matching, current stack looks like this:");

for (auto* context : vm.execution_context_stack()) {
dbgln("context: {}", context);
}

// NO-OP: 4. Let realm be realm execution context's Realm component.
// NO-OP: 5. Set realm's agent to agent.

Expand Down
3 changes: 2 additions & 1 deletion Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ EnvironmentSettingsObject& current_settings_object()
auto& event_loop = HTML::main_thread_event_loop();
auto& vm = event_loop.vm();

dbgln("vm.current_realm()->host_defined() has value: {}", (bool) vm.current_realm()->host_defined());
dbgln("(vm: {}).current_realm(): {}", &vm, vm.current_realm());
dbgln("(vm: {}).current_realm()->host_defined() has value: {}", &vm, (bool) vm.current_realm()->host_defined());

// Then, the current settings object is the environment settings object of the current Realm Record.
return verify_cast<EnvironmentSettingsObject>(*vm.current_realm()->host_defined());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void WindowEnvironmentSettingsObject::setup(AK::URL const& creation_url, Nonnull
settings_object->top_level_creation_url = top_level_creation_url;
settings_object->top_level_origin = top_level_origin;

dbgln("7. Set realm's [[HostDefined]] field to settings object.");
dbgln("WindowEnvironmentSettingsObject::setup calling (realm: {}).set_host_defined(settings_object: {})", realm, settings_object);

// 7. Set realm's [[HostDefined]] field to settings object.
realm->set_host_defined(move(settings_object));
Expand Down

0 comments on commit 1e8a758

Please sign in to comment.