3 const { addDebuggerToGlobal } = ChromeUtils.importESModule(
4 "resource://gre/modules/jsdebugger.sys.mjs"
7 const SYSTEM_PRINCIPAL = Cc["@mozilla.org/systemprincipal;1"].createInstance(
11 function addTestingFunctionsToGlobal(global) {
14 const testingFunctions = Cu.getJSTestingFunctions();
15 for (let k in testingFunctions) {
17 this[k] = testingFunctions[k];
24 if (!global.newGlobal) {
25 global.newGlobal = newGlobal;
27 if (!global.Debugger) {
28 addDebuggerToGlobal(global);
32 addTestingFunctionsToGlobal(this);
34 /* Create a new global, with all the JS shell testing functions. Similar to the
35 * newGlobal function exposed to JS shells, and useful for porting JS shell
36 * tests to xpcshell tests.
38 function newGlobal(args) {
39 const global = new Cu.Sandbox(SYSTEM_PRINCIPAL, {
40 freshCompartment: true,
43 addTestingFunctionsToGlobal(global);
47 add_task(function test_json_parse_with_source_xrays() {
48 let sandbox = new Cu.Sandbox("about:blank");
50 var sourceWrapper = Cu.evalInSandbox("JSON.parse('5.0', (k,v,{source}) => ({src: source, val: v}));", sandbox);
51 Assert.deepEqual(sourceWrapper, {src: "5.0", val: 5});
52 sandbox.reviver = (k,v,{source}) => { return { orig: source }};
53 sourceWrapper = Cu.evalInSandbox("JSON.parse('2.4', reviver);", sandbox);
54 Assert.deepEqual(sourceWrapper, { orig: "2.4"});
56 // Get rid of the extra global when experimental.json_parse_with_source pref is removed
57 var g = newGlobal({ newCompartment: true });
59 let other = new Cu.Sandbox("about:blank");
60 let rawWrapper = other.eval('JSON.rawJSON(4.32)');
62 Assert.ok(g.eval("JSON.isRawJSON(rawWrapper);"));
63 Assert.equal(Cu.evalInSandbox("JSON.stringify(rawWrapper)", g), "4.32");
65 // rawJSON is a data property, so the Xray should hide it
66 Assert.equal(g.eval("rawWrapper.wrappedJSObject.rawJSON"), "4.32");
67 Assert.equal(g.eval("rawWrapper.rawJSON"), undefined);
69 let src = Cu.evalInSandbox(`
70 other.eval('JSON.parse("4.32", (k,v,{source}) => { return {source,v}})');
72 Assert.ok(Cu.isXrayWrapper(src));
73 Assert.deepEqual(src, {source:"4.32", v:4.32});