atom);
}
-/* Private fields can be added even on non extensible objects or
- Proxies */
+/* Per the nonextensible-applies-to-private proposal, private fields cannot
+ be added to non-extensible objects. Note that a Proxy's JSObject stays
+ extensible (its trap reflects the target), so private fields can still be
+ stamped onto Proxies. */
static int JS_DefinePrivateField(JSContext *ctx, JSValueConst obj,
JSValueConst name, JSValue val)
{
}
prop = js_symbol_to_atom(ctx, (JSValue)name);
p = JS_VALUE_GET_OBJ(obj);
+ if (unlikely(!p->extensible)) {
+ JS_ThrowTypeError(ctx, "object is not extensible");
+ goto fail;
+ }
prs = find_own_property(&pr, p, prop);
if (prs) {
JS_ThrowTypeErrorAtom(ctx, "private class field '%s' already exists",
if (JS_IsObject(obj)) {
p1 = JS_VALUE_GET_OBJ(obj);
+ if (unlikely(!p1->extensible)) {
+ JS_FreeAtom(ctx, brand_atom);
+ JS_ThrowTypeError(ctx, "object is not extensible");
+ return -1;
+ }
prs = find_own_property(&pr, p1, brand_atom);
if (unlikely(prs)) {
JS_FreeAtom(ctx, brand_atom);