Skip to content

Commit

Permalink
Fix partial-evaluation-constant assertion failure in ImportMetaNode.
Browse files Browse the repository at this point in the history
  • Loading branch information
woess committed Jan 28, 2025
1 parent e4702fb commit 784d46f
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.js.nodes.module;

import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.NeverDefault;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.strings.TruffleString;
import com.oracle.truffle.js.nodes.JavaScriptBaseNode;
import com.oracle.truffle.js.nodes.access.CreateDataPropertyNode;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.Strings;
import com.oracle.truffle.js.runtime.builtins.JSOrdinary;
import com.oracle.truffle.js.runtime.objects.JSModuleRecord;
import com.oracle.truffle.js.runtime.objects.JSObject;

/**
* Create the {@code import.meta} object of a module.
*/
@ImportStatic({Strings.class})
public abstract class CreateImportMetaNode extends JavaScriptBaseNode {

protected CreateImportMetaNode() {
}

@NeverDefault
public static CreateImportMetaNode create() {
return CreateImportMetaNodeGen.create();
}

public abstract JSObject execute(JSModuleRecord module);

@Specialization(guards = {"context.hasImportMetaInitializerBeenSet()"})
protected static JSObject doCustomInitializer(JSModuleRecord module,
@Bind("getJSContext()") JSContext context) {
JSObject metaObj = JSOrdinary.createWithNullPrototype(context);
context.notifyImportMetaInitializer(metaObj, module);
return metaObj;
}

@Fallback
protected static JSObject doDefaultInitializer(JSModuleRecord module,
@Bind("getJSContext()") JSContext context,
@Cached(parameters = {"context", "URL"}) CreateDataPropertyNode setURINode,
@Cached TruffleString.FromJavaStringNode fromJavaString) {
JSObject metaObj = JSOrdinary.createWithNullPrototype(context);
setURINode.executeVoid(metaObj, Strings.fromJavaString(fromJavaString, module.getURL()));
return metaObj;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -42,30 +42,43 @@

import java.util.Set;

import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Executed;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.NeverDefault;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.instrumentation.Tag;
import com.oracle.truffle.js.nodes.JavaScriptNode;
import com.oracle.truffle.js.runtime.objects.JSModuleRecord;
import com.oracle.truffle.js.runtime.objects.JSObject;

/**
* Returns the {@code import.meta} object of a module, initializing it if necessary.
*/
public class ImportMetaNode extends JavaScriptNode {
public abstract class ImportMetaNode extends JavaScriptNode {

@Child private JavaScriptNode moduleNode;
@Child @Executed JavaScriptNode moduleNode;

ImportMetaNode(JavaScriptNode moduleNode) {
this.moduleNode = moduleNode;
}

@NeverDefault
public static JavaScriptNode create(JavaScriptNode moduleNode) {
return new ImportMetaNode(moduleNode);
return ImportMetaNodeGen.create(moduleNode);
}

@Override
public Object execute(VirtualFrame frame) {
JSModuleRecord module = (JSModuleRecord) moduleNode.execute(frame);
return module.getImportMeta();
@Specialization(guards = {"importMeta != null"})
protected static JSObject getImportMeta(@SuppressWarnings("unused") JSModuleRecord module,
@Bind("module.getImportMetaOrNull()") JSObject importMeta) {
return importMeta;
}

@Fallback
protected static JSObject createImportMeta(Object module,
@Cached CreateImportMetaNode createImportMetaNode) {
return ((JSModuleRecord) module).getImportMeta(createImportMetaNode);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -50,18 +50,18 @@
import com.oracle.js.parser.ir.Module.ExportEntry;
import com.oracle.js.parser.ir.Module.ModuleRequest;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.strings.TruffleString;
import com.oracle.truffle.js.nodes.module.CreateImportMetaNode;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSArguments;
import com.oracle.truffle.js.runtime.JSRealm;
import com.oracle.truffle.js.runtime.Strings;
import com.oracle.truffle.js.runtime.builtins.JSFunction;
import com.oracle.truffle.js.runtime.builtins.JSFunctionData;
import com.oracle.truffle.js.runtime.builtins.JSFunctionObject;
import com.oracle.truffle.js.runtime.builtins.JSOrdinary;
import com.oracle.truffle.js.runtime.builtins.JSPromiseObject;
import com.oracle.truffle.js.runtime.util.Pair;

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

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

public JSModuleRecord(JSModuleData parsedModule, JSModuleLoader moduleLoader) {
this(parsedModule, moduleLoader, null);
Expand Down Expand Up @@ -107,26 +107,21 @@ public JSModuleData getModuleData() {
return parsedModule;
}

public JSDynamicObject getImportMeta() {
if (importMeta == null) {
importMeta = createMetaObject();
}
public JSObject getImportMetaOrNull() {
return importMeta;
}

private JSDynamicObject createMetaObject() {
JSObject metaObj = JSOrdinary.createWithNullPrototype(context);
if (context.hasImportMetaInitializerBeenSet()) {
context.notifyImportMetaInitializer(metaObj, this);
} else {
initializeMetaObject(metaObj);
public JSObject getImportMeta(CreateImportMetaNode createImportMeta) {
JSObject metaObj = importMeta;
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.SLOWPATH_PROBABILITY, metaObj == null)) {
importMeta = metaObj = createImportMeta.execute(this);
}
return metaObj;
}

@TruffleBoundary
private void initializeMetaObject(JSObject metaObj) {
JSObject.set(metaObj, Strings.URL, Strings.fromJavaString(getSource().getURI().toString()));
public String getURL() {
return getSource().getURI().toString();
}

@Override
Expand Down

0 comments on commit 784d46f

Please sign in to comment.