private static IRubyObject hashGet(final ThreadContext context, final RubyHash hash, final String key) { IRubyObject val = hash.fastARef(context.runtime.newSymbol(key)); if (val == null || val == context.nil) return null; return val; }
@JRubyMethod(name = { "[]", "find" }) public final IRubyObject find(ThreadContext context, IRubyObject query) { if (query instanceof RubySymbol) { IRubyObject value = kv_map.fastARef(query); return value != null ? value : context.nil; } else if (query instanceof RubyInteger) { RubySymbol symbol = valueToSymbol.get((Long)((RubyInteger) query).getLongValue()); return symbol != null ? symbol : context.nil; } else { return context.nil; } }
@JRubyMethod(name = { "[]", "find" }) public final IRubyObject find(ThreadContext context, IRubyObject query) { if (query instanceof RubySymbol) { IRubyObject value = kv_map.fastARef(query); return value != null ? value : context.nil; } else if (query instanceof RubyInteger) { RubySymbol symbol = valueToSymbol.get((Long)((RubyInteger) query).getLongValue()); return symbol != null ? symbol : context.nil; } else { return context.nil; } }
@Override public void visit(ThreadContext context, RubyHash self, IRubyObject key, IRubyObject value, int index, RubyHash otherHash) { IRubyObject value2 = otherHash.fastARef(key); if (value2 == null) { // other hash does not contain key throw MISMATCH; } if (!Helpers.rbEql(context, value, value2).isTrue()) { throw MISMATCH; } } };
@Override public void visit(ThreadContext context, RubyHash self, IRubyObject key, IRubyObject value, int index, RubyHash otherHash) { IRubyObject value2 = otherHash.fastARef(key); if (value2 == null) { // other hash does not contain key throw MISMATCH; } if (!Helpers.rbEql(context, value, value2).isTrue()) { throw MISMATCH; } } };
@Override public void visit(ThreadContext context, RubyHash self, IRubyObject key, IRubyObject value, int index, RubyHash otherHash) { IRubyObject value2 = otherHash.fastARef(key); if (value2 == null) { // other hash does not contain key throw MISMATCH; } if (!Helpers.rbEqual(context, value, value2).isTrue()) { throw MISMATCH; } } };
private synchronized IRubyObject lookupAndCacheValue(IRubyObject obj) { IRubyObject value = enums.fastARef(obj); if (value.isNil() || !(value instanceof RubyInteger)) { throw obj.getRuntime().newArgumentError("invalid enum value, " + obj.inspect()); } IdentityHashMap<RubySymbol, RubyInteger> s2v = new IdentityHashMap<RubySymbol, RubyInteger>(symbolToValue); s2v.put((RubySymbol) obj, (RubyInteger) value); this.symbolToValue = new IdentityHashMap<RubySymbol, RubyInteger>(s2v); return value; } }
private synchronized IRubyObject lookupAndCacheValue(ThreadContext context, IRubyObject obj) { IRubyObject value = enums instanceof Enums ? ((Enums)enums).mapSymbol(context, obj) : ((RubyHash)enums).fastARef(obj); if (value.isNil() || !(value instanceof RubyInteger)) { throw obj.getRuntime().newArgumentError("invalid enum value, " + obj.inspect()); } IdentityHashMap<RubySymbol, RubyInteger> s2v = new IdentityHashMap<RubySymbol, RubyInteger>(symbolToValue); s2v.put((RubySymbol) obj, (RubyInteger) value); this.symbolToValue = new IdentityHashMap<RubySymbol, RubyInteger>(s2v); return value; } }
private Type lookupType(Ruby runtime, IRubyObject name) { IRubyObject type = ffi.typedefs.fastARef(name); if (type instanceof Type) { return (Type) type; } if ((type = ffi.ffiModule.callMethod(runtime.getCurrentContext(), "find_type", name)) instanceof Type) { return (Type) type; } throw runtime.newTypeError("cannot resolve type " + name); } }
private Type lookupType(Ruby runtime, IRubyObject name, IRubyObject typeMap) { IRubyObject type = ffi.typedefs.fastARef(name); if (type instanceof Type) { return (Type) type; } IRubyObject args[] = new IRubyObject[]{name, typeMap}; if ((type = ffi.ffiModule.callMethod(runtime.getCurrentContext(), "find_type", args)) instanceof Type) { return (Type) type; } throw runtime.newTypeError("cannot resolve type " + name); } }
private RubyHash makeHash(ThreadContext context, RubyHash hash, Block block) { for (int i = 0; i < realLength; i++) { IRubyObject v = elt(i); IRubyObject k = block.yield(context, v); if (hash.fastARef(k) == null) hash.fastASet(k, v); } return hash; }
public static IRubyObject receiveKeywordArg(ThreadContext context, IRubyObject[] args, int required, String id, boolean acceptsKeywordArgument) { RubyHash keywordArguments = extractKwargsHash(context, args, required, acceptsKeywordArgument); if (keywordArguments == null) return UndefinedValue.UNDEFINED; RubySymbol keywordName = context.runtime.newSymbol(id); if (keywordArguments.fastARef(keywordName) == null) return UndefinedValue.UNDEFINED; // SSS FIXME: Can we use an internal delete here? // Enebo FIXME: Delete seems wrong if we are doing this for duplication purposes. return keywordArguments.delete(context, keywordName, Block.NULL_BLOCK); }
public static IRubyObject receiveKeywordArg(ThreadContext context, IRubyObject[] args, int required, String id, boolean acceptsKeywordArgument) { RubyHash keywordArguments = extractKwargsHash(context, args, required, acceptsKeywordArgument); if (keywordArguments == null) return UndefinedValue.UNDEFINED; RubySymbol keywordName = context.runtime.newSymbol(id); if (keywordArguments.fastARef(keywordName) == null) return UndefinedValue.UNDEFINED; // SSS FIXME: Can we use an internal delete here? // Enebo FIXME: Delete seems wrong if we are doing this for duplication purposes. return keywordArguments.delete(context, keywordName, Block.NULL_BLOCK); }
private static IRubyObject hashARef(Ruby runtime, IRubyObject hash, String symbol) { if (hash == null || !(hash instanceof RubyHash)) return runtime.getNil(); IRubyObject value = ((RubyHash) hash).fastARef(runtime.newSymbol(symbol)); return value == null ? runtime.getNil() : value; }
static void checkUnsupportedOptions(ThreadContext context, RubyHash opts, String[] unsupported, String error) { final Ruby runtime = context.runtime; for (String key : unsupported) { if (opts.fastARef(runtime.newSymbol(key)) != null) { runtime.getWarnings().warn(error + ": " + key); } } }
/** * Check if HOME environment variable is not nil nor empty * @param context */ private static RubyString checkHome(ThreadContext context) { Ruby runtime = context.runtime; IRubyObject home = runtime.getENV().fastARef(RubyString.newStringShared(runtime, RubyDir.HOME)); if (home == null || home == context.nil || ((RubyString) home).size() == 0) { throw runtime.newArgumentError("couldn't find HOME environment -- expanding `~'"); } return (RubyString) home; }
@JRubyMethod(module = true) public static IRubyObject subclasses(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject opts) { boolean recurseAll = false; opts = ArgsUtil.getOptionsArg(context.runtime, opts); if (opts != context.nil) { IRubyObject all = ((RubyHash) opts).fastARef(context.runtime.newSymbol("all")); if (all != null) recurseAll = all.isTrue(); } return subclasses(context, recv, arg instanceof RubyClass ? (RubyClass) arg : arg.getMetaClass(), recurseAll); }
@JRubyMethod(module = true) public static IRubyObject subclasses(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject opts) { boolean recurseAll = false; opts = ArgsUtil.getOptionsArg(context.runtime, opts); if (opts != context.nil) { IRubyObject all = ((RubyHash) opts).fastARef(context.runtime.newSymbol("all")); if (all != null) recurseAll = all.isTrue(); } return subclasses(context, recv, arg instanceof RubyClass ? (RubyClass) arg : arg.getMetaClass(), recurseAll); }
@JRubyMethod(name = { "[]", "find" }) public final IRubyObject find(ThreadContext context, IRubyObject query) { if (query instanceof RubySymbol) { IRubyObject value = kv_map.fastARef(query); return value != null ? value : context.runtime.getNil(); } else if (query instanceof RubyInteger) { RubySymbol symbol = valueToSymbol.get((int) ((RubyInteger) query).getLongValue()); return symbol != null ? symbol : context.runtime.getNil(); } else { return context.runtime.getNil(); } }
public static int intValue(IRubyObject obj, RubyHash enums) { if (obj instanceof RubyInteger) { return (int) ((RubyInteger) obj).getLongValue(); } else if (obj instanceof RubySymbol) { IRubyObject value = enums.fastARef(obj); if (value.isNil()) { throw obj.getRuntime().newArgumentError("invalid enum value, " + obj.inspect()); } return (int) longValue(value); } else { return (int) longValue(obj); } }