本文整理汇总了Java中org.jruby.runtime.ThreadContext类的典型用法代码示例。如果您正苦于以下问题:Java ThreadContext类的具体用法?Java ThreadContext怎么用?Java ThreadContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ThreadContext类属于org.jruby.runtime包,在下文中一共展示了ThreadContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: instantiateClass
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
/**
* instantiateClass
*
* @param runtime
* @param name
* @param args
* @return
*/
public static IRubyObject instantiateClass(Ruby runtime, String name, IRubyObject... args)
{
ThreadContext threadContext = runtime.getCurrentContext();
IRubyObject result = null;
// try to load the class
RubyClass rubyClass = runtime.getClass(name);
// instantiate it, if it exists
if (rubyClass != null)
{
result = rubyClass.newInstance(threadContext, args, Block.NULL_BLOCK);
}
return result;
}
示例2: set
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
@JRubyMethod(name = "set", required=2)
public IRubyObject set(ThreadContext context, IRubyObject i, IRubyObject val) {
int j = RubyNumeric.num2int(i);
if (j >=0 && j < cnt) {
if (j >= tailoff()) {
RubyArray newTail = tail.aryDup();
newTail.store(j & 0x01f, val);
return new PersistentVector(context.runtime, getMetaClass()).initialize(context, cnt, shift, root, newTail);
}
return new PersistentVector(context.runtime, getMetaClass()).initialize(context, cnt, shift, doSet(context, shift, root, j, val), tail);
}
if (j == cnt)
add(context, val);
throw new IndexOutOfBoundsException();
}
示例3: pushTail
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
private Node pushTail(ThreadContext context, int level, Node parent, Node tailnode){
int subidx = ((cnt - 1) >>> level) & 0x01f;
Node ret = new Node(context.runtime, Node).initialize_params_arry(context, parent.edit, parent.array.aryDup());
Node nodeToInsert;
if(level == 5)
{
nodeToInsert = tailnode;
}
else
{
IRubyObject child = parent.array.entry(subidx);
nodeToInsert = (!child.isNil())?
pushTail(context, level-5,(Node) child, tailnode)
:newPath(context, root.edit,level-5, tailnode);
}
ret.array.store(subidx, nodeToInsert);
return ret;
}
示例4: add
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
@JRubyMethod(name = {"add", "append"}, required = 1)
public IRubyObject add(ThreadContext context, IRubyObject val) {
if (cnt - tailoff() < 32) {
PersistentVector ret = new PersistentVector(context.runtime, getMetaClass());
RubyArray newTail = tail.aryDup();
newTail.append(val);
return ret.initialize(context, this.cnt+1, this.shift, this.root, newTail);
}
Node newroot;
Node tailnode = new Node(context.runtime, Node).initialize_params_arry(context, root.edit, tail);
int newshift = shift;
if ((cnt >>> 5) > (1 << shift)) {
newroot = new Node(context.runtime, Node).initialize_params(context, root.edit);
newroot.array.store(0, root);
newroot.array.store(1, newPath(context, root.edit, shift, tailnode));
newshift += 5;
} else
newroot = pushTail(context, shift, root, tailnode);
RubyArray arry = RubyArray.newArray(context.runtime);
arry.store(0, val);
return new PersistentVector(context.runtime, getMetaClass()).initialize(context, cnt + 1, newshift, newroot, arry);
}
示例5: flatten
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
/**
* This is a convenience method which will run the given block on the first element
* of each tuple contained.
*
* @param context the context the method is being executed in
* @param block a block to call on the elements of the bag
* @return enumerator object if null block given, nil otherwise
*/
@JRubyMethod(name = {"flat_each", "flatten"})
public IRubyObject flatten(ThreadContext context, Block block) throws ExecException {
Ruby runtime = context.getRuntime();
if (!block.isGiven())
return PigJrubyLibrary.enumeratorize(runtime, this, "flatten");
/* In a future release of JRuby when enumeratorize is made public (which is planned), should replace the above with the below
if (!block.isGiven())
return RubyEnumerator.enumeratorize(context.getRuntime(), this, "flatten");
*/
for (Tuple t : this)
block.yield(context, PigJrubyLibrary.pigToRuby(runtime, t.get(0)));
return context.nil;
}
示例6: validateTypeClass
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
protected static void validateTypeClass(ThreadContext context, Descriptors.FieldDescriptor.Type type, IRubyObject value) {
Ruby runtime = context.runtime;
if (!(value instanceof RubyModule)) {
throw runtime.newArgumentError("TypeClass has incorrect type");
}
RubyModule klass = (RubyModule) value;
IRubyObject descriptor = klass.getInstanceVariable(DESCRIPTOR_INSTANCE_VAR);
if (descriptor.isNil()) {
throw runtime.newArgumentError("Type class has no descriptor. Please pass a " +
"class or enum as returned by the DescriptorPool.");
}
if (type == Descriptors.FieldDescriptor.Type.MESSAGE) {
if (! (descriptor instanceof RubyDescriptor)) {
throw runtime.newArgumentError("Descriptor has an incorrect type");
}
} else if (type == Descriptors.FieldDescriptor.Type.ENUM) {
if (! (descriptor instanceof RubyEnumDescriptor)) {
throw runtime.newArgumentError("Descriptor has an incorrect type");
}
}
}
示例7: compare
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
/**
* This calls to the static method compare of DataByteArray. Given two DataByteArrays, it will call it
* on the underlying bytes.
*
* @param context the context the method is being executed in
* @param self an class which contains metadata on the calling class (required for static Ruby methods)
* @param arg1 a RubyDataByteArray or byte array to compare
* @param arg2 a RubyDataByteArray or byte array to compare
* @return the Fixnum result of comparing arg1 and arg2's bytes
*/
@JRubyMethod
public static RubyFixnum compare(ThreadContext context, IRubyObject self, IRubyObject arg1, IRubyObject arg2) {
byte[] buf1, buf2;
if (arg1 instanceof RubyDataByteArray) {
buf1 = ((RubyDataByteArray)arg1).getDBA().get();
} else {
buf1 = (byte[])arg1.toJava(byte[].class);
}
if (arg2 instanceof RubyDataByteArray) {
buf2 = ((RubyDataByteArray)arg2).getDBA().get();
} else {
buf2 = (byte[])arg2.toJava(byte[].class);
}
return RubyFixnum.newFixnum(context.getRuntime(), DataByteArray.compare(buf1, buf2));
}
示例8: add
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
/**
* A ruby method that adds two numbers. In practice we would do some more
* complex operation in java possibly using a library method, or a pure java
* method of our own creation (possible private).
* @param context ThreadContext
* @param recv the receiver
* @param args array of input arguments
* @return The outcome of doing a plus b.
*/
@JRubyMethod(name = "add", module = true, rest = true)
public static IRubyObject add(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
// Arity.checkArgumentCount(runtime, args, Arity.OPTIONAL.getValue(), 2);
int a = (int) args[0].toJava(Integer.class);
int b = (int) args[1].toJava(Integer.class);
int result = a + b;
return runtime.newFixnum(result);
}
示例9: mult
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
/**
* Multiplies two numbers (in practice you would implement some method in java,
* probably using an external library)
* @param context ThreadContext
* @param args the ruby way of coping with more than two arguments
* @return result probably RubyFixnum
*/
@JRubyMethod(name = "multiply", rest = true)
public IRubyObject mult(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
// Arity.checkArgumentCount(runtime, args, Arity.OPTIONAL.getValue(), 2);
int a = (int) args[0].toJava(Integer.class);
int b = (int) args[1].toJava(Integer.class);
int result = a * b;
return runtime.newFixnum(result);
}
示例10: buildModuleFromDescriptor
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
private RubyModule buildModuleFromDescriptor(ThreadContext context) {
Ruby runtime = context.runtime;
Utils.checkNameAvailability(context, name.asJavaString());
RubyModule enumModule = RubyModule.newModule(runtime);
for (Descriptors.EnumValueDescriptor value : descriptor.getValues()) {
enumModule.defineConstant(value.getName(), runtime.newFixnum(value.getNumber()));
}
enumModule.instance_variable_set(runtime.newString(Utils.DESCRIPTOR_INSTANCE_VAR), this);
enumModule.defineAnnotatedMethods(RubyEnum.class);
return enumModule;
}
示例11: toCHM
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
private ConcurrentHashMap<IRubyObject, IRubyObject> toCHM(ThreadContext context, IRubyObject options) {
Ruby runtime = context.getRuntime();
if (!options.isNil() && options.respondsTo("[]")) {
IRubyObject rInitialCapacity = options.callMethod(context, "[]", runtime.newSymbol("initial_capacity"));
IRubyObject rLoadFactor = options.callMethod(context, "[]", runtime.newSymbol("load_factor"));
int initialCapacity = !rInitialCapacity.isNil() ? RubyNumeric.num2int(rInitialCapacity.convertToInteger()) : DEFAULT_INITIAL_CAPACITY;
float loadFactor = !rLoadFactor.isNil() ? (float)RubyNumeric.num2dbl(rLoadFactor.convertToFloat()) : DEFAULT_LOAD_FACTOR;
return newCHM(initialCapacity, loadFactor);
} else {
return newCHM();
}
}
示例12: getName
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
/**
* This method allows the user to see the name of the alias of the FieldSchema of the encapsulated
* Schema. This method only works if the Schema has one FieldSchema.
*
* @param context the context the method is being executed in
* @return the name of the Schema
*/
@JRubyMethod(name = "name")
public RubyString getName(ThreadContext context) {
try {
if (internalSchema.size() != 1)
throw new RuntimeException("Can only get name if there is one schema present");
return RubyString.newString(context.getRuntime(), internalSchema.getField(0).alias);
} catch (FrontendException e) {
throw new RuntimeException("Unable to get field from Schema", e);
}
}
示例13: tuple
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
/**
* This is a ruby method which takes an array of arguments and constructs a Tuple schema from them. The name
* will be set automatically.
*
* @param context the context the method is being executed in
* @param self the RubyClass for the Class object this was invoked on
* @param arg a list of arguments to instantiate the new RubySchema
* @return the new RubySchema
*/
@JRubyMethod(meta = true, name = {"t", "tuple"})
public static RubySchema tuple(ThreadContext context, IRubyObject self, IRubyObject arg) {
if (arg instanceof RubyArray) {
Schema s = rubyArgToSchema(arg);
Ruby runtime = context.getRuntime();
return new RubySchema(runtime, runtime.getClass("Schema"), s);
} else {
throw new RuntimeException("Bad argument given to Schema.tuple");
}
}
示例14: addInPlace
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
/**
* This method provides addition semantics, modifying the original Schema in place.
* This method can be given any number of arguments, much as with the constructor.
*
* @param context the context the method is being executed in
* @param args a varargs which can be any valid set of arguments that
* can initialize a RubySchema
*/
@JRubyMethod(name = "add!", rest = true)
public void addInPlace(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
List<Schema.FieldSchema> lfs = internalSchema.getFields();
RubySchema rs = new RubySchema(runtime, runtime.getClass("Schema")).initialize(args);
for (Schema.FieldSchema fs : rs.getInternalSchema().getFields())
lfs.add(fs);
RubySchema.fixSchemaNames(internalSchema);
}
示例15: CurrentBindingsIterator
import org.jruby.runtime.ThreadContext; //导入依赖的package包/类
CurrentBindingsIterator(ThreadContext context) {
ThreadContextInternals contextInternals = new ThreadContextInternals(context);
this.frameStack = contextInternals.getFrameStack();
this.frameIndex = contextInternals.getFrameIndex();
this.scopeStack = contextInternals.getScopeStack();
this.scopeIndex = contextInternals.getScopeIndex();
this.backtrace = contextInternals.getBacktrace();
this.backtraceIndex = contextInternals.getBacktraceIndex();
}