Skip to content

Commit 300ef5d

Browse files
committed
arguments accessed from the function itself are readonly
1 parent 184a9b6 commit 300ef5d

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

rhino/src/main/java/org/mozilla/javascript/Arguments.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @see org.mozilla.javascript.NativeCall
1515
* @author Norris Boyd
1616
*/
17-
final class Arguments extends ScriptableObject {
17+
class Arguments extends ScriptableObject {
1818
private static final long serialVersionUID = 4275508002492040609L;
1919

2020
private static final String CLASS_NAME = "Arguments";
@@ -311,4 +311,61 @@ protected boolean defineOwnProperty(
311311
}
312312
return true;
313313
}
314+
315+
static final class ReadonlyArguments extends Arguments {
316+
private boolean initialized;
317+
318+
public ReadonlyArguments(Arguments arguments, Context cx) {
319+
super(arguments.activation, cx);
320+
initialized = true;
321+
}
322+
323+
@Override
324+
public void put(int index, Scriptable start, Object value) {
325+
if (initialized) {
326+
return;
327+
}
328+
super.put(index, start, value);
329+
}
330+
331+
@Override
332+
public void put(String name, Scriptable start, Object value) {
333+
if (initialized) {
334+
return;
335+
}
336+
super.put(name, start, value);
337+
}
338+
339+
@Override
340+
public void put(Symbol key, Scriptable start, Object value) {
341+
if (initialized) {
342+
return;
343+
}
344+
super.put(key, start, value);
345+
}
346+
347+
@Override
348+
public void delete(int index) {
349+
if (initialized) {
350+
return;
351+
}
352+
super.delete(index);
353+
}
354+
355+
@Override
356+
public void delete(String name) {
357+
if (initialized) {
358+
return;
359+
}
360+
super.delete(name);
361+
}
362+
363+
@Override
364+
public void delete(Symbol key) {
365+
if (initialized) {
366+
return;
367+
}
368+
super.delete(key);
369+
}
370+
}
314371
}

rhino/src/main/java/org/mozilla/javascript/BaseFunction.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,15 @@ private Object getArguments() {
713713
}
714714
Context cx = Context.getContext();
715715
NativeCall activation = ScriptRuntime.findFunctionActivation(cx, this);
716-
return (activation == null) ? null : activation.get("arguments", activation);
716+
// return (activation == null) ? null : activation.get("arguments", activation);
717+
if (activation == null) {
718+
return null;
719+
}
720+
Object arguments = activation.get("arguments", activation);
721+
if (arguments instanceof Arguments && cx.getLanguageVersion() >= Context.VERSION_ES6) {
722+
return new Arguments.ReadonlyArguments((Arguments) arguments, cx);
723+
}
724+
return arguments;
717725
}
718726

719727
private static Scriptable jsConstructor(

tests/src/test/java/org/mozilla/javascript/tests/es6/ArgumentsTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,7 @@ public void readOnlyWhenAccessedThroughFunction() {
361361
+ "test('hello', 'world');\n"
362362
+ "res";
363363

364-
// Utils.assertWithAllModes_ES6("2 hello world undefined undefined", code);
365-
Utils.assertWithAllModes_ES6("2 hello hi undefined you", code);
364+
Utils.assertWithAllModes_ES6("2 hello world undefined undefined", code);
366365
Utils.assertWithAllModes_1_8("2 hello hi undefined you", code);
367366
}
368367

@@ -605,8 +604,7 @@ public void argumentsEqualsFnArguments() {
605604
+ "test('hello', 'world');\n"
606605
+ "res";
607606

608-
// Utils.assertWithAllModes_ES6("null false false", code);
609-
Utils.assertWithAllModes_ES6("null true true", code);
607+
Utils.assertWithAllModes_ES6("null false false", code);
610608
Utils.assertWithAllModes_1_8("null true true", code);
611609
}
612610

0 commit comments

Comments
 (0)