Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow type sharing for Object.create objects #3901

Merged
merged 1 commit into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/Runtime/Language/ModuleNamespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "Types/PropertyIndexRanges.h"
#include "Types/SimpleDictionaryPropertyDescriptor.h"
#include "Types/SimpleDictionaryTypeHandler.h"
#include "Types/NullTypeHandler.h"
#include "ModuleNamespace.h"
#include "ModuleNamespaceEnumerator.h"

Expand Down
24 changes: 21 additions & 3 deletions lib/Runtime/Library/JavascriptLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,9 @@ namespace Js
DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr, typeHandler, true, true);
}

SimplePathTypeHandler * typeHandler = SimplePathTypeHandler::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true);
nullPrototypeObjectType = DynamicType::New(scriptContext, TypeIds_Object, nullValue, nullptr, typeHandler, true, true);

// Initialize regex types
TypePath *const regexResultPath = TypePath::New(recycler);
regexResultPath->Add(BuiltInPropertyRecords::input);
Expand Down Expand Up @@ -6897,9 +6900,24 @@ namespace Js
DynamicObject* JavascriptLibrary::CreateObject(RecyclableObject* prototype, uint16 requestedInlineSlotCapacity)
{
Assert(JavascriptOperators::IsObjectOrNull(prototype));

DynamicType* dynamicType = CreateObjectType(prototype, requestedInlineSlotCapacity);
return DynamicObject::New(this->GetRecycler(), dynamicType);
DynamicType* type = nullptr;
// If requested capacity is 0, we can't shrink, so it is already fixed and we can reuse the cached types
// For other inline slot capacities, we might want to shrink so we can't use the cached types (whose slot capacities are fixed)
//
// REVIEW: Do we really need non-fixed inline slot capacity? The obvious downside is it prevents type sharing with the cached types
if (requestedInlineSlotCapacity == 0 && JavascriptOperators::IsNull(prototype))
{
type = GetNullPrototypeObjectType();
}
else if(requestedInlineSlotCapacity == 0 && prototype == GetObjectPrototype())
{
type = GetObjectType();
}
else
{
type = CreateObjectType(prototype, requestedInlineSlotCapacity);
}
return DynamicObject::New(this->GetRecycler(), type);
}

PropertyStringCacheMap* JavascriptLibrary::EnsurePropertyStringMap()
Expand Down
2 changes: 2 additions & 0 deletions lib/Runtime/Library/JavascriptLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ namespace Js
Field(DynamicType *) numberTypeDynamic;
Field(DynamicType *) objectTypes[PreInitializedObjectTypeCount];
Field(DynamicType *) objectHeaderInlinedTypes[PreInitializedObjectTypeCount];
Field(DynamicType *) nullPrototypeObjectType;
Field(DynamicType *) regexPrototypeType;
Field(DynamicType *) regexType;
Field(DynamicType *) regexResultType;
Expand Down Expand Up @@ -813,6 +814,7 @@ namespace Js
DynamicType * GetObjectLiteralType(uint16 requestedInlineSlotCapacity);
DynamicType * GetObjectHeaderInlinedLiteralType(uint16 requestedInlineSlotCapacity);
DynamicType * GetObjectType() const { return objectTypes[0]; }
DynamicType * GetNullPrototypeObjectType() const { return nullPrototypeObjectType; }
DynamicType * GetObjectHeaderInlinedType() const { return objectHeaderInlinedTypes[0]; }
StaticType * GetSymbolTypeStatic() const { return symbolTypeStatic; }
DynamicType * GetSymbolTypeDynamic() const { return symbolTypeDynamic; }
Expand Down
13 changes: 4 additions & 9 deletions lib/Runtime/Library/JavascriptObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeLibraryPch.h"
#include "Types/NullTypeHandler.h"

namespace Js
{
Expand Down Expand Up @@ -1603,7 +1602,6 @@ namespace Js

ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Recycler *recycler = scriptContext->GetRecycler();


CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_create)
Expand All @@ -1615,18 +1613,15 @@ namespace Js
JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NotObjectOrNull, _u("Object.create"));
}

TypeId typeId = JavascriptOperators::GetTypeId(args[1]);
if (typeId != TypeIds_Null && !JavascriptOperators::IsObjectType(typeId))
Var protoVar = args[1];
if (!JavascriptOperators::IsObjectOrNull(protoVar))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NotObjectOrNull, _u("Object.create"));
}

//Create a new DynamicType with first argument as prototype and non shared type
RecyclableObject *prototype = RecyclableObject::FromVar(args[1]);
DynamicType *objectType = DynamicType::New(scriptContext, TypeIds_Object, prototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), false);
RecyclableObject* protoObj = RecyclableObject::FromVar(protoVar);
DynamicObject* object = function->GetLibrary()->CreateObject(protoObj);

//Create a new Object using this type.
DynamicObject* object = DynamicObject::New(recycler, objectType);
JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
#if ENABLE_DEBUG_CONFIG_OPTIONS
if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
Expand Down