| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.interpreter.evaluator.values; |
|---|
| 19 | |
|---|
| 20 | import com.sun.fortress.compiler.WellKnownNames; |
|---|
| 21 | import static com.sun.fortress.exceptions.InterpreterBug.bug; |
|---|
| 22 | import static com.sun.fortress.exceptions.ProgramError.errorMsg; |
|---|
| 23 | import com.sun.fortress.interpreter.evaluator.Environment; |
|---|
| 24 | import com.sun.fortress.interpreter.evaluator.Evaluator; |
|---|
| 25 | import com.sun.fortress.interpreter.evaluator.types.FType; |
|---|
| 26 | import com.sun.fortress.nodes.Applicable; |
|---|
| 27 | import com.sun.fortress.nodes.Expr; |
|---|
| 28 | import com.sun.fortress.nodes_util.NodeUtil; |
|---|
| 29 | import com.sun.fortress.useful.Hasher; |
|---|
| 30 | import com.sun.fortress.useful.Useful; |
|---|
| 31 | |
|---|
| 32 | import java.util.Collections; |
|---|
| 33 | import java.util.List; |
|---|
| 34 | |
|---|
| 35 | public class MethodClosure extends FunctionClosure implements Method { |
|---|
| 36 | |
|---|
| 37 | final int selfParameterIndex; |
|---|
| 38 | |
|---|
| 39 | final FType definer; |
|---|
| 40 | |
|---|
| 41 | public MethodClosure(Environment within, Applicable fndef, FType definer) { |
|---|
| 42 | super(within, fndef); |
|---|
| 43 | this.definer = definer; |
|---|
| 44 | selfParameterIndex = NodeUtil.selfParameterIndex(getDef()); |
|---|
| 45 | |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | public MethodClosure(Environment within, Applicable fndef, FType definer, List<FType> args) { |
|---|
| 49 | super(within, fndef, args); |
|---|
| 50 | this.definer = definer; |
|---|
| 51 | selfParameterIndex = NodeUtil.selfParameterIndex(getDef()); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | protected List<Parameter> adjustParameterList(List<Parameter> params2) { |
|---|
| 62 | return selfParameterIndex == -1 ? params2 : Useful.removeIndex(selfParameterIndex, params2); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | protected Environment envForApplication(FObject selfValue) { |
|---|
| 69 | return selfValue.getLexicalEnv(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public FValue applyMethod(FObject selfValue, List<FValue> args) { |
|---|
| 73 | args = conditionallyUnwrapTupledArgs(args); |
|---|
| 74 | Expr body = getBodyNull(); |
|---|
| 75 | |
|---|
| 76 | if (body != null) { |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | Evaluator eval = new Evaluator(buildEnvFromEnvAndParams(envForApplication(selfValue), args)); |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | eval.e.putValueRaw(selfName(), selfValue); |
|---|
| 90 | return eval.eval(body); |
|---|
| 91 | } else if (def instanceof Method) { |
|---|
| 92 | return ((Method) def).applyMethod(selfValue, args); |
|---|
| 93 | } else { |
|---|
| 94 | return bug(errorMsg("MethodClosure ", this, " has neither body nor def instanceof Method")); |
|---|
| 95 | |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public FValue applyMethod(FObject self) { |
|---|
| 100 | return applyMethod(self, Collections.<FValue>emptyList()); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | public FValue applyMethod(FObject self, FValue a) { |
|---|
| 107 | return applyMethod(self, Collections.singletonList(a)); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | public FValue applyMethod(FObject self, FValue a, FValue b) { |
|---|
| 111 | return applyMethod(self, Useful.list(a, b)); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | public FValue applyMethod(FObject self, FValue a, FValue b, FValue c) { |
|---|
| 115 | return applyMethod(self, Useful.list(a, b, c)); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | public FValue applyInnerPossiblyGeneric(List<FValue> args) { |
|---|
| 126 | if (selfParameterIndex == -1) { |
|---|
| 127 | return bug(errorMsg("MethodClosure for dotted method ", |
|---|
| 128 | this, |
|---|
| 129 | " was invoked as if it were a functional method.")); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | FObject self = (FObject) args.get(selfParameterIndex).getValue(); |
|---|
| 134 | args = Useful.removeIndex(selfParameterIndex, args); |
|---|
| 135 | return applyMethod(self, args); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | public FValue applyToArgs() { |
|---|
| 139 | return bug(errorMsg("No recipient object for method ", this)); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | public FValue applyToArgs(FValue a) { |
|---|
| 143 | return applyToArgs(toSelf(a), Collections.<FValue>emptyList()); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | public FValue applyToArgs(FValue a, FValue b) { |
|---|
| 150 | if (selfParameterIndex <= 0) { |
|---|
| 151 | return applyToArgs(toSelf(a), Collections.singletonList(b)); |
|---|
| 152 | } |
|---|
| 153 | return applyToArgs(toSelf(b), Collections.singletonList(a)); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | public FValue applyToArgs(FValue a, FValue b, FValue c) { |
|---|
| 157 | if (selfParameterIndex <= 0) { |
|---|
| 158 | return applyToArgs(toSelf(a), Useful.list(b, c)); |
|---|
| 159 | } else if (selfParameterIndex == 1) { |
|---|
| 160 | return applyToArgs(toSelf(b), Useful.list(a, c)); |
|---|
| 161 | } else { |
|---|
| 162 | return applyToArgs(toSelf(c), Useful.list(a, b)); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | public FValue applyToArgs(FValue a, FValue b, FValue c, FValue d) { |
|---|
| 167 | if (selfParameterIndex <= 0) { |
|---|
| 168 | return applyToArgs(toSelf(a), Useful.list(b, c, d)); |
|---|
| 169 | } else if (selfParameterIndex == 1) { |
|---|
| 170 | return applyToArgs(toSelf(b), Useful.list(a, c, d)); |
|---|
| 171 | } else if (selfParameterIndex == 2) { |
|---|
| 172 | return applyToArgs(toSelf(c), Useful.list(a, b, d)); |
|---|
| 173 | } else { |
|---|
| 174 | return applyToArgs(toSelf(d), Useful.list(a, b, c)); |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | protected FObject toSelf(FValue a) { |
|---|
| 179 | if (a instanceof FObject) return (FObject) a; |
|---|
| 180 | return bug(errorMsg("Non-object recipient ", a, " for method ", this)); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | public String selfName() { |
|---|
| 184 | return WellKnownNames.secretSelfName; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | public static Hasher<MethodClosure> signatureEquivalence = new Hasher<MethodClosure>() { |
|---|
| 188 | |
|---|
| 189 | @Override |
|---|
| 190 | public long hash(MethodClosure x) { |
|---|
| 191 | return Simple_fcn.signatureEquivalence.hash(x); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | @Override |
|---|
| 195 | public boolean equiv(MethodClosure x, MethodClosure y) { |
|---|
| 196 | return Simple_fcn.signatureEquivalence.equiv(x, y); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | }; |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | @Override |
|---|
| 205 | public String asMethodName() { |
|---|
| 206 | |
|---|
| 207 | return NodeUtil.nameAsMethod(getDef()); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | public FType getDefiner() { |
|---|
| 211 | return definer; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | } |
|---|