Skip to content

Commit

Permalink
Implement Object.fromEntries
Browse files Browse the repository at this point in the history
  • Loading branch information
rhuanjl committed Aug 23, 2018
1 parent a21da6b commit 4f03f21
Show file tree
Hide file tree
Showing 17 changed files with 7,705 additions and 7,002 deletions.
2 changes: 2 additions & 0 deletions lib/Runtime/Base/JnDirectFields.h
Expand Up @@ -156,6 +156,7 @@ ENTRY(freeze)
ENTRY(from)
ENTRY(fromCharCode)
ENTRY(fromCodePoint)
ENTRY(fromEntries)
ENTRY(function)
ENTRY(Function)
ENTRY(getDate)
Expand Down Expand Up @@ -605,6 +606,7 @@ ENTRY(InitInternalProperties)
ENTRY(methodName)
ENTRY(registerChakraLibraryFunction)
ENTRY(registerFunction)
ENTRY(staticMethod)
ENTRY(toLength)
ENTRY(toInteger)
ENTRY(arraySpeciesCreate)
Expand Down
2,196 changes: 1,098 additions & 1,098 deletions lib/Runtime/Library/InJavascript/Intl.js.bc.32b.h

Large diffs are not rendered by default.

2,196 changes: 1,098 additions & 1,098 deletions lib/Runtime/Library/InJavascript/Intl.js.bc.64b.h

Large diffs are not rendered by default.

2,200 changes: 1,100 additions & 1,100 deletions lib/Runtime/Library/InJavascript/Intl.js.nojit.bc.32b.h

Large diffs are not rendered by default.

2,172 changes: 1,086 additions & 1,086 deletions lib/Runtime/Library/InJavascript/Intl.js.nojit.bc.64b.h

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion lib/Runtime/Library/JavascriptLibrary.cpp
Expand Up @@ -3610,7 +3610,7 @@ namespace Js
// so that the update is in sync with profiler
JavascriptLibrary* library = objectConstructor->GetLibrary();
ScriptContext* scriptContext = objectConstructor->GetScriptContext();
int propertyCount = 18;
int propertyCount = 19;
if (scriptContext->GetConfig()->IsES6ObjectExtensionsEnabled())
{
propertyCount += 2;
Expand Down Expand Up @@ -3677,6 +3677,13 @@ namespace Js
library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::entries, &JavascriptObject::EntryInfo::Entries, 1));
}

#ifdef ENABLE_JS_BUILTINS
if (scriptContext->IsJsBuiltInEnabled())
{
library->EnsureBuiltInEngineIsReady();
}
#endif

objectConstructor->SetHasNoEnumerableProperties(true);

return true;
Expand Down
30 changes: 30 additions & 0 deletions lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js
Expand Up @@ -16,6 +16,7 @@
ArrayFilter: { className: "Array", methodName: "filter", argumentsCount: 1, forceInline: true /*optional*/ },
ArrayFlat: { className: "Array", methodName: "flat", argumentsCount: 0, forceInline: true /*optional*/ },
ArrayFlatMap: { className: "Array", methodName: "flatMap", argumentsCount: 1, forceInline: true /*optional*/ },
ObjectFromEntries: { className: "Object", staticMethod: true, methodName: "fromEntries", argumentsCount: 1, forceInline: true /*optional*/ },
};

var setPrototype = platform.builtInSetPrototype;
Expand All @@ -38,10 +39,12 @@
__chakraLibrary.ArrayIterator.prototype = CreateObject(iteratorPrototype);
__chakraLibrary.raiseNeedObjectOfType = platform.raiseNeedObjectOfType;
__chakraLibrary.raiseThis_NullOrUndefined = platform.raiseThis_NullOrUndefined;
__chakraLibrary.raiseNeedObject = platform.raiseNeedObject;
__chakraLibrary.raiseLengthIsTooBig = platform.raiseLengthIsTooBig;
__chakraLibrary.raiseFunctionArgument_NeedFunction = platform.raiseFunctionArgument_NeedFunction;
__chakraLibrary.callInstanceFunc = platform.builtInCallInstanceFunction;
__chakraLibrary.functionBind = platform.builtInJavascriptFunctionEntryBind;
__chakraLibrary.objectDefineProperty = _objectDefineProperty;

_objectDefineProperty(__chakraLibrary.ArrayIterator.prototype, 'next',
// Object's getter and setter can get overriden on the prototype, in that case while setting the value attributes, we will end up with TypeError
Expand Down Expand Up @@ -415,4 +418,31 @@
return A;
});

platform.registerFunction(FunctionsEnum.ObjectFromEntries, function (iterable) {
// #sec-object.fromentries
"use strict";
if (iterable === null || iterable === undefined) {
__chakraLibrary.raiseNeedObject("Object.prototype.fromEntries");
}

const o = {};
const propDescriptor = {
enumerable : true,
configurable : true,
writable : true,
value : undefined
};

let key;
for (const entry of iterable) {
if (typeof entry !== "object") {
__chakraLibrary.raiseNeedObject("Object.prototype.fromEntries");
}

key = entry[0];
propDescriptor.value = entry[1];
__chakraLibrary.objectDefineProperty(o, key, propDescriptor);
}
return o;
});
});
1,463 changes: 785 additions & 678 deletions lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js.bc.32b.h

Large diffs are not rendered by default.

1,463 changes: 785 additions & 678 deletions lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js.bc.64b.h

Large diffs are not rendered by default.

1,356 changes: 729 additions & 627 deletions lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js.nojit.bc.32b.h

Large diffs are not rendered by default.

1,356 changes: 729 additions & 627 deletions lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js.nojit.bc.64b.h

Large diffs are not rendered by default.

31 changes: 27 additions & 4 deletions lib/Runtime/Library/JsBuiltInEngineInterfaceExtensionObject.cpp
Expand Up @@ -224,22 +224,43 @@ namespace Js
}
}

DynamicObject* JsBuiltInEngineInterfaceExtensionObject::GetPrototypeFromName(Js::PropertyIds propertyId, ScriptContext* scriptContext)
DynamicObject* JsBuiltInEngineInterfaceExtensionObject::GetPrototypeFromName(Js::PropertyIds propertyId, bool staticMethod, ScriptContext* scriptContext)
{
JavascriptLibrary* library = scriptContext->GetLibrary();

if (staticMethod)
{
switch (propertyId) {
case PropertyIds::Array:
return library->arrayConstructor;

case PropertyIds::Object:
return library->objectConstructor;

case PropertyIds::String:
return library->stringConstructor;

default:
AssertOrFailFastMsg(false, "Unable to find a constructor that match with this className.");
return nullptr;
}
}

switch (propertyId) {
case PropertyIds::Array:
return library->arrayPrototype;

case PropertyIds::Object:
return library->objectPrototype;

case PropertyIds::String:
return library->stringPrototype;

case PropertyIds::__chakraLibrary:
return library->GetChakraLib();

default:
AssertMsg(false, "Unable to find a prototype that match with this className.");
AssertOrFailFastMsg(false, "Unable to find a prototype that match with this className.");
return nullptr;
}
}
Expand Down Expand Up @@ -297,7 +318,7 @@ namespace Js
func->GetFunctionProxy()->SetIsPublicLibraryCode();
func->GetFunctionProxy()->EnsureDeserialized()->SetDisplayName(methodName->GetString(), methodName->GetLength(), 0);

DynamicObject* chakraLibraryObject = GetPrototypeFromName(PropertyIds::__chakraLibrary, scriptContext);
DynamicObject* chakraLibraryObject = GetPrototypeFromName(PropertyIds::__chakraLibrary, false, scriptContext);
PropertyIds functionIdentifier = JavascriptOperators::GetPropertyId(methodName, scriptContext);

// Link the function to __chakraLibrary.
Expand Down Expand Up @@ -335,6 +356,7 @@ namespace Js
Var argumentsCountProperty = JavascriptOperators::OP_GetProperty(funcInfo, Js::PropertyIds::argumentsCount, scriptContext);
Var forceInlineProperty = JavascriptOperators::OP_GetProperty(funcInfo, Js::PropertyIds::forceInline, scriptContext);
Var aliasProperty = JavascriptOperators::OP_GetProperty(funcInfo, Js::PropertyIds::alias, scriptContext);
Var staticMethodProperty = JavascriptOperators::OP_GetProperty(funcInfo, Js::PropertyIds::staticMethod, scriptContext);

Assert(JavascriptString::Is(classNameProperty));
Assert(JavascriptString::Is(methodNameProperty));
Expand All @@ -344,6 +366,7 @@ namespace Js
JavascriptString* methodName = JavascriptString::FromVar(methodNameProperty);
int argumentsCount = TaggedInt::ToInt32(argumentsCountProperty);

BOOL staticMethod = JavascriptConversion::ToBoolean(staticMethodProperty, scriptContext);
BOOL forceInline = JavascriptConversion::ToBoolean(forceInlineProperty, scriptContext);

JavascriptFunction* func = JavascriptFunction::FromVar(args.Values[2]);
Expand All @@ -352,7 +375,7 @@ namespace Js
func->GetFunctionProxy()->SetIsPublicLibraryCode();
func->GetFunctionProxy()->EnsureDeserialized()->SetDisplayName(methodName->GetString(), methodName->GetLength(), 0);

DynamicObject* prototype = GetPrototypeFromName(JavascriptOperators::GetPropertyId(className, scriptContext), scriptContext);
DynamicObject* prototype = GetPrototypeFromName(JavascriptOperators::GetPropertyId(className, scriptContext), staticMethod, scriptContext);
PropertyIds functionIdentifier = methodName->BufferEquals(_u("Symbol.iterator"), 15)? PropertyIds::_symbolIterator :
JavascriptOperators::GetPropertyId(methodName, scriptContext);

Expand Down
Expand Up @@ -42,7 +42,7 @@ namespace Js

void EnsureJsBuiltInByteCode(ScriptContext * scriptContext);

static DynamicObject* GetPrototypeFromName(Js::PropertyIds propertyId, ScriptContext* scriptContext);
static DynamicObject* GetPrototypeFromName(Js::PropertyIds propertyId, bool staticMethod, ScriptContext* scriptContext);
static void RecordDefaultIteratorFunctions(Js::PropertyIds propertyId, ScriptContext * scriptContext, JavascriptFunction* iteratorFunc);
static void RecordCommonNativeInterfaceBuiltIns(Js::PropertyIds propertyId, ScriptContext * scriptContext, JavascriptFunction * scriptFunction);
static Var EntryJsBuiltIn_RegisterChakraLibraryFunction(RecyclableObject* function, CallInfo callInfo, ...);
Expand Down
9 changes: 6 additions & 3 deletions test/DebuggerCommon/ES6_intl_simple_attach.js.dbg.baseline
Expand Up @@ -57,7 +57,8 @@
"is": "function <large string>",
"assign": "function <large string>",
"values": "function <large string>",
"entries": "function <large string>"
"entries": "function <large string>",
"fromEntries": "function <large string>"
},
"hasOwnProperty": {
"#__proto__": "function <large string>",
Expand Down Expand Up @@ -205,7 +206,8 @@
"is": "function <large string>",
"assign": "function <large string>",
"values": "function <large string>",
"entries": "function <large string>"
"entries": "function <large string>",
"fromEntries": "function <large string>"
},
"hasOwnProperty": {
"#__proto__": "function <large string>",
Expand Down Expand Up @@ -353,7 +355,8 @@
"is": "function <large string>",
"assign": "function <large string>",
"values": "function <large string>",
"entries": "function <large string>"
"entries": "function <large string>",
"fromEntries": "function <large string>"
},
"hasOwnProperty": {
"#__proto__": "function <large string>",
Expand Down
3 changes: 2 additions & 1 deletion test/DebuggerCommon/symbols.js.dbg.baseline
Expand Up @@ -32,7 +32,8 @@
"is": "function <large string>",
"assign": "function <large string>",
"values": "function <large string>",
"entries": "function <large string>"
"entries": "function <large string>",
"fromEntries": "function <large string>"
},
"hasOwnProperty": {
"#__proto__": "function <large string>",
Expand Down

0 comments on commit 4f03f21

Please sign in to comment.