Package Rakudo.Metamodel

Examples of Rakudo.Metamodel.RakudoObject


            }

            // Named slurpy?
            else if ((param.Flags & Parameter.NAMED_SLURPY_FLAG) != 0)
            {
                RakudoObject slurpyHolder = tc.DefaultHashType.getSTable().REPR.instance_of(tc, tc.DefaultHashType);
                c.LexPad.Storage[param.VariableLexpadPosition] = slurpyHolder;
                for (String name : nameds.keySet().toArray(new String[0]))
                    if (seenNames == null || !seenNames.containsKey(name))
                        Ops.llmapping_bind_at_key(tc, slurpyHolder,
                            Ops.box_str(tc, name, tc.DefaultStrBoxType),
                            nameds.get(name));
            }

            // Positional slurpy?
            else if ((param.Flags & Parameter.POS_SLURPY_FLAG) != 0)
            {
                RakudoObject slurpyHolder = tc.DefaultArrayType.getSTable().REPR.instance_of(tc, tc.DefaultArrayType);
                c.LexPad.Storage[param.VariableLexpadPosition] = slurpyHolder;
                int j;
                for (j = curPositional; j < positionals.length; j++)
                    Ops.lllist_push(tc, slurpyHolder, positionals[j]);
                curPositional = j;
            }

            // Named?
            else if (param.Name != null)
            {
                // Yes, try and get argument.
                if (nameds.containsKey(param.Name))
                {
                    // We have an argument, just bind it.
                    RakudoObject value = nameds.get(param.Name);
                    c.LexPad.Storage[param.VariableLexpadPosition] = value;
                    if (seenNames == null)
                        seenNames = new HashMap<String, Boolean>();
                    seenNames.put(param.Name, true);
                }
View Full Code Here


    /// </summary>
    /// <param name="Code"></param>
    public static RakudoObject WrapNativeMethod(RakudoCodeRef.IFunc_Body code)
    {
        Representation repr = REPRRegistry.get_REPR_by_name("KnowHOWREPR");
        RakudoObject wrapper = repr.type_object_for(null,null);
        wrapper.getSTable().SpecialInvoke = code;
        return wrapper;
    }
View Full Code Here

    /// <returns></returns>
    public static RakudoObject CallHandler(ThreadContext TC, RakudoObject Handler, RakudoObject ExceptionObject)
    {
        // Invoke the handler. Note that in some cases we never return from it;
        // for example, the return exception handler does .leave.
        RakudoObject Returned = Handler.getSTable().Invoke(TC, Handler, CaptureHelper.FormWith(new RakudoObject[] { ExceptionObject }));

        // So, we returned. Let's see if it's resumable.
        RakudoObject ResumableMeth = Returned.getSTable().FindMethod(TC, Returned, "resumable", Hints.NO_HINT);
        RakudoObject Resumable = ResumableMeth.getSTable().Invoke(TC, ResumableMeth, CaptureHelper.FormWith(new RakudoObject[] { Returned }));
        if (Ops.unbox_int(TC, Resumable) != 0)
        {
            // Resumable, so don't need to stack unwind. Simply return
            // from here.
            return Returned;
View Full Code Here

    public static void DieFromUnhandledException(ThreadContext TC, RakudoObject Exception)
    {
        // Try to stringify the exception object.
        try
        {
            RakudoObject StrMeth = Exception.getSTable().FindMethod(TC, Exception, "Str", Hints.NO_HINT);
            RakudoObject Stringified = StrMeth.getSTable().Invoke(TC, StrMeth, CaptureHelper.FormWith(new RakudoObject[] { Exception }));
            System.out.println(Ops.unbox_str(TC, Stringified));
        }
        catch ( Exception ex )
        {
            System.err.println("Died from an exception, and died trying to stringify it too.");
View Full Code Here

    {
        // Allocate slot mapping table.
        SlotAllocation = new HashMap<RakudoObject, HashMap<String, Integer>>();

        // Walk through the parents list.
        RakudoObject currentClass = WHAT;
        int currentSlot = 0;
        while (currentClass != null)
        {
            // Get attributes and iterate over them.
            RakudoObject HOW = currentClass.getSTable().HOW;
            RakudoObject attributesMeth = HOW.getSTable().FindMethod(tc, HOW, "attributes", Hints.NO_HINT);
            HashMap<String, RakudoObject> localBoxInt1 = new HashMap<String, RakudoObject>();
            localBoxInt1.put("local", Ops.box_int(tc, 1, tc.DefaultBoolBoxType));
            RakudoObject attributes = attributesMeth.getSTable().Invoke(tc, attributesMeth, CaptureHelper.FormWith(
                new RakudoObject[] { HOW, currentClass },
                localBoxInt1)); // new HashMap<String, RakudoObject>() { { "local", Ops.box_int(tc, 1, tc.DefaultBoolBoxType) } }));
            RakudoObject attributesElemsMeth = attributes.getSTable().FindMethod(tc, attributes, "elems", Hints.NO_HINT);
            int attributesElems = Ops.unbox_int(tc, attributesElemsMeth.getSTable().Invoke(tc, attributesElemsMeth,
                CaptureHelper.FormWith(new RakudoObject[] { attributes })));
            RakudoObject attrAtPosMeth = attributes.getSTable().FindMethod(tc, attributes, "at_pos", Hints.NO_HINT);
            for (int i = 0; i < attributesElems; i++)
            {
                // Get the attribute, then get its name.
                RakudoObject attr = attrAtPosMeth.getSTable().Invoke(tc, attrAtPosMeth, CaptureHelper.FormWith(
                    new RakudoObject[] { attributes, Ops.box_int(tc, i, tc.DefaultIntBoxType) }));
                RakudoObject nameMeth = attr.getSTable().FindMethod(tc, attr, "name", Hints.NO_HINT);
                String name = Ops.unbox_str(tc, attr.getSTable().Invoke(tc, nameMeth, CaptureHelper.FormWith(
                    new RakudoObject[] { attr })));

                // Allocate a slot.
                if (!SlotAllocation.containsKey(currentClass))
                    SlotAllocation.put(currentClass, new HashMap<String, Integer>());
                SlotAllocation.get(currentClass).put(name, currentSlot);
                currentSlot++;
            }

            // Find the next parent(s).
            RakudoObject parentsMeth = HOW.getSTable().FindMethod(tc, HOW, "parents", Hints.NO_HINT);
            HashMap<String,RakudoObject> localBoxInt2 = new HashMap<String,RakudoObject>();
            localBoxInt2.put("local", Ops.box_int(tc, 1, tc.DefaultBoolBoxType));
            RakudoObject parents = parentsMeth.getSTable().Invoke(tc, parentsMeth, CaptureHelper.FormWith(
                new RakudoObject[] { HOW, currentClass },
                localBoxInt2)); // new HashMap<String,RakudoObject>() { { "local", Ops.box_int(tc, 1, tc.DefaultBoolBoxType) } }));
            // Check how many parents we have.
            RakudoObject parentElemsMeth = parents.getSTable().FindMethod(tc, parents, "elems", Hints.NO_HINT);
            int parentElems = Ops.unbox_int(tc, parentElemsMeth.getSTable().Invoke(tc, parentElemsMeth,
                CaptureHelper.FormWith(new RakudoObject[] { parents })));
            if (parentElems == 0)
            {
                // We're done. \o/
                Slots = currentSlot;
                break;
            }
            else if (parentElems > 1)
            {
                // Multiple inheritance, so we can't compute this hierarchy.
                SlotAllocation = new HashMap<RakudoObject, HashMap<String, Integer>>();
                return;
            }
            else
            {
                // Just one. Get next parent.
                RakudoObject atPosMeth = parents.getSTable().FindMethod(tc, parents, "at_pos", Hints.NO_HINT);
                currentClass = atPosMeth.getSTable().Invoke(tc, atPosMeth, CaptureHelper.FormWith(
                    new RakudoObject[] { parents, Ops.box_int(tc, 0, tc.DefaultIntBoxType) }));
            }
        }
    }
View Full Code Here

    public static RakudoObject Bootstrap()
    {
        // Create our KnowHOW type object. Note we don't have a HOW
        // just yet, so pass in null.
        Representation REPR = REPRRegistry.get_REPR_by_name("KnowHOWREPR");
        RakudoObject KnowHOW = REPR.type_object_for(null,null);

        // We'll set up a dictionary of our various methods to go into
        // KnowHOW's HOW, since we'll want to work with them a bit.
        HashMap<String, RakudoObject> KnowHOWMeths = new HashMap<String, RakudoObject>();
        KnowHOWMeths.put("new_type", CodeObjectUtility.WrapNativeMethod( new RakudoCodeRef.IFunc_Body() { // C# has a lambda
            public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject capture)
            {
                // We first create a new HOW instance.
                RakudoObject knowHOWTypeObj = CaptureHelper.GetPositional(capture, 0);
                RakudoObject HOW = knowHOWTypeObj.getSTable().REPR.instance_of(tc, knowHOWTypeObj.getSTable().WHAT);

                // If we have a name arg, stash that value away.
                RakudoObject typeName = CaptureHelper.GetNamed(capture, "name");
                ((KnowHOWREPR.KnowHOWInstance)HOW).Name = typeName != null ? typeName : Ops.box_str(tc, "<anon>");

                // Now create a new type object to go with it of the
                // desired REPR; we default to P6opaque (note that the
                // KnowHOW repr only knows how to store a table of
                // methods and attributes, it can't be used for an
                // instance object that actually wants to store some
                // instance data).
                RakudoObject REPRName = CaptureHelper.GetNamed(capture, "repr");
                if (REPRName != null)
                {
                    // Look up the REPR.
                    Representation REPRToUse = REPRRegistry.get_REPR_by_name(Ops.unbox_str(null, REPRName));
                    return REPRToUse.type_object_for(null, HOW);
                }
                else
                {
                    // Just go with the P6opaque REPR.
                    return REPRRegistry.get_REPR_by_name("P6opaque").type_object_for(tc, HOW);
                }
            }
        }));
        KnowHOWMeths.put("add_attribute", CodeObjectUtility.WrapNativeMethod( new RakudoCodeRef.IFunc_Body() { // an anonymous class where C# uses a lambda
            public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject capture)
            {
                KnowHOWREPR.KnowHOWInstance HOW = (KnowHOWREPR.KnowHOWInstance)CaptureHelper.GetPositional(capture, 0);
                RakudoObject Attr = CaptureHelper.GetPositional(capture, 2);
                HOW.Attributes.add(Attr);
                return CaptureHelper.Nil();
            }
        }));
        KnowHOWMeths.put("add_method", CodeObjectUtility.WrapNativeMethod( new RakudoCodeRef.IFunc_Body() { // an anonymous class where C# uses a lambda
            public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject capture)
            {
                KnowHOWREPR.KnowHOWInstance HOW = (KnowHOWREPR.KnowHOWInstance)CaptureHelper.GetPositional(capture, 0);
                String name = CaptureHelper.GetPositionalAsString(capture, 2);
                RakudoObject method = CaptureHelper.GetPositional(capture, 3);
                HOW.Methods.put(name, method);
                return CaptureHelper.Nil();
            }
        }));
        KnowHOWMeths.put("find_method", CodeObjectUtility.WrapNativeMethod( new RakudoCodeRef.IFunc_Body() { // an anonymous class where C# uses a lambda
            public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject capture)
            {
                // We go to some effort to be really fast in here, 'cus it's a
                // hot path for dynamic dispatches.
                RakudoObject[] Positionals = ((P6capture.Instance)capture).Positionals;
                assert Positionals.length >= 3;
                KnowHOWREPR.KnowHOWInstance HOW = (KnowHOWREPR.KnowHOWInstance)Positionals[0];
                if (HOW.Methods.containsKey(Ops.unbox_str(tc, Positionals[2])))
                    return HOW.Methods.get(Ops.unbox_str(tc, Positionals[2]));
                else {
                    // throw new NoSuchMethodException("No such method " + Ops.unbox_str(tc, Positionals[1]));
                    throw new UnsupportedOperationException("No such method " + Ops.unbox_str(tc, Positionals[1]));
                }
            }
        }));
        KnowHOWMeths.put("compose", CodeObjectUtility.WrapNativeMethod( new RakudoCodeRef.IFunc_Body() { // an anonymous class where C# uses a lambda
            public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject capture)
            {
                RakudoObject obj = CaptureHelper.GetPositional(capture, 1);
                return obj;
            }
        }));
        KnowHOWMeths.put("attributes", CodeObjectUtility.WrapNativeMethod(new RakudoCodeRef.IFunc_Body() { // an anonymous class where C# uses a lambda
            public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject capture)
            {
                // Safe to just return a P6list instance that points at
                // the same thing we hold internally, since a list is
                // immutable. However, if default list type has no HOW,
                // we will see if we can find one that does have it.
                KnowHOWREPR.KnowHOWInstance HOW = (KnowHOWREPR.KnowHOWInstance)CaptureHelper.GetPositional(capture, 0);
                RakudoObject listType = MostDefinedListType(tc);
                RakudoObject result = listType.getSTable().REPR.instance_of(tc, listType);
                ((P6list.Instance)result).Storage = HOW.Attributes;
                return result;
            }
        }));
        KnowHOWMeths.put("methods", CodeObjectUtility.WrapNativeMethod(new RakudoCodeRef.IFunc_Body() { // an anonymous class where C# uses a lambda
            public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject capture)
            {
                // Return the methods list.
                KnowHOWREPR.KnowHOWInstance HOW = (KnowHOWREPR.KnowHOWInstance)CaptureHelper.GetPositional(capture, 0);
                RakudoObject listType = MostDefinedListType(tc);
                RakudoObject result = listType.getSTable().REPR.instance_of(tc, listType);
                ((P6list.Instance)result).Storage.addAll(HOW.Methods.values());
                return result;
            }
        }));
        KnowHOWMeths.put("parents", CodeObjectUtility.WrapNativeMethod(new RakudoCodeRef.IFunc_Body() { // an anonymous class
            public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject capture)
            {
                // A pure prototype never has any parents, so return an empty list.
                RakudoObject listType = MostDefinedListType(tc);
                return listType.getSTable().REPR.instance_of(tc, listType);
            }
        }));

        KnowHOWMeths.put("type_check", CodeObjectUtility.WrapNativeMethod(new RakudoCodeRef.IFunc_Body() { // an anonymous class
            public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject capture)
            {
                // Can only match against ourselves.
                RakudoObject self = CaptureHelper.GetPositional(capture, 1);
                RakudoObject check = CaptureHelper.GetPositional(capture, 2);
                return Ops.box_int(tc, self.getSTable().WHAT == check.getSTable().WHAT ? 1 : 0, tc.DefaultBoolBoxType);
            }
        }));

        // We create a KnowHOW instance that can describe itself.
        // This means .HOW.HOW.HOW.HOW etc will always return that,
View Full Code Here

        // Create a new HOW instance.
        KnowHOWREPR.KnowHOWInstance HOW = (KnowHOWREPR.KnowHOWInstance)knowHOW.getSTable().REPR.instance_of(null, knowHOW);

        // We base the attribute on P6str, since we just want to store an
        // attribute name for now.
        RakudoObject knowHOWAttribute = REPRRegistry.get_REPR_by_name("P6str").type_object_for(null, HOW);

        // Add methods new and Str.
        HOW.Methods.put("new", CodeObjectUtility.WrapNativeMethod(new RakudoCodeRef.IFunc_Body() {  // the C# version uses a lambda
            public RakudoObject Invoke(ThreadContext tc, RakudoObject obj, RakudoObject capture)
            {
                RakudoObject WHAT = CaptureHelper.GetPositional(capture, 0).getSTable().WHAT;
                String name = Ops.unbox_str(tc, CaptureHelper.GetNamed(capture, "name"));
                return Ops.box_str(tc, name, WHAT);
            }
        }));
        HOW.Methods.put("name", CodeObjectUtility.WrapNativeMethod(new RakudoCodeRef.IFunc_Body() { // the C# version uses a lambda
            public RakudoObject Invoke(ThreadContext tc, RakudoObject obj, RakudoObject capture)
            {
                RakudoObject self = CaptureHelper.GetPositional(capture, 0);
                return Ops.box_str(tc, Ops.unbox_str(tc, self), tc.DefaultStrBoxType);
            }
        }));

        return knowHOWAttribute;
View Full Code Here

    /// <summary>
    /// This finds a method with the given name or using a hint.
    /// </summary>
    public RakudoObject FindMethod(ThreadContext tc, RakudoObject obj, String name, int hint)
    {
        RakudoObject cachedMethod;

        // Does this s-table have a special overridden finder?
        if (SpecialFindMethod != null)
            return SpecialFindMethod.SpecialFindMethod(tc, obj, name, hint);

        // See if we can find it by hint.
        if (hint != Hints.NO_HINT && obj.getSTable().VTable != null && hint < obj.getSTable().VTable.length)
        {
            // Yes, just grab it from the v-table.
            return obj.getSTable().VTable[hint];
        }

        // Otherwise, try method cache.
        else if (MethodCache != null && MethodCache.containsKey(name))
            { cachedMethod = MethodCache.get(name); return cachedMethod; }

        // Otherwise, go ask the meta-object.
        else
        {
            // Find the find_method method.
            RakudoObject HOW = obj.getSTable().HOW;
            RakudoObject method = obj.getSTable().CachedFindMethod;
            if (method == null)
                obj.getSTable().CachedFindMethod = method = HOW.getSTable().FindMethod(
                    tc, HOW, "find_method", Hints.NO_HINT);

            // Call it.
            RakudoObject capture = CaptureHelper.FormWith(new RakudoObject[] { HOW, obj, Ops.box_str(tc, name, tc.DefaultStrBoxType) });
            return method.getSTable().Invoke(tc, method, capture);
        }
    }
View Full Code Here

    public RakudoObject Invoke(ThreadContext threadContext, RakudoObject method, RakudoObject capture)
    {
        if (SpecialInvoke != null)
            return SpecialInvoke.Invoke(threadContext, method, capture);
        SharedTable sharedTable = method.getSTable();
        RakudoObject invokable = sharedTable.CachedInvoke; if (invokable == null) { invokable = sharedTable.CachedInvoke = method.getSTable().FindMethod(threadContext, method, "postcircumfix:<( )>", Hints.NO_HINT); }
        return invokable.getSTable().Invoke(threadContext, method, capture);
    }
View Full Code Here

                    return Ops.box_int(threadContext, 1, threadContext.DefaultBoolBoxType);
            return Ops.box_int(threadContext, 0, threadContext.DefaultBoolBoxType);
        }
        else
        {
            RakudoObject checker = HOW.getSTable().FindMethod(threadContext, HOW, "type_check", Hints.NO_HINT);
            RakudoObject capture = CaptureHelper.FormWith(new RakudoObject[] { HOW, object, checkee });
            return checker.getSTable().Invoke(threadContext, checker, capture);
        }
    }
View Full Code Here

TOP

Related Classes of Rakudo.Metamodel.RakudoObject

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.