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:
@@ -86,8 +86,13 @@ public class ClosureAnnotator {
|
||||
|
||||
private void mapFilesToNamespaces(Collection<JetFile> files) {
|
||||
for (JetFile file : files) {
|
||||
FqName fqName = JetPsiUtil.getFQName(file);
|
||||
namespaceName2Files.putValue(fqName, file);
|
||||
if (file.isScript()) {
|
||||
namespaceName2Files.putValue(FqName.ROOT, file);
|
||||
}
|
||||
else {
|
||||
FqName fqName = JetPsiUtil.getFQName(file);
|
||||
namespaceName2Files.putValue(fqName, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1340,7 +1340,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
||||
// assert !superCall;
|
||||
// callableMethod = ClosureCodegen.asCallableMethod((FunctionDescriptor) fd);
|
||||
//}
|
||||
if (fd instanceof ExpressionAsFunctionDescriptor || (fd instanceof SimpleFunctionDescriptor && fd.getContainingDeclaration() instanceof FunctionDescriptor)) {
|
||||
if (fd instanceof ExpressionAsFunctionDescriptor || (fd instanceof SimpleFunctionDescriptor && (fd.getContainingDeclaration() instanceof FunctionDescriptor || fd.getContainingDeclaration() instanceof ScriptDescriptor))) {
|
||||
SimpleFunctionDescriptor invoke = CodegenUtil.createInvoke((FunctionDescriptor) fd);
|
||||
callableMethod = ClosureCodegen.asCallableMethod(invoke);
|
||||
}
|
||||
|
||||
@@ -80,6 +80,9 @@ public class NamespaceCodegen {
|
||||
else if (declaration instanceof JetClassOrObject) {
|
||||
state.getInjector().getClassCodegen().generate(context, (JetClassOrObject) declaration);
|
||||
}
|
||||
else if (declaration instanceof JetScript) {
|
||||
state.getInjector().getScriptCodegen().generate(context, (JetScript) declaration);
|
||||
}
|
||||
// else if (declaration instanceof JetFile) {
|
||||
// JetFile childNamespace = (JetFile) declaration;
|
||||
// state.forNamespace(childNamespace).generate(childNamespace);
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.ScriptDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetScript;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.java.JdkNames;
|
||||
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
|
||||
import org.objectweb.asm.MethodAdapter;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.commons.InstructionAdapter;
|
||||
import sun.tools.tree.InlineReturnStatement;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* @author Stepan Koltsov
|
||||
*/
|
||||
public class ScriptCodegen {
|
||||
|
||||
public static final String LAST_EXPRESSION_VALUE_FIELD_NAME = "rv";
|
||||
|
||||
@NotNull
|
||||
private GenerationState state;
|
||||
@NotNull
|
||||
private ClassFileFactory classFileFactory;
|
||||
@NotNull
|
||||
private JetTypeMapper jetTypeMapper;
|
||||
|
||||
|
||||
@Inject
|
||||
public void setState(@NotNull GenerationState state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setClassFileFactory(@NotNull ClassFileFactory classFileFactory) {
|
||||
this.classFileFactory = classFileFactory;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setJetTypeMapper(@NotNull JetTypeMapper jetTypeMapper) {
|
||||
this.jetTypeMapper = jetTypeMapper;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void generate(CodegenContext context, JetScript scriptDeclaration) {
|
||||
ScriptDescriptor scriptDescriptor = (ScriptDescriptor) state.getBindingContext().get(BindingContext.SCRIPT, scriptDeclaration);
|
||||
ClassBuilder classBuilder = classFileFactory.newVisitor("Script.class");
|
||||
classBuilder.defineClass(scriptDeclaration,
|
||||
Opcodes.V1_6,
|
||||
Opcodes.ACC_PUBLIC,
|
||||
"Script",
|
||||
null,
|
||||
JdkNames.JL_OBJECT.getInternalName(),
|
||||
new String[0]);
|
||||
|
||||
Type blockType = jetTypeMapper.mapType(scriptDescriptor.getReturnType(), MapTypeMode.VALUE);
|
||||
|
||||
classBuilder.newField(null, Opcodes.ACC_PUBLIC, LAST_EXPRESSION_VALUE_FIELD_NAME, blockType.getDescriptor(), null, null);
|
||||
|
||||
MethodVisitor mv = classBuilder.newMethod(scriptDeclaration, Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
|
||||
|
||||
mv.visitCode();
|
||||
|
||||
InstructionAdapter instructionAdapter = new InstructionAdapter(mv);
|
||||
|
||||
instructionAdapter.load(0, Type.getObjectType("Script"));
|
||||
instructionAdapter.invokespecial(JdkNames.JL_OBJECT.getInternalName(), "<init>", "()V");
|
||||
|
||||
instructionAdapter.load(0, Type.getObjectType("Script"));
|
||||
StackValue stackValue = new ExpressionCodegen(mv, new FrameMap(), Type.VOID_TYPE, context, state).gen(scriptDeclaration.getBlockExpression());
|
||||
if (stackValue.type != Type.VOID_TYPE) {
|
||||
instructionAdapter.putfield("Script", LAST_EXPRESSION_VALUE_FIELD_NAME, blockType.getDescriptor());
|
||||
}
|
||||
|
||||
instructionAdapter.areturn(Type.VOID_TYPE);
|
||||
mv.visitMaxs(-1, -1);
|
||||
mv.visitEnd();
|
||||
|
||||
classBuilder.done();
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import org.jetbrains.jet.codegen.GenerationState;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderFactory;
|
||||
import org.jetbrains.jet.codegen.JetTypeMapper;
|
||||
import org.jetbrains.jet.codegen.ClassCodegen;
|
||||
import org.jetbrains.jet.codegen.ScriptCodegen;
|
||||
import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethods;
|
||||
import org.jetbrains.jet.codegen.ClassFileFactory;
|
||||
import org.jetbrains.jet.codegen.ClosureAnnotator;
|
||||
@@ -56,6 +57,7 @@ public class InjectorForJvmCodegen {
|
||||
private final ClassBuilderFactory classBuilderFactory;
|
||||
private JetTypeMapper jetTypeMapper;
|
||||
private ClassCodegen classCodegen;
|
||||
private ScriptCodegen scriptCodegen;
|
||||
private IntrinsicMethods intrinsics;
|
||||
private ClassFileFactory classFileFactory;
|
||||
private ClosureAnnotator closureAnnotator;
|
||||
@@ -80,6 +82,7 @@ public class InjectorForJvmCodegen {
|
||||
this.classBuilderFactory = classBuilderFactory;
|
||||
this.jetTypeMapper = new JetTypeMapper();
|
||||
this.classCodegen = new ClassCodegen();
|
||||
this.scriptCodegen = new ScriptCodegen();
|
||||
this.intrinsics = new IntrinsicMethods();
|
||||
this.classFileFactory = new ClassFileFactory();
|
||||
this.closureAnnotator = new ClosureAnnotator();
|
||||
@@ -93,6 +96,10 @@ public class InjectorForJvmCodegen {
|
||||
this.classCodegen.setJetTypeMapper(jetTypeMapper);
|
||||
this.classCodegen.setState(generationState);
|
||||
|
||||
this.scriptCodegen.setClassFileFactory(classFileFactory);
|
||||
this.scriptCodegen.setJetTypeMapper(jetTypeMapper);
|
||||
this.scriptCodegen.setState(generationState);
|
||||
|
||||
this.intrinsics.setMyProject(project);
|
||||
this.intrinsics.setMyStdLib(jetStandardLibrary);
|
||||
|
||||
@@ -130,6 +137,10 @@ public class InjectorForJvmCodegen {
|
||||
return this.classCodegen;
|
||||
}
|
||||
|
||||
public ScriptCodegen getScriptCodegen() {
|
||||
return this.scriptCodegen;
|
||||
}
|
||||
|
||||
public IntrinsicMethods getIntrinsics() {
|
||||
return this.intrinsics;
|
||||
}
|
||||
|
||||
@@ -22,5 +22,6 @@ package org.jetbrains.jet.lang.resolve.java;
|
||||
public class JdkNames {
|
||||
|
||||
public static final JvmClassName JL_OBJECT = new JvmClassName("java.lang.Object");
|
||||
public static final JvmClassName JL_STRING = new JvmClassName("java.lang.String");
|
||||
|
||||
}
|
||||
|
||||
@@ -38,6 +38,11 @@ public class ScriptDescriptor extends DeclarationDescriptorImpl {
|
||||
this.returnType = returnType;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JetType getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DeclarationDescriptor substitute(TypeSubstitutor substitutor) {
|
||||
throw new IllegalStateException("nothing to substitute in script");
|
||||
|
||||
@@ -143,7 +143,12 @@ public class JetPsiUtil {
|
||||
}
|
||||
|
||||
public static FqName getFQName(JetFile file) {
|
||||
return getFQName(file.getNamespaceHeader());
|
||||
if (file.isScript()) {
|
||||
return FqName.ROOT;
|
||||
}
|
||||
else {
|
||||
return getFQName(file.getNamespaceHeader());
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -630,7 +630,7 @@ public class BodyResolver {
|
||||
Maps.<JetPattern, List<VariableDescriptor>>newHashMap(),
|
||||
new LabelResolver(),
|
||||
trace,
|
||||
this.context.getRootScope(),
|
||||
scope,
|
||||
DataFlowInfo.EMPTY,
|
||||
NO_EXPECTED_TYPE,
|
||||
false);
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.WriteThroughScope;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lang.types.SubstitutionUtils;
|
||||
@@ -207,8 +208,9 @@ public class TypeHierarchyResolver {
|
||||
private void processScript(JetScript script) {
|
||||
NamespaceDescriptorImpl ns = namespaceFactory.createNamespaceDescriptorPathIfNeeded(FqName.ROOT);
|
||||
ScriptDescriptor scriptDescriptor = new ScriptDescriptor(ns);
|
||||
WriteThroughScope scriptScope = new WriteThroughScope(
|
||||
outerScope, ns.getMemberScope(), new TraceBasedRedeclarationHandler(trace));
|
||||
//WriteThroughScope scriptScope = new WriteThroughScope(
|
||||
// outerScope, ns.getMemberScope(), new TraceBasedRedeclarationHandler(trace));
|
||||
WritableScopeImpl scriptScope = new WritableScopeImpl(outerScope, scriptDescriptor, RedeclarationHandler.DO_NOTHING);
|
||||
scriptScope.changeLockLevel(WritableScope.LockLevel.BOTH);
|
||||
context.getScriptScopes().put(script, scriptScope);
|
||||
context.getScripts().put(script, scriptDescriptor);
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
// expected: null
|
||||
System.out!!.println("hello world")
|
||||
@@ -0,0 +1 @@
|
||||
"O" + "K"
|
||||
@@ -0,0 +1,11 @@
|
||||
// expected: 3628800
|
||||
|
||||
fun factorial(n: Int): Int {
|
||||
var product = 1
|
||||
for (i in 1..n) {
|
||||
product *= i
|
||||
}
|
||||
return product
|
||||
}
|
||||
|
||||
factorial(10)
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import org.jetbrains.jet.codegen.ClassCodegen;
|
||||
import org.jetbrains.jet.codegen.ClassFileFactory;
|
||||
import org.jetbrains.jet.codegen.GenerationState;
|
||||
import org.jetbrains.jet.codegen.JetTypeMapper;
|
||||
import org.jetbrains.jet.codegen.ScriptCodegen;
|
||||
import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethods;
|
||||
import org.jetbrains.jet.lang.ModuleConfiguration;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
||||
@@ -156,6 +157,7 @@ public class AllInjectorsGenerator {
|
||||
generator.addParameter(ClassBuilderFactory.class);
|
||||
generator.addPublicField(JetTypeMapper.class);
|
||||
generator.addPublicField(ClassCodegen.class);
|
||||
generator.addPublicField(ScriptCodegen.class);
|
||||
generator.addField(true, IntrinsicMethods.class, "intrinsics", null);
|
||||
generator.addPublicField(ClassFileFactory.class);
|
||||
generator.generate("compiler/backend/src", "org.jetbrains.jet.di", "InjectorForJvmCodegen");
|
||||
|
||||
Reference in New Issue
Block a user