Skip to content

Commit 77e978f

Browse files
committed
[GR-22168] Increase test coverage.
PullRequest: js/1452
2 parents cd72398 + 021f5fe commit 77e978f

File tree

6 files changed

+140
-13
lines changed

6 files changed

+140
-13
lines changed

graal-js/src/com.oracle.truffle.js.parser/src/com/oracle/truffle/js/parser/GraalJSTranslator.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,6 @@ protected final ScriptNode translateScript(FunctionNode functionNode) {
286286
return ScriptNode.fromFunctionRoot(context, functionExpression.getFunctionNode());
287287
}
288288

289-
protected final JavaScriptNode translateExpression(Expression expression) {
290-
try (EnvironmentCloseable dummyFunctionEnv = enterFunctionEnvironment(true, false, false, false, false, false)) {
291-
currentFunction().setNeedsParentFrame(true);
292-
currentFunction().freeze(); // cannot add frame slots
293-
return transform(expression);
294-
}
295-
}
296-
297289
protected final JavaScriptNode transformFunction(FunctionNode functionNode) {
298290
return transform(functionNode);
299291
}

graal-js/src/com.oracle.truffle.js.parser/src/com/oracle/truffle/js/parser/JavaScriptTranslator.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import com.oracle.js.parser.ir.Scope;
4646
import com.oracle.truffle.api.source.Source;
4747
import com.oracle.truffle.js.lang.JavaScriptLanguage;
48-
import com.oracle.truffle.js.nodes.JavaScriptNode;
4948
import com.oracle.truffle.js.nodes.NodeFactory;
5049
import com.oracle.truffle.js.nodes.ScriptNode;
5150
import com.oracle.truffle.js.nodes.function.FunctionRootNode;
@@ -113,10 +112,6 @@ public static ScriptNode translateFunction(NodeFactory factory, JSContext contex
113112
return new JavaScriptTranslator(factory, context, source, prologLength, env, isParentStrict).translateScript(rootNode);
114113
}
115114

116-
public static JavaScriptNode translateExpression(NodeFactory factory, JSContext context, Environment env, Source source, boolean isParentStrict, com.oracle.js.parser.ir.Expression expression) {
117-
return new JavaScriptTranslator(factory, context, source, 0, env, isParentStrict).translateExpression(expression);
118-
}
119-
120115
public static JSModuleRecord translateModule(NodeFactory factory, JSContext context, Source source, JSModuleLoader moduleLoader) {
121116
FunctionNode parsed = GraalJSParserHelper.parseModule(context, source, context.getParserOptions().putStrict(true));
122117
JavaScriptTranslator translator = new JavaScriptTranslator(factory, context, source, 0, null, true);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
* Test otherwise untested indirect eval.
9+
*/
10+
11+
load('../assert.js');
12+
13+
var indirecteval = eval;
14+
assertSame(42, indirecteval(42));
15+
assertSame(42n, indirecteval(42n));
16+
assertSame(42, indirecteval(Debug.createSafeInteger(42)));
17+
assertSame(42.5, indirecteval(42.5));
18+
assertSame(true, indirecteval(true));
19+
20+
var symbol = Symbol('test');
21+
assertSame(symbol, indirecteval(symbol));
22+
23+
var obj = {foo:'bar'};
24+
assertSame(obj, indirecteval(obj));
25+
26+
true;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
* Test otherwise untested JSToObjectArray node.
9+
*/
10+
11+
load('../assert.js');
12+
13+
function fn() { return arguments; }
14+
var obj = {};
15+
16+
function testReflect(value) {
17+
assertFail( ()=>{Reflect.apply(fn, obj, value);}, "is not an Object");
18+
}
19+
20+
testReflect(true);
21+
testReflect(42);
22+
testReflect(42.5);
23+
testReflect("test");
24+
25+
true;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.truffle.js.test.nashorn;
42+
43+
import org.graalvm.polyglot.Context;
44+
import org.graalvm.polyglot.Source;
45+
import org.graalvm.polyglot.Value;
46+
import org.junit.Assert;
47+
import org.junit.Test;
48+
49+
import com.oracle.truffle.js.lang.JavaScriptLanguage;
50+
import com.oracle.truffle.js.runtime.JSContextOptions;
51+
import com.oracle.truffle.js.test.JSTest;
52+
53+
public class NashornGlobalTest {
54+
private static boolean testIntl(String sourceText) {
55+
try (Context context = JSTest.newContextBuilder().option(JSContextOptions.NASHORN_COMPATIBILITY_MODE_NAME,
56+
"true").option(JSContextOptions.SCRIPTING_NAME, "true").allowAllAccess(true).build()) {
57+
Value result = context.eval(Source.newBuilder(JavaScriptLanguage.ID, sourceText, "nashorn-global-test").buildLiteral());
58+
Assert.assertTrue(result.isBoolean());
59+
return result.asBoolean();
60+
}
61+
}
62+
63+
@Test
64+
public void testEXEC() {
65+
Assert.assertTrue(testIntl("var a = $EXEC('ls'); a.length > 0;"));
66+
}
67+
68+
@Test
69+
public void testLoadFileNonExistent() {
70+
String src = "var ret=false; var FILE = Java.type('java.io.File'); \n" +
71+
"try { load(new FILE('nonexistent.file')); } \n" +
72+
"catch (ex) { ret = ex instanceof EvalError && ex.message.indexOf('nonexistent.file') >= 0; }; \n" +
73+
"ret;";
74+
Assert.assertTrue(testIntl(src));
75+
}
76+
77+
@Test
78+
public void testLoadURLNonExistent() {
79+
String src = "var ret=false; var URL = Java.type('java.net.URL'); \n" +
80+
"try { load(new URL('file://nonexistent.file')); } \n" +
81+
"catch (ex) { ret = ex instanceof EvalError && ex.message.indexOf('nonexistent.file') >= 0; }; \n" +
82+
"ret;";
83+
Assert.assertTrue(testIntl(src));
84+
}
85+
}

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/builtins/JSCollator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.truffle.api.object.LocationModifier;
6363
import com.oracle.truffle.api.object.Property;
6464
import com.oracle.truffle.api.object.Shape;
65+
import com.oracle.truffle.api.profiles.BranchProfile;
6566
import com.oracle.truffle.js.builtins.intl.CollatorFunctionBuiltins;
6667
import com.oracle.truffle.js.builtins.intl.CollatorPrototypeBuiltins;
6768
import com.oracle.truffle.js.lang.JavaScriptLanguage;
@@ -348,6 +349,7 @@ private static CallTarget createGetCompareCallTarget(JSContext context) {
348349

349350
@CompilationFinal private ContextReference<JSRealm> realmRef;
350351
@Child private PropertySetNode setBoundObjectNode = PropertySetNode.createSetHidden(BOUND_OBJECT_KEY, context);
352+
private final BranchProfile errorBranch = BranchProfile.create();
351353

352354
@Override
353355
public Object execute(VirtualFrame frame) {
@@ -360,6 +362,7 @@ public Object execute(VirtualFrame frame) {
360362
InternalState state = getInternalState((DynamicObject) collatorObj);
361363

362364
if (state == null || !state.initializedCollator) {
365+
errorBranch.enter();
363366
throw Errors.createTypeErrorMethodCalledOnNonObjectOrWrongType("compare");
364367
}
365368

@@ -385,6 +388,7 @@ public Object execute(VirtualFrame frame) {
385388

386389
return state.boundCompareFunction;
387390
}
391+
errorBranch.enter();
388392
throw Errors.createTypeErrorTypeXExpected(CLASS_NAME);
389393
}
390394
});

0 commit comments

Comments
 (0)