Diff
Modified: trunk/JSTests/ChangeLog (235588 => 235589)
--- trunk/JSTests/ChangeLog 2018-09-02 05:35:49 UTC (rev 235588)
+++ trunk/JSTests/ChangeLog 2018-09-02 16:41:45 UTC (rev 235589)
@@ -1,3 +1,16 @@
+2018-09-02 Yusuke Suzuki <[email protected]>
+
+ Implement Object.fromEntries
+ https://bugs.webkit.org/show_bug.cgi?id=188481
+
+ Reviewed by Darin Adler.
+
+ * stress/object-from-entries.js: Added.
+ (shouldBe):
+ (shouldThrow):
+ (shouldBe.JSON.stringify.Object.getOwnPropertyDescriptor):
+ (shouldBe.set get shouldThrow):
+
2018-08-24 Yusuke Suzuki <[email protected]>
Function object should convert params to string before throw a parsing error
Added: trunk/JSTests/stress/object-from-entries.js (0 => 235589)
--- trunk/JSTests/stress/object-from-entries.js (rev 0)
+++ trunk/JSTests/stress/object-from-entries.js 2018-09-02 16:41:45 UTC (rev 235589)
@@ -0,0 +1,196 @@
+function shouldBe(actual, expected) {
+ if (actual !== expected)
+ throw new Error('bad value: ' + actual);
+}
+
+function shouldThrow(func, errorMessage) {
+ var errorThrown = false;
+ var error = null;
+ try {
+ func();
+ } catch (e) {
+ errorThrown = true;
+ error = e;
+ }
+ if (!errorThrown)
+ throw new Error('not thrown');
+ if (String(error) !== errorMessage)
+ throw new Error(`bad error: ${String(error)}`);
+}
+
+shouldBe(JSON.stringify(Object.getOwnPropertyDescriptor(Object, "fromEntries")), `{"writable":true,"enumerable":false,"configurable":true}`);
+shouldBe(Object.fromEntries.length, 1);
+
+shouldThrow(() => Object.fromEntries(null), `TypeError: null is not an object`);
+shouldThrow(() => Object.fromEntries(undefined), `TypeError: undefined is not an object`);
+shouldThrow(() => Object.fromEntries(0), `TypeError: undefined is not a function`);
+shouldThrow(() => Object.fromEntries(true), `TypeError: undefined is not a function`);
+shouldThrow(() => Object.fromEntries(Symbol("Cocoa")), `TypeError: undefined is not a function`);
+shouldThrow(() => Object.fromEntries("Cocoa"), `TypeError: Object.fromEntries requires the first iterable parameter yields objects`);
+shouldThrow(() => Object.fromEntries([0]), `TypeError: Object.fromEntries requires the first iterable parameter yields objects`);
+shouldThrow(() => Object.fromEntries([["Cocoa", "Cappuccino"], 0]), `TypeError: Object.fromEntries requires the first iterable parameter yields objects`);
+
+{
+ let object = Object.fromEntries([]);
+ shouldBe(JSON.stringify(object), `{}`);
+}
+{
+ let object = Object.fromEntries([["Cocoa", "Cappuccino"]]);
+ shouldBe(JSON.stringify(object), `{"Cocoa":"Cappuccino"}`);
+ shouldBe(JSON.stringify(Object.getOwnPropertyDescriptor(object, "Cocoa")), `{"value":"Cappuccino","writable":true,"enumerable":true,"configurable":true}`);
+}
+{
+ let obj = { abc: 1, def: 2, ghij: 3 };
+ let res = Object.fromEntries(
+ Object.entries(obj)
+ .filter(([ key, val ]) => key.length === 3)
+ .map(([ key, val ]) => [ key, val * 2 ])
+ );
+ shouldBe(JSON.stringify(res), `{"abc":2,"def":4}`);
+}
+{
+ let map = new Map([ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]);
+ let obj = Object.fromEntries(map);
+ shouldBe(JSON.stringify(obj), `{"a":1,"b":2,"c":3}`);
+}
+{
+ let arr = [ { name: 'Alice', age: 40 }, { name: 'Bob', age: 36 } ];
+ let obj = Object.fromEntries(arr.map(({ name, age }) => [ name, age ]));
+ shouldBe(JSON.stringify(obj), `{"Alice":40,"Bob":36}`);
+}
+{
+ Object.defineProperty(Object.prototype, "bad", {
+ get() { throw new Error("out"); },
+ set(v) { throw new Error("out"); }
+ });
+ shouldThrow(() => {
+ let object = {};
+ object.bad;
+ }, `Error: out`);
+ shouldThrow(() => {
+ let object = {};
+ object.bad = 42;
+ }, `Error: out`);
+ let object = Object.fromEntries([["bad", "value"]]);
+ shouldBe(JSON.stringify(object), `{"bad":"value"}`);
+}
+{
+ var counter = 0;
+ class Recorder {
+ constructor(first, second)
+ {
+ this.first = first;
+ this.second = second;
+ }
+
+ get 0()
+ {
+ shouldBe(counter++, this.first);
+ return this.first;
+ }
+
+ get 1()
+ {
+ shouldBe(counter++, this.second);
+ return this.second;
+ }
+ }
+ var result = Object.fromEntries([new Recorder(0, 1), new Recorder(2, 3)]);
+ shouldBe(result[0], 1);
+ shouldBe(result[2], 3);
+ shouldBe(counter, 4);
+}
+{
+ class Iterable {
+ constructor()
+ {
+ }
+
+ *[Symbol.iterator]()
+ {
+ yield [0, 1];
+ yield [1, 2];
+ }
+ }
+
+ var result = Object.fromEntries(new Iterable);
+ shouldBe(result[0], 1);
+ shouldBe(result[1], 2);
+}
+{
+ class Iterator {
+ constructor()
+ {
+ this.index = 0;
+ }
+
+ next()
+ {
+ if (this.index === 4)
+ throw new Error("out");
+
+ this.index++;
+ return {
+ value: [0, 1],
+ done: false
+ };
+ }
+ }
+
+ class Iterable {
+ constructor()
+ {
+ }
+
+ [Symbol.iterator]()
+ {
+ return new Iterator;
+ }
+ }
+
+ try {
+ Object.fromEntries(new Iterable);
+ } catch (error) {
+ shouldBe(String(error), `Error: out`);
+ }
+}
+{
+ let array = [[], ['c', 'd']];
+ let object = Object.fromEntries(array);
+ shouldBe(JSON.stringify(Object.keys(object).sort()), `["c","undefined"]`);
+ shouldBe(object.c, 'd');
+ shouldBe(object.undefined, undefined);
+}
+{
+ let symbol = Symbol('Cocoa');
+ let array = [[symbol, 42]];
+ let object = Object.fromEntries(array);
+ shouldBe(Object.getOwnPropertySymbols(object).length, 1);
+ shouldBe(Object.getOwnPropertyNames(object).length, 0);
+ shouldBe(object.hasOwnProperty(symbol), true);
+ shouldBe(object[symbol], 42);
+}
+{
+ Object.defineProperty(Object.prototype, "hello", {
+ get() {
+ throw new Error("out");
+ },
+ set() {
+ throw new Error("out");
+ }
+ });
+ let result = Object.fromEntries([["hello", 42]]);
+ shouldBe(result.hello, 42);
+}
+{
+ let array = [['a', 'b'], ['c', 'd']];
+ Object.defineProperty(array, 0, {
+ get()
+ {
+ array.push(['e', 'f']);
+ return ['a', 'b'];
+ }
+ });
+ let object = Object.fromEntries(array);
+ shouldBe(JSON.stringify(object), `{"a":"b","c":"d","e":"f"}`);
+}
Modified: trunk/LayoutTests/ChangeLog (235588 => 235589)
--- trunk/LayoutTests/ChangeLog 2018-09-02 05:35:49 UTC (rev 235588)
+++ trunk/LayoutTests/ChangeLog 2018-09-02 16:41:45 UTC (rev 235589)
@@ -1,3 +1,13 @@
+2018-09-02 Yusuke Suzuki <[email protected]>
+
+ Implement Object.fromEntries
+ https://bugs.webkit.org/show_bug.cgi?id=188481
+
+ Reviewed by Darin Adler.
+
+ * js/Object-getOwnPropertyNames-expected.txt:
+ * js/script-tests/Object-getOwnPropertyNames.js:
+
2018-08-31 Simon Fraser <[email protected]>
Clean up TestExpectations so that ./Tools/Scripts/lint-test-expectations
Modified: trunk/LayoutTests/js/Object-getOwnPropertyNames-expected.txt (235588 => 235589)
--- trunk/LayoutTests/js/Object-getOwnPropertyNames-expected.txt 2018-09-02 05:35:49 UTC (rev 235588)
+++ trunk/LayoutTests/js/Object-getOwnPropertyNames-expected.txt 2018-09-02 16:41:45 UTC (rev 235589)
@@ -42,7 +42,7 @@
PASS getSortedOwnPropertyNames(decodeURIComponent) is ['length', 'name']
PASS getSortedOwnPropertyNames(encodeURI) is ['length', 'name']
PASS getSortedOwnPropertyNames(encodeURIComponent) is ['length', 'name']
-PASS getSortedOwnPropertyNames(Object) is ['assign', 'create', 'defineProperties', 'defineProperty', 'entries', 'freeze', 'getOwnPropertyDescriptor', 'getOwnPropertyDescriptors', 'getOwnPropertyNames', 'getOwnPropertySymbols', 'getPrototypeOf', 'is', 'isExtensible', 'isFrozen', 'isSealed', 'keys', 'length', 'name', 'preventExtensions', 'prototype', 'seal', 'setPrototypeOf', 'values']
+PASS getSortedOwnPropertyNames(Object) is ['assign', 'create', 'defineProperties', 'defineProperty', 'entries', 'freeze', 'fromEntries', 'getOwnPropertyDescriptor', 'getOwnPropertyDescriptors', 'getOwnPropertyNames', 'getOwnPropertySymbols', 'getPrototypeOf', 'is', 'isExtensible', 'isFrozen', 'isSealed', 'keys', 'length', 'name', 'preventExtensions', 'prototype', 'seal', 'setPrototypeOf', 'values']
PASS getSortedOwnPropertyNames(Object.prototype) is ['__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', '__proto__', 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf']
PASS getSortedOwnPropertyNames(Function) is ['length', 'name', 'prototype']
PASS getSortedOwnPropertyNames(Function.prototype) is ['apply', 'arguments', 'bind', 'call', 'caller', 'constructor', 'length', 'name', 'toString']
Modified: trunk/LayoutTests/js/script-tests/Object-getOwnPropertyNames.js (235588 => 235589)
--- trunk/LayoutTests/js/script-tests/Object-getOwnPropertyNames.js 2018-09-02 05:35:49 UTC (rev 235588)
+++ trunk/LayoutTests/js/script-tests/Object-getOwnPropertyNames.js 2018-09-02 16:41:45 UTC (rev 235589)
@@ -51,7 +51,7 @@
"encodeURI": "['length', 'name']",
"encodeURIComponent": "['length', 'name']",
// Built-in ECMA objects
- "Object": "['assign', 'create', 'defineProperties', 'defineProperty', 'entries', 'freeze', 'getOwnPropertyDescriptor', 'getOwnPropertyDescriptors', 'getOwnPropertyNames', 'getOwnPropertySymbols', 'getPrototypeOf', 'is', 'isExtensible', 'isFrozen', 'isSealed', 'keys', 'length', 'name', 'preventExtensions', 'prototype', 'seal', 'setPrototypeOf', 'values']",
+ "Object": "['assign', 'create', 'defineProperties', 'defineProperty', 'entries', 'freeze', 'fromEntries', 'getOwnPropertyDescriptor', 'getOwnPropertyDescriptors', 'getOwnPropertyNames', 'getOwnPropertySymbols', 'getPrototypeOf', 'is', 'isExtensible', 'isFrozen', 'isSealed', 'keys', 'length', 'name', 'preventExtensions', 'prototype', 'seal', 'setPrototypeOf', 'values']",
"Object.prototype": "['__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', '__proto__', 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf']",
"Function": "['length', 'name', 'prototype']",
"Function.prototype": "['apply', 'arguments', 'bind', 'call', 'caller', 'constructor', 'length', 'name', 'toString']",
Modified: trunk/Source/_javascript_Core/ChangeLog (235588 => 235589)
--- trunk/Source/_javascript_Core/ChangeLog 2018-09-02 05:35:49 UTC (rev 235588)
+++ trunk/Source/_javascript_Core/ChangeLog 2018-09-02 16:41:45 UTC (rev 235589)
@@ -1,3 +1,18 @@
+2018-09-02 Yusuke Suzuki <[email protected]>
+
+ Implement Object.fromEntries
+ https://bugs.webkit.org/show_bug.cgi?id=188481
+
+ Reviewed by Darin Adler.
+
+ Object.fromEntries becomes stage 3[1]. This patch implements it by using builtin JS.
+
+ [1]: https://tc39.github.io/proposal-object-from-entries/
+
+ * builtins/ObjectConstructor.js:
+ (fromEntries):
+ * runtime/ObjectConstructor.cpp:
+
2018-08-24 Yusuke Suzuki <[email protected]>
Function object should convert params to string before throw a parsing error
Modified: trunk/Source/_javascript_Core/builtins/ObjectConstructor.js (235588 => 235589)
--- trunk/Source/_javascript_Core/builtins/ObjectConstructor.js 2018-09-02 05:35:49 UTC (rev 235588)
+++ trunk/Source/_javascript_Core/builtins/ObjectConstructor.js 2018-09-02 16:41:45 UTC (rev 235589)
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2016 Oleksandr Skachkov <[email protected]>.
* Copyright (C) 2015 Jordan Harband. All rights reserved.
+ * Copyright (C) 2018 Yusuke Suzuki <[email protected]>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -39,3 +40,20 @@
return properties;
}
+
+function fromEntries(iterable)
+{
+ "use strict";
+
+ let object = {};
+
+ for (let entry of iterable) {
+ if (!@isObject(entry))
+ @throwTypeError("Object.fromEntries requires the first iterable parameter yields objects");
+ let key = entry[0];
+ let value = entry[1];
+ @putByValDirect(object, key, value);
+ }
+
+ return object;
+}
Modified: trunk/Source/_javascript_Core/runtime/ObjectConstructor.cpp (235588 => 235589)
--- trunk/Source/_javascript_Core/runtime/ObjectConstructor.cpp 2018-09-02 05:35:49 UTC (rev 235588)
+++ trunk/Source/_javascript_Core/runtime/ObjectConstructor.cpp 2018-09-02 16:41:45 UTC (rev 235589)
@@ -87,6 +87,7 @@
assign objectConstructorAssign DontEnum|Function 2
values objectConstructorValues DontEnum|Function 1
entries JSBuiltin DontEnum|Function 1
+ fromEntries JSBuiltin DontEnum|Function 1
@end
*/