Skip to content

Commit

Permalink
[Truffle] More privatisation.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Dec 3, 2016
1 parent 92c1ab5 commit 540ce66
Show file tree
Hide file tree
Showing 12 changed files with 539 additions and 187 deletions.
2 changes: 1 addition & 1 deletion truffle/src/main/java/org/jruby/truffle/RubyLanguage.java
Expand Up @@ -17,11 +17,11 @@
import com.oracle.truffle.api.instrumentation.StandardTags;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.Source;
import org.jruby.runtime.Constants;
import org.jruby.truffle.core.kernel.TraceManager;
import org.jruby.truffle.language.LazyRubyRootNode;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.stdlib.CoverageManager;
import org.jruby.truffle.util.Constants;

import java.io.IOException;

Expand Down
Expand Up @@ -46,17 +46,10 @@
import org.joni.Syntax;
import org.joni.WarnCallback;
import org.joni.exception.JOniException;
import org.jruby.Ruby;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.parser.ReOptions;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.encoding.EncodingCapable;
import org.jruby.runtime.encoding.MarshalEncoding;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.string.StringSupport;
import org.jruby.truffle.parser.ReOptions;
import org.jruby.util.ByteList;
import org.jruby.util.RegexpSupport;
import org.jruby.util.collections.WeakValuedMap;
import org.jruby.util.io.EncodingUtils;

Expand All @@ -67,7 +60,7 @@
import static org.jruby.util.StringSupport.EMPTY_STRING_ARRAY;
import static org.jruby.util.StringSupport.codeRangeScan;

public class ClassicRegexp implements ReOptions, EncodingCapable, MarshalEncoding {
public class ClassicRegexp implements ReOptions {
private final RubyContext context;
private Regex pattern;
private ByteList str = ByteList.EMPTY_BYTELIST;
Expand All @@ -85,27 +78,15 @@ public void setEncodingNone() {
options.setEncodingNone(true);
}

@Override
public Encoding getEncoding() {
return pattern.getEncoding();
}

@Override
public void setEncoding(Encoding encoding) {
// FIXME: Which encoding should be changed here?
// FIXME: transcode?
}

@Override
public boolean shouldMarshalEncoding() {
return getEncoding() != ASCIIEncoding.INSTANCE;
}

@Override
public Encoding getMarshalEncoding() {
return getEncoding();
}
// FIXME: Maybe these should not be static?
static final WeakValuedMap<ByteList, Regex> patternCache = new WeakValuedMap<>();

private static Regex makeRegexp(RubyContext runtime, ByteList bytes, RegexpOptions options, Encoding enc) {
Expand Down Expand Up @@ -154,7 +135,7 @@ private static class SearchMatchTask {
this.match = match;
}

public Integer run(ThreadContext context, Matcher matcher) throws InterruptedException {
public Integer run(Object context, Matcher matcher) throws InterruptedException {
return match ?
matcher.matchInterruptible(start, range, option) :
matcher.searchInterruptible(start, range, option);
Expand Down Expand Up @@ -182,11 +163,11 @@ public static ClassicRegexp newRegexp(RubyContext runtime, ByteList pattern, int

// used only by the compiler/interpreter (will set the literal flag)
public static ClassicRegexp newRegexp(RubyContext runtime, ByteList pattern, RegexpOptions options) {
try {
//try {
return new ClassicRegexp(runtime, pattern, (RegexpOptions)options.clone());
} catch (RaiseException re) {
throw new org.jruby.truffle.language.control.RaiseException(runtime.getCoreExceptions().syntaxError(re.getMessage(), null));
}
//} catch (RaiseException re) {
// throw new org.jruby.truffle.language.control.RaiseException(runtime.getCoreExceptions().syntaxError(re.getMessage(), null));
//}
}

/**
Expand All @@ -198,18 +179,6 @@ public static ClassicRegexp newRegexpParser(RubyContext runtime, ByteList patter
return new ClassicRegexp(runtime, pattern, (RegexpOptions)options.clone());
}

/** rb_reg_options
*/
public RegexpOptions getOptions() {
check();
return options;
}

public final Regex getPattern() {
check();
return pattern;
}

private static void preprocessLight(RubyContext context, ByteList str, Encoding enc, Encoding[]fixedEnc, RegexpSupport.ErrorMode mode) {
if (enc.isAsciiCompatible()) {
fixedEnc[0] = null;
Expand Down Expand Up @@ -592,10 +561,6 @@ private static int scanForCodeRange(ByteList str) {
return cr;
}

private void check() {
if (pattern == null) throw getRuntime().newTypeError("uninitialized Regexp");
}

/** rb_reg_quote
*
*/
Expand Down Expand Up @@ -740,20 +705,13 @@ public ClassicRegexp regexpInitialize(ByteList bytes, Encoding enc, RegexpOption
return this;
}

@JRubyMethod
public Object options() {
return getRuntime().newFixnum(getOptions().toOptions());
}

public static void appendOptions(ByteList to, RegexpOptions options) {
if (options.isMultiline()) to.append((byte)'m');
if (options.isIgnorecase()) to.append((byte)'i');
if (options.isExtended()) to.append((byte)'x');
}

public ByteList toByteList() {
check();

RegexpOptions newOptions = (RegexpOptions)options.clone();
int p = str.getBegin();
int len = str.getRealSize();
Expand Down Expand Up @@ -925,14 +883,4 @@ public String[] getNames() {
return names;
}

@JRubyMethod
public Object encoding(ThreadContext context) {
Encoding enc = (pattern == null) ? str.getEncoding() : pattern.getEncoding();
return context.runtime.getEncodingService().getEncoding(enc);
}

public Ruby getRuntime() {
throw new UnsupportedOperationException();
}

}
Expand Up @@ -75,8 +75,6 @@
import org.jruby.truffle.language.threadlocal.ThreadLocalObject;
import org.jruby.truffle.util.StringUtils;
import org.jruby.util.ByteList;
import org.jruby.util.RegexpSupport;
import org.jruby.util.RegexpSupport.ErrorMode;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand All @@ -94,7 +92,7 @@ public static Matcher createMatcher(RubyContext context, DynamicObject regexp, D
if (regex.getEncoding() != enc) {
final Encoding[] fixedEnc = new Encoding[] { null };
final ByteList sourceByteList = RopeOperations.getByteListReadOnly(Layouts.REGEXP.getSource(regexp));
final ByteList preprocessed = ClassicRegexp.preprocess(context, sourceByteList, enc, fixedEnc, ErrorMode.RAISE);
final ByteList preprocessed = ClassicRegexp.preprocess(context, sourceByteList, enc, fixedEnc, RegexpSupport.ErrorMode.RAISE);
final RegexpOptions options = Layouts.REGEXP.getOptions(regexp);
final Encoding newEnc = checkEncoding(regexp, stringRope, true);
regex = new Regex(preprocessed.getUnsafeBytes(), preprocessed.getBegin(), preprocessed.getBegin() + preprocessed.getRealSize(),
Expand Down
@@ -0,0 +1,33 @@
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/

package org.jruby.truffle.core.regexp;

public class RegexpSupport {

public enum ErrorMode {RAISE, PREPROCESS, DESC}

}
Expand Up @@ -13,7 +13,7 @@
import com.oracle.truffle.api.object.DynamicObjectFactory;
import com.oracle.truffle.api.object.dsl.Layout;
import org.jruby.truffle.core.basicobject.BasicObjectLayout;
import org.jruby.util.Random;
import org.jruby.truffle.util.Random;

@Layout
public interface RandomizerLayout extends BasicObjectLayout {
Expand Down

0 comments on commit 540ce66

Please sign in to comment.