Skip to content

Commit ed1eee6

Browse files
committed
[GR-39223] Allow to enable intl-402 option in ScriptEngine.
PullRequest: js/2490
2 parents fa1737a + 474af3c commit ed1eee6

File tree

2 files changed

+110
-0
lines changed
  • graal-js/src
    • com.oracle.truffle.js.scriptengine.test/src/com/oracle/truffle/js/scriptengine/test
    • com.oracle.truffle.js.scriptengine/src/com/oracle/truffle/js/scriptengine

2 files changed

+110
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2022, 2022, 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.scriptengine.test;
42+
43+
import static org.junit.Assert.assertEquals;
44+
import static org.junit.Assert.assertTrue;
45+
import static org.junit.Assert.fail;
46+
47+
import javax.script.Bindings;
48+
import javax.script.ScriptContext;
49+
import javax.script.ScriptEngine;
50+
import javax.script.ScriptEngineManager;
51+
import javax.script.ScriptException;
52+
import org.junit.Test;
53+
54+
public class GR39223 {
55+
56+
private static ScriptEngine getEngine() {
57+
ScriptEngineManager manager = new ScriptEngineManager();
58+
return manager.getEngineByName(TestEngine.TESTED_ENGINE_NAME);
59+
}
60+
61+
@Test
62+
public void testIntlDefault() throws ScriptException {
63+
ScriptEngine engine = getEngine();
64+
Object result = engine.eval("typeof Intl");
65+
assertEquals("undefined", result);
66+
}
67+
68+
@Test
69+
public void testIntlEnabled() throws ScriptException {
70+
ScriptEngine engine = getEngine();
71+
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
72+
bindings.put("polyglot.js.intl-402", true);
73+
Object result = engine.eval("typeof Intl");
74+
assertEquals("object", result);
75+
}
76+
77+
@Test
78+
public void testIntlDisabled() throws ScriptException {
79+
ScriptEngine engine = getEngine();
80+
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
81+
bindings.put("polyglot.js.intl-402", false);
82+
Object result = engine.eval("typeof Intl");
83+
assertEquals("undefined", result);
84+
}
85+
86+
@Test
87+
public void testIntlInvalid() {
88+
ScriptEngine engine = getEngine();
89+
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
90+
try {
91+
bindings.put("polyglot.js.intl-402", "foo");
92+
fail("Exception expected");
93+
} catch (IllegalArgumentException ex) {
94+
String message = ex.getMessage();
95+
assertTrue(message, message.contains("polyglot.js.intl-402"));
96+
}
97+
}
98+
99+
}

graal-js/src/com.oracle.truffle.js.scriptengine/src/com/oracle/truffle/js/scriptengine/GraalJSScriptEngine.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,17 @@ public String getOptionKey() {
271271
public Builder setOption(Builder builder, Object value) {
272272
return builder.option("js.ecmascript-version", String.valueOf(value));
273273
}
274+
}, new MagicBindingsOptionSetter() {
275+
276+
@Override
277+
public String getOptionKey() {
278+
return MAGIC_OPTION_PREFIX + "intl-402";
279+
}
280+
281+
@Override
282+
public Builder setOption(Builder builder, Object value) {
283+
return builder.option("js.intl-402", String.valueOf(toBoolean(this, value)));
284+
}
274285
}};
275286

276287
private static final EconomicSet<String> MAGIC_BINDINGS_OPTION_KEYS = EconomicSet.create();

0 commit comments

Comments
 (0)