Skip to content

Commit 71030b0

Browse files
committed
streamline fallback code and improved type check
1 parent 7096411 commit 71030b0

File tree

5 files changed

+52
-17
lines changed

5 files changed

+52
-17
lines changed

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ public boolean has(String name, Scriptable start) {
184184
return booleanTrapResult;
185185
}
186186

187-
return ScriptableObject.hasProperty(target, name);
187+
if (start == this) {
188+
start = target;
189+
}
190+
return target.has(name, start);
188191
}
189192

190193
/**
@@ -233,7 +236,10 @@ public boolean has(int index, Scriptable start) {
233236
return booleanTrapResult;
234237
}
235238

236-
return ScriptableObject.hasProperty(target, index);
239+
if (start == this) {
240+
start = target;
241+
}
242+
return target.has(index, start);
237243
}
238244

239245
/**
@@ -263,7 +269,11 @@ public boolean has(Symbol key, Scriptable start) {
263269
return booleanTrapResult;
264270
}
265271

266-
return ScriptableObject.hasProperty(target, key);
272+
if (start == this) {
273+
start = target;
274+
}
275+
SymbolScriptable symbolScriptableTarget = ensureSymbolScriptable(target);
276+
return symbolScriptableTarget.has(key, start);
267277
}
268278

269279
/**
@@ -438,7 +448,10 @@ public Object get(String name, Scriptable start) {
438448
return trapResult;
439449
}
440450

441-
return ScriptRuntime.getObjectProp(target, name, Context.getContext());
451+
if (start == this) {
452+
start = target;
453+
}
454+
return target.get(name, start);
442455
}
443456

444457
/**
@@ -495,7 +508,10 @@ public Object get(int index, Scriptable start) {
495508
return trapResult;
496509
}
497510

498-
return ScriptRuntime.getObjectIndex(target, index, Context.getContext());
511+
if (start == this) {
512+
start = target;
513+
}
514+
return target.get(index, start);
499515
}
500516

501517
/**
@@ -612,7 +628,10 @@ public void put(String name, Scriptable start, Object value) {
612628
return; // true
613629
}
614630

615-
ScriptableObject.putProperty(target, name, value);
631+
if (start == this) {
632+
start = target;
633+
}
634+
target.put(name, start, value);
616635
}
617636

618637
/**
@@ -672,7 +691,10 @@ public void put(int index, Scriptable start, Object value) {
672691
return; // true
673692
}
674693

675-
ScriptableObject.putProperty(target, index, value);
694+
if (start == this) {
695+
start = target;
696+
}
697+
target.put(index, start, value);
676698
}
677699

678700
/**
@@ -1270,11 +1292,10 @@ private static NativeProxy constructor(Context cx, Scriptable scope, Object[] ar
12701292
"2",
12711293
Integer.toString(args.length));
12721294
}
1273-
ScriptableObject trgt = ensureScriptableObject(args[0]);
1274-
1275-
ScriptableObject hndlr = ensureScriptableObject(args[1]);
1295+
ScriptableObject target = ensureScriptableObjectButNotSymbol(args[0]);
1296+
ScriptableObject handler = ensureScriptableObjectButNotSymbol(args[1]);
12761297

1277-
NativeProxy proxy = new NativeProxy(trgt, hndlr);
1298+
NativeProxy proxy = new NativeProxy(target, handler);
12781299
proxy.setPrototypeDirect(ScriptableObject.getClassPrototype(scope, PROXY_TAG));
12791300
proxy.setParentScope(scope);
12801301
return proxy;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,13 @@ protected static ScriptableObject ensureScriptableObject(Object arg) {
19281928
throw ScriptRuntime.typeErrorById("msg.arg.not.object", ScriptRuntime.typeof(arg));
19291929
}
19301930

1931+
protected static ScriptableObject ensureScriptableObjectButNotSymbol(Object arg) {
1932+
if (arg instanceof Symbol) {
1933+
throw ScriptRuntime.typeErrorById("msg.arg.not.object", ScriptRuntime.typeof(arg));
1934+
}
1935+
return ensureScriptableObject(arg);
1936+
}
1937+
19311938
/**
19321939
* Search for names in a class, adding the resulting methods as properties.
19331940
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
package org.mozilla.javascript;
88

99
/**
10-
* A Symbol is a JavaScript objecy that obeys the special properties of the Symbol prototype. This
10+
* A Symbol is a JavaScript object that obeys the special properties of the Symbol prototype. This
1111
* interface lets us possibly support multiple implementations of Symbol.
1212
*
1313
* @since 1.7.8

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public void ctorMissingArgs() {
6363
"try { new Proxy(null, {}) } catch(e) { '' + e }");
6464
}
6565

66+
@Test
67+
public void ctorWrongArgs() {
68+
Utils.assertWithAllOptimizationLevelsES6(
69+
"TypeError: Expected argument of type object, but instead had type symbol",
70+
"try { new Proxy({}, Symbol()) } catch(e) { '' + e }");
71+
Utils.assertWithAllOptimizationLevelsES6(
72+
"TypeError: Expected argument of type object, but instead had type symbol",
73+
"try { new Proxy(Symbol(), {}) } catch(e) { '' + e }");
74+
}
75+
6676
@Test
6777
public void ctorAsFunction() {
6878
Utils.assertWithAllOptimizationLevelsES6(

tests/testsrc/test262.properties

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ built-ins/Promise 392/631 (62.12%)
16191619
resolve-thenable-deferred.js {unsupported: [async]}
16201620
resolve-thenable-immed.js {unsupported: [async]}
16211621

1622-
built-ins/Proxy 79/311 (25.4%)
1622+
built-ins/Proxy 76/311 (24.44%)
16231623
construct/arguments-realm.js
16241624
construct/call-parameters.js
16251625
construct/call-parameters-new-target.js
@@ -1639,7 +1639,7 @@ built-ins/Proxy 79/311 (25.4%)
16391639
deleteProperty/return-false-not-strict.js non-strict
16401640
deleteProperty/return-false-strict.js strict
16411641
deleteProperty/targetdesc-is-configurable-target-is-not-extensible.js
1642-
deleteProperty/trap-is-missing-target-is-proxy.js
1642+
deleteProperty/trap-is-missing-target-is-proxy.js strict
16431643
deleteProperty/trap-is-null-target-is-proxy.js
16441644
deleteProperty/trap-is-undefined-strict.js strict
16451645
deleteProperty/trap-is-undefined-target-is-proxy.js
@@ -1661,7 +1661,6 @@ built-ins/Proxy 79/311 (25.4%)
16611661
has/return-false-target-prop-exists-using-with.js non-strict
16621662
has/return-false-targetdesc-not-configurable-using-with.js non-strict
16631663
has/return-is-abrupt-with.js non-strict
1664-
has/return-true-target-prop-exists-using-with.js non-strict
16651664
has/trap-is-missing-target-is-proxy.js
16661665
has/trap-is-not-callable-using-with.js non-strict
16671666
ownKeys/trap-is-undefined-target-is-proxy.js
@@ -1693,9 +1692,7 @@ built-ins/Proxy 79/311 (25.4%)
16931692
set/trap-is-null-receiver.js
16941693
set/trap-is-null-target-is-proxy.js
16951694
set/trap-is-undefined-target-is-proxy.js
1696-
create-handler-not-object-throw-symbol.js
16971695
create-target-is-not-a-constructor.js
1698-
create-target-not-object-throw-symbol.js
16991696
get-fn-realm.js
17001697
get-fn-realm-recursive.js
17011698
property-order.js

0 commit comments

Comments
 (0)