Commits

Taku Miyakawa committed e0cc7e2

CallResult is now a class, and prohibits inheritation from outer packages

  • Participants
  • Parent commits e1350cf
  • Branches issue520-call-result-type-hierarchy

Comments (0)

Files changed (4)

src/main/java/org/kink_lang/kink/CallResult.java

 /**
  * Result of a function call,
  * which can be a value or a tail-call result.
- *
- * <p>The user code must not implement this interface.</p>
  */
-public interface CallResult {
+public class CallResult {
+
+
+    /**
+     * Result which indicates a tail-call result.
+     *
+     * <p>The field has package-local scope,
+     * not to be seen from the user code.
+     * {@linkplain org.kink_lang.kink.internal.invoke.Invoker} class
+     * fetches the value of the field via reflection.</p>
+     */
+    static final CallResult TAIL_CALL_RESULT = new CallResult();
+
+
+    /**
+     * The constructor can be accessed only from the class itself,
+     * or {@linkplain Value} class.
+     */
+    CallResult() {
+    }
+
 }
 
 // vim: et sw=4 sts=4

src/main/java/org/kink_lang/kink/Value.java

 /**
  * Kink value.
  */
-public final class Value implements CallResult {
+public final class Value extends CallResult {
 
 
     /**

src/main/java/org/kink_lang/kink/internal/invoke/Invoker.java

 import org.kink_lang.kink.CallResult;
 import org.kink_lang.kink.Area;
 import org.kink_lang.kink.Trace;
+import org.kink_lang.common.WrappingCheckedExceptionToRTE;
 
+import java.lang.reflect.Field;
 import java.util.List;
 import java.util.Collections;
+import java.security.PrivilegedAction;
+import java.security.AccessController;
 
 /**
  * Facade of function invocations.
     private static final ThreadLocal< Context > CONTEXTS = new ThreadLocal< Context >();
 
 
+    /** Result which indicates tail-calls. */
+    private static final CallResult TAIL_CALL_RESULT = WrappingCheckedExceptionToRTE.call( () -> {
+        Field field = CallResult.class.getDeclaredField( "TAIL_CALL_RESULT" );
+        AccessController.doPrivileged( (PrivilegedAction< Void >) () -> {
+            field.setAccessible( true );
+            return null;
+        } );
+        return (CallResult) field.get( null );
+    } );
+
+
     /** Should be instantiated. */
     private Invoker() {
         throw new UnsupportedOperationException( "Should not be instantiated" );
             context.setupInvocation( symbol , area , receiver , fun
                     , rawArg0 , rawArg1 , rawArg2 , rawArgs );
         }
-        return TailCallResult.instance();
+        return TAIL_CALL_RESULT;
     }
 }
 

src/main/java/org/kink_lang/kink/internal/invoke/TailCallResult.java

-/*
- * Copyright (c) 2013 Miyakawa Taku
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-package org.kink_lang.kink.internal.invoke;
-
-import org.kink_lang.kink.CallResult;
-
-/**
- * Class to indicate a tail-call result.
- *
- * <p>Instances themselves do not hold any information.
- * The current thread context holds the information about calling.</p>
- */
-class TailCallResult implements CallResult {
-
-
-    /** Singleton tail-call result. */
-    private static final TailCallResult INSTANCE = new TailCallResult();
-
-    /**
-     * Returns the singleton tail-call result.
-     */
-    static TailCallResult instance() {
-        return INSTANCE;
-    }
-}
-
-// vim: et sw=4 sts=4