@@ -16,24 +16,24 @@ exports.is = value => {
1616exports . isImpl = value => {
1717 return utils . isObject ( value ) && value instanceof Impl . implementation ;
1818} ;
19- exports . convert = ( value , { context = "The provided value" } = { } ) => {
19+ exports . convert = ( globalObject , value , { context = "The provided value" } = { } ) => {
2020 if ( exports . is ( value ) ) {
2121 return utils . implForWrapper ( value ) ;
2222 }
23- throw new TypeError ( `${ context } is not of type 'TextDecoder'.` ) ;
23+ throw new globalObject . TypeError ( `${ context } is not of type 'TextDecoder'.` ) ;
2424} ;
2525
26- function makeWrapper ( globalObject ) {
27- if ( globalObject [ ctorRegistrySymbol ] === undefined ) {
28- throw new Error ( "Internal error: invalid global object" ) ;
26+ function makeWrapper ( globalObject , newTarget ) {
27+ let proto ;
28+ if ( newTarget !== undefined ) {
29+ proto = newTarget . prototype ;
2930 }
3031
31- const ctor = globalObject [ ctorRegistrySymbol ] [ "TextDecoder" ] ;
32- if ( ctor === undefined ) {
33- throw new Error ( "Internal error: constructor TextDecoder is not installed on the passed global object" ) ;
32+ if ( ! utils . isObject ( proto ) ) {
33+ proto = globalObject [ ctorRegistrySymbol ] [ "TextDecoder" ] . prototype ;
3434 }
3535
36- return Object . create ( ctor . prototype ) ;
36+ return Object . create ( proto ) ;
3737}
3838
3939exports . create = ( globalObject , constructorArgs , privateData ) => {
@@ -64,8 +64,8 @@ exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {})
6464 return wrapper ;
6565} ;
6666
67- exports . new = globalObject => {
68- const wrapper = makeWrapper ( globalObject ) ;
67+ exports . new = ( globalObject , newTarget ) => {
68+ const wrapper = makeWrapper ( globalObject , newTarget ) ;
6969
7070 exports . _internalSetup ( wrapper , globalObject ) ;
7171 Object . defineProperty ( wrapper , implSymbol , {
@@ -82,25 +82,32 @@ exports.new = globalObject => {
8282
8383const exposed = new Set ( [ "Window" , "Worker" ] ) ;
8484
85- exports . install = ( globalObject , globalNames = [ "Window" ] ) => {
85+ exports . install = ( globalObject , globalNames ) => {
8686 if ( ! globalNames . some ( globalName => exposed . has ( globalName ) ) ) {
8787 return ;
8888 }
89+
90+ const ctorRegistry = utils . initCtorRegistry ( globalObject ) ;
8991 class TextDecoder {
9092 constructor ( ) {
9193 const args = [ ] ;
9294 {
9395 let curArg = arguments [ 0 ] ;
9496 if ( curArg !== undefined ) {
95- curArg = conversions [ "DOMString" ] ( curArg , { context : "Failed to construct 'TextDecoder': parameter 1" } ) ;
97+ curArg = conversions [ "DOMString" ] ( curArg , {
98+ context : "Failed to construct 'TextDecoder': parameter 1" ,
99+ globals : globalObject
100+ } ) ;
96101 } else {
97102 curArg = "utf-8" ;
98103 }
99104 args . push ( curArg ) ;
100105 }
101106 {
102107 let curArg = arguments [ 1 ] ;
103- curArg = TextDecoderOptions . convert ( curArg , { context : "Failed to construct 'TextDecoder': parameter 2" } ) ;
108+ curArg = TextDecoderOptions . convert ( globalObject , curArg , {
109+ context : "Failed to construct 'TextDecoder': parameter 2"
110+ } ) ;
104111 args . push ( curArg ) ;
105112 }
106113 return exports . setup ( Object . create ( new . target . prototype ) , globalObject , args ) ;
@@ -109,7 +116,7 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
109116 decode ( ) {
110117 const esValue = this !== null && this !== undefined ? this : globalObject ;
111118 if ( ! exports . is ( esValue ) ) {
112- throw new TypeError ( "Illegal invocation " ) ;
119+ throw new globalObject . TypeError ( "'decode' called on an object that is not a valid instance of TextDecoder. " ) ;
113120 }
114121 const args = [ ] ;
115122 {
@@ -118,7 +125,7 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
118125 if ( utils . isArrayBuffer ( curArg ) ) {
119126 } else if ( ArrayBuffer . isView ( curArg ) ) {
120127 } else {
121- throw new TypeError (
128+ throw new globalObject . TypeError (
122129 "Failed to execute 'decode' on 'TextDecoder': parameter 1" + " is not of any supported type."
123130 ) ;
124131 }
@@ -127,7 +134,7 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
127134 }
128135 {
129136 let curArg = arguments [ 1 ] ;
130- curArg = TextDecodeOptions . convert ( curArg , {
137+ curArg = TextDecodeOptions . convert ( globalObject , curArg , {
131138 context : "Failed to execute 'decode' on 'TextDecoder': parameter 2"
132139 } ) ;
133140 args . push ( curArg ) ;
@@ -139,7 +146,9 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
139146 const esValue = this !== null && this !== undefined ? this : globalObject ;
140147
141148 if ( ! exports . is ( esValue ) ) {
142- throw new TypeError ( "Illegal invocation" ) ;
149+ throw new globalObject . TypeError (
150+ "'get encoding' called on an object that is not a valid instance of TextDecoder."
151+ ) ;
143152 }
144153
145154 return esValue [ implSymbol ] [ "encoding" ] ;
@@ -149,7 +158,9 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
149158 const esValue = this !== null && this !== undefined ? this : globalObject ;
150159
151160 if ( ! exports . is ( esValue ) ) {
152- throw new TypeError ( "Illegal invocation" ) ;
161+ throw new globalObject . TypeError (
162+ "'get fatal' called on an object that is not a valid instance of TextDecoder."
163+ ) ;
153164 }
154165
155166 return esValue [ implSymbol ] [ "fatal" ] ;
@@ -159,7 +170,9 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
159170 const esValue = this !== null && this !== undefined ? this : globalObject ;
160171
161172 if ( ! exports . is ( esValue ) ) {
162- throw new TypeError ( "Illegal invocation" ) ;
173+ throw new globalObject . TypeError (
174+ "'get ignoreBOM' called on an object that is not a valid instance of TextDecoder."
175+ ) ;
163176 }
164177
165178 return esValue [ implSymbol ] [ "ignoreBOM" ] ;
@@ -172,10 +185,7 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
172185 ignoreBOM : { enumerable : true } ,
173186 [ Symbol . toStringTag ] : { value : "TextDecoder" , configurable : true }
174187 } ) ;
175- if ( globalObject [ ctorRegistrySymbol ] === undefined ) {
176- globalObject [ ctorRegistrySymbol ] = Object . create ( null ) ;
177- }
178- globalObject [ ctorRegistrySymbol ] [ interfaceName ] = TextDecoder ;
188+ ctorRegistry [ interfaceName ] = TextDecoder ;
179189
180190 Object . defineProperty ( globalObject , interfaceName , {
181191 configurable : true ,
0 commit comments