Skip to content

Commit 83f6d64

Browse files
committed
New operator should not forget to evaluate arguments.
1 parent 0d853db commit 83f6d64

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
6+
*/
7+
8+
/**
9+
* Tests of the evaluation of 'new' operator.
10+
*
11+
* @option nashorn-compat
12+
*/
13+
14+
load('assert.js');
15+
16+
(function missingClassArgumentsEvaluated() {
17+
var constructor = java.ThisClassDoesNotExist;
18+
19+
assertFail(function() {
20+
new constructor(constructor = Object);
21+
});
22+
23+
assertSame(Object, constructor);
24+
})();
25+
26+
(function jsAdapterArgumentsEvaluated() {
27+
var constructor = new JSAdapter({});
28+
29+
try {
30+
new constructor(constructor = Object);
31+
} catch (e) {
32+
// GraalJS does not throw but Nashorn throws java.lang.invoke.WrongMethodTypeException: Parameter counts differ.
33+
// This test checks the evaluation of arguments which should be performed no matter if this kind of error is thrown or not
34+
// => this test does not care whether the error is thrown.
35+
}
36+
37+
assertSame(Object, constructor);
38+
})();
39+
40+
true;

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/nodes/function/JSNewNode.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ public Object doNewReturnThis(VirtualFrame frame, DynamicObject target) {
157157

158158
@Specialization(guards = "isJSAdapter(target)")
159159
public Object doJSAdapter(VirtualFrame frame, DynamicObject target) {
160+
Object[] args = getAbstractFunctionArguments(frame);
160161
Object newFunction = JSObject.get(JSAdapter.getAdaptee(target), JSAdapter.NEW);
161162
if (JSFunction.isJSFunction(newFunction)) {
162-
Object[] args = getAbstractFunctionArguments(frame);
163163
return JSFunction.call((DynamicObject) newFunction, target, args);
164164
} else {
165165
return Undefined.instance;
@@ -197,9 +197,9 @@ protected Object doNewJSProxy(VirtualFrame frame, DynamicObject proxy) {
197197
return result;
198198
}
199199

200-
@TruffleBoundary
201200
@Specialization(guards = "isJavaPackage(target)")
202-
public Object createClassNotFoundError(DynamicObject target) {
201+
public Object createClassNotFoundError(VirtualFrame frame, DynamicObject target) {
202+
getAbstractFunctionArguments(frame);
203203
throw Errors.createTypeErrorClassNotFound(JavaPackage.getPackageName(target));
204204
}
205205

@@ -253,9 +253,14 @@ private Object extend(Class<?> type, TruffleLanguage.Env env) {
253253
return env.asHostSymbol(adapterClass);
254254
}
255255

256-
@TruffleBoundary
257256
@Specialization(guards = {"!isJSFunction(target)", "!isJSAdapter(target)", "!isProxy(target)", "!isJavaPackage(target)", "!isForeignObject(target)"})
258-
public Object createFunctionTypeError(Object target) {
257+
public Object createFunctionTypeError(VirtualFrame frame, Object target) {
258+
getAbstractFunctionArguments(frame);
259+
return throwFunctionTypeError(target);
260+
}
261+
262+
@TruffleBoundary
263+
private Object throwFunctionTypeError(Object target) {
259264
String targetStr = getTarget().expressionToString();
260265
Object targetForError = targetStr == null ? target : targetStr;
261266
if (context.isOptionNashornCompatibilityMode()) {

graal-js/test/test262.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,14 +1697,6 @@
16971697
"filePath" : "language/expressions/dynamic-import/usage-from-eval.js",
16981698
"status" : "FAIL",
16991699
"comment" : "TODO: evaluate (Test262 tests update 2018-10-29)"
1700-
}, {
1701-
"filePath" : "language/expressions/new/ctorExpr-isCtor-after-args-eval-fn-wrapup.js",
1702-
"status" : "FAIL",
1703-
"comment" : "new failures update 2020-04-01"
1704-
}, {
1705-
"filePath" : "language/expressions/new/ctorExpr-isCtor-after-args-eval.js",
1706-
"status" : "FAIL",
1707-
"comment" : "new failures update 2020-04-01"
17081700
}, {
17091701
"filePath" : "language/statements/class/accessor-name-inst-computed-yield-expr.js",
17101702
"status" : "FAIL",

0 commit comments

Comments
 (0)