Skip to content

Commit c241586

Browse files
Jake ChampionJakeChampion
authored andcommitted
Throw TypeError instead of Error if constructor called as regular function.
Section 3.7.1 of the WebIDL (https://webidl.spec.whatwg.org/#interface-object) specifies in step 1.2 that if a WebIDL interface is called with `NewTarget` undefined (i.e. not as a constructor) then to throw a TypeError. Our implementation was throwing an Error and not a TypeError. This commit updates our implementation to throw a TypeError with the same message format that is used within Firefox.
1 parent 131d794 commit c241586

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Our own version of spidermonkey/js/friend/ErrorNumbers.msg
3+
* where we can add our own custom error messages for use within the runtime
4+
*/
5+
6+
/*
7+
* This is our JavaScript error message file.
8+
*
9+
* The format for each JS error message is:
10+
*
11+
* MSG_DEF(<SYMBOLIC_NAME>, <ARGUMENT_COUNT>, <EXCEPTION_NAME>,
12+
* <FORMAT_STRING>)
13+
*
14+
* where ;
15+
* <SYMBOLIC_NAME> is a legal C identifer that will be used in the
16+
* JS engine source.
17+
*
18+
* <ARGUMENT_COUNT> is an integer literal specifying the total number of
19+
* replaceable arguments in the following format string.
20+
*
21+
* <EXCEPTION_NAME> is an enum JSExnType value, defined in js/ErrorReport.h.
22+
*
23+
* <FORMAT_STRING> is a string literal, optionally containing sequences
24+
* {X} where X is an integer representing the argument number that will
25+
* be replaced with a string value when the error is reported.
26+
*
27+
* e.g.
28+
*
29+
* MSG_DEF(JSMSG_NOT_A_SUBSPECIES, 2, JSEXN_TYPEERROR,
30+
* "{0} is not a member of the {1} family")
31+
*
32+
* can be used:
33+
*
34+
* JS_ReportErrorNumberASCII(JSMSG_NOT_A_SUBSPECIES, "Rhino", "Monkey");
35+
*
36+
* to report:
37+
*
38+
* "TypeError: Rhino is not a member of the Monkey family"
39+
*/
40+
41+
// clang-format off
42+
MSG_DEF(JSMSG_NOT_AN_ERROR, 0, JSEXN_ERR, "<Error #0 is reserved>")
43+
MSG_DEF(JSMSG_BUILTIN_CTOR_NO_NEW, 1, JSEXN_TYPEERR, "calling a builtin {0} constructor without new is forbidden")
44+
//clang-format on

c-dependencies/js-compute-runtime/js-compute-builtins.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ using JS::MutableHandleValue;
5555

5656
using JS::PersistentRooted;
5757

58+
struct JSErrorFormatString;
59+
60+
enum JSErrNum {
61+
#define MSG_DEF(name, count, exception, format) name,
62+
#include "./error-numbers.msg"
63+
#undef MSG_DEF
64+
JSErrNum_Limit
65+
};
66+
67+
const JSErrorFormatString js_ErrorFormatString[JSErrNum_Limit] = {
68+
#define MSG_DEF(name, count, exception, format) {#name, format, count, exception},
69+
#include "./error-numbers.msg"
70+
#undef MSG_DEF
71+
};
72+
73+
const JSErrorFormatString *GetErrorMessage(void *userRef, unsigned errorNumber) {
74+
if (errorNumber > 0 && errorNumber < JSErrNum_Limit) {
75+
return &js_ErrorFormatString[errorNumber];
76+
}
77+
78+
return nullptr;
79+
}
80+
5881
enum class Mode { PreWizening, PostWizening };
5982

6083
Mode execution_mode = Mode::PreWizening;
@@ -175,7 +198,8 @@ inline bool ThrowIfNotConstructing(JSContext *cx, const CallArgs &args, const ch
175198
if (args.isConstructing()) {
176199
return true;
177200
}
178-
JS_ReportErrorASCII(cx, "Constructor %s requires 'new'", builtinName);
201+
202+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BUILTIN_CTOR_NO_NEW, builtinName);
179203
return false;
180204
}
181205

tests/wpt-harness/expectations/encoding/idlharness.any.js.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"status": 0
2525
},
2626
"TextDecoder interface: existence and properties of interface object": {
27-
"status": 1
27+
"status": 0
2828
},
2929
"TextDecoder interface object length": {
3030
"status": 0
@@ -75,7 +75,7 @@
7575
"status": 1
7676
},
7777
"TextEncoder interface: existence and properties of interface object": {
78-
"status": 1
78+
"status": 0
7979
},
8080
"TextEncoder interface object length": {
8181
"status": 0

tests/wpt-harness/expectations/fetch/api/request/request-error.any.js.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"status": 1
4949
},
5050
"Untitled": {
51-
"status": 1
51+
"status": 0
5252
},
5353
"Request should get its content-type from the init request": {
5454
"status": 0

0 commit comments

Comments
 (0)