瀏覽代碼

LibJS/Bytecode: Don't emit `GetGlobal undefined`

We know that `undefined` in the global scope is always the proper
undefined value. This commit takes advantage of that by simply emitting
a constant undefined value instead.

Unfortunately we can't be so sure in other scopes.
Andreas Kling 1 年之前
父節點
當前提交
caf2e675bf
共有 1 個文件被更改,包括 4 次插入0 次删除
  1. 4 0
      Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp

+ 4 - 0
Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp

@@ -382,6 +382,10 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> Identifier::generate_by
         return local;
     }
 
+    if (is_global() && m_string == "undefined"sv) {
+        return generator.add_constant(js_undefined());
+    }
+
     auto dst = choose_dst(generator, preferred_dst);
     if (is_global()) {
         generator.emit<Bytecode::Op::GetGlobal>(dst, generator.intern_identifier(m_string), generator.next_global_variable_cache());