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

In strict mode, ScriptableObject.putImpl(Object,int,Scriptable,Object) prematurely breaks on !isExtensible, setters don't get a chance to be called #616

Closed
stijnkliemesch opened this issue Nov 8, 2019 · 1 comment

Comments

@stijnkliemesch
Copy link
Contributor

stijnkliemesch commented Nov 8, 2019

Executing the following code:

public static void main(String[] cmdlineArgs) {
    final Context context = ContextFactory.getGlobal().enterContext();
    try {
        final Script compiledScript = context.compileString(""
                + "\"use strict\";\n"
                + "var a = function(){this._x = 2;};\n"
                + "Object.defineProperty(a.prototype, \"x\", {get:(function x() {return this._x;}), set:(function x(x) {this._x = x;})});\n"
                + "Object.freeze(a.prototype);\n"
                + "var b = new a();\n"
                + "var c = b.x;\n"
                + "b.x = 1;\n"
                + "b.x+c;",
                "eval", 1, null
        );
        ScriptableObject global = context.initSafeStandardObjects();
        final Object obj
                = ScriptRuntime.doTopCall(
                        (Context cx, Scriptable scope, Scriptable thisObj, Object[] args) -> compiledScript.exec(context, scope),
                        context, global, null, new Object[]{}, true);
        System.out.println(obj);
        int i = 0;
    } finally {
        Context.exit();
    }
}

Expected output is:

 3.0

instead, this results in a thrown Exception

Exception in thread "main" org.mozilla.javascript.EcmaError: TypeError: Cannot add properties to this object because extensible is false. (eval#7)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4251)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4229)
    at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4262)
    at org.mozilla.javascript.ScriptRuntime.typeError0(ScriptRuntime.java:4268)
    at org.mozilla.javascript.ScriptableObject.putImpl(ScriptableObject.java:2813)
    at org.mozilla.javascript.ScriptableObject.put(ScriptableObject.java:521)
    at org.mozilla.javascript.IdScriptableObject.put(IdScriptableObject.java:479)
    at org.mozilla.javascript.ScriptableObject.putProperty(ScriptableObject.java:2507)
    at org.mozilla.javascript.ScriptRuntime.setObjectProp(ScriptRuntime.java:1743)
    at org.mozilla.javascript.ScriptRuntime.setObjectProp(ScriptRuntime.java:1737)
    at org.mozilla.javascript.gen.eval_1._c_script_0(eval:5)
    at org.mozilla.javascript.gen.eval_1.call(eval)
    at org.mozilla.javascript.gen.eval_1.exec(eval)
...
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:412)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3561)
...
@stijnkliemesch stijnkliemesch changed the title In strict mode, ScriptableObject.putImpl(Object,int,Scriptable.Object) prematurely breaks on !isExtensible, setters don't get a chance to be called In strict mode, ScriptableObject.putImpl(Object,int,Scriptable,Object) prematurely breaks on !isExtensible, setters don't get a chance to be called Nov 8, 2019
gbrail pushed a commit that referenced this issue Nov 25, 2019
No longer prematurely throws a TypeError for non-extensible objects.

Affects strict mode only.

No longer prematurely throws a TypeError in ScriptableObject.putImpl(Object,int,Scriptable,Object) when instance member isExtensible is false.

Instead, now it first queries the slotmap, so that if a GetterSlot instance is returned, any present setter may be called. All other outcomes of this method should be unaffected.
@gbrail
Copy link
Collaborator

gbrail commented Nov 25, 2019

Thanks!

@gbrail gbrail closed this as completed Nov 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants