TestInterfaceJS.sys.mjs (5103B)
1 /* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 /* global noSuchMethodExistsYo1, noSuchMethodExistsYo2, noSuchMethodExistsYo3 */ 6 7 export function TestInterfaceJS() {} 8 9 TestInterfaceJS.prototype = { 10 QueryInterface: ChromeUtils.generateQI([ 11 "nsIDOMGlobalPropertyInitializer", 12 "mozITestInterfaceJS", 13 ]), 14 15 init(win) { 16 this._win = win; 17 }, 18 19 __init(anyArg, objectArg, dictionaryArg) { 20 this._anyAttr = undefined; 21 this._objectAttr = null; 22 this._anyArg = anyArg; 23 this._objectArg = objectArg; 24 this._dictionaryArg = dictionaryArg; 25 }, 26 27 get anyArg() { 28 return this._anyArg; 29 }, 30 get objectArg() { 31 return this._objectArg; 32 }, 33 getDictionaryArg() { 34 return this._dictionaryArg; 35 }, 36 get anyAttr() { 37 return this._anyAttr; 38 }, 39 set anyAttr(val) { 40 this._anyAttr = val; 41 }, 42 get objectAttr() { 43 return this._objectAttr; 44 }, 45 set objectAttr(val) { 46 this._objectAttr = val; 47 }, 48 getDictionaryAttr() { 49 return this._dictionaryAttr; 50 }, 51 setDictionaryAttr(val) { 52 this._dictionaryAttr = val; 53 }, 54 pingPongAny(any) { 55 return any; 56 }, 57 pingPongObject(obj) { 58 return obj; 59 }, 60 pingPongObjectOrString(objectOrString) { 61 return objectOrString; 62 }, 63 pingPongDictionary(dict) { 64 return dict; 65 }, 66 pingPongDictionaryOrLong(dictOrLong) { 67 return dictOrLong.anyMember || dictOrLong; 68 }, 69 pingPongRecord(rec) { 70 return JSON.stringify(rec); 71 }, 72 objectSequenceLength(seq) { 73 return seq.length; 74 }, 75 anySequenceLength(seq) { 76 return seq.length; 77 }, 78 79 getCallerPrincipal() { 80 return Cu.getWebIDLCallerPrincipal().origin; 81 }, 82 83 convertSVS(svs) { 84 return svs; 85 }, 86 87 pingPongUnion(x) { 88 return x; 89 }, 90 pingPongUnionContainingNull(x) { 91 return x; 92 }, 93 pingPongNullableUnion(x) { 94 return x; 95 }, 96 returnBadUnion() { 97 return 3; 98 }, 99 100 testSequenceOverload() {}, 101 testSequenceUnion() {}, 102 103 testThrowError() { 104 throw new this._win.Error("We are an Error"); 105 }, 106 107 testThrowDOMException() { 108 throw new this._win.DOMException( 109 "We are a DOMException", 110 "NotSupportedError" 111 ); 112 }, 113 114 testThrowTypeError() { 115 throw new this._win.TypeError("We are a TypeError"); 116 }, 117 118 testThrowNsresult() { 119 // This is explicitly testing preservation of raw thrown Crs in XPCJS 120 // eslint-disable-next-line mozilla/no-throw-cr-literal 121 throw Cr.NS_BINDING_ABORTED; 122 }, 123 124 testThrowNsresultFromNative() { 125 // We want to throw an exception that we generate from an nsresult thrown 126 // by a C++ component. 127 Services.io.notImplemented(); 128 }, 129 130 testThrowCallbackError(callback) { 131 callback(); 132 }, 133 134 testThrowXraySelfHosted() { 135 this._win.Array.prototype.forEach(); 136 }, 137 138 testThrowSelfHosted() { 139 Array.prototype.forEach(); 140 }, 141 142 testPromiseWithThrowingChromePromiseInit() { 143 return new this._win.Promise(function () { 144 noSuchMethodExistsYo1(); 145 }); 146 }, 147 148 testPromiseWithThrowingContentPromiseInit(func) { 149 return new this._win.Promise(func); 150 }, 151 152 testPromiseWithDOMExceptionThrowingPromiseInit() { 153 return new this._win.Promise(() => { 154 throw new this._win.DOMException( 155 "We are a second DOMException", 156 "NotFoundError" 157 ); 158 }); 159 }, 160 161 testPromiseWithThrowingChromeThenFunction() { 162 return this._win.Promise.resolve(5).then(function () { 163 noSuchMethodExistsYo2(); 164 }); 165 }, 166 167 testPromiseWithThrowingContentThenFunction(func) { 168 return this._win.Promise.resolve(10).then(func); 169 }, 170 171 testPromiseWithDOMExceptionThrowingThenFunction() { 172 return this._win.Promise.resolve(5).then(() => { 173 throw new this._win.DOMException( 174 "We are a third DOMException", 175 "NetworkError" 176 ); 177 }); 178 }, 179 180 testPromiseWithThrowingChromeThenable() { 181 var thenable = { 182 then() { 183 noSuchMethodExistsYo3(); 184 }, 185 }; 186 return new this._win.Promise(function (resolve) { 187 resolve(thenable); 188 }); 189 }, 190 191 testPromiseWithThrowingContentThenable(thenable) { 192 // Waive Xrays on the thenable, because we're calling resolve() in the 193 // chrome compartment, so that's the compartment the "then" property get 194 // will happen in, and if we leave the Xray in place the function-valued 195 // property won't return the function. 196 return this._win.Promise.resolve(Cu.waiveXrays(thenable)); 197 }, 198 199 testPromiseWithDOMExceptionThrowingThenable() { 200 var thenable = { 201 then: () => { 202 throw new this._win.DOMException( 203 "We are a fourth DOMException", 204 "TypeMismatchError" 205 ); 206 }, 207 }; 208 return new this._win.Promise(function (resolve) { 209 resolve(thenable); 210 }); 211 }, 212 213 get onsomething() { 214 return this.__DOM_IMPL__.getEventHandler("onsomething"); 215 }, 216 217 set onsomething(val) { 218 this.__DOM_IMPL__.setEventHandler("onsomething", val); 219 }, 220 };