public static RubyClass createPointerClass(Ruby runtime, RubyModule module) { RubyClass pointerClass = module.defineClassUnder( "Pointer", module.getClass(AbstractMemory.ABSTRACT_MEMORY_RUBY_CLASS), RubyInstanceConfig.REIFY_RUBY_CLASSES ? new ReifyingAllocator(Pointer.class) : PointerAllocator.INSTANCE); pointerClass.defineAnnotatedMethods(Pointer.class); pointerClass.defineAnnotatedConstants(Pointer.class); pointerClass.setReifiedClass(Pointer.class); pointerClass.kindOf = new RubyModule.KindOf() { @Override public boolean isKindOf(IRubyObject obj, RubyModule type) { return obj instanceof Pointer && super.isKindOf(obj, type); } }; module.defineClassUnder( "NullPointerError", runtime.getRuntimeError(), runtime.getRuntimeError().getAllocator()); // Add Pointer::NULL as a constant Pointer nullPointer = new Pointer(runtime, pointerClass, new NullMemoryIO(runtime)); pointerClass.setConstant("NULL", nullPointer); runtime .getNilClass() .addMethod("to_ptr", new NilToPointerMethod(runtime.getNilClass(), nullPointer)); return pointerClass; }
private IRubyObject prepareRaiseException(Ruby runtime, IRubyObject[] args, Block block) { if (args.length == 0) { IRubyObject lastException = errorInfo; if (lastException.isNil()) { return new RaiseException(runtime, runtime.getRuntimeError(), "", false).getException(); } return lastException; } IRubyObject exception; ThreadContext context = getRuntime().getCurrentContext(); if (args.length == 1) { if (args[0] instanceof RubyString) { return runtime.getRuntimeError().newInstance(context, args, block); } if (!args[0].respondsTo("exception")) { return runtime.newTypeError("exception class/object expected").getException(); } exception = args[0].callMethod(context, "exception"); } else { if (!args[0].respondsTo("exception")) { return runtime.newTypeError("exception class/object expected").getException(); } exception = args[0].callMethod(context, "exception", args[1]); } if (!runtime.getException().isInstance(exception)) { return runtime.newTypeError("exception object expected").getException(); } if (args.length == 3) { ((RubyException) exception).set_backtrace(args[2]); } return exception; }