-
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Truffle] Make require ready for compilation and add fine-grained bou…
…ndaries. * Keep direct references to storage of well-known global variables. * Extract lookupForExistingModule() in its own node since it's shared. * Support some global variable aliases.
- 9.4.12.0
- 9.4.11.0
- 9.4.10.0
- 9.4.9.0
- 9.4.8.0
- 9.4.7.0
- 9.4.6.0
- 9.4.5.0
- 9.4.4.0
- 9.4.3.0
- 9.4.2.0
- 9.4.1.0
- 9.4.0.0
- 9.3.15.0
- 9.3.14.0
- 9.3.13.0
- 9.3.12.0
- 9.3.11.0
- 9.3.10.0
- 9.3.9.0
- 9.3.8.0
- 9.3.7.0
- 9.3.6.0
- 9.3.5.0
- 9.3.4.0
- 9.3.3.0
- 9.3.2.0
- 9.3.1.0
- 9.3.0.0
- 9.2.21.0
- 9.2.20.1
- 9.2.20.0
- 9.2.19.0
- 9.2.18.0
- 9.2.17.0
- 9.2.16.0
- 9.2.15.0
- 9.2.14.0
- 9.2.13.0
- 9.2.12.0
- 9.2.11.1
- 9.2.11.0
- 9.2.10.0
- 9.2.9.0
- 9.2.8.0
- 9.2.7.0
- 9.2.6.0
- 9.2.5.0
- 9.2.4.1
- 9.2.4.0
- 9.2.3.0
- 9.2.2.0
- 9.2.1.0
- 9.2.0.0
- 9.1.17.0
- 9.1.16.0
- 9.1.15.0
- 9.1.14.0
- 9.1.13.0
- 9.1.12.0
- 9.1.11.0
- 9.1.10.0
- 9.1.9.0
- 9.1.8.0
- 9.1.7.0
- 9.1.6.0
- 9.1.5.0
- 9.1.4.0
- 9.1.3.0
Showing
13 changed files
with
384 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
truffle/src/main/java/org/jruby/truffle/language/loader/RequireNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package org.jruby.truffle.language.loader; | ||
|
||
import com.oracle.truffle.api.CallTarget; | ||
import com.oracle.truffle.api.CompilerDirectives; | ||
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; | ||
import com.oracle.truffle.api.dsl.Cached; | ||
import com.oracle.truffle.api.dsl.NodeChild; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.frame.VirtualFrame; | ||
import com.oracle.truffle.api.interop.ForeignAccess; | ||
import com.oracle.truffle.api.interop.InteropException; | ||
import com.oracle.truffle.api.interop.Message; | ||
import com.oracle.truffle.api.interop.TruffleObject; | ||
import com.oracle.truffle.api.nodes.IndirectCallNode; | ||
import com.oracle.truffle.api.nodes.Node; | ||
import com.oracle.truffle.api.object.DynamicObject; | ||
import com.oracle.truffle.api.profiles.BranchProfile; | ||
import com.oracle.truffle.api.profiles.ConditionProfile; | ||
import com.oracle.truffle.api.source.Source; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
import org.jcodings.specific.UTF8Encoding; | ||
import org.jruby.truffle.RubyLanguage; | ||
import org.jruby.truffle.core.string.StringOperations; | ||
import org.jruby.truffle.language.RubyNode; | ||
import org.jruby.truffle.language.RubyRootNode; | ||
import org.jruby.truffle.language.control.RaiseException; | ||
import org.jruby.truffle.language.dispatch.CallDispatchHeadNode; | ||
import org.jruby.truffle.language.methods.DeclarationContext; | ||
import org.jruby.truffle.language.parser.ParserContext; | ||
|
||
@NodeChild("feature") | ||
public abstract class RequireNode extends RubyNode { | ||
|
||
@Child IndirectCallNode callNode = IndirectCallNode.create(); | ||
@Child CallDispatchHeadNode isInLoadedFeatures = CallDispatchHeadNode.createMethodCall(); | ||
@Child CallDispatchHeadNode addToLoadedFeatures = CallDispatchHeadNode.createMethodCall(); | ||
|
||
@Child Node isExecutableNode = Message.IS_EXECUTABLE.createNode(); | ||
@Child Node executeNode = Message.createExecute(0).createNode(); | ||
|
||
public static RequireNode create() { | ||
return RequireNodeGen.create(null); | ||
} | ||
|
||
public abstract boolean executeRequire(VirtualFrame frame, String feature); | ||
|
||
@Specialization | ||
protected boolean require(VirtualFrame frame, String feature, | ||
@Cached("create()") BranchProfile errorProfile, | ||
@Cached("createBinaryProfile()") ConditionProfile isLoadedProfile) { | ||
final FeatureLoader featureLoader = getContext().getFeatureLoader(); | ||
|
||
final String expandedPath = featureLoader.findFeature(feature); | ||
|
||
if (expandedPath == null) { | ||
errorProfile.enter(); | ||
throw new RaiseException(getContext().getCoreExceptions().loadErrorCannotLoad(feature, this)); | ||
} | ||
|
||
final DynamicObject pathString = StringOperations.createString(getContext(), | ||
StringOperations.encodeRope(expandedPath, UTF8Encoding.INSTANCE)); | ||
|
||
if (isLoadedProfile.profile(isFeatureLoaded(frame, pathString))) { | ||
return false; | ||
} | ||
|
||
final ReentrantLockFreeingMap<String> fileLocks = featureLoader.getFileLocks(); | ||
|
||
while (true) { | ||
final ReentrantLock lock = fileLocks.get(expandedPath); | ||
|
||
if (lock.isHeldByCurrentThread()) { | ||
// circular require | ||
// TODO (pitr-ch 20-Mar-2016): warn user | ||
return false; | ||
} | ||
|
||
if (!fileLocks.lock(this, getContext().getThreadManager(), expandedPath, lock)) { | ||
continue; | ||
} | ||
|
||
try { | ||
if (isFeatureLoaded(frame, pathString)) { | ||
return false; | ||
} | ||
|
||
final Source source; | ||
try { | ||
source = getContext().getSourceCache().getSource(expandedPath); | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
|
||
final String mimeType = source.getMimeType(); | ||
switch (mimeType) { | ||
case RubyLanguage.MIME_TYPE: { | ||
final RubyRootNode rootNode = getContext().getCodeLoader().parse( | ||
source, | ||
UTF8Encoding.INSTANCE, | ||
ParserContext.TOP_LEVEL, | ||
null, | ||
true, | ||
this); | ||
|
||
final CodeLoader.DeferredCall deferredCall = getContext().getCodeLoader().prepareExecute( | ||
ParserContext.TOP_LEVEL, | ||
DeclarationContext.TOP_LEVEL, | ||
rootNode, | ||
null, | ||
coreLibrary().getMainObject()); | ||
|
||
deferredCall.call(frame, callNode); | ||
break; | ||
} | ||
|
||
case RubyLanguage.CEXT_MIME_TYPE: { | ||
featureLoader.ensureCExtImplementationLoaded(frame, callNode); | ||
|
||
final CallTarget callTarget = featureLoader.parseSource(source); | ||
callNode.call(frame, callTarget, new Object[] {}); | ||
|
||
final TruffleObject initFunction = getInitFunction(expandedPath); | ||
|
||
if (!ForeignAccess.sendIsExecutable(isExecutableNode, frame, initFunction)) { | ||
CompilerDirectives.transferToInterpreter(); | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
try { | ||
ForeignAccess.sendExecute(executeNode, frame, initFunction); | ||
} catch (InteropException e) { | ||
CompilerDirectives.transferToInterpreter(); | ||
throw new RuntimeException(e); | ||
} | ||
break; | ||
} | ||
|
||
default: | ||
errorProfile.enter(); | ||
throw new RaiseException(unknownLanguage(expandedPath, mimeType)); | ||
} | ||
|
||
addToLoadedFeatures(frame, pathString); | ||
|
||
return true; | ||
} finally { | ||
fileLocks.unlock(expandedPath, lock); | ||
} | ||
} | ||
} | ||
|
||
@TruffleBoundary | ||
private DynamicObject unknownLanguage(String expandedPath, final String mimeType) { | ||
return getContext().getCoreExceptions().internalError( | ||
"unknown language " + mimeType + " for " + expandedPath, | ||
callNode); | ||
} | ||
|
||
@TruffleBoundary | ||
private TruffleObject getInitFunction(final String expandedPath) { | ||
final Object initFunction = getContext().getEnv().importSymbol("@Init_" + getBaseName(expandedPath)); | ||
|
||
if (!(initFunction instanceof TruffleObject)) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
return (TruffleObject) initFunction; | ||
} | ||
|
||
@TruffleBoundary | ||
private String getBaseName(String path) { | ||
final String name = new File(path).getName(); | ||
final int firstDot = name.indexOf('.'); | ||
if (firstDot == -1) { | ||
return name; | ||
} else { | ||
return name.substring(0, firstDot); | ||
} | ||
} | ||
|
||
public boolean isFeatureLoaded(VirtualFrame frame, DynamicObject feature) { | ||
final DynamicObject loadedFeatures = getContext().getCoreLibrary().getLoadedFeatures(); | ||
synchronized (getContext().getFeatureLoader().getLoadedFeaturesLock()) { | ||
return isInLoadedFeatures.callBoolean(frame, loadedFeatures, "include?", null, feature); | ||
} | ||
} | ||
|
||
private void addToLoadedFeatures(VirtualFrame frame, DynamicObject feature) { | ||
final DynamicObject loadedFeatures = coreLibrary().getLoadedFeatures(); | ||
synchronized (getContext().getFeatureLoader().getLoadedFeaturesLock()) { | ||
addToLoadedFeatures.call(frame, loadedFeatures, "<<", null, feature); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
truffle/src/main/java/org/jruby/truffle/language/objects/LookupForExistingModuleNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (c) 2013, 2016 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 1.0 | ||
* GNU General Public License version 2 | ||
* GNU Lesser General Public License version 2.1 | ||
*/ | ||
package org.jruby.truffle.language.objects; | ||
|
||
import com.oracle.truffle.api.CompilerDirectives; | ||
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; | ||
import com.oracle.truffle.api.dsl.Cached; | ||
import com.oracle.truffle.api.dsl.NodeChild; | ||
import com.oracle.truffle.api.dsl.NodeChildren; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.frame.VirtualFrame; | ||
import com.oracle.truffle.api.object.DynamicObject; | ||
import com.oracle.truffle.api.profiles.ConditionProfile; | ||
import org.jruby.truffle.Layouts; | ||
import org.jruby.truffle.core.string.StringOperations; | ||
import org.jruby.truffle.language.LexicalScope; | ||
import org.jruby.truffle.language.RubyConstant; | ||
import org.jruby.truffle.language.RubyNode; | ||
import org.jruby.truffle.language.control.RaiseException; | ||
import org.jruby.truffle.language.loader.RequireNode; | ||
|
||
@NodeChildren({ @NodeChild("name"), @NodeChild("lexicalParent") }) | ||
public abstract class LookupForExistingModuleNode extends RubyNode { | ||
|
||
@Child private RequireNode requireNode; | ||
|
||
public abstract RubyConstant executeLookupForExistingModule(VirtualFrame frame, String name, DynamicObject lexicalParent); | ||
|
||
@Specialization(guards = "isRubyModule(lexicalParent)") | ||
public RubyConstant lookupForExistingModule(VirtualFrame frame, String name, DynamicObject lexicalParent, | ||
@Cached("createBinaryProfile()") ConditionProfile autoloadProfile) { | ||
RubyConstant constant = deepConstantSearch(name, lexicalParent); | ||
|
||
// If a constant already exists with this class/module name and it's an autoload module, we have to trigger | ||
// the autoload behavior before proceeding. | ||
|
||
if (autoloadProfile.profile(constant != null && constant.isAutoload())) { | ||
|
||
// We know that we're redefining this constant as we're defining a class/module with that name. We remove | ||
// the constant here rather than just overwrite it in order to prevent autoload loops in either the require | ||
// call or the recursive execute call. | ||
|
||
Layouts.MODULE.getFields(lexicalParent).removeConstant(getContext(), this, name); | ||
getRequireNode().executeRequire(frame, StringOperations.getString(getContext(), (DynamicObject) constant.getValue())); | ||
return deepConstantSearch(name, lexicalParent); | ||
} | ||
|
||
return constant; | ||
} | ||
|
||
@TruffleBoundary | ||
private RubyConstant deepConstantSearch(String name, DynamicObject lexicalParent) { | ||
RubyConstant constant = Layouts.MODULE.getFields(lexicalParent).getConstant(name); | ||
|
||
final DynamicObject objectClass = getContext().getCoreLibrary().getObjectClass(); | ||
|
||
if (constant == null && lexicalParent == objectClass) { | ||
for (DynamicObject included : Layouts.MODULE.getFields(objectClass).prependedAndIncludedModules()) { | ||
constant = Layouts.MODULE.getFields(included).getConstant(name); | ||
|
||
if (constant != null) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (constant != null && !constant.isVisibleTo(getContext(), LexicalScope.NONE, lexicalParent)) { | ||
throw new RaiseException(getContext().getCoreExceptions().nameErrorPrivateConstant(lexicalParent, name, this)); | ||
} | ||
return constant; | ||
} | ||
|
||
public RequireNode getRequireNode() { | ||
if (requireNode == null) { | ||
CompilerDirectives.transferToInterpreter(); | ||
requireNode = insert(RequireNode.create()); | ||
} | ||
return requireNode; | ||
} | ||
|
||
} |