All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.truffleruby.language.supercall.ZSuperOutsideMethodNode Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved. This
 * code is released under a tri EPL/GPL/LGPL license. You can use it,
 * redistribute it and/or modify it under the terms of the:
 *
 * Eclipse Public License version 2.0, or
 * GNU General Public License version 2, or
 * GNU Lesser General Public License version 2.1.
 */
package org.truffleruby.language.supercall;

import org.truffleruby.RubyContext;
import org.truffleruby.RubyLanguage;
import org.truffleruby.core.string.FrozenStrings;
import org.truffleruby.language.RubyContextSourceNode;
import org.truffleruby.language.RubyNode;
import org.truffleruby.language.arguments.RubyArguments;
import org.truffleruby.language.control.RaiseException;
import org.truffleruby.language.methods.InternalMethod;

import com.oracle.truffle.api.frame.VirtualFrame;

/** Represents a super call with implicit arguments without a surrounding method */
public final class ZSuperOutsideMethodNode extends RubyContextSourceNode {

    final boolean insideDefineMethod;

    @Child private LookupSuperMethodNode lookupSuperMethodNode = LookupSuperMethodNodeGen.create();

    public ZSuperOutsideMethodNode(boolean insideDefineMethod) {
        this.insideDefineMethod = insideDefineMethod;
    }

    @Override
    public Object execute(VirtualFrame frame) {
        // This is MRI behavior
        if (insideDefineMethod) { // TODO (eregon, 22 July 2015): This check should be more dynamic.
            throw new RaiseException(getContext(), coreExceptions().runtimeError(
                    "implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments explicitly.",
                    this));
        } else {
            throw new RaiseException(getContext(), coreExceptions().noSuperMethodOutsideMethodError(this));
        }
    }

    @Override
    public Object isDefined(VirtualFrame frame, RubyLanguage language, RubyContext context) {
        final Object self = RubyArguments.getSelf(frame);
        final InternalMethod superMethod = lookupSuperMethodNode.executeLookupSuperMethod(frame, self);

        if (superMethod == null) {
            return nil;
        } else {
            return FrozenStrings.SUPER;
        }
    }

    @Override
    public RubyNode cloneUninitialized() {
        var copy = new ZSuperOutsideMethodNode(insideDefineMethod);
        return copy.copyFlags(this);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy