Commits

Taku Miyakawa committed b74ea60

Now use("SOME_MODULE.*") does not import .show function (closing issue #480)

Comments (0)

Files changed (5)

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

 
 package org.kink_lang.kink;
 
+import org.kink_lang.kink.internal.Checker;
+
 /**
  * Factory of a module.
  *
     public Value getModule() {
         // Parent of the module holding 'string' method, not to be imported by 'use' method
         Value moduleParent = Value.value();
-        Fun nameFun = Fun.ofConstant( Value.value( getName() ) );
-        moduleParent.set( SET_SHOW , Value.value( nameFun ) );
-
         Value module = moduleParent.child();
         module.define( getDefinitionClass() );
+        moduleParent.set( SET_SHOW , Value.value( showFun( module , getName() ) ) );
         return module;
     }
 
 
+    /** GetSite of {@code SHOW_BUILDER.new}. */
+    private static final GetSite NEW = new GetSite( "new" );
+
+
+    /** GetSite of {@code /show_builder.add}. */
+    private static final GetSite ADD = new GetSite( "add" );
+
+
+    /** GetSite of {@code /show_builder.build}. */
+    private static final GetSite BUILD = new GetSite( "build" );
+
+
+    /**
+     * Returns .show function of Java modules.
+     */
+    private static Fun showFun( final Value module , final String moduleName ) {
+        return new Fun() {
+            @Override public Value run( Value receiver , Value ... args ) {
+                Checker.checkAssignCount( args , 0 );
+                if ( receiver == module ) {
+                    return Value.value( moduleName );
+                } else {
+                    Value showBuilderModule = Modules.find( "SHOW_BUILDER" );
+                    Value showBuilder = showBuilderModule.call( NEW , Value.value( "env" ) );
+                    showBuilder.call( ADD , Value.value( "hash" ) );
+                    return showBuilder.tailCall( BUILD , receiver );
+                }
+            }
+        };
+    }
+
+
 }
 
 // vim: et sw=4 sts=4

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

             return null;
         }
         LOGGER.debug( "Load module {} from {}" , moduleName , program.getName() );
-        Value env = Envs.env();
+        Value envParent = Envs.env();
         Fun showFun = new Fun() {
             @Override public Value run( Value actualEnv , Value ... args ) {
                 String show = "<env:#module_name=" + Strings.show( moduleName )
                 return Value.value( show );
             }
         };
-        env.set( SET_SHOW , Value.value( showFun ) );
+        envParent.set( SET_SHOW , Value.value( showFun ) );
+        Value env = envParent.child();
         program.compile().call( env );
 
         Set< String > publicSymbols = Envs.publicMethodSymbols( env );

src/main/java/org/kink_lang/kink/internal/module/Core.java

         }
 
 
-        @Define
-        public Value show( @Receiver Value envOrCore ) {
-            return Value.value( envOrCore == CoreHolder.CORE
-                    ? "CORE"
-                    : "<env:hash=" + Value.value( envOrCore.hashCode() ) + ">" );
-        }
-
-
     }
 }
 

src/test/java/org/kink_lang/kink/ModuleFactoryTest.java

 
 import org.junit.Test;
 import static org.junit.Assert.assertThat;
+import static org.junit.matchers.JUnitMatchers.containsString;
 
 import static org.hamcrest.CoreMatchers.is;
-
+import static org.hamcrest.CoreMatchers.not;
 
 /**
  * @see ModuleFactory Test target.
 
 
     @Test
+    public void test_show_module_is_module_name() {
+        Value mod = new StubModule().getModule();
+        assertThat( mod.toString() , is( "StubModule" ) );
+    }
+
+
+    @Test
+    public void test_show_of_child_of_module_represents_env() {
+        Value mod = new StubModule().getModule();
+        Value env = mod.child();
+        assertThat( env.toString() , is( not( "StubModule" ) ) );
+        assertThat( env.toString() , containsString( "env" ) );
+    }
+
+
+    @Test
     public void can_get_module() {
         Value mod = new StubModule().getModule();
         assertThat( mod.call( PING ).asString( "" ) , is( "ping!" ) );

src/test/java/org/kink_lang/kink/ModulesTest.java

 
 import java.io.ByteArrayInputStream;
 import static java.nio.charset.StandardCharsets.UTF_8;
+import java.util.Collections;
 
 import org.slf4j.LoggerFactory;
 
 
 
     @Test
+    public void load_features_from_kink_module() {
+        Value start = Modules.findKink( "_internal/START" );
+        assertThat( start.getOwnVarSymbols() , is( Collections.singleton( "start" ) ) );
+    }
+
+
+    @Test
     public void cannot_find_absent() {
         assertThat( Modules.isFound( "Absent" ) , is( false ) );
         assertThat( Modules.findKink( "Absent" ) , is( nullValue() ) );