private String toString(Object methodName) { if (methodName instanceof String) { return (String) methodName; } else if (RubyGuards.isRubyString(methodName)) { return methodName.toString(); } else if (RubyGuards.isRubySymbol(methodName)) { return Layouts.SYMBOL.getString((DynamicObject) methodName); } else { throw new UnsupportedOperationException(); } }
public int doInt(VirtualFrame frame, Object object) { // TODO CS 14-Nov-15 this code is crazy - should have separate nodes for ToRubyInteger and // ToJavaInt final Object integerObject = executeIntOrLong(frame, object); if (wasInteger.profile(integerObject instanceof Integer)) { return (int) integerObject; } if (wasLong.profile(integerObject instanceof Long)) { final long longValue = (long) integerObject; if (wasLongInRange.profile(CoreLibrary.fitsIntoInteger(longValue))) { return (int) longValue; } } CompilerDirectives.transferToInterpreter(); if (RubyGuards.isRubyBignum(object)) { throw new RaiseException( getContext().getCoreLibrary().rangeError("bignum too big to convert into `long'", this)); } else { throw new UnsupportedOperationException(object.getClass().toString()); } }
private Object lookupRestKeywordArgumentHash(VirtualFrame frame) { final Object hash = readUserKeywordsHashNode.execute(frame); if (noHash.profile(hash == null)) { return Layouts.HASH.createHash( coreLibrary().getHashFactory(), null, 0, null, null, null, null, false); } CompilerDirectives.bailout("Ruby keyword arguments aren't optimized"); final DynamicObject hashObject = (DynamicObject) hash; final List<Map.Entry<Object, Object>> entries = new ArrayList<>(); outer: for (Map.Entry<Object, Object> keyValue : HashOperations.iterableKeyValues(hashObject)) { if (!RubyGuards.isRubySymbol(keyValue.getKey())) { continue; } for (String excludedKeyword : excludedKeywords) { if (excludedKeyword.equals(keyValue.getKey().toString())) { continue outer; } } entries.add(keyValue); } return BucketsStrategy.create( getContext(), entries, Layouts.HASH.getCompareByIdentity(hashObject)); }
public Object execute(VirtualFrame frame) { final Object childValue = child.execute(frame); if (!(RubyGuards.isRubyString(childValue)) && childValue != nil()) { CompilerDirectives.transferToInterpreter(); throw new RaiseException(getContext().getCoreLibrary().typeErrorMustBe("$/", "String", this)); } return childValue; }
@TruffleBoundary protected InternalMethod doLookup(InternalMethod currentMethod, DynamicObject selfMetaClass) { assert RubyGuards.isRubyClass(selfMetaClass); InternalMethod superMethod = ModuleOperations.lookupSuperMethod(currentMethod, selfMetaClass); // TODO (eregon, 12 June 2015): Is this correct? if (superMethod != null && superMethod.isUndefined()) { superMethod = null; } return superMethod; }
private Object timeToUnit(long time, DynamicObject unit) { assert RubyGuards.isRubySymbol(unit); if (unit == nanosecondSymbol) { return time; } else if (unit == floatSecondSymbol) { return time / 1e9; } else { throw new UnsupportedOperationException(Layouts.SYMBOL.getString(unit)); } }
private DispatchNode doDynamicObject( VirtualFrame frame, DispatchNode first, Object receiverObject, Object methodName, Object[] argumentsObjects) { final DynamicObject callerClass; if (ignoreVisibility) { callerClass = null; } else if (getDispatchAction() == DispatchAction.RESPOND_TO_METHOD) { final Frame callerFrame = getContext() .getCallStack() .getCallerFrameIgnoringSend() .getFrame(FrameInstance.FrameAccess.READ_ONLY, true); callerClass = coreLibrary().getMetaClass(RubyArguments.getSelf(callerFrame.getArguments())); } else { callerClass = coreLibrary().getMetaClass(RubyArguments.getSelf(frame)); } final InternalMethod method = lookup(callerClass, receiverObject, toString(methodName), ignoreVisibility); if (method == null) { return createMethodMissingNode(first, methodName, receiverObject); } final DynamicObject receiverMetaClass = coreLibrary().getMetaClass(receiverObject); if (RubyGuards.isRubySymbol(receiverObject)) { return new CachedBoxedSymbolDispatchNode( getContext(), methodName, first, method, getDispatchAction()); } else if (Layouts.CLASS.getIsSingleton(receiverMetaClass)) { return new CachedSingletonDispatchNode( getContext(), methodName, first, ((DynamicObject) receiverObject), receiverMetaClass, method, getDispatchAction()); } else { return new CachedBoxedDispatchNode( getContext(), methodName, first, ((DynamicObject) receiverObject).getShape(), receiverMetaClass, method, getDispatchAction()); } }
private Object tryConvertToHash( VirtualFrame frame, final int argumentCount, final Object lastArgument) { if (respondsToToHashProfile.profile(respondToToHash(frame, lastArgument))) { final Object converted = callToHash(frame, lastArgument); if (convertedIsHashProfile.profile(RubyGuards.isRubyHash(converted))) { RubyArguments.setArgument(frame, argumentCount - 1, converted); return converted; } } return null; }
@Specialization(guards = "isRubyModule(lexicalParentModule)") public Object defineModule(VirtualFrame frame, DynamicObject lexicalParentModule) { final RubyConstant constant = lookupForExistingModule(frame, name, lexicalParentModule); final DynamicObject definingModule; if (needToDefineProfile.profile(constant == null)) { definingModule = ModuleNodes.createModule( getContext(), coreLibrary().getModuleClass(), lexicalParentModule, name, this); } else { final Object constantValue = constant.getValue(); if (!RubyGuards.isRubyModule(constantValue) || RubyGuards.isRubyClass(constantValue)) { errorProfile.enter(); throw new RaiseException(coreExceptions().typeErrorIsNotA(name, "module", this)); } definingModule = (DynamicObject) constantValue; } return definingModule; }
@Override public Object execute(VirtualFrame frame) { if (RubyGuards.isRubyString(ForeignAccess.getReceiver(frame))) { final DynamicObject string = (DynamicObject) ForeignAccess.getReceiver(frame); final int index = (int) ForeignAccess.getArguments(frame).get(labelIndex); if (index >= Layouts.STRING.getRope(string).byteLength()) { return 0; } else { return (byte) StringOperations.getByteListReadOnly(string).get(index); } } else { CompilerDirectives.transferToInterpreter(); throw new IllegalStateException("Not implemented"); } }
@Override public Object execute(VirtualFrame frame) { final int argumentCount = RubyArguments.getArgumentsCount(frame); if (notEnoughArgumentsProfile.profile(argumentCount <= minArgumentCount)) { return null; } final Object lastArgument = RubyArguments.getArgument(frame, argumentCount - 1); if (lastArgumentIsHashProfile.profile(RubyGuards.isRubyHash(lastArgument))) { return lastArgument; } return tryConvertToHash(frame, argumentCount, lastArgument); }
public static boolean isSingleByteOptimizable(DynamicObject string) { assert RubyGuards.isRubyString(string); return Layouts.STRING.getRope(string).isSingleByteOptimizable(); }
public static boolean isAsciiCompatible(DynamicObject string) { assert RubyGuards.isRubyString(string); return Layouts.STRING.getRope(string).getEncoding().isAsciiCompatible(); }
public RubiniusPrimitiveCallConstructor(DynamicObject method) { assert RubyGuards.isRubyMethod(method); this.method = method; }
public static boolean isFixedWidthEncoding(DynamicObject string) { assert RubyGuards.isRubyString(string); return Layouts.STRING.getRope(string).getEncoding().isFixedWidth(); }
public static boolean isValidUtf8(DynamicObject string) { assert RubyGuards.isRubyString(string); return StringOperations.isCodeRangeValid(string) && Layouts.STRING.getRope(string).getEncoding().isUTF8(); }
public static boolean isEmpty(DynamicObject string) { assert RubyGuards.isRubyString(string); return Layouts.STRING.getRope(string).isEmpty(); }
public static boolean is7Bit(DynamicObject string) { assert RubyGuards.isRubyString(string); return StringOperations.codeRange(string) == CodeRange.CR_7BIT; }
public static boolean isBrokenCodeRange(DynamicObject string) { assert RubyGuards.isRubyString(string); return StringOperations.codeRange(string) == CodeRange.CR_BROKEN; }
public static boolean isRopeBuffer(DynamicObject string) { assert RubyGuards.isRubyString(string); return rope(string) instanceof RopeBuffer; }