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

ADD: ES6 Object.values / Object.entries / Object.fromEntries #902

Merged
merged 15 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 23 additions & 4 deletions src/org/mozilla/javascript/NativeObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,20 +438,39 @@ public Object execIdCall(
if (key == Scriptable.NOT_FOUND) {
key = Undefined.instance;
}
if (key instanceof ScriptableObject) {
ScriptableObject so = (ScriptableObject) key;
if (so.has(SymbolKey.TO_PRIMITIVE, so)) {
key = so.get(SymbolKey.TO_PRIMITIVE, so);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edge case? if object has Symbol.toPrimitive -> take it.
Shouldn't this be handled by ScriptRuntime.toPrimitive - respectively ScriptableObject.getDefaultValue?

if (key instanceof Function) {
Function fun = (Function)key;
key = fun.call(cx, fun.getParentScope(), entry, ScriptRuntime.emptyArgs);
Function fun = (Function) key;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test fromEntries/evaluation-order.js expects, that method is called.

Can someone point out, where this happens in https://tc39.es/ecma262/#sec-object.fromentries ?
as far as I understand, it calls CreateDataPropertyOnObject and then ToPropertyKey, which calls ToPrimitive

I assume it may happen in "ToPrimitive", but I use ScriptRuntime.toString(key) which invokes ScriptabbleObject.getDefaultValue(String.class) - so maybe there is an implementation error.

key =
fun.call(
cx,
fun.getParentScope(),
entry,
new Object[] {"string"});
}
Object value = entry.get(1, entry);
if (value == Scriptable.NOT_FOUND) {
value = Undefined.instance;
}
if (value instanceof Function) {
Function fun = (Function)value;
value = fun.call(cx, fun.getParentScope(), entry, ScriptRuntime.emptyArgs);
Function fun = (Function) value;
value =
fun.call(
cx,
fun.getParentScope(),
entry,
ScriptRuntime.emptyArgs);
}

if (key instanceof Integer) {
obj.put((Integer) key, obj, value);
} else if (key instanceof Symbol && obj instanceof SymbolScriptable) {
((SymbolScriptable) obj).put((Symbol) key, obj, value);
} else {
obj.put(ScriptRuntime.toString(key), obj, value);
}
Expand Down
20 changes: 13 additions & 7 deletions src/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -4291,23 +4291,29 @@ public static Scriptable newObjectLiteral(
Object id = propertyIds[i];
int getterSetter = getterSetters == null ? 0 : getterSetters[i];
Object value = propertyValues[i];
if (id instanceof String) {
if (id instanceof Symbol) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required to support arrays like this:

var key = Symbol();
var arr = [key, 'value'];

Symbol sym = (Symbol) id;
SymbolScriptable so = (SymbolScriptable) object;
so.put(sym, object, value);
} else if (id instanceof Integer) {
int index = ((Integer) id).intValue();
object.put(index, object, value);
} else {
// we might get a computed double.
String stringId = ScriptRuntime.toString(id);
if (getterSetter == 0) {
if (isSpecialProperty((String) id)) {
Ref ref = specialRef(object, (String) id, cx, scope);
if (isSpecialProperty(stringId)) {
Ref ref = specialRef(object, stringId, cx, scope);
ref.set(cx, scope, value);
} else {
object.put((String) id, object, value);
object.put(stringId, object, value);
}
} else {
ScriptableObject so = (ScriptableObject) object;
Callable getterOrSetter = (Callable) value;
boolean isSetter = getterSetter == 1;
so.setGetterOrSetter((String) id, 0, getterOrSetter, isSetter);
}
} else {
int index = ((Integer) id).intValue();
object.put(index, object, value);
}
}
return object;
Expand Down
1 change: 0 additions & 1 deletion testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,6 @@ built-ins/Object
! fromEntries/iterator-not-closed-for-throwing-done-accessor.js
! fromEntries/iterator-not-closed-for-throwing-next.js
! fromEntries/iterator-not-closed-for-uncallable-next.js
! fromEntries/supports-symbols.js
! fromEntries/to-property-key.js
! fromEntries/uses-keys-not-iterator.js
! getOwnPropertyDescriptor/15.2.3.3-4-187.js
Expand Down