Bug 1842441 - Part 4: Add the documentation and testcase for conflicting names in bindings object and variables. r=jandem

Also fixed the wrong order of environment chain in E.1. direct eval case.

Depends on D183133

Differential Revision: https://phabricator.services.mozilla.com/D183134
This commit is contained in:
Tooru Fujisawa 2023-07-11 10:41:25 +00:00
Родитель ad1239d42e
Коммит dba24f36ef
4 изменённых файлов: 250 добавлений и 15 удалений

Просмотреть файл

@ -12,9 +12,13 @@ dbg.onEnterFrame = frame => {
const bindings = {
bindings_prop: 50,
bindings_prop_var: 61,
bindings_prop_lexical: 71,
bindings_prop_unqualified: 81,
};
const envs = JSON.parse(frame.evalWithBindings(`
const {envs, vars} = JSON.parse(frame.evalWithBindings(`
// Put direct eval to de-optimize lexical.
eval("");
var qualified = 10;
@ -22,6 +26,16 @@ unqualified = 20;
let lexical = 30;
this.prop = 40;
var bindings_prop_var = 60;
let bindings_prop_lexical = 70;
bindings_prop_unqualified = 80;
const vars = {
bindings_prop_var,
bindings_prop_lexical,
bindings_prop_unqualified,
};
const envs = [];
let env = getInnerMostEnvironmentObject();
while (env) {
@ -32,13 +46,34 @@ while (env) {
lexical: !!Object.getOwnPropertyDescriptor(env, "lexical"),
prop: !!Object.getOwnPropertyDescriptor(env, "prop"),
bindings_prop: !!Object.getOwnPropertyDescriptor(env, "bindings_prop"),
bindings_prop_var: !!Object.getOwnPropertyDescriptor(env, "bindings_prop_var"),
bindings_prop_var_value: Object.getOwnPropertyDescriptor(env, "bindings_prop_var")?.value,
bindings_prop_lexical: !!Object.getOwnPropertyDescriptor(env, "bindings_prop_lexical"),
bindings_prop_lexical_value: Object.getOwnPropertyDescriptor(env, "bindings_prop_lexical")?.value,
bindings_prop_unqualified: !!Object.getOwnPropertyDescriptor(env, "bindings_prop_unqualified"),
bindings_prop_unqualified_value: Object.getOwnPropertyDescriptor(env, "bindings_prop_unqualified")?.value,
});
env = getEnclosingEnvironmentObject(env);
}
JSON.stringify(envs);
JSON.stringify({envs, vars});
`, bindings).return);
assertEq(vars.bindings_prop_var, 60,
"qualified var should read the value set by the declaration");
assertEq(vars.bindings_prop_lexical, 70,
"lexical should read the value set by the declaration");
assertEq(vars.bindings_prop_unqualified, 80,
"unqualified name should read the value set by the assignment");
assertEq(bindings.bindings_prop_var, 61,
"the original bindings property must not be overwritten for var");
assertEq(bindings.bindings_prop_lexical, 71,
"the original bindings property must not be overwritten for lexical");
assertEq(bindings.bindings_prop_unqualified, 81,
"the original bindings property must not be overwritten for unqualified");
assertEq(envs.length, 5);
let i = 0, env;
@ -50,6 +85,12 @@ JSON.stringify(envs);
assertEq(env.lexical, true, "lexical must live in the BlockLexicalEnvironmentObject");
assertEq(env.prop, false);
assertEq(env.bindings_prop_var, false);
assertEq(env.bindings_prop_lexical, true,
"lexical must live in the BlockLexicalEnvironmentObject even if it conflicts with the bindings object property");
assertEq(env.bindings_prop_lexical_value, 70);
assertEq(env.bindings_prop_unqualified, false);
env = envs[i]; i++;
assertEq(env.type, "WithEnvironmentObject");
assertEq(env.qualified, false);
@ -58,6 +99,19 @@ JSON.stringify(envs);
assertEq(env.prop, false);
assertEq(env.bindings_prop, true, "bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_var, true,
"bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_var_value, 60,
"bindings property must be overwritten for var");
assertEq(env.bindings_prop_lexical, true,
"bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_lexical_value, 71,
"bindings property must not be overwritten for lexical");
assertEq(env.bindings_prop_unqualified, true,
"bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_unqualified_value, 80,
"bindings property must not be overwritten for unqualified");
env = envs[i]; i++;
assertEq(env.type, "[DebugProxy] CallObject");
assertEq(env.qualified, true, "qualified var must live in the CallObject");
@ -66,6 +120,13 @@ JSON.stringify(envs);
assertEq(env.prop, false);
assertEq(env.bindings_prop, false);
assertEq(env.bindings_prop_var, true,
"qualified var binding must be created in the CallObject even if it conflicts with the bindings object property");
assertEq(env.bindings_prop_var_value, undefined,
"qualified var value must not be set if it conflicts with the bindings object property");
assertEq(env.bindings_prop_lexical, false);
assertEq(env.bindings_prop_unqualified, false);
env = envs[i]; i++;
assertEq(env.type, "[DebugProxy] GlobalLexicalEnvironmentObject");
assertEq(env.qualified, false);
@ -74,6 +135,10 @@ JSON.stringify(envs);
assertEq(env.prop, false);
assertEq(env.bindings_prop, false);
assertEq(env.bindings_prop_var, false);
assertEq(env.bindings_prop_lexical, false);
assertEq(env.bindings_prop_unqualified, false);
env = envs[i]; i++;
assertEq(env.type, "*global*");
assertEq(env.qualified, false);
@ -81,6 +146,10 @@ JSON.stringify(envs);
assertEq(env.lexical, false);
assertEq(env.prop, true, "this property must live in the global");
assertEq(env.bindings_prop, false);
assertEq(env.bindings_prop_var, false);
assertEq(env.bindings_prop_lexical, false);
assertEq(env.bindings_prop_unqualified, false);
};
g.eval(`

Просмотреть файл

@ -12,14 +12,28 @@ dbg.onEnterFrame = frame => {
const bindings = {
bindings_prop: 50,
bindings_prop_var: 61,
bindings_prop_lexical: 71,
bindings_prop_unqualified: 81,
};
const envs = JSON.parse(frame.evalWithBindings(`
const {envs, vars} = JSON.parse(frame.evalWithBindings(`
var qualified = 10;
unqualified = 20;
let lexical = 30;
this.prop = 40;
var bindings_prop_var = 60;
let bindings_prop_lexical = 70;
bindings_prop_unqualified = 80;
const vars = {
bindings_prop_var,
bindings_prop_lexical,
bindings_prop_unqualified,
};
const envs = [];
let env = getInnerMostEnvironmentObject();
while (env) {
@ -30,18 +44,40 @@ while (env) {
lexical: !!Object.getOwnPropertyDescriptor(env, "lexical"),
prop: !!Object.getOwnPropertyDescriptor(env, "prop"),
bindings_prop: !!Object.getOwnPropertyDescriptor(env, "bindings_prop"),
bindings_prop_var: !!Object.getOwnPropertyDescriptor(env, "bindings_prop_var"),
bindings_prop_var_value: Object.getOwnPropertyDescriptor(env, "bindings_prop_var")?.value,
bindings_prop_lexical: !!Object.getOwnPropertyDescriptor(env, "bindings_prop_lexical"),
bindings_prop_lexical_value: Object.getOwnPropertyDescriptor(env, "bindings_prop_lexical")?.value,
bindings_prop_unqualified: !!Object.getOwnPropertyDescriptor(env, "bindings_prop_unqualified"),
bindings_prop_unqualified_value: Object.getOwnPropertyDescriptor(env, "bindings_prop_unqualified")?.value,
});
env = getEnclosingEnvironmentObject(env);
}
JSON.stringify(envs);
JSON.stringify({envs, vars});
`, bindings).return);
assertEq(vars.bindings_prop_var, 60,
"qualified var should read the value set by the declaration");
assertEq(vars.bindings_prop_lexical, 70,
"lexical should read the value set by the declaration");
assertEq(vars.bindings_prop_unqualified, 80,
"unqualified name should read the value set by the assignment");
assertEq(bindings.bindings_prop_var, 61,
"the original bindings property must not be overwritten for var");
assertEq(bindings.bindings_prop_lexical, 71,
"the original bindings property must not be overwritten for lexical");
assertEq(bindings.bindings_prop_unqualified, 81,
"the original bindings property must not be overwritten for unqualified");
assertEq(envs.length, 4);
let i = 0, env;
// NOTE: lexical is optimized and uses frame slot.
// NOTE: lexical is optimized and uses frame slot, even if it conflicts with
// bindings object properties.
env = envs[i]; i++;
assertEq(env.type, "WithEnvironmentObject");
@ -51,6 +87,19 @@ JSON.stringify(envs);
assertEq(env.prop, false);
assertEq(env.bindings_prop, true, "bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_var, true,
"bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_var_value, 60,
"bindings property must be overwritten for var");
assertEq(env.bindings_prop_lexical, true,
"bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_lexical_value, 71,
"bindings property must not be overwritten for lexical");
assertEq(env.bindings_prop_unqualified, true,
"bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_unqualified_value, 80,
"bindings property must be overwritten for unqualified");
env = envs[i]; i++;
assertEq(env.type, "[DebugProxy] CallObject");
assertEq(env.qualified, true, "qualified var must live in the CallObject");
@ -59,6 +108,13 @@ JSON.stringify(envs);
assertEq(env.prop, false);
assertEq(env.bindings_prop, false);
assertEq(env.bindings_prop_var, true,
"qualified var binding must be created in the CallObject even if it conflicts with the bindings object property");
assertEq(env.bindings_prop_var_value, undefined,
"qualified var value must not be set if it conflicts with the bindings object property");
assertEq(env.bindings_prop_lexical, false);
assertEq(env.bindings_prop_unqualified, false);
env = envs[i]; i++;
assertEq(env.type, "[DebugProxy] GlobalLexicalEnvironmentObject");
assertEq(env.qualified, false);
@ -67,6 +123,10 @@ JSON.stringify(envs);
assertEq(env.prop, false);
assertEq(env.bindings_prop, false);
assertEq(env.bindings_prop_var, false);
assertEq(env.bindings_prop_lexical, false);
assertEq(env.bindings_prop_unqualified, false);
env = envs[i]; i++;
assertEq(env.type, "*global*");
assertEq(env.qualified, false);
@ -74,6 +134,10 @@ JSON.stringify(envs);
assertEq(env.lexical, false);
assertEq(env.prop, true, "this property must live in the global");
assertEq(env.bindings_prop, false);
assertEq(env.bindings_prop_var, false);
assertEq(env.bindings_prop_lexical, false);
assertEq(env.bindings_prop_unqualified, false);
};
g.eval(`

Просмотреть файл

@ -7,14 +7,32 @@ const gw = dbg.addDebuggee(g);
const bindings = {
bindings_prop: 50,
bindings_prop_var: 61,
bindings_prop_lexical: 71,
bindings_prop_lexical2: 71,
bindings_prop_unqualified: 81,
};
const envs = JSON.parse(gw.executeInGlobalWithBindings(`
const {envs, vars} = JSON.parse(gw.executeInGlobalWithBindings(`
var qualified = 10;
unqualified = 20;
let lexical = 30;
this.prop = 40;
var bindings_prop_var = 60;
let bindings_prop_lexical = 70;
let bindings_prop_lexical2;
bindings_prop_lexical2 = 70;
bindings_prop_unqualified = 80;
const vars = {
bindings_prop_var,
bindings_prop_lexical,
bindings_prop_lexical2,
bindings_prop_unqualified,
};
const envs = [];
let env = getInnerMostEnvironmentObject();
while (env) {
@ -25,13 +43,40 @@ while (env) {
lexical: !!Object.getOwnPropertyDescriptor(env, "lexical"),
prop: !!Object.getOwnPropertyDescriptor(env, "prop"),
bindings_prop: !!Object.getOwnPropertyDescriptor(env, "bindings_prop"),
bindings_prop_var: !!Object.getOwnPropertyDescriptor(env, "bindings_prop_var"),
bindings_prop_var_value: Object.getOwnPropertyDescriptor(env, "bindings_prop_var")?.value,
bindings_prop_lexical: !!Object.getOwnPropertyDescriptor(env, "bindings_prop_lexical"),
bindings_prop_lexical_value: Object.getOwnPropertyDescriptor(env, "bindings_prop_lexical")?.value,
bindings_prop_lexical2: !!Object.getOwnPropertyDescriptor(env, "bindings_prop_lexical2"),
bindings_prop_lexical2_value: Object.getOwnPropertyDescriptor(env, "bindings_prop_lexical2")?.value,
bindings_prop_unqualified: !!Object.getOwnPropertyDescriptor(env, "bindings_prop_unqualified"),
bindings_prop_unqualified_value: Object.getOwnPropertyDescriptor(env, "bindings_prop_unqualified")?.value,
});
env = getEnclosingEnvironmentObject(env);
}
JSON.stringify(envs);
JSON.stringify({envs, vars});
`, bindings).return);
assertEq(vars.bindings_prop_var, 60,
"qualified var should read the value set by the declaration");
assertEq(vars.bindings_prop_lexical, 71,
"TODO: lexical variable cannot read the value set by the declaration, given the name lookup finds the WithEnvironmentObject first");
assertEq(vars.bindings_prop_lexical2, 70,
"lexical should read the value set by the assignment");
assertEq(vars.bindings_prop_unqualified, 80,
"unqualified name should read the value set by the assignment");
assertEq(bindings.bindings_prop_var, 61,
"the original bindings property must not be overwritten for var");
assertEq(bindings.bindings_prop_lexical, 71,
"the original bindings property must not be overwritten for lexical");
assertEq(bindings.bindings_prop_lexical2, 71,
"the original bindings property must not be overwritten for lexical");
assertEq(bindings.bindings_prop_unqualified, 81,
"the original bindings property must not be overwritten for unqualified");
assertEq(envs.length, 3);
let i = 0, env;
@ -44,6 +89,23 @@ assertEq(env.lexical, false);
assertEq(env.prop, false);
assertEq(env.bindings_prop, true, "bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_var, true,
"bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_var_value, 60,
"bindings property must be overwritten for var");
assertEq(env.bindings_prop_lexical, true,
"bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_lexical_value, 71,
"bindings property must not be overwritten for lexical declaration");
assertEq(env.bindings_prop_lexical2, true,
"bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_lexical2_value, 70,
"bindings property must be overwritten for assignment on lexical");
assertEq(env.bindings_prop_unqualified, true,
"bindings property must live in the with env for bindings");
assertEq(env.bindings_prop_unqualified_value, 80,
"bindings property must not be overwritten for unqualified");
env = envs[i]; i++;
assertEq(env.type, "GlobalLexicalEnvironmentObject");
assertEq(env.qualified, false);
@ -52,6 +114,16 @@ assertEq(env.lexical, true, "lexical must live in the GlobalLexicalEnvironmentOb
assertEq(env.prop, false);
assertEq(env.bindings_prop, false);
assertEq(env.bindings_prop_var, false);
assertEq(env.bindings_prop_lexical, true,
"lexical must live in the GlobalLexicalEnvironmentObject even if it conflicts with the bindings object property");
assertEq(env.bindings_prop_lexical_value, 70);
assertEq(env.bindings_prop_lexical2, true,
"lexical must live in the GlobalLexicalEnvironmentObject even if it conflicts with the bindings object property");
assertEq(env.bindings_prop_lexical2_value, undefined,
"lexical value must not be set by the assignment if it conflicts with the bindings object property");
assertEq(env.bindings_prop_unqualified, false);
env = envs[i]; i++;
assertEq(env.type, "*global*");
assertEq(env.qualified, true, "qualified var must live in the global");
@ -59,3 +131,12 @@ assertEq(env.unqualified, true, "unqualified name must live in the global");
assertEq(env.lexical, false);
assertEq(env.prop, true, "this property must live in the global");
assertEq(env.bindings_prop, false);
assertEq(env.bindings_prop_var, true,
"qualified var binding must be created in the global even if it conflicts with the bindings object property");
assertEq(env.bindings_prop_var_value, undefined,
"qualified var value must not be set if it conflicts with the bindings object property");
assertEq(env.bindings_prop_var_value, undefined);
assertEq(env.bindings_prop_lexical, false);
assertEq(env.bindings_prop_lexical2, false);
assertEq(env.bindings_prop_unqualified, false);

Просмотреть файл

@ -374,10 +374,14 @@ extern PropertyName* EnvironmentCoordinateNameSlow(JSScript* script,
* E.1. Debugger.Frame.prototype.evalWithBindings
*
* Debugger.Frame.prototype.evalWithBindings uses WithEnvironmentObject for
* given bindings, and the frame's enclosing scope
* given bindings, and the frame's enclosing scope.
*
* If qualified 'var's or unqualified names conflict with the bindings object's
* properties, they go to the WithEnvironmentObject.
*
* If the frame is function, it has the following env chain.
* lexical variables are optimized and uses frame slots:
* lexical variables are optimized and uses frame slots, regardless of the name
* conflicts with bindings:
*
* global (unqualified names)
* |
@ -385,7 +389,7 @@ extern PropertyName* EnvironmentCoordinateNameSlow(JSScript* script,
* |
* [DebugProxy] CallObject (qualified 'var's)
* |
* WithEnvironmentObject wrapping bindings
* WithEnvironmentObject wrapping bindings (conflicting 'var's and names)
*
* If the script has direct eval, BlockLexicalEnvironmentObject is created for
* it:
@ -396,9 +400,9 @@ extern PropertyName* EnvironmentCoordinateNameSlow(JSScript* script,
* |
* [DebugProxy] CallObject (qualified 'var's)
* |
* BlockLexicalEnvironmentObject (lexical)
* WithEnvironmentObject wrapping bindings (conflicting 'var's and names)
* |
* WithEnvironmentObject wrapping bindings
* BlockLexicalEnvironmentObject (lexical vars, and conflicting lexical vars)
*
* NOTE: Debugger.Frame.prototype.eval uses the frame's enclosing scope only,
* and it doesn't use any dynamic environment, but still uses
@ -407,13 +411,30 @@ extern PropertyName* EnvironmentCoordinateNameSlow(JSScript* script,
* E.2. Debugger.Object.prototype.executeInGlobalWithBindings
*
* Debugger.Object.prototype.executeInGlobalWithBindings uses
* WithEnvironmentObject for given bindings, and the object's global scope:
* WithEnvironmentObject for given bindings, and the object's global scope.
*
* If qualified 'var's or unqualified names conflict with the bindings object's
* properties, they go to the WithEnvironmentObject.
*
* global (qualified 'var's and unqualified names)
* |
* GlobalLexicalEnvironmentObject[this=global] (lexical)
* GlobalLexicalEnvironmentObject[this=global] (lexical vars and conflicting)
* |
* WithEnvironmentObject wrapping bindings
* WithEnvironmentObject wrapping bindings (conflicting 'var's and names)
*
* TODO:
* If lexical variable names conflict with the bindings object's
* properties, the write on them within declarations is done for the
* GlobalLexicalEnvironmentObject,
* but the write within assignments and the read on lexicals are done from the
* WithEnvironmentObject (bug 1841964).
*
* // bindings = { x: 10, y: 20 };
*
* let x = 11; // written to GlobalLexicalEnvironmentObject
* x; // read from WithEnvironmentObject
* y = 21; // written to WithEnvironmentObject
* y; // read from WithEnvironmentObject
*
* NOTE: Debugger.Object.prototype.executeInGlobal uses the object's global
* scope only, and it doesn't use any dynamic environment or