Skip to content

Commit 096c84e

Browse files
committed
change initial type to prevent conversions when creating bound function
1 parent 08161b0 commit 096c84e

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

lib/Runtime/Library/JavascriptExternalFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace Js
5858

5959
bool __cdecl JavascriptExternalFunction::DeferredLengthInitializer(DynamicObject * instance, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
6060
{
61-
Js::JavascriptLibrary::InitializeFunction<true, true, true>(instance, typeHandler, mode);
61+
Js::JavascriptLibrary::InitializeFunction<true, true, true, true>(instance, typeHandler, mode);
6262

6363
JavascriptExternalFunction* object = static_cast<JavascriptExternalFunction*>(instance);
6464

lib/Runtime/Library/JavascriptLibrary.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ namespace Js
594594
Js::DeferredTypeHandler<Js::JavascriptExternalFunction::DeferredConstructorInitializer>::GetDefaultInstance(), true, true);
595595

596596
boundFunctionType = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, BoundFunction::NewInstance,
597-
GetDeferredFunctionTypeHandler(), true, true);
597+
GetDeferredFunctionWithLengthUnsetTypeHandler(), true, true);
598598
crossSiteDeferredFunctionType = CreateDeferredFunctionTypeNoProfileThunk(
599599
scriptContext->CurrentCrossSiteThunk, true /*isShared*/);
600600
crossSiteDeferredPrototypeFunctionType = CreateDeferredPrototypeFunctionTypeNoProfileThunk(
@@ -784,7 +784,7 @@ namespace Js
784784
JavascriptArray::EnsureCalculationOfAllocationBuckets<Js::JavascriptArray>();
785785
}
786786

787-
template<bool addPrototype, bool addName, bool useLengthType>
787+
template<bool addPrototype, bool addName, bool useLengthType, bool addLength>
788788
bool JavascriptLibrary::InitializeFunction(DynamicObject *instance, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
789789
{
790790
JavascriptFunction * function = VarTo<JavascriptFunction>(instance);
@@ -833,7 +833,8 @@ namespace Js
833833
{
834834
ParseableFunctionInfo * funcInfo = scriptFunction->GetFunctionProxy()->EnsureDeserialized();
835835

836-
if (useLengthType)
836+
CompileAssert(!addLength || useLengthType);
837+
if (addLength)
837838
{
838839
function->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(funcInfo->GetReportedInParamsCount() - 1), PropertyConfigurable, nullptr, PropertyOperation_None, SideEffects_None);
839840
}
@@ -914,10 +915,10 @@ namespace Js
914915
}
915916
};
916917

917-
template<bool isNameAvailable, bool isPrototypeAvailable, bool isLengthAvailable>
918+
template<bool isNameAvailable, bool isPrototypeAvailable, bool isLengthAvailable, bool addLength>
918919
DynamicTypeHandler * JavascriptLibrary::GetDeferredFunctionTypeHandlerBase()
919920
{
920-
return DeferredTypeHandler<InitializeFunction<isPrototypeAvailable, isNameAvailable, isLengthAvailable>, InitializeFunctionDeferredTypeHandlerFilter<isNameAvailable, isPrototypeAvailable, isLengthAvailable>>::GetDefaultInstance();
921+
return DeferredTypeHandler<InitializeFunction<isPrototypeAvailable, isNameAvailable, isLengthAvailable, addLength>, InitializeFunctionDeferredTypeHandlerFilter<isNameAvailable, isPrototypeAvailable, isLengthAvailable>>::GetDefaultInstance();
921922
}
922923

923924
template<bool isNameAvailable, bool isPrototypeAvailable>
@@ -993,6 +994,11 @@ namespace Js
993994
return GetDeferredFunctionTypeHandlerBase</*isNameAvailable*/ true, /*isPrototypeAvailable*/ false, /* isLengthAvailable */ true>();
994995
}
995996

997+
DynamicTypeHandler * JavascriptLibrary::GetDeferredFunctionWithLengthUnsetTypeHandler()
998+
{
999+
return GetDeferredFunctionTypeHandlerBase</*isNameAvailable*/ true, /*isPrototypeAvailable*/ false, /* isLengthAvailable */ true, /* addLength */ false>();
1000+
}
1001+
9961002
DynamicTypeHandler * JavascriptLibrary::ScriptFunctionTypeHandler(bool noPrototypeProperty, bool isAnonymousFunction)
9971003
{
9981004
DynamicTypeHandler * scriptFunctionTypeHandler = nullptr;

lib/Runtime/Library/JavascriptLibrary.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,11 +902,12 @@ namespace Js
902902

903903
DynamicTypeHandler * GetDeferredFunctionTypeHandler();
904904
DynamicTypeHandler * GetDeferredFunctionWithLengthTypeHandler();
905+
DynamicTypeHandler* GetDeferredFunctionWithLengthUnsetTypeHandler();
905906
DynamicTypeHandler * GetDeferredPrototypeFunctionWithNameAndLengthTypeHandler();
906907
DynamicTypeHandler * ScriptFunctionTypeHandler(bool noPrototypeProperty, bool isAnonymousFunction);
907908
DynamicTypeHandler * GetDeferredAnonymousFunctionWithLengthTypeHandler();
908909
DynamicTypeHandler * GetDeferredAnonymousFunctionTypeHandler();
909-
template<bool isNameAvailable, bool isPrototypeAvailable = true, bool isLengthAvailable = false>
910+
template<bool isNameAvailable, bool isPrototypeAvailable = true, bool isLengthAvailable = false, bool addLength = isLengthAvailable>
910911
static DynamicTypeHandler * GetDeferredFunctionTypeHandlerBase();
911912
template<bool isNameAvailable, bool isPrototypeAvailable = true>
912913
static DynamicTypeHandler * GetDeferredGeneratorFunctionTypeHandlerBase();
@@ -1236,7 +1237,7 @@ namespace Js
12361237
#endif
12371238

12381239
public:
1239-
template<bool addPrototype, bool addName, bool useLengthType>
1240+
template<bool addPrototype, bool addName, bool useLengthType, bool addLength>
12401241
static bool __cdecl InitializeFunction(DynamicObject* function, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode);
12411242
virtual void Finalize(bool isShutdown) override;
12421243

0 commit comments

Comments
 (0)