Skip to content

Commit

Permalink
Showing 5 changed files with 104 additions and 1 deletion.
6 changes: 6 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/RubyContext.java
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
import org.jruby.truffle.core.objectspace.ObjectSpaceManager;
import org.jruby.truffle.core.rope.RopeTable;
import org.jruby.truffle.core.rubinius.RubiniusPrimitiveManager;
import org.jruby.truffle.core.string.StringLiterals;
import org.jruby.truffle.core.symbol.SymbolTable;
import org.jruby.truffle.core.thread.ThreadManager;
import org.jruby.truffle.extra.AttachmentsManager;
@@ -74,6 +75,7 @@ public class RubyContext extends ExecutionContext {
private final AtExitManager atExitManager = new AtExitManager(this);
private final SourceCache sourceCache = new SourceCache(new SourceLoader(this));
private final CallStackManager callStack = new CallStackManager(this);
private final StringLiterals stringLiterals = new StringLiterals(this);

private final CompilerOptions compilerOptions = Truffle.getRuntime().createCompilerOptions();

@@ -316,4 +318,8 @@ public InteropManager getInteropManager() {
public CallStackManager getCallStack() {
return callStack;
}

public StringLiterals getStringLiterals() {
return stringLiterals;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 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.core.string;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.object.DynamicObject;
import org.jcodings.specific.USASCIIEncoding;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.rope.CodeRange;
import org.jruby.truffle.core.rope.Rope;

import java.nio.charset.StandardCharsets;

public class StringLiteral {

private final RubyContext context;
private final String literal;

@CompilationFinal private volatile Rope rope;

public StringLiteral(RubyContext context, String literal) {
assert is7Bit(literal);
this.context = context;
this.literal = literal;
}

public Rope getRope() {
if (rope == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
rope = createRope();
}

return rope;
}

public DynamicObject getString() {
return StringOperations.createString(context, getRope());
}

private Rope createRope() {
// getRope is fully synchronised and returns identically the same object for the same parameters

return context.getRopeTable().getRope(
literal.getBytes(StandardCharsets.US_ASCII),
USASCIIEncoding.INSTANCE,
CodeRange.CR_7BIT);
}

private static boolean is7Bit(String literal) {
for (int n = 0; n < literal.length(); n++) {
if (literal.charAt(n) > 0b1111111) {
return false;
}
}

return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 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.core.string;

import org.jruby.truffle.RubyContext;

public class StringLiterals {

private final RubyContext context;

public final StringLiteral NIL;

public StringLiterals(RubyContext context) {
this.context = context;
NIL = new StringLiteral(context, "nil");
}

}
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.core.rope.CodeRange;
import org.jruby.truffle.core.rope.Rope;
import org.jruby.truffle.core.string.StringLiterals;
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.truffle.platform.Sockets;
import org.jruby.util.ByteList;
@@ -170,6 +171,10 @@ protected DynamicObject createString(Rope rope) {
return StringOperations.createString(getContext(), rope);
}

protected StringLiterals stringLiterals() {
return getContext().getStringLiterals();
}

protected POSIX posix() {
return getContext().getNativePlatform().getPosix();
}
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ public Object execute(VirtualFrame frame) {

@Override
public Object isDefined(VirtualFrame frame) {
return create7BitString("nil", UTF8Encoding.INSTANCE);
return stringLiterals().NIL.getString();
}

}

0 comments on commit 36c437e

Please sign in to comment.