Skip to content

Commit 178f652

Browse files
committed
[MERGE #6005 @akroshg] Calling Promise's function as constructor should throw an error
Merge pull request #6005 from akroshg:promise Added appropriate attributes for that.
2 parents 28b007f + f74c5a9 commit 178f652

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/Runtime/Library/JavascriptLibrary.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6136,7 +6136,7 @@ namespace Js
61366136
{
61376137
Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
61386138

6139-
FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
6139+
FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint, FunctionInfo::Attributes::ErrorOnNew);
61406140
DynamicType* type = CreateDeferredPrototypeFunctionType(entryPoint);
61416141

61426142
return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseReactionTaskFunction, type, functionInfo, reaction, argument);
@@ -6146,7 +6146,7 @@ namespace Js
61466146
{
61476147
Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
61486148

6149-
FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
6149+
FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint, FunctionInfo::Attributes::ErrorOnNew);
61506150
DynamicType* type = CreateDeferredPrototypeFunctionType(entryPoint);
61516151

61526152
return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseResolveThenableTaskFunction, type, functionInfo, promise, thenable, thenFunction);
@@ -6169,7 +6169,7 @@ namespace Js
61696169
{
61706170
Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
61716171

6172-
FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
6172+
FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint, FunctionInfo::Attributes::ErrorOnNew);
61736173
DynamicType* type = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, entryPoint, GetDeferredAnonymousFunctionTypeHandler());
61746174

61756175
JavascriptPromiseThenFinallyFunction* function = RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseThenFinallyFunction, type, functionInfo, OnFinally, Constructor, shouldThrow);
@@ -6182,7 +6182,7 @@ namespace Js
61826182
{
61836183
Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
61846184

6185-
FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
6185+
FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint, FunctionInfo::Attributes::ErrorOnNew);
61866186
DynamicType* type = CreateDeferredPrototypeFunctionType(entryPoint);
61876187

61886188
return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseThunkFinallyFunction, type, functionInfo, value, shouldThrow);

test/Bugs/misc_bugs.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ var tests = [
210210
catch (e) {
211211
}
212212
}
213+
},
214+
{
215+
name: "calling promise's function as constructor should not be allowed",
216+
body: function () {
217+
var var_0 = new Promise(function () {});
218+
var var_1 = function () {};
219+
220+
var_0.then = function (a, b) {
221+
var_2 = b;
222+
};
223+
var_3 = Promise.prototype.finally.call(var_0, var_1);
224+
assert.throws(() => { new var_2([]).var_3(); },TypeError);
225+
}
213226
}
214227

215228
];

0 commit comments

Comments
 (0)