Package org.jruby

Examples of org.jruby.Ruby


    public IRubyObject get_array_of_float(ThreadContext context, IRubyObject offset, IRubyObject length) {
        int count = Util.int32Value(length);
        checkBounds(context, offset, count * 4);
        float[] array = new float[count];
        getMemoryIO().get(getOffset(offset), array, 0, array.length);
        Ruby runtime = context.getRuntime();
        RubyArray arr = RubyArray.newArray(runtime, array.length);
        for (int i = 0; i < array.length; ++i) {
            arr.add(RubyFloat.newFloat(runtime, array[i]));
        }
        return arr;
View Full Code Here


    public IRubyObject get_array_of_float64(ThreadContext context, IRubyObject offset, IRubyObject length) {
        int count = Util.int32Value(length);
        checkBounds(context, offset, count * 8);
        double[] array = new double[count];
        getMemoryIO().get(getOffset(offset), array, 0, array.length);
        Ruby runtime = context.getRuntime();
        RubyArray arr = RubyArray.newArray(runtime, array.length);
        for (int i = 0; i < array.length; ++i) {
            arr.add(RubyFloat.newFloat(runtime, array[i]));
        }
        return arr;
View Full Code Here

        return off;
    }
   
    @JRubyMethod(name = "add_field", required = 2, optional = 1)
    public IRubyObject add(ThreadContext context, IRubyObject[] args) {
        final Ruby runtime = context.getRuntime();
        IRubyObject name = args[0];
        NativeType type = NativeType.valueOf(Util.int32Value(args[1]));
        int offset = args.length > 2 ? Util.int32Value(args[2]) : -1;
        int align = 8, sizeBits = 8;
        switch (type) {
            case INT8:
            case UINT8:
                align = 8; sizeBits = 8;
                break;
            case INT16:
            case UINT16:
                align = 16; sizeBits = 16;
                break;
            case INT32:
            case UINT32:
                align = 32; sizeBits = 32;
                break;
            case INT64:
            case UINT64:
                align = LONG_ALIGN;
                sizeBits = 64;
                break;
            case LONG:
            case ULONG:
                align = LONG_ALIGN;
                sizeBits = LONG_SIZE;
                break;
            case FLOAT32:
                align = FLOAT_ALIGN;
                sizeBits = 32;
                break;
            case FLOAT64:
                align = DOUBLE_ALIGN;
                sizeBits = 64;
                break;
            case POINTER:
                align = Platform.getPlatform().addressSize();
                sizeBits = LONG_ALIGN;
                break;
            case STRING:
            case RBXSTRING:
                align = ADDRESS_ALIGN;
                sizeBits = ADDRESS_SIZE;
                break;
        }
        if (offset < 0) {
            offset = alignMember(this.size, align);
        }
        StructLayout.Member field = createMember(type, offset);
        if (field == null) {
            throw runtime.newArgumentError("Unknown field type: " + type);
        }
       
        fields.put(createKey(runtime, name), field);
        this.size = offset + (sizeBits / 8);
        return runtime.getNil();
    }
View Full Code Here

        return runtime.getNil();
    }
   
    @JRubyMethod(name = "add_char_array", required = 2, optional = 1)
    public IRubyObject add_char_array(ThreadContext context, IRubyObject[] args) {
        final Ruby runtime = context.getRuntime();
        IRubyObject name = args[0];
        int strlen = Util.int32Value(args[1]);
        long offset = args.length > 2 ? Util.int64Value(args[2]) : -1;
        if (offset < 0) {
            offset = alignMember(this.size, 8);
        }
        StructLayout.Member field = CharArrayMember.create(offset, strlen);
        fields.put(createKey(runtime, name), field);
        this.size += strlen;
        return runtime.getNil();
    }
View Full Code Here

    public JavaProxy(Ruby runtime, RubyClass klazz) {
        super(runtime, klazz);
    }
   
    public static RubyClass createJavaProxy(ThreadContext context) {
        Ruby runtime = context.getRuntime();
       
        RubyClass javaProxy = runtime.defineClass("JavaProxy", runtime.getObject(), new ObjectAllocator() {
            public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
                return new JavaProxy(runtime, klazz);
            }
        });
       
        RubyClass singleton = javaProxy.getSingletonClass();
       
        singleton.addReadWriteAttribute(context, "java_class");
       
        javaProxy.defineAnnotatedMethods(JavaProxy.class);
        javaProxy.includeModule(runtime.fastGetModule("JavaProxyMethods"));
       
        return javaProxy;
    }
View Full Code Here

import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;

public class JavaInterfaceTemplate {
    public static RubyModule createJavaInterfaceTemplateModule(ThreadContext context) {
        Ruby runtime = context.getRuntime();
        RubyModule javaInterfaceTemplate = runtime.defineModule("JavaInterfaceTemplate");

        RubyClass singleton = javaInterfaceTemplate.getSingletonClass();
        singleton.addReadAttribute(context, "java_class");
        singleton.defineAnnotatedMethods(JavaInterfaceTemplate.class);
View Full Code Here

    // OLD TODO from Ruby code:
    // This should be implemented in JavaClass.java, where we can
    // check for reserved Ruby names, conflicting methods, etc.
    @JRubyMethod(backtrace = true, visibility = Visibility.PRIVATE)
    public static IRubyObject implement(ThreadContext context, IRubyObject self, IRubyObject clazz) {
        Ruby runtime = context.getRuntime();

        if (!(clazz instanceof RubyModule)) {
            throw runtime.newTypeError(clazz, runtime.getModule());
        }

        RubyModule targetModule = (RubyModule)clazz;
        JavaClass javaClass = (JavaClass)self.getInstanceVariables().fastGetInstanceVariable("@java_class");
       
        Method[] javaInstanceMethods = javaClass.javaClass().getMethods();
        DynamicMethod dummyMethod = new org.jruby.internal.runtime.methods.JavaMethod(targetModule, Visibility.PUBLIC) {
            @Override
            public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
                // dummy bodies for default impls
                return context.getRuntime().getNil();
            }
        };
       
        for (int i = 0; i < javaInstanceMethods.length; i++) {
            Method method = javaInstanceMethods[i];
            String name = method.getName();
            if (targetModule.searchMethod(name) != UndefinedMethod.INSTANCE) continue;
           
            targetModule.addMethod(name, dummyMethod);
        }
       
        return runtime.getNil();
    }
View Full Code Here

        return RuntimeHelpers.invokeSuper(context, self, clazz, block);
    }

    private static void appendFeaturesToClass(ThreadContext context, IRubyObject self, RubyClass clazz) {
        Ruby runtime = context.getRuntime();
        IRubyObject javaClassObj = self.getInstanceVariables().fastGetInstanceVariable("@java_class");

        // initialize this if it hasn't been
        if (javaClassObj == null) {
            javaClassObj = runtime.getNil();
            self.getInstanceVariables().setInstanceVariable("@java_class", javaClassObj);
        }

        // initialize these if they haven't been
        IRubyObject javaClass = clazz.getInstanceVariables().fastGetInstanceVariable("@java_class");
        if (javaClass == null) {
            javaClass = runtime.getNil();
            clazz.getInstanceVariables().fastSetInstanceVariable("@java_class", javaClass);
        }
        IRubyObject javaProxyClass = clazz.getInstanceVariables().fastGetInstanceVariable("@java_proxy_class");
        if (javaProxyClass == null) {
            javaProxyClass = runtime.getNil();
            clazz.getInstanceVariables().fastSetInstanceVariable("@java_proxy_class", javaProxyClass);
        }

        // not allowed for original (non-generated) Java classes
        // note: not allowing for any previously created class right now;
        // this restriction might be loosened later for generated classes
        if ((javaClass.isTrue() && !clazz.getSingletonClass().isMethodBound("java_proxy_class", false)) ||
                javaProxyClass.isTrue()) {
            throw runtime.newArgumentError("can not add Java interface to existing Java class");
        }
       
        IRubyObject javaInterfaces = clazz.getInstanceVariables().fastGetInstanceVariable("@java_interfaces");
        if (javaInterfaces == null) {
            javaInterfaces = RubyArray.newArray(runtime, javaClassObj);
            clazz.getInstanceVariables().fastSetInstanceVariable("@java_interfaces", javaInterfaces);

            // setup new, etc unless this is a ConcreteJavaProxy subclass
            if (!clazz.isMethodBound("__jcreate!", false)) {
                // First we make modifications to the class, to adapt it to being
                // both a Ruby class and a proxy for a Java type

                RubyClass singleton = clazz.getSingletonClass();

                // list of interfaces we implement
                singleton.addReadAttribute(context, "java_interfaces");
               
                // We capture the original "new" and make it private
                DynamicMethod newMethod = singleton.searchMethod("new").dup();
                singleton.addMethod("__jredef_new", newMethod);
                newMethod.setVisibility(Visibility.PRIVATE);

                // The replacement "new" allocates and inits the Ruby object as before, but
                // also instantiates our proxified Java object by calling __jcreate!
                singleton.addMethod("new", new org.jruby.internal.runtime.methods.JavaMethod(singleton, Visibility.PUBLIC) {
                    @Override
                    public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
                        assert self instanceof RubyClass : "new defined on non-class";

                        RubyClass clazzSelf = (RubyClass)self;
                        IRubyObject newObj = clazzSelf.allocate();
                        RuntimeHelpers.invoke(context, newObj, "__jcreate!", args, block);
                        RuntimeHelpers.invoke(context, newObj, "initialize", args, block);

                        return newObj;
                    }
                });
               
                // Next, we define a few private methods that we'll use to manipulate
                // the Java object contained within this Ruby object
               
                // jcreate instantiates the proxy object which implements all interfaces
                // and which is wrapped and implemented by this object
                clazz.addMethod("__jcreate!", new JavaMethodNoBlock(clazz, Visibility.PRIVATE) {
                    @Override
                    public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args) {
                        IRubyObject javaInterfaces = RuntimeHelpers.invoke(context, self.getMetaClass(), "java_interfaces");
                        return jcreateProxy(self, javaInterfaces, args);
                    }
                });
               
                // Used by our duck-typification of Proc into interface types, to allow
                // coercing a simple proc into an interface parameter.
                clazz.addMethod("__jcreate_meta!", new JavaMethodNoBlock(clazz, Visibility.PRIVATE) {
                    @Override
                    public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args) {
                        IRubyObject javaInterfaces = RuntimeHelpers.invoke(context, self.getSingletonClass(), "java_interfaces");
                        IRubyObject result = jcreateProxy(self, javaInterfaces, args);
                        return result;
                    }
                });

                clazz.includeModule(runtime.getModule("JavaProxyMethods"));

                // If we hold a Java object, we need a java_class accessor
                clazz.addMethod("java_class", new JavaMethodZero(clazz, Visibility.PUBLIC) {
                    @Override
                    public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
View Full Code Here

    private static void appendFeaturesToModule(ThreadContext context, IRubyObject self, RubyModule module) {
        // assuming the user wants a collection of interfaces that can be
        // included together. make it so.
       
        Ruby runtime = context.getRuntime();

        // not allowed for existing Java interface modules
        if (module.getInstanceVariables().fastHasInstanceVariable("@java_class") &&
                module.getInstanceVariables().fastGetInstanceVariable("@java_class").isTrue()) {
            throw runtime.newTypeError("can not add Java interface to existing Java interface");
        }
       
        // To turn a module into an "interface collection" we add a class instance
        // variable to hold the list of interfaces, and modify append_features
        // for this module to call append_features on each of those interfaces as
View Full Code Here

    public ConcreteJavaProxy(Ruby runtime, RubyClass klazz) {
        super(runtime, klazz);
    }
   
    public static RubyClass createConcreteJavaProxy(ThreadContext context) {
        Ruby runtime = context.getRuntime();
       
        RubyClass concreteJavaProxy = runtime.defineClass("ConcreteJavaProxy",
                runtime.getJavaSupport().getJavaProxyClass(),
                new ObjectAllocator() {
            public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
                return new ConcreteJavaProxy(runtime, klazz);
            }
        });
View Full Code Here

TOP

Related Classes of org.jruby.Ruby

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.