generate bytecode for script
It is just prototype * does not make top level symbols visible as class members yet * does not take parameters * Script class name is hardcoded now
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.jet.codegen;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.testFramework.UsefulTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -33,6 +34,7 @@ import org.jetbrains.jet.parsing.JetParsingTest;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.MalformedURLException;
|
||||
@@ -40,6 +42,8 @@ import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
@@ -123,8 +127,10 @@ public abstract class CodegenTestCase extends UsefulTestCase {
|
||||
}
|
||||
|
||||
protected void blackBoxFile(String filename) {
|
||||
loadFile(filename);
|
||||
String actual;
|
||||
String content = loadFile(filename);
|
||||
Matcher matcher = Pattern.compile("// expected: (.*)").matcher(content);
|
||||
String expectedValue = matcher.find() ? matcher.group(1) : "OK";
|
||||
Object actual;
|
||||
try {
|
||||
actual = blackBox();
|
||||
} catch (NoClassDefFoundError e) {
|
||||
@@ -134,21 +140,32 @@ public abstract class CodegenTestCase extends UsefulTestCase {
|
||||
System.out.println(generateToText());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (!"OK".equals(actual)) {
|
||||
if (!Objects.equal(expectedValue, actual)) {
|
||||
System.out.println(generateToText());
|
||||
}
|
||||
assertEquals("OK", actual);
|
||||
assertEquals(expectedValue, actual);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String blackBox() throws Exception {
|
||||
ClassFileFactory codegens = generateClassesInFile();
|
||||
GeneratedClassLoader loader = createClassLoader(codegens);
|
||||
|
||||
try {
|
||||
String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile)).getFqName().getFqName();
|
||||
Class<?> namespaceClass = loader.loadClass(fqName);
|
||||
Method method = namespaceClass.getMethod("box");
|
||||
return (String) method.invoke(null);
|
||||
if (myFile.isScript()) {
|
||||
Class<?> scriptClass = loader.loadClass("Script");
|
||||
Object scriptInstance = scriptClass.newInstance();
|
||||
Field field = scriptClass.getDeclaredField("rv");
|
||||
field.setAccessible(true);
|
||||
Object result = field.get(scriptInstance);
|
||||
return result != null ? result.toString() : "null";
|
||||
}
|
||||
else {
|
||||
String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile)).getFqName().getFqName();
|
||||
Class<?> namespaceClass = loader.loadClass(fqName);
|
||||
Method method = namespaceClass.getMethod("box");
|
||||
return (String) method.invoke(null);
|
||||
}
|
||||
} finally {
|
||||
loader.dispose();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.codegen;
|
||||
|
||||
import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
|
||||
|
||||
/**
|
||||
* @author Stepan Koltsov
|
||||
*/
|
||||
public class ScriptGenTest extends CodegenTestCase {
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(CompilerSpecialMode.ALT_HEADERS);
|
||||
}
|
||||
|
||||
public void testHelloWorld() {
|
||||
blackBoxFile("script/helloWorld.ktscript");
|
||||
}
|
||||
|
||||
public void testString() {
|
||||
blackBoxFile("script/string.ktscript");
|
||||
}
|
||||
|
||||
public void testTopLevelFunction() {
|
||||
blackBoxFile("script/topLevelFunction.ktscript");
|
||||
// TODO: check function is visible as instance field (it is currently not)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user