From a3eb5a6a3c7732b953c57558cdff8e82ae743227 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Wed, 18 Oct 2017 15:16:36 +0300 Subject: [PATCH] (refs #7562) Procedural SCL modules Change-Id: I299d723990673d945e67d46615edf90b55c6e8e6 --- .../META-INF/MANIFEST.MF | 1 + .../build.properties | 3 +- .../scl/SCL/ProceduralModule.scl | 5 ++ .../ProceduralModuleSourceRepository.java | 69 +++++++++++++++++++ .../procedural/ProceduralValueDefinition.java | 15 ++++ .../scl/osgi/internal/Activator.java | 12 ++++ 6 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 bundles/org.simantics.scl.compiler/scl/SCL/ProceduralModule.scl create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/ProceduralModuleSourceRepository.java create mode 100644 bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/procedural/ProceduralValueDefinition.java diff --git a/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF b/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF index 1007cf675..105c28de9 100644 --- a/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.scl.compiler/META-INF/MANIFEST.MF @@ -66,6 +66,7 @@ Export-Package: org.cojen.classfile, org.simantics.scl.compiler.runtime, org.simantics.scl.compiler.source, org.simantics.scl.compiler.source.repository, + org.simantics.scl.compiler.source.repository.procedural, org.simantics.scl.compiler.testing, org.simantics.scl.compiler.testing.repository, org.simantics.scl.compiler.top, diff --git a/bundles/org.simantics.scl.compiler/build.properties b/bundles/org.simantics.scl.compiler/build.properties index b7f9238d6..4b829c259 100644 --- a/bundles/org.simantics.scl.compiler/build.properties +++ b/bundles/org.simantics.scl.compiler/build.properties @@ -4,4 +4,5 @@ bin.includes = META-INF/,\ .,\ OSGI-INF/org.simantics.scl.compiler.source.repository.BuiltinModuleSourceRepository.xml,\ OSGI-INF/org.simantics.scl.compiler.elaboration.java.LoggingModule.xml,\ - sclTests/ + sclTests/,\ + scl/ diff --git a/bundles/org.simantics.scl.compiler/scl/SCL/ProceduralModule.scl b/bundles/org.simantics.scl.compiler/scl/SCL/ProceduralModule.scl new file mode 100644 index 000000000..a740ded92 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/scl/SCL/ProceduralModule.scl @@ -0,0 +1,5 @@ +importJava "org.simantics.scl.compiler.source.repository.procedural.ProceduralValueDefinition" where + data ProceduralValueDefinition + + @JavaName "" + valueDefinition :: Typeable t => String -> t -> ProceduralValueDefinition \ No newline at end of file diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/ProceduralModuleSourceRepository.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/ProceduralModuleSourceRepository.java new file mode 100644 index 000000000..4899f88ba --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/ProceduralModuleSourceRepository.java @@ -0,0 +1,69 @@ +package org.simantics.scl.compiler.source.repository; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.simantics.scl.compiler.common.names.Name; +import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant; +import org.simantics.scl.compiler.elaboration.modules.SCLValue; +import org.simantics.scl.compiler.errors.Failable; +import org.simantics.scl.compiler.module.ConcreteModule; +import org.simantics.scl.compiler.module.repository.ModuleRepository; +import org.simantics.scl.compiler.module.repository.UpdateListener; +import org.simantics.scl.compiler.runtime.RuntimeModule; +import org.simantics.scl.compiler.source.ModuleSource; +import org.simantics.scl.compiler.source.PrecompiledModuleSource; +import org.simantics.scl.compiler.source.repository.procedural.ProceduralValueDefinition; +import org.simantics.scl.compiler.top.ValueNotFound; + +public abstract class ProceduralModuleSourceRepository implements ModuleSourceRepository { + + public static final String PREFIX = "procedural:"; + + protected abstract ModuleRepository getModuleRepository(UpdateListener listener); + + @Override + public Collection getModuleNames() { + return Collections.emptyList(); + } + + @Override + public ModuleSource getModuleSource(String moduleName, UpdateListener listener) { + if(!moduleName.startsWith(PREFIX)) + return null; + + String sourceModuleName = moduleName.substring(PREFIX.length()); + Failable sourceModuleF = getModuleRepository(listener) + .getRuntimeModule(sourceModuleName, listener); + if(!sourceModuleF.didSucceed()) + return null; + + RuntimeModule sourceModule = sourceModuleF.getResult(); + Object values; + try { + values = sourceModule.getValue("values"); + } catch (ValueNotFound e) { + return null; + } + + if(!(values instanceof List)) + return null; + + ConcreteModule module = new ConcreteModule(moduleName); + for(Object item : (List)values) { + if(!(item instanceof ProceduralValueDefinition)) + return null; + ProceduralValueDefinition def = (ProceduralValueDefinition)item; + + SCLValue value = new SCLValue(Name.create(moduleName, def.name)); + value.setType(def.type); + value.setExpression(new EExternalConstant(def.value, def.type)); + value.setInlineInSimplification(true); + module.addValue(value); + } + module.setParentClassLoader(getClass().getClassLoader()); + return new PrecompiledModuleSource(module); + } + +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/procedural/ProceduralValueDefinition.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/procedural/ProceduralValueDefinition.java new file mode 100644 index 000000000..dfe344091 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/procedural/ProceduralValueDefinition.java @@ -0,0 +1,15 @@ +package org.simantics.scl.compiler.source.repository.procedural; + +import org.simantics.scl.compiler.types.Type; + +public class ProceduralValueDefinition { + public final Type type; + public final String name; + public final Object value; + + public ProceduralValueDefinition(Type type, String name, Object value) { + this.type = type; + this.name = name; + this.value = value; + } +} diff --git a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/Activator.java b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/Activator.java index ea113017a..d7a9e9e7f 100644 --- a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/Activator.java +++ b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/Activator.java @@ -4,8 +4,12 @@ import java.util.Hashtable; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +import org.simantics.scl.compiler.module.repository.ModuleRepository; +import org.simantics.scl.compiler.module.repository.UpdateListener; import org.simantics.scl.compiler.source.repository.ModuleSourceRepository; +import org.simantics.scl.compiler.source.repository.ProceduralModuleSourceRepository; import org.simantics.scl.compiler.source.repository.SourceRepositories; +import org.simantics.scl.osgi.SCLOsgi; public class Activator implements BundleActivator { @@ -23,6 +27,14 @@ public class Activator implements BundleActivator { bundleContext.registerService(ModuleSourceRepository.class, SourceRepositories.BUILTIN_SOURCE_REPOSITORY, properties); + bundleContext.registerService(ModuleSourceRepository.class, + new ProceduralModuleSourceRepository() { + @Override + protected ModuleRepository getModuleRepository(UpdateListener listener) { + return SCLOsgi.MODULE_REPOSITORY; + } + }, + properties); } public void stop(BundleContext bundleContext) throws Exception { -- 2.47.1