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

Extract the Object.seal/freeze/isSealed/isFrozen logic, fixing a few bugs in the process #922

Merged
merged 4 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
111 changes: 111 additions & 0 deletions src/org/mozilla/javascript/AbstractEcmaObjectOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.mozilla.javascript;

/**
* Abstract Object Operations as defined by EcmaScript
*
* @see <a href="https://262.ecma-international.org/11.0/#sec-operations-on-objects">Abstract
* Operations - Operations on Objects</a>
*/
class AbstractEcmaObjectOperations {
static enum INTEGRITY_LEVEL {
FROZEN,
SEALED
}

/**
* Implementation of Abstract Object operation testIntegrityLevel as defined by EcmaScript
*
* @param o
* @param level
* @return boolean
* @see <a
* href="https://262.ecma-international.org/11.0/#sec-testintegritylevel">TestIntegrityLevel</a>
*/
static boolean testIntegrityLevel(Object o, INTEGRITY_LEVEL level) {
ScriptableObject obj = ScriptableObject.ensureScriptableObject(o);
Context cx = Context.getCurrentContext();
p-bakker marked this conversation as resolved.
Show resolved Hide resolved

if (obj.isExtensible()) return Boolean.FALSE;
p-bakker marked this conversation as resolved.
Show resolved Hide resolved

for (Object name : obj.getIds(true, true)) {
ScriptableObject desc = obj.getOwnPropertyDescriptor(cx, name);
if (Boolean.TRUE.equals(desc.get("configurable"))) return Boolean.FALSE;

if (level == INTEGRITY_LEVEL.FROZEN
&& desc.isDataDescriptor(desc)
&& Boolean.TRUE.equals(desc.get("writable"))) return Boolean.FALSE;
}

return Boolean.TRUE;
}

/**
* Implementation of Abstract Object operation setIntegrityLevel as defined by EcmaScript
*
* @param o
* @param level
* @return boolean
* @see <a
* href="https://262.ecma-international.org/11.0/#sec-setintegritylevel">SetIntegrityLevel</a>
*/
static boolean setIntegrityLevel(Object o, INTEGRITY_LEVEL level) {
/*
1. Assert: Type(O) is Object.
2. Assert: level is either sealed or frozen.
3. Let status be ? O.[[PreventExtensions]]().
4. If status is false, return false.
5. Let keys be ? O.[[OwnPropertyKeys]]().
6. If level is sealed, then
a. For each element k of keys, do
i. Perform ? DefinePropertyOrThrow(O, k, PropertyDescriptor { [[Configurable]]: false }).
7. Else,
a. Assert: level is frozen.
b. For each element k of keys, do
i. Let currentDesc be ? O.[[GetOwnProperty]](k).
ii. If currentDesc is not undefined, then
1. If IsAccessorDescriptor(currentDesc) is true, then
a. Let desc be the PropertyDescriptor { [[Configurable]]: false }.
2. Else,
a. Let desc be the PropertyDescriptor { [[Configurable]]: false, [[Writable]]: false }.
3. Perform ? DefinePropertyOrThrow(O, k, desc).
8. Return true.

NOTES
- Step 3 calls for the .preventExtensions() before updating the propertyDescriptors,
In Rhino however .preventExtensions() never returns false
and calling it before will block updating the propertyDescriptors afterwards
- While steps 6.a.i and 7.b.ii.3 call for the Abstract DefinePropertyOrThrow operation,
the conditions under which a throw would occur aren't applicable when freezing or sealing an object
*/
ScriptableObject obj = ScriptableObject.ensureScriptableObject(o);
Context cx = Context.getCurrentContext();

for (Object key : obj.getIds(true, true)) {
ScriptableObject desc = obj.getOwnPropertyDescriptor(cx, key);

if (level == INTEGRITY_LEVEL.SEALED) {
if (Boolean.TRUE.equals(desc.get("configurable"))) {
desc.put("configurable", desc, Boolean.FALSE);

obj.defineOwnProperty(cx, key, desc, false);
}
} else {
if (obj.isDataDescriptor(desc) && Boolean.TRUE.equals(desc.get("writable"))) {
desc.put("writable", desc, Boolean.FALSE);
}
if (Boolean.TRUE.equals(desc.get("configurable"))) {
desc.put("configurable", desc, Boolean.FALSE);
}
obj.defineOwnProperty(cx, key, desc, false);
}
}

obj.preventExtensions();

return true;
}

static boolean definePropertyOrThrow(Object o, Object p, Scriptable desc) {
p-bakker marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
}
1 change: 0 additions & 1 deletion src/org/mozilla/javascript/BaseFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ protected void setInstanceIdAttributes(int id, int attr) {
argumentsAttributes = attr;
return;
}
super.setInstanceIdAttributes(id, attr);
}

@Override
Expand Down
Loading