From dd02d84fffc47b312c05841b415c6cea8231bafd Mon Sep 17 00:00:00 2001 From: Tooru Fujisawa Date: Mon, 7 Aug 2023 12:12:24 +0000 Subject: [PATCH] Bug 1842701 - Part 4: Add tests. r=nbp Differential Revision: https://phabricator.services.mozilla.com/D184199 --- ...bject-executeInGlobalWithBindings-empty.js | 51 +++++++++ ...eInGlobalWithBindings-existing-bindings.js | 103 ++++++++++++++++++ ...executeInGlobalWithBindings-no-use-eval.js | 63 +++++++++++ ...ject-executeInGlobalWithBindings-no-use.js | 54 +++++++++ ...teInGlobalWithBindings-shadow-only-eval.js | 88 +++++++++++++++ ...executeInGlobalWithBindings-shadow-only.js | 86 +++++++++++++++ 6 files changed, 445 insertions(+) create mode 100644 js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-empty.js create mode 100644 js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-existing-bindings.js create mode 100644 js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-no-use-eval.js create mode 100644 js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-no-use.js create mode 100644 js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-shadow-only-eval.js create mode 100644 js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-shadow-only.js diff --git a/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-empty.js b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-empty.js new file mode 100644 index 000000000000..8e89d51dd71c --- /dev/null +++ b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-empty.js @@ -0,0 +1,51 @@ +// Verify the environment chain for Debugger.Object described in +// js/src/vm/EnvironmentObject.h. + +// Passing empty bindings shouldn't create WithEnvironmentObject. + +const g = newGlobal({newCompartment: true}); +const dbg = new Debugger(); +const gw = dbg.addDebuggee(g); + +const bindings = {}; + +const envs = JSON.parse(gw.executeInGlobalWithBindings(` +var qualified = 10; +unqualified = 20; +let lexical = 30; +this.prop = 40; + +const envs = []; +let env = getInnerMostEnvironmentObject(); +while (env) { + envs.push({ + type: getEnvironmentObjectType(env) || "*global*", + qualified: !!Object.getOwnPropertyDescriptor(env, "qualified"), + unqualified: !!Object.getOwnPropertyDescriptor(env, "unqualified"), + lexical: !!Object.getOwnPropertyDescriptor(env, "lexical"), + prop: !!Object.getOwnPropertyDescriptor(env, "prop"), + }); + + env = getEnclosingEnvironmentObject(env); +} +JSON.stringify(envs); +`, bindings).return); + +assertEq(envs.length, 2, + "WithEnvironmentObject shouldn't be created if there's no binding"); + +let i = 0, env; + +env = envs[i]; i++; +assertEq(env.type, "GlobalLexicalEnvironmentObject"); +assertEq(env.qualified, false); +assertEq(env.unqualified, false); +assertEq(env.lexical, true, "lexical must live in the GlobalLexicalEnvironmentObject"); +assertEq(env.prop, false); + +env = envs[i]; i++; +assertEq(env.type, "*global*"); +assertEq(env.qualified, true, "qualified var must live in the global"); +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"); diff --git a/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-existing-bindings.js b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-existing-bindings.js new file mode 100644 index 000000000000..f7f14108e178 --- /dev/null +++ b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-existing-bindings.js @@ -0,0 +1,103 @@ +// Verify the environment chain for Debugger.Object described in +// js/src/vm/EnvironmentObject.h. + +// WithEnvironmentObject shouldn't contain bindings conflicts with existing +// globals. + +const g = newGlobal({newCompartment: true}); +const dbg = new Debugger(); +const gw = dbg.addDebuggee(g); + +const bindings = { + bindings_prop: 50, + + bindings_prop_var: 61, + bindings_prop_lexical: 71, + bindings_prop_unqualified: 81, +}; + +gw.executeInGlobal(` +var bindings_prop_var = 60; +let bindings_prop_lexical = 70; +bindings_prop_unqualified = 80; +`); + +const {envs, vars} = JSON.parse(gw.executeInGlobalWithBindings(` +const vars = { + bindings_prop, + bindings_prop_var, + bindings_prop_lexical, + bindings_prop_unqualified, +}; + +const envs = []; +let env = getInnerMostEnvironmentObject(); +while (env) { + envs.push({ + type: getEnvironmentObjectType(env) || "*global*", + 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, vars}); +`, bindings).return); + +assertEq(vars.bindings_prop, 50, + "qualified var should read the value set by the declaration"); +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, 3); + +let i = 0, env; + +env = envs[i]; i++; +assertEq(env.type, "WithEnvironmentObject"); +assertEq(env.bindings_prop, true, "bindings property must live in the with env for bindings"); + +assertEq(env.bindings_prop_var, false, + "bindings property must not live in the with env for bindings if it conflicts with existing global"); +assertEq(env.bindings_prop_lexical, false, + "bindings property must not live in the with env for bindings if it conflicts with existing global"); +assertEq(env.bindings_prop_unqualified, false, + "bindings property must not live in the with env for bindings if it conflicts with existing global"); +assertEq(env.bindings_prop_unqualified, false, + "bindings property must not live in the with env for bindings if it conflicts with existing global"); + +env = envs[i]; i++; +assertEq(env.type, "GlobalLexicalEnvironmentObject"); +assertEq(env.bindings_prop, false); + +assertEq(env.bindings_prop_var, false); +assertEq(env.bindings_prop_lexical, true); +assertEq(env.bindings_prop_lexical_value, 70); +assertEq(env.bindings_prop_unqualified, false); + +env = envs[i]; i++; +assertEq(env.type, "*global*"); +assertEq(env.bindings_prop, false); + +assertEq(env.bindings_prop_var, true); +assertEq(env.bindings_prop_var_value, 60); +assertEq(env.bindings_prop_lexical, false); +assertEq(env.bindings_prop_unqualified, true); +assertEq(env.bindings_prop_unqualified_value, 80); diff --git a/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-no-use-eval.js b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-no-use-eval.js new file mode 100644 index 000000000000..4b004a5e35e0 --- /dev/null +++ b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-no-use-eval.js @@ -0,0 +1,63 @@ +// Verify the environment chain for Debugger.Object described in +// js/src/vm/EnvironmentObject.h. + +// WithEnvironmentObject should be created if the script has direct eval, +// even if no binding is used outside of direct eval. + +const g = newGlobal({newCompartment: true}); +const dbg = new Debugger(); +const gw = dbg.addDebuggee(g); + +const bindings = { + bindings_prop: 61, +}; + +const envs = JSON.parse(gw.executeInGlobalWithBindings(` +eval(''); +var qualified = 10; +unqualified = 20; +let lexical = 30; +this.prop = 40; + +const envs = []; +let env = getInnerMostEnvironmentObject(); +while (env) { + envs.push({ + type: getEnvironmentObjectType(env) || "*global*", + qualified: !!Object.getOwnPropertyDescriptor(env, "qualified"), + unqualified: !!Object.getOwnPropertyDescriptor(env, "unqualified"), + lexical: !!Object.getOwnPropertyDescriptor(env, "lexical"), + prop: !!Object.getOwnPropertyDescriptor(env, "prop"), + }); + + env = getEnclosingEnvironmentObject(env); +} +JSON.stringify(envs); +`, bindings).return); + +assertEq(envs.length, 3, + "WithEnvironmentObject should be created if the script has direct " + + "eval, even if no binding is used outside of direct eval"); + +let i = 0, env; + +env = envs[i]; i++; +assertEq(env.type, "WithEnvironmentObject"); +assertEq(env.qualified, false); +assertEq(env.unqualified, false); +assertEq(env.lexical, false); +assertEq(env.prop, false); + +env = envs[i]; i++; +assertEq(env.type, "GlobalLexicalEnvironmentObject"); +assertEq(env.qualified, false); +assertEq(env.unqualified, false); +assertEq(env.lexical, true, "lexical must live in the GlobalLexicalEnvironmentObject"); +assertEq(env.prop, false); + +env = envs[i]; i++; +assertEq(env.type, "*global*"); +assertEq(env.qualified, true, "qualified var must live in the global"); +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"); diff --git a/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-no-use.js b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-no-use.js new file mode 100644 index 000000000000..b71d906b5102 --- /dev/null +++ b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-no-use.js @@ -0,0 +1,54 @@ +// Verify the environment chain for Debugger.Object described in +// js/src/vm/EnvironmentObject.h. + +// WithEnvironmentObject shouldn't be created if no binding is used. + +const g = newGlobal({newCompartment: true}); +const dbg = new Debugger(); +const gw = dbg.addDebuggee(g); + +const bindings = { + bindings_prop: 61, +}; + +const envs = JSON.parse(gw.executeInGlobalWithBindings(` +var qualified = 10; +unqualified = 20; +let lexical = 30; +this.prop = 40; + +const envs = []; +let env = getInnerMostEnvironmentObject(); +while (env) { + envs.push({ + type: getEnvironmentObjectType(env) || "*global*", + qualified: !!Object.getOwnPropertyDescriptor(env, "qualified"), + unqualified: !!Object.getOwnPropertyDescriptor(env, "unqualified"), + lexical: !!Object.getOwnPropertyDescriptor(env, "lexical"), + prop: !!Object.getOwnPropertyDescriptor(env, "prop"), + }); + + env = getEnclosingEnvironmentObject(env); +} +JSON.stringify(envs); +`, bindings).return); + +assertEq(envs.length, 2, + "WithEnvironmentObject shouldn't be created if no binding is " + + "used"); + +let i = 0, env; + +env = envs[i]; i++; +assertEq(env.type, "GlobalLexicalEnvironmentObject"); +assertEq(env.qualified, false); +assertEq(env.unqualified, false); +assertEq(env.lexical, true, "lexical must live in the GlobalLexicalEnvironmentObject"); +assertEq(env.prop, false); + +env = envs[i]; i++; +assertEq(env.type, "*global*"); +assertEq(env.qualified, true, "qualified var must live in the global"); +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"); diff --git a/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-shadow-only-eval.js b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-shadow-only-eval.js new file mode 100644 index 000000000000..c89d19a04c87 --- /dev/null +++ b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-shadow-only-eval.js @@ -0,0 +1,88 @@ +// Verify the environment chain for Debugger.Object described in +// js/src/vm/EnvironmentObject.h. + +// WithEnvironmentObject shouldn't be created if if all bindings are shadowed, +// even if the script has direct eval. + +const g = newGlobal({newCompartment: true}); +const dbg = new Debugger(); +const gw = dbg.addDebuggee(g); + +const bindings = { + bindings_prop_var: 61, + bindings_prop_lexical: 71, + bindings_prop_lexical2: 71, +}; + +const {envs, vars} = JSON.parse(gw.executeInGlobalWithBindings(` +eval(''); +var bindings_prop_var = 60; +let bindings_prop_lexical = 70; +let bindings_prop_lexical2; +bindings_prop_lexical2 = 70; + +const vars = { + bindings_prop_var, + bindings_prop_lexical, + bindings_prop_lexical2, +}; + +const envs = []; +let env = getInnerMostEnvironmentObject(); +while (env) { + envs.push({ + type: getEnvironmentObjectType(env) || "*global*", + + 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, + }); + + env = getEnclosingEnvironmentObject(env); +} +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_lexical2, 70, + "lexical 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(envs.length, 2, + "WithEnvironmentObject shouldn't be created if if all bindings are " + + "shadowed, even if the script has direct eval"); + +let i = 0, env; + +env = envs[i]; i++; +assertEq(env.type, "GlobalLexicalEnvironmentObject"); +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, 70, + "lexical value must be set by the assignment even if it conflicts with the bindings object property"); + +env = envs[i]; i++; +assertEq(env.type, "*global*"); + +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, 60, + "qualified var value must be set even if it conflicts with the bindings object property"); +assertEq(env.bindings_prop_lexical, false); +assertEq(env.bindings_prop_lexical2, false); diff --git a/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-shadow-only.js b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-shadow-only.js new file mode 100644 index 000000000000..5291396bb149 --- /dev/null +++ b/js/src/jit-test/tests/debug/envChain_object-executeInGlobalWithBindings-shadow-only.js @@ -0,0 +1,86 @@ +// Verify the environment chain for Debugger.Object described in +// js/src/vm/EnvironmentObject.h. + +// WithEnvironmentObject shouldn't be created if all bindings are shadowed. + +const g = newGlobal({newCompartment: true}); +const dbg = new Debugger(); +const gw = dbg.addDebuggee(g); + +const bindings = { + bindings_prop_var: 61, + bindings_prop_lexical: 71, + bindings_prop_lexical2: 71, +}; + +const {envs, vars} = JSON.parse(gw.executeInGlobalWithBindings(` +var bindings_prop_var = 60; +let bindings_prop_lexical = 70; +let bindings_prop_lexical2; +bindings_prop_lexical2 = 70; + +const vars = { + bindings_prop_var, + bindings_prop_lexical, + bindings_prop_lexical2, +}; + +const envs = []; +let env = getInnerMostEnvironmentObject(); +while (env) { + envs.push({ + type: getEnvironmentObjectType(env) || "*global*", + + 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, + }); + + env = getEnclosingEnvironmentObject(env); +} +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_lexical2, 70, + "lexical 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(envs.length, 2, + "WithEnvironmentObject shouldn't be created if all bindings are " + + "shadowed"); + +let i = 0, env; + +env = envs[i]; i++; +assertEq(env.type, "GlobalLexicalEnvironmentObject"); +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, 70, + "lexical value must be set by the assignment even if it conflicts with the bindings object property"); + +env = envs[i]; i++; +assertEq(env.type, "*global*"); + +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, 60, + "qualified var value must be set even if it conflicts with the bindings object property"); +assertEq(env.bindings_prop_lexical, false); +assertEq(env.bindings_prop_lexical2, false);