Mercurial > hg > truffle
changeset 22012:09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Sat, 25 Jul 2015 10:25:36 +0200 |
parents | 95d5d6a93968 |
children | 551da6052741 |
files | truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/AccessorTest.java truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ImplicitExplicitExportTest.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/vm/TruffleVM.java |
diffstat | 4 files changed, 77 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/AccessorTest.java Sat Jul 25 10:25:36 2015 +0200 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.api.test.vm; + +import com.oracle.truffle.api.impl.Accessor; +import com.oracle.truffle.api.test.vm.ImplicitExplicitExportTest.ExportImportLanguage1; +import static com.oracle.truffle.api.test.vm.ImplicitExplicitExportTest.L1; +import com.oracle.truffle.api.vm.TruffleVM; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import static org.junit.Assert.*; +import org.junit.BeforeClass; +import org.junit.Test; + +public class AccessorTest { + public static Accessor API; + + @BeforeClass + public static void initAccessors() throws Exception { + Field f = Accessor.class.getDeclaredField("API"); + f.setAccessible(true); + API = (Accessor) f.get(null); + } + + @Test + public void canGetAccessToOwnLanguageInstance() throws Exception { + TruffleVM vm = TruffleVM.newVM().build(); + TruffleVM.Language language = vm.getLanguages().get(L1); + assertNotNull("L1 language is defined", language); + + Object ret = vm.eval(L1, "return nothing"); + assertNull("nothing is returned", ret); + + Object afterInitialization = findLanguageByClass(vm); + assertNotNull("Language found", afterInitialization); + assertTrue("Right instance", afterInitialization instanceof ExportImportLanguage1); + } + + Object findLanguageByClass(TruffleVM vm) throws IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + Method find = Accessor.class.getDeclaredMethod("findLanguage", TruffleVM.class, Class.class); + find.setAccessible(true); + Object language1 = find.invoke(API, vm, ExportImportLanguage1.class); + return language1; + } +}
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ImplicitExplicitExportTest.java Thu Jul 23 17:23:05 2015 +0200 +++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ImplicitExplicitExportTest.java Sat Jul 25 10:25:36 2015 +0200 @@ -171,7 +171,7 @@ } } - private static final String L1 = "application/x-test-import-export-1"; + static final String L1 = "application/x-test-import-export-1"; private static final String L2 = "application/x-test-import-export-2"; private static final String L3 = "application/x-test-import-export-3";
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java Thu Jul 23 17:23:05 2015 +0200 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java Sat Jul 25 10:25:36 2015 +0200 @@ -162,7 +162,7 @@ } protected TruffleLanguage findLanguage(TruffleVM vm, Class<? extends TruffleLanguage> languageClass) { - return API.findLanguage(vm, languageClass); + return SPI.findLanguage(vm, languageClass); } protected Closeable executionStart(TruffleVM vm, Debugger[] fillIn, Source s) {
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/vm/TruffleVM.java Thu Jul 23 17:23:05 2015 +0200 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/vm/TruffleVM.java Sat Jul 25 10:25:36 2015 +0200 @@ -567,6 +567,10 @@ TruffleLanguage findLanguage(Probe probe) { Class<? extends TruffleLanguage> languageClazz = SPI.findLanguage(probe); + return findLanguage(languageClazz); + } + + TruffleLanguage findLanguage(Class<? extends TruffleLanguage> languageClazz) { for (Map.Entry<String, Language> entrySet : langs.entrySet()) { Language languageDescription = entrySet.getValue(); if (languageClazz.isInstance(languageDescription.impl)) { @@ -648,6 +652,11 @@ } @Override + protected TruffleLanguage findLanguage(TruffleVM vm, Class<? extends TruffleLanguage> languageClass) { + return vm.findLanguage(languageClass); + } + + @Override protected Closeable executionStart(TruffleVM aThis, Debugger[] fillIn, Source s) { return super.executionStart(aThis, fillIn, s); }