Skip to content

Commit 900af29

Browse files
OracleLabsAutomationelkorchi
authored andcommitted
[GR-62383] Backport to 24.2: Compilation failure in ImportMetaNode.
PullRequest: js/3447
2 parents 8d1fb86 + 18c267f commit 900af29

File tree

7 files changed

+141
-41
lines changed

7 files changed

+141
-41
lines changed

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/builtins/IteratorHelperPrototypeBuiltins.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import com.oracle.truffle.api.CompilerDirectives;
4444
import com.oracle.truffle.api.dsl.Cached;
45+
import com.oracle.truffle.api.dsl.Cached.Exclusive;
4546
import com.oracle.truffle.api.dsl.Cached.Shared;
4647
import com.oracle.truffle.api.dsl.ImportStatic;
4748
import com.oracle.truffle.api.dsl.Specialization;
@@ -136,7 +137,7 @@ public Object suspendedStart(VirtualFrame frame, JSIteratorHelperObject thisObj,
136137
@Specialization(guards = {"thisObj.getGeneratorState() == SuspendedYield"})
137138
public Object suspendedYield(VirtualFrame frame, JSIteratorHelperObject thisObj,
138139
@Cached("create(getContext())") @Shared IteratorCloseNode outerIteratorCloseNode,
139-
@Cached("create(getContext())") IteratorCloseNode innerIteratorCloseNode) {
140+
@Cached("create(getContext())") @Exclusive IteratorCloseNode innerIteratorCloseNode) {
140141
thisObj.setGeneratorState(JSFunction.GeneratorState.Executing);
141142

142143
try {

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/builtins/ObjectPrototypeBuiltins.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -394,6 +394,12 @@ protected static TruffleString uncached(JSObject object) {
394394

395395
public abstract static class FormatCacheNode extends JavaScriptBaseNode {
396396

397+
@Child TruffleString.EqualNode equalsNode;
398+
399+
protected FormatCacheNode() {
400+
this.equalsNode = TruffleString.EqualNode.create();
401+
}
402+
397403
public abstract TruffleString execute(TruffleString name);
398404

399405
public static FormatCacheNode create() {
@@ -404,8 +410,7 @@ public static FormatCacheNode create() {
404410
@Specialization(guards = {"stringEquals(equalsNode, cachedName, name)"}, limit = "10")
405411
protected TruffleString doCached(TruffleString name,
406412
@Cached("name") TruffleString cachedName,
407-
@Cached("doUncached(name)") TruffleString cachedResult,
408-
@Cached TruffleString.EqualNode equalsNode) {
413+
@Cached("doUncached(name)") TruffleString cachedResult) {
409414
return cachedResult;
410415
}
411416

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/nodes/CompileRegexNode.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -55,11 +55,12 @@
5555
import com.oracle.truffle.js.runtime.RegexCompilerInterface;
5656
import com.oracle.truffle.js.runtime.Strings;
5757

58-
@ImportStatic(JSConfig.class)
58+
@ImportStatic({JSConfig.class, Strings.class})
5959
public abstract class CompileRegexNode extends JavaScriptBaseNode {
6060

6161
private final JSContext context;
6262
@Child private InteropLibrary isCompiledRegexNullNode;
63+
@Child TruffleString.EqualNode equalsNode = TruffleString.EqualNode.create();
6364

6465
protected CompileRegexNode(JSContext context) {
6566
this.context = context;
@@ -81,13 +82,11 @@ public final Object compile(Object pattern, Object flags) {
8182
protected abstract Object executeCompile(Object pattern, Object flags);
8283

8384
@SuppressWarnings("unused")
84-
@Specialization(guards = {"stringEquals(equalsNode, pattern, cachedPattern)", "stringEquals(equalsNode2, flags, cachedFlags)"}, limit = "MaxCompiledRegexCacheLength")
85+
@Specialization(guards = {"equals(equalsNode, pattern, cachedPattern)", "equals(equalsNode, flags, cachedFlags)"}, limit = "MaxCompiledRegexCacheLength")
8586
protected Object getCached(TruffleString pattern, TruffleString flags,
8687
@Cached("pattern") TruffleString cachedPattern,
8788
@Cached("flags") TruffleString cachedFlags,
8889
@Cached("createAssumedValue()") AssumedValue<Object> cachedCompiledRegex,
89-
@Cached TruffleString.EqualNode equalsNode,
90-
@Cached TruffleString.EqualNode equalsNode2,
9190
@Cached @Shared TruffleString.ToJavaStringNode toJavaString) {
9291
// Note: we must not compile the regex while holding the AST lock (initializing @Cached).
9392
Object cached = cachedCompiledRegex.get();
@@ -98,10 +97,6 @@ protected Object getCached(TruffleString pattern, TruffleString flags,
9897
return cached;
9998
}
10099

101-
protected static boolean stringEquals(TruffleString.EqualNode node, TruffleString a, TruffleString b) {
102-
return Strings.equals(node, a, b);
103-
}
104-
105100
@Specialization(guards = {"!TrimCompiledRegexCache"})
106101
protected Object doCompileNoTrimCache(TruffleString pattern, TruffleString flags,
107102
@Cached @Shared TruffleString.ToJavaStringNode toJavaString) {

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/nodes/binary/JSExponentiateNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -81,8 +81,8 @@ protected double doDouble(double a, double b) {
8181
}
8282

8383
@Specialization(guards = "isBigIntNegativeVal(b)")
84-
protected void doBigIntNegativeExponent(@SuppressWarnings("unused") BigInt a, @SuppressWarnings("unused") BigInt b) {
85-
throw Errors.createRangeError("Exponent must be positve");
84+
protected Object doBigIntNegativeExponent(@SuppressWarnings("unused") BigInt a, @SuppressWarnings("unused") BigInt b) {
85+
throw Errors.createRangeError("BigInt exponent must not be negative");
8686
}
8787

8888
@Specialization(guards = {"isBigIntZero(a)", "!isBigIntZero(b)", "!isBigIntNegativeVal(b)"})
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2025, 2025, 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.nodes.module;
42+
43+
import com.oracle.truffle.api.dsl.Bind;
44+
import com.oracle.truffle.api.dsl.Cached;
45+
import com.oracle.truffle.api.dsl.Fallback;
46+
import com.oracle.truffle.api.dsl.ImportStatic;
47+
import com.oracle.truffle.api.dsl.NeverDefault;
48+
import com.oracle.truffle.api.dsl.Specialization;
49+
import com.oracle.truffle.api.strings.TruffleString;
50+
import com.oracle.truffle.js.nodes.JavaScriptBaseNode;
51+
import com.oracle.truffle.js.nodes.access.CreateDataPropertyNode;
52+
import com.oracle.truffle.js.runtime.JSContext;
53+
import com.oracle.truffle.js.runtime.Strings;
54+
import com.oracle.truffle.js.runtime.builtins.JSOrdinary;
55+
import com.oracle.truffle.js.runtime.objects.JSModuleRecord;
56+
import com.oracle.truffle.js.runtime.objects.JSObject;
57+
58+
/**
59+
* Create the {@code import.meta} object of a module.
60+
*/
61+
@ImportStatic({Strings.class})
62+
public abstract class CreateImportMetaNode extends JavaScriptBaseNode {
63+
64+
protected CreateImportMetaNode() {
65+
}
66+
67+
@NeverDefault
68+
public static CreateImportMetaNode create() {
69+
return CreateImportMetaNodeGen.create();
70+
}
71+
72+
public abstract JSObject execute(JSModuleRecord module);
73+
74+
@Specialization(guards = {"context.hasImportMetaInitializerBeenSet()"})
75+
protected static JSObject doCustomInitializer(JSModuleRecord module,
76+
@Bind("getJSContext()") JSContext context) {
77+
JSObject metaObj = JSOrdinary.createWithNullPrototype(context);
78+
context.notifyImportMetaInitializer(metaObj, module);
79+
return metaObj;
80+
}
81+
82+
@Fallback
83+
protected static JSObject doDefaultInitializer(JSModuleRecord module,
84+
@Bind("getJSContext()") JSContext context,
85+
@Cached(parameters = {"context", "URL"}) CreateDataPropertyNode setURINode,
86+
@Cached TruffleString.FromJavaStringNode fromJavaString) {
87+
JSObject metaObj = JSOrdinary.createWithNullPrototype(context);
88+
setURINode.executeVoid(metaObj, Strings.fromJavaString(fromJavaString, module.getURL()));
89+
return metaObj;
90+
}
91+
}

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/nodes/module/ImportMetaNode.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -42,30 +42,43 @@
4242

4343
import java.util.Set;
4444

45-
import com.oracle.truffle.api.frame.VirtualFrame;
45+
import com.oracle.truffle.api.dsl.Bind;
46+
import com.oracle.truffle.api.dsl.Cached;
47+
import com.oracle.truffle.api.dsl.Executed;
48+
import com.oracle.truffle.api.dsl.Fallback;
49+
import com.oracle.truffle.api.dsl.NeverDefault;
50+
import com.oracle.truffle.api.dsl.Specialization;
4651
import com.oracle.truffle.api.instrumentation.Tag;
4752
import com.oracle.truffle.js.nodes.JavaScriptNode;
4853
import com.oracle.truffle.js.runtime.objects.JSModuleRecord;
54+
import com.oracle.truffle.js.runtime.objects.JSObject;
4955

5056
/**
5157
* Returns the {@code import.meta} object of a module, initializing it if necessary.
5258
*/
53-
public class ImportMetaNode extends JavaScriptNode {
59+
public abstract class ImportMetaNode extends JavaScriptNode {
5460

55-
@Child private JavaScriptNode moduleNode;
61+
@Child @Executed JavaScriptNode moduleNode;
5662

5763
ImportMetaNode(JavaScriptNode moduleNode) {
5864
this.moduleNode = moduleNode;
5965
}
6066

67+
@NeverDefault
6168
public static JavaScriptNode create(JavaScriptNode moduleNode) {
62-
return new ImportMetaNode(moduleNode);
69+
return ImportMetaNodeGen.create(moduleNode);
6370
}
6471

65-
@Override
66-
public Object execute(VirtualFrame frame) {
67-
JSModuleRecord module = (JSModuleRecord) moduleNode.execute(frame);
68-
return module.getImportMeta();
72+
@Specialization(guards = {"importMeta != null"})
73+
protected static JSObject getImportMeta(@SuppressWarnings("unused") JSModuleRecord module,
74+
@Bind("module.getImportMetaOrNull()") JSObject importMeta) {
75+
return importMeta;
76+
}
77+
78+
@Fallback
79+
protected static JSObject createImportMeta(Object module,
80+
@Cached CreateImportMetaNode createImportMetaNode) {
81+
return ((JSModuleRecord) module).getImportMeta(createImportMetaNode);
6982
}
7083

7184
@Override

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/objects/JSModuleRecord.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -50,18 +50,18 @@
5050
import com.oracle.js.parser.ir.Module.ExportEntry;
5151
import com.oracle.js.parser.ir.Module.ModuleRequest;
5252
import com.oracle.truffle.api.CompilerAsserts;
53+
import com.oracle.truffle.api.CompilerDirectives;
5354
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5455
import com.oracle.truffle.api.frame.FrameDescriptor;
5556
import com.oracle.truffle.api.source.Source;
5657
import com.oracle.truffle.api.strings.TruffleString;
58+
import com.oracle.truffle.js.nodes.module.CreateImportMetaNode;
5759
import com.oracle.truffle.js.runtime.Errors;
5860
import com.oracle.truffle.js.runtime.JSArguments;
5961
import com.oracle.truffle.js.runtime.JSRealm;
60-
import com.oracle.truffle.js.runtime.Strings;
6162
import com.oracle.truffle.js.runtime.builtins.JSFunction;
6263
import com.oracle.truffle.js.runtime.builtins.JSFunctionData;
6364
import com.oracle.truffle.js.runtime.builtins.JSFunctionObject;
64-
import com.oracle.truffle.js.runtime.builtins.JSOrdinary;
6565
import com.oracle.truffle.js.runtime.builtins.JSPromiseObject;
6666
import com.oracle.truffle.js.runtime.util.Pair;
6767

@@ -74,7 +74,7 @@ public class JSModuleRecord extends CyclicModuleRecord {
7474
private final JSModuleLoader moduleLoader;
7575

7676
/** Lazily initialized import.meta object ({@code [[ImportMeta]]}). */
77-
private JSDynamicObject importMeta;
77+
private JSObject importMeta;
7878

7979
public JSModuleRecord(JSModuleData parsedModule, JSModuleLoader moduleLoader) {
8080
this(parsedModule, moduleLoader, null);
@@ -107,26 +107,21 @@ public JSModuleData getModuleData() {
107107
return parsedModule;
108108
}
109109

110-
public JSDynamicObject getImportMeta() {
111-
if (importMeta == null) {
112-
importMeta = createMetaObject();
113-
}
110+
public JSObject getImportMetaOrNull() {
114111
return importMeta;
115112
}
116113

117-
private JSDynamicObject createMetaObject() {
118-
JSObject metaObj = JSOrdinary.createWithNullPrototype(context);
119-
if (context.hasImportMetaInitializerBeenSet()) {
120-
context.notifyImportMetaInitializer(metaObj, this);
121-
} else {
122-
initializeMetaObject(metaObj);
114+
public JSObject getImportMeta(CreateImportMetaNode createImportMeta) {
115+
JSObject metaObj = importMeta;
116+
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.SLOWPATH_PROBABILITY, metaObj == null)) {
117+
importMeta = metaObj = createImportMeta.execute(this);
123118
}
124119
return metaObj;
125120
}
126121

127122
@TruffleBoundary
128-
private void initializeMetaObject(JSObject metaObj) {
129-
JSObject.set(metaObj, Strings.URL, Strings.fromJavaString(getSource().getURI().toString()));
123+
public String getURL() {
124+
return getSource().getURI().toString();
130125
}
131126

132127
@Override

0 commit comments

Comments
 (0)