@@ -16,24 +16,24 @@ exports.is = value => {
16
16
exports . isImpl = value => {
17
17
return utils . isObject ( value ) && value instanceof Impl . implementation ;
18
18
} ;
19
- exports . convert = ( value , { context = "The provided value" } = { } ) => {
19
+ exports . convert = ( globalObject , value , { context = "The provided value" } = { } ) => {
20
20
if ( exports . is ( value ) ) {
21
21
return utils . implForWrapper ( value ) ;
22
22
}
23
- throw new TypeError ( `${ context } is not of type 'TextDecoder'.` ) ;
23
+ throw new globalObject . TypeError ( `${ context } is not of type 'TextDecoder'.` ) ;
24
24
} ;
25
25
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 ;
29
30
}
30
31
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 ;
34
34
}
35
35
36
- return Object . create ( ctor . prototype ) ;
36
+ return Object . create ( proto ) ;
37
37
}
38
38
39
39
exports . create = ( globalObject , constructorArgs , privateData ) => {
@@ -64,8 +64,8 @@ exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {})
64
64
return wrapper ;
65
65
} ;
66
66
67
- exports . new = globalObject => {
68
- const wrapper = makeWrapper ( globalObject ) ;
67
+ exports . new = ( globalObject , newTarget ) => {
68
+ const wrapper = makeWrapper ( globalObject , newTarget ) ;
69
69
70
70
exports . _internalSetup ( wrapper , globalObject ) ;
71
71
Object . defineProperty ( wrapper , implSymbol , {
@@ -82,25 +82,32 @@ exports.new = globalObject => {
82
82
83
83
const exposed = new Set ( [ "Window" , "Worker" ] ) ;
84
84
85
- exports . install = ( globalObject , globalNames = [ "Window" ] ) => {
85
+ exports . install = ( globalObject , globalNames ) => {
86
86
if ( ! globalNames . some ( globalName => exposed . has ( globalName ) ) ) {
87
87
return ;
88
88
}
89
+
90
+ const ctorRegistry = utils . initCtorRegistry ( globalObject ) ;
89
91
class TextDecoder {
90
92
constructor ( ) {
91
93
const args = [ ] ;
92
94
{
93
95
let curArg = arguments [ 0 ] ;
94
96
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
+ } ) ;
96
101
} else {
97
102
curArg = "utf-8" ;
98
103
}
99
104
args . push ( curArg ) ;
100
105
}
101
106
{
102
107
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
+ } ) ;
104
111
args . push ( curArg ) ;
105
112
}
106
113
return exports . setup ( Object . create ( new . target . prototype ) , globalObject , args ) ;
@@ -109,7 +116,7 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
109
116
decode ( ) {
110
117
const esValue = this !== null && this !== undefined ? this : globalObject ;
111
118
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. " ) ;
113
120
}
114
121
const args = [ ] ;
115
122
{
@@ -118,7 +125,7 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
118
125
if ( utils . isArrayBuffer ( curArg ) ) {
119
126
} else if ( ArrayBuffer . isView ( curArg ) ) {
120
127
} else {
121
- throw new TypeError (
128
+ throw new globalObject . TypeError (
122
129
"Failed to execute 'decode' on 'TextDecoder': parameter 1" + " is not of any supported type."
123
130
) ;
124
131
}
@@ -127,7 +134,7 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
127
134
}
128
135
{
129
136
let curArg = arguments [ 1 ] ;
130
- curArg = TextDecodeOptions . convert ( curArg , {
137
+ curArg = TextDecodeOptions . convert ( globalObject , curArg , {
131
138
context : "Failed to execute 'decode' on 'TextDecoder': parameter 2"
132
139
} ) ;
133
140
args . push ( curArg ) ;
@@ -139,7 +146,9 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
139
146
const esValue = this !== null && this !== undefined ? this : globalObject ;
140
147
141
148
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
+ ) ;
143
152
}
144
153
145
154
return esValue [ implSymbol ] [ "encoding" ] ;
@@ -149,7 +158,9 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
149
158
const esValue = this !== null && this !== undefined ? this : globalObject ;
150
159
151
160
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
+ ) ;
153
164
}
154
165
155
166
return esValue [ implSymbol ] [ "fatal" ] ;
@@ -159,7 +170,9 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
159
170
const esValue = this !== null && this !== undefined ? this : globalObject ;
160
171
161
172
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
+ ) ;
163
176
}
164
177
165
178
return esValue [ implSymbol ] [ "ignoreBOM" ] ;
@@ -172,10 +185,7 @@ exports.install = (globalObject, globalNames = ["Window"]) => {
172
185
ignoreBOM : { enumerable : true } ,
173
186
[ Symbol . toStringTag ] : { value : "TextDecoder" , configurable : true }
174
187
} ) ;
175
- if ( globalObject [ ctorRegistrySymbol ] === undefined ) {
176
- globalObject [ ctorRegistrySymbol ] = Object . create ( null ) ;
177
- }
178
- globalObject [ ctorRegistrySymbol ] [ interfaceName ] = TextDecoder ;
188
+ ctorRegistry [ interfaceName ] = TextDecoder ;
179
189
180
190
Object . defineProperty ( globalObject , interfaceName , {
181
191
configurable : true ,
0 commit comments