Skip to content

Commit

Permalink
Fix some NPEs in tests (#987)
Browse files Browse the repository at this point in the history
Fix some NullPointerExceptions triggered by test262 tests. Also, fix the handling of "this" in prototype functions of Symbol.
  • Loading branch information
gbrail authored Jul 2, 2021
1 parent e721bb4 commit 3e205d7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ private void visitLiteral(Node node, Node child) {
}
} else if (type == Token.OBJECTLIT) {
propertyIds = (Object[]) node.getProp(Node.OBJECT_IDS_PROP);
count = propertyIds.length;
count = propertyIds == null ? 0 : propertyIds.length;
} else {
throw badTree(node);
}
Expand Down
8 changes: 4 additions & 4 deletions src/org/mozilla/javascript/NativeSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,18 @@ public Object execIdCall(
return construct(cx, scope, args);

case Id_toString:
return getSelf(thisObj).toString();
return getSelf(cx, scope, thisObj).toString();
case Id_valueOf:
case SymbolId_toPrimitive:
return getSelf(thisObj).js_valueOf();
return getSelf(cx, scope, thisObj).js_valueOf();
default:
return super.execIdCall(f, cx, scope, thisObj, args);
}
}

private static NativeSymbol getSelf(Object thisObj) {
private static NativeSymbol getSelf(Context cx, Scriptable scope, Object thisObj) {
try {
return (NativeSymbol) thisObj;
return (NativeSymbol) ScriptRuntime.toObject(cx, scope, thisObj);
} catch (ClassCastException cce) {
throw ScriptRuntime.typeErrorById("msg.invalid.type", thisObj.getClass().getName());
}
Expand Down
6 changes: 5 additions & 1 deletion src/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ void addError(String messageId, int position, int length) {
}

void addError(String messageId, String messageArg) {
addError(messageId, messageArg, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg);
if (ts == null) {
addError(messageId, messageArg, 0, 0);
} else {
addError(messageId, messageArg, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg);
}
}

void addError(String messageId, int c) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/optimizer/BodyCodegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -2110,7 +2110,7 @@ private void addLoadPropertyValues(Node node, Node child, int count) {

private void visitObjectLiteral(Node node, Node child, boolean topLevel) {
Object[] properties = (Object[]) node.getProp(Node.OBJECT_IDS_PROP);
int count = properties.length;
int count = properties == null ? 0 : properties.length;

// If code budget is tight swap out literals into separate method
if (!topLevel
Expand Down
26 changes: 11 additions & 15 deletions src/org/mozilla/javascript/optimizer/OptTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */


package org.mozilla.javascript.optimizer;

import java.util.Map;

import org.mozilla.javascript.Kit;
import org.mozilla.javascript.Node;
import org.mozilla.javascript.NodeTransformer;
Expand All @@ -20,11 +18,9 @@
* @see NodeTransformer
* @author Norris Boyd
*/

class OptTransformer extends NodeTransformer {

OptTransformer(Map<String,OptFunctionNode> possibleDirectCalls, ObjArray directCallTargets)
{
OptTransformer(Map<String, OptFunctionNode> possibleDirectCalls, ObjArray directCallTargets) {
this.possibleDirectCalls = possibleDirectCalls;
this.directCallTargets = directCallTargets;
}
Expand All @@ -41,17 +37,18 @@ protected void visitCall(Node node, ScriptNode tree) {
super.visitCall(node, tree);
}

private void detectDirectCall(Node node, ScriptNode tree)
{
private void detectDirectCall(Node node, ScriptNode tree) {
if (tree.getType() == Token.FUNCTION) {
Node left = node.getFirstChild();

// count the arguments
int argCount = 0;
Node arg = left.getNext();
while (arg != null) {
arg = arg.getNext();
argCount++;
if (left != null) {
Node arg = left.getNext();
while (arg != null) {
arg = arg.getNext();
argCount++;
}
}

if (argCount == 0) {
Expand Down Expand Up @@ -83,9 +80,8 @@ private void detectDirectCall(Node node, ScriptNode tree)
OptFunctionNode ofn;
ofn = possibleDirectCalls.get(targetName);
if (ofn != null
&& argCount == ofn.fnode.getParamCount()
&& !ofn.fnode.requiresActivation())
{
&& argCount == ofn.fnode.getParamCount()
&& !ofn.fnode.requiresActivation()) {
// Refuse to directCall any function with more
// than 32 parameters - prevent code explosion
// for wacky test cases
Expand All @@ -103,6 +99,6 @@ private void detectDirectCall(Node node, ScriptNode tree)
}
}

private Map<String,OptFunctionNode> possibleDirectCalls;
private Map<String, OptFunctionNode> possibleDirectCalls;
private ObjArray directCallTargets;
}
10 changes: 3 additions & 7 deletions testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ built-ins/String 121/1114 (10.86%)

built-ins/StringIteratorPrototype 0/7 (0.0%)

built-ins/Symbol 32/85 (37.65%)
built-ins/Symbol 30/85 (35.29%)
asyncIterator 2/2 (100.0%)
for/cross-realm.js {unsupported: [cross-realm]}
hasInstance/cross-realm.js {unsupported: [cross-realm]}
Expand All @@ -1324,8 +1324,6 @@ built-ins/Symbol 32/85 (37.65%)
prototype/description/wrapper.js
prototype/Symbol.toPrimitive/name.js
prototype/Symbol.toPrimitive/prop-desc.js
prototype/Symbol.toPrimitive/this-val-non-obj.js
prototype/valueOf/this-val-non-obj.js
replace/cross-realm.js {unsupported: [cross-realm]}
search/cross-realm.js {unsupported: [cross-realm]}
species 4/4 (100.0%)
Expand Down Expand Up @@ -5401,7 +5399,7 @@ language/statements/for 263/384 (68.49%)
tco-lhs-body.js {unsupported: [tail-call-optimization]}
tco-var-body.js {unsupported: [tail-call-optimization]}

language/statements/for-in 44/114 (38.6%)
language/statements/for-in 43/114 (37.72%)
dstr/obj-rest-not-last-element-invalid.js {unsupported: [object-rest]}
12.6.4-2.js
cptn-decl-abrupt-empty.js
Expand All @@ -5419,7 +5417,6 @@ language/statements/for-in 44/114 (38.6%)
decl-gen.js
head-const-bound-names-fordecl-tdz.js
head-const-fresh-binding-per-iteration.js
head-let-bound-names-dup.js
head-let-bound-names-fordecl-tdz.js
head-let-bound-names-in-stmt.js
head-let-destructuring.js
Expand Down Expand Up @@ -5447,7 +5444,7 @@ language/statements/for-in 44/114 (38.6%)
scope-head-lex-open.js
scope-head-var-none.js non-strict

language/statements/for-of 472/725 (65.1%)
language/statements/for-of 471/725 (64.97%)
dstr/array-elem-init-assignment.js
dstr/array-elem-init-evaluation.js
dstr/array-elem-init-fn-name-arrow.js
Expand Down Expand Up @@ -5888,7 +5885,6 @@ language/statements/for-of 472/725 (65.1%)
head-const-fresh-binding-per-iteration.js
head-decl-no-expr.js
head-expr-no-expr.js
head-let-bound-names-dup.js
head-let-bound-names-fordecl-tdz.js
head-let-bound-names-in-stmt.js
head-let-fresh-binding-per-iteration.js
Expand Down

0 comments on commit 3e205d7

Please sign in to comment.