JS: serialize .kjsm package-wise, adapt decompiler & stub builder

Instead of multiple .kjsm files for different classes and .kotlin_string_table,
.kotlin_file_table, .kotlin_classes files for each package, serialize the
contents of each package to a single foo/bar/baz/baz.kjsm file. The short name
of the file is the last segment in the FQ name of the package, or
"root-package" if the package is root.

There are two main reasons for this change:
1) Such structure takes less space, is more IO-friendly and will not cause
   multiple exceptions as the old one, where we sometimes tried to read
   non-existing files
2) This is exactly the same format that is used to serialize built-in
   declarations (.kotlin_builtins) at the moment, which will allow us to reuse
   some code

Also write a separate Header protobuf message to the beginning of the .kjsm
file. This will be used as arguments of the kotlin.Metadata annotation are used
in the JVM-specific parts of the compiler: to be able to provide some general
information about the binary file without parsing the whole protobuf data.

This commit breaks decompiled text & stub builder consistency tests. This is OK
because they're removed in a future commit.

Fixes EA-79605, EA-81947, EA-84277 and maybe EA-86787

 #KT-10894 Fixed
 #KT-14124 Fixed
 #KT-15755 Fixed
This commit is contained in:
Alexander Udalov
2017-01-25 15:17:57 +03:00
parent 46bf057b47
commit 63c24c56ec
23 changed files with 1660 additions and 419 deletions
+12
View File
@@ -21,6 +21,18 @@ import "core/deserialization/src/descriptors.proto";
option java_outer_classname = "JsProtoBuf";
option optimize_for = LITE_RUNTIME;
message Header {
/*
preRelease
*/
optional int32 flags = 1;
// (patch << 16) + (minor << 8) + major
optional int32 js_code_binary_version = 2;
optional string package_fq_name = 3;
}
message File {
required int32 id = 1;
repeated Annotation annotation = 2;
@@ -21,6 +21,657 @@ public final class JsProtoBuf {
registry.add(org.jetbrains.kotlin.serialization.js.JsProtoBuf.typeAnnotation);
registry.add(org.jetbrains.kotlin.serialization.js.JsProtoBuf.typeParameterAnnotation);
}
public interface HeaderOrBuilder extends
// @@protoc_insertion_point(interface_extends:org.jetbrains.kotlin.serialization.js.Header)
org.jetbrains.kotlin.protobuf.MessageLiteOrBuilder {
/**
* <code>optional int32 flags = 1;</code>
*
* <pre>
*preRelease
* </pre>
*/
boolean hasFlags();
/**
* <code>optional int32 flags = 1;</code>
*
* <pre>
*preRelease
* </pre>
*/
int getFlags();
/**
* <code>optional int32 js_code_binary_version = 2;</code>
*
* <pre>
* (patch &lt;&lt; 16) + (minor &lt;&lt; 8) + major
* </pre>
*/
boolean hasJsCodeBinaryVersion();
/**
* <code>optional int32 js_code_binary_version = 2;</code>
*
* <pre>
* (patch &lt;&lt; 16) + (minor &lt;&lt; 8) + major
* </pre>
*/
int getJsCodeBinaryVersion();
/**
* <code>optional string package_fq_name = 3;</code>
*/
boolean hasPackageFqName();
/**
* <code>optional string package_fq_name = 3;</code>
*/
java.lang.String getPackageFqName();
/**
* <code>optional string package_fq_name = 3;</code>
*/
org.jetbrains.kotlin.protobuf.ByteString
getPackageFqNameBytes();
}
/**
* Protobuf type {@code org.jetbrains.kotlin.serialization.js.Header}
*/
public static final class Header extends
org.jetbrains.kotlin.protobuf.GeneratedMessageLite implements
// @@protoc_insertion_point(message_implements:org.jetbrains.kotlin.serialization.js.Header)
HeaderOrBuilder {
// Use Header.newBuilder() to construct.
private Header(org.jetbrains.kotlin.protobuf.GeneratedMessageLite.Builder builder) {
super(builder);
this.unknownFields = builder.getUnknownFields();
}
private Header(boolean noInit) { this.unknownFields = org.jetbrains.kotlin.protobuf.ByteString.EMPTY;}
private static final Header defaultInstance;
public static Header getDefaultInstance() {
return defaultInstance;
}
public Header getDefaultInstanceForType() {
return defaultInstance;
}
private final org.jetbrains.kotlin.protobuf.ByteString unknownFields;
private Header(
org.jetbrains.kotlin.protobuf.CodedInputStream input,
org.jetbrains.kotlin.protobuf.ExtensionRegistryLite extensionRegistry)
throws org.jetbrains.kotlin.protobuf.InvalidProtocolBufferException {
initFields();
int mutable_bitField0_ = 0;
org.jetbrains.kotlin.protobuf.ByteString.Output unknownFieldsOutput =
org.jetbrains.kotlin.protobuf.ByteString.newOutput();
org.jetbrains.kotlin.protobuf.CodedOutputStream unknownFieldsCodedOutput =
org.jetbrains.kotlin.protobuf.CodedOutputStream.newInstance(
unknownFieldsOutput);
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
default: {
if (!parseUnknownField(input, unknownFieldsCodedOutput,
extensionRegistry, tag)) {
done = true;
}
break;
}
case 8: {
bitField0_ |= 0x00000001;
flags_ = input.readInt32();
break;
}
case 16: {
bitField0_ |= 0x00000002;
jsCodeBinaryVersion_ = input.readInt32();
break;
}
case 26: {
org.jetbrains.kotlin.protobuf.ByteString bs = input.readBytes();
bitField0_ |= 0x00000004;
packageFqName_ = bs;
break;
}
}
}
} catch (org.jetbrains.kotlin.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
throw new org.jetbrains.kotlin.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this);
} finally {
try {
unknownFieldsCodedOutput.flush();
} catch (java.io.IOException e) {
// Should not happen
} finally {
unknownFields = unknownFieldsOutput.toByteString();
}
makeExtensionsImmutable();
}
}
public static org.jetbrains.kotlin.protobuf.Parser<Header> PARSER =
new org.jetbrains.kotlin.protobuf.AbstractParser<Header>() {
public Header parsePartialFrom(
org.jetbrains.kotlin.protobuf.CodedInputStream input,
org.jetbrains.kotlin.protobuf.ExtensionRegistryLite extensionRegistry)
throws org.jetbrains.kotlin.protobuf.InvalidProtocolBufferException {
return new Header(input, extensionRegistry);
}
};
@java.lang.Override
public org.jetbrains.kotlin.protobuf.Parser<Header> getParserForType() {
return PARSER;
}
private int bitField0_;
public static final int FLAGS_FIELD_NUMBER = 1;
private int flags_;
/**
* <code>optional int32 flags = 1;</code>
*
* <pre>
*preRelease
* </pre>
*/
public boolean hasFlags() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>optional int32 flags = 1;</code>
*
* <pre>
*preRelease
* </pre>
*/
public int getFlags() {
return flags_;
}
public static final int JS_CODE_BINARY_VERSION_FIELD_NUMBER = 2;
private int jsCodeBinaryVersion_;
/**
* <code>optional int32 js_code_binary_version = 2;</code>
*
* <pre>
* (patch &lt;&lt; 16) + (minor &lt;&lt; 8) + major
* </pre>
*/
public boolean hasJsCodeBinaryVersion() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>optional int32 js_code_binary_version = 2;</code>
*
* <pre>
* (patch &lt;&lt; 16) + (minor &lt;&lt; 8) + major
* </pre>
*/
public int getJsCodeBinaryVersion() {
return jsCodeBinaryVersion_;
}
public static final int PACKAGE_FQ_NAME_FIELD_NUMBER = 3;
private java.lang.Object packageFqName_;
/**
* <code>optional string package_fq_name = 3;</code>
*/
public boolean hasPackageFqName() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
/**
* <code>optional string package_fq_name = 3;</code>
*/
public java.lang.String getPackageFqName() {
java.lang.Object ref = packageFqName_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
org.jetbrains.kotlin.protobuf.ByteString bs =
(org.jetbrains.kotlin.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
packageFqName_ = s;
}
return s;
}
}
/**
* <code>optional string package_fq_name = 3;</code>
*/
public org.jetbrains.kotlin.protobuf.ByteString
getPackageFqNameBytes() {
java.lang.Object ref = packageFqName_;
if (ref instanceof java.lang.String) {
org.jetbrains.kotlin.protobuf.ByteString b =
org.jetbrains.kotlin.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
packageFqName_ = b;
return b;
} else {
return (org.jetbrains.kotlin.protobuf.ByteString) ref;
}
}
private void initFields() {
flags_ = 0;
jsCodeBinaryVersion_ = 0;
packageFqName_ = "";
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
memoizedIsInitialized = 1;
return true;
}
public void writeTo(org.jetbrains.kotlin.protobuf.CodedOutputStream output)
throws java.io.IOException {
getSerializedSize();
if (((bitField0_ & 0x00000001) == 0x00000001)) {
output.writeInt32(1, flags_);
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
output.writeInt32(2, jsCodeBinaryVersion_);
}
if (((bitField0_ & 0x00000004) == 0x00000004)) {
output.writeBytes(3, getPackageFqNameBytes());
}
output.writeRawBytes(unknownFields);
}
private int memoizedSerializedSize = -1;
public int getSerializedSize() {
int size = memoizedSerializedSize;
if (size != -1) return size;
size = 0;
if (((bitField0_ & 0x00000001) == 0x00000001)) {
size += org.jetbrains.kotlin.protobuf.CodedOutputStream
.computeInt32Size(1, flags_);
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
size += org.jetbrains.kotlin.protobuf.CodedOutputStream
.computeInt32Size(2, jsCodeBinaryVersion_);
}
if (((bitField0_ & 0x00000004) == 0x00000004)) {
size += org.jetbrains.kotlin.protobuf.CodedOutputStream
.computeBytesSize(3, getPackageFqNameBytes());
}
size += unknownFields.size();
memoizedSerializedSize = size;
return size;
}
private static final long serialVersionUID = 0L;
@java.lang.Override
protected java.lang.Object writeReplace()
throws java.io.ObjectStreamException {
return super.writeReplace();
}
public static org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header parseFrom(
org.jetbrains.kotlin.protobuf.ByteString data)
throws org.jetbrains.kotlin.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header parseFrom(
org.jetbrains.kotlin.protobuf.ByteString data,
org.jetbrains.kotlin.protobuf.ExtensionRegistryLite extensionRegistry)
throws org.jetbrains.kotlin.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header parseFrom(byte[] data)
throws org.jetbrains.kotlin.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header parseFrom(
byte[] data,
org.jetbrains.kotlin.protobuf.ExtensionRegistryLite extensionRegistry)
throws org.jetbrains.kotlin.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header parseFrom(
java.io.InputStream input,
org.jetbrains.kotlin.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header parseDelimitedFrom(
java.io.InputStream input,
org.jetbrains.kotlin.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header parseFrom(
org.jetbrains.kotlin.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header parseFrom(
org.jetbrains.kotlin.protobuf.CodedInputStream input,
org.jetbrains.kotlin.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header prototype) {
return newBuilder().mergeFrom(prototype);
}
public Builder toBuilder() { return newBuilder(this); }
/**
* Protobuf type {@code org.jetbrains.kotlin.serialization.js.Header}
*/
public static final class Builder extends
org.jetbrains.kotlin.protobuf.GeneratedMessageLite.Builder<
org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header, Builder>
implements
// @@protoc_insertion_point(builder_implements:org.jetbrains.kotlin.serialization.js.Header)
org.jetbrains.kotlin.serialization.js.JsProtoBuf.HeaderOrBuilder {
// Construct using org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
}
private static Builder create() {
return new Builder();
}
public Builder clear() {
super.clear();
flags_ = 0;
bitField0_ = (bitField0_ & ~0x00000001);
jsCodeBinaryVersion_ = 0;
bitField0_ = (bitField0_ & ~0x00000002);
packageFqName_ = "";
bitField0_ = (bitField0_ & ~0x00000004);
return this;
}
public Builder clone() {
return create().mergeFrom(buildPartial());
}
public org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header getDefaultInstanceForType() {
return org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header.getDefaultInstance();
}
public org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header build() {
org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
public org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header buildPartial() {
org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header result = new org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
to_bitField0_ |= 0x00000001;
}
result.flags_ = flags_;
if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
to_bitField0_ |= 0x00000002;
}
result.jsCodeBinaryVersion_ = jsCodeBinaryVersion_;
if (((from_bitField0_ & 0x00000004) == 0x00000004)) {
to_bitField0_ |= 0x00000004;
}
result.packageFqName_ = packageFqName_;
result.bitField0_ = to_bitField0_;
return result;
}
public Builder mergeFrom(org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header other) {
if (other == org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header.getDefaultInstance()) return this;
if (other.hasFlags()) {
setFlags(other.getFlags());
}
if (other.hasJsCodeBinaryVersion()) {
setJsCodeBinaryVersion(other.getJsCodeBinaryVersion());
}
if (other.hasPackageFqName()) {
bitField0_ |= 0x00000004;
packageFqName_ = other.packageFqName_;
}
setUnknownFields(
getUnknownFields().concat(other.unknownFields));
return this;
}
public final boolean isInitialized() {
return true;
}
public Builder mergeFrom(
org.jetbrains.kotlin.protobuf.CodedInputStream input,
org.jetbrains.kotlin.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (org.jetbrains.kotlin.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (org.jetbrains.kotlin.serialization.js.JsProtoBuf.Header) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_;
private int flags_ ;
/**
* <code>optional int32 flags = 1;</code>
*
* <pre>
*preRelease
* </pre>
*/
public boolean hasFlags() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>optional int32 flags = 1;</code>
*
* <pre>
*preRelease
* </pre>
*/
public int getFlags() {
return flags_;
}
/**
* <code>optional int32 flags = 1;</code>
*
* <pre>
*preRelease
* </pre>
*/
public Builder setFlags(int value) {
bitField0_ |= 0x00000001;
flags_ = value;
return this;
}
/**
* <code>optional int32 flags = 1;</code>
*
* <pre>
*preRelease
* </pre>
*/
public Builder clearFlags() {
bitField0_ = (bitField0_ & ~0x00000001);
flags_ = 0;
return this;
}
private int jsCodeBinaryVersion_ ;
/**
* <code>optional int32 js_code_binary_version = 2;</code>
*
* <pre>
* (patch &lt;&lt; 16) + (minor &lt;&lt; 8) + major
* </pre>
*/
public boolean hasJsCodeBinaryVersion() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>optional int32 js_code_binary_version = 2;</code>
*
* <pre>
* (patch &lt;&lt; 16) + (minor &lt;&lt; 8) + major
* </pre>
*/
public int getJsCodeBinaryVersion() {
return jsCodeBinaryVersion_;
}
/**
* <code>optional int32 js_code_binary_version = 2;</code>
*
* <pre>
* (patch &lt;&lt; 16) + (minor &lt;&lt; 8) + major
* </pre>
*/
public Builder setJsCodeBinaryVersion(int value) {
bitField0_ |= 0x00000002;
jsCodeBinaryVersion_ = value;
return this;
}
/**
* <code>optional int32 js_code_binary_version = 2;</code>
*
* <pre>
* (patch &lt;&lt; 16) + (minor &lt;&lt; 8) + major
* </pre>
*/
public Builder clearJsCodeBinaryVersion() {
bitField0_ = (bitField0_ & ~0x00000002);
jsCodeBinaryVersion_ = 0;
return this;
}
private java.lang.Object packageFqName_ = "";
/**
* <code>optional string package_fq_name = 3;</code>
*/
public boolean hasPackageFqName() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
/**
* <code>optional string package_fq_name = 3;</code>
*/
public java.lang.String getPackageFqName() {
java.lang.Object ref = packageFqName_;
if (!(ref instanceof java.lang.String)) {
org.jetbrains.kotlin.protobuf.ByteString bs =
(org.jetbrains.kotlin.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
packageFqName_ = s;
}
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>optional string package_fq_name = 3;</code>
*/
public org.jetbrains.kotlin.protobuf.ByteString
getPackageFqNameBytes() {
java.lang.Object ref = packageFqName_;
if (ref instanceof String) {
org.jetbrains.kotlin.protobuf.ByteString b =
org.jetbrains.kotlin.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
packageFqName_ = b;
return b;
} else {
return (org.jetbrains.kotlin.protobuf.ByteString) ref;
}
}
/**
* <code>optional string package_fq_name = 3;</code>
*/
public Builder setPackageFqName(
java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
packageFqName_ = value;
return this;
}
/**
* <code>optional string package_fq_name = 3;</code>
*/
public Builder clearPackageFqName() {
bitField0_ = (bitField0_ & ~0x00000004);
packageFqName_ = getDefaultInstance().getPackageFqName();
return this;
}
/**
* <code>optional string package_fq_name = 3;</code>
*/
public Builder setPackageFqNameBytes(
org.jetbrains.kotlin.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
packageFqName_ = value;
return this;
}
// @@protoc_insertion_point(builder_scope:org.jetbrains.kotlin.serialization.js.Header)
}
static {
defaultInstance = new Header(true);
defaultInstance.initFields();
}
// @@protoc_insertion_point(class_scope:org.jetbrains.kotlin.serialization.js.Header)
}
public interface FileOrBuilder extends
// @@protoc_insertion_point(interface_extends:org.jetbrains.kotlin.serialization.js.File)
org.jetbrains.kotlin.protobuf.MessageLiteOrBuilder {
@@ -31,9 +31,11 @@ import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.StringTableImpl
import org.jetbrains.kotlin.serialization.deserialization.DeserializationConfiguration
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.utils.JsBinaryVersion
import org.jetbrains.kotlin.utils.KotlinJavascriptMetadataUtils
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.DataOutputStream
import java.util.*
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
@@ -70,31 +72,7 @@ object KotlinJavascriptSerializationUtil {
importedModules.forEach { builder.addImportedModule(it) }
for (fqName in getPackagesFqNames(module)) {
val part = JsProtoBuf.Library.Part.newBuilder()
serializePackage(bindingContext, module, fqName, object : SerializerCallbacks {
override fun writeClass(classId: ClassId, classProto: ProtoBuf.Class) {
part.addClass_(classProto)
}
override fun writePackage(fqName: FqName, packageProto: ProtoBuf.Package) {
part.`package` = packageProto
}
override fun writeFiles(fqName: FqName, filesProto: JsProtoBuf.Files) {
part.files = filesProto
}
override fun writeStringTable(fqName: FqName, stringTable: StringTableImpl) {
val (strings, qualifiedNames) = stringTable.buildProto()
part.strings = strings
part.qualifiedNames = qualifiedNames
}
override fun writeClassNames(fqName: FqName, classNames: List<Name>, stringTable: StringTableImpl) {
// Do nothing
}
})
val part = serializePackageFragment(bindingContext, fqName, module)
if (part.hasPackage() || part.class_Count > 0) {
builder.addPart(part)
}
@@ -103,6 +81,38 @@ object KotlinJavascriptSerializationUtil {
return builder.build()
}
private fun serializePackageFragment(
bindingContext: BindingContext,
fqName: FqName,
module: ModuleDescriptor
): JsProtoBuf.Library.Part {
val part = JsProtoBuf.Library.Part.newBuilder()
serializePackage(bindingContext, module, fqName, object : SerializerCallbacks {
override fun writeClass(classId: ClassId, classProto: ProtoBuf.Class) {
part.addClass_(classProto)
}
override fun writePackage(fqName: FqName, packageProto: ProtoBuf.Package) {
part.`package` = packageProto
}
override fun writeFiles(fqName: FqName, filesProto: JsProtoBuf.Files) {
part.files = filesProto
}
override fun writeStringTable(fqName: FqName, stringTable: StringTableImpl) {
val (strings, qualifiedNames) = stringTable.buildProto()
part.strings = strings
part.qualifiedNames = qualifiedNames
}
override fun writeClassNames(fqName: FqName, classNames: List<Name>, stringTable: StringTableImpl) {
// Do nothing
}
})
return part.build()
}
fun metadataAsString(bindingContext: BindingContext, jsDescriptor: JsModuleDescriptor<ModuleDescriptor>): String =
KotlinJavascriptMetadataUtils.formatMetadataAsString(jsDescriptor.name, jsDescriptor.serializeToBinaryMetadata(bindingContext))
@@ -179,46 +189,30 @@ object KotlinJavascriptSerializationUtil {
fun toContentMap(bindingContext: BindingContext, module: ModuleDescriptor): Map<String, ByteArray> {
val contentMap = hashMapOf<String, ByteArray>()
fun writeFile(fileName: String, bytes: ByteArray) {
contentMap[fileName] = bytes
}
for (fqName in getPackagesFqNames(module)) {
serializePackage(bindingContext, module, fqName, object : SerializerCallbacks {
override fun writeClass(classId: ClassId, classProto: ProtoBuf.Class) {
val bytes = ByteArrayOutputStream().apply(classProto::writeTo).toByteArray()
writeFile(KotlinJavascriptSerializedResourcePaths.getClassMetadataPath(classId), bytes)
}
val part = serializePackageFragment(bindingContext, fqName, module)
if (part.class_Count == 0 && part.`package`.let { packageProto ->
packageProto.functionCount == 0 && packageProto.propertyCount == 0 && packageProto.typeAliasCount == 0
}) continue
override fun writePackage(fqName: FqName, packageProto: ProtoBuf.Package) {
if (packageProto.functionCount > 0 || packageProto.propertyCount > 0 || packageProto.typeAliasCount > 0) {
val bytes = ByteArrayOutputStream().apply(packageProto::writeTo).toByteArray()
writeFile(KotlinJavascriptSerializedResourcePaths.getPackageFilePath(fqName), bytes)
}
}
val packageFqName = fqName.asString()
override fun writeFiles(fqName: FqName, filesProto: JsProtoBuf.Files) {
val bytes = ByteArrayOutputStream().apply(filesProto::writeTo).toByteArray()
writeFile(KotlinJavascriptSerializedResourcePaths.getFileListFilePath(fqName), bytes)
}
val header = JsProtoBuf.Header.newBuilder()
// TODO: write pre-release flag if needed
// TODO: write JS code binary version
header.packageFqName = packageFqName
override fun writeStringTable(fqName: FqName, stringTable: StringTableImpl) {
val bytes = ByteArrayOutputStream().apply(stringTable::serializeTo).toByteArray()
if (bytes.isNotEmpty()) {
writeFile(KotlinJavascriptSerializedResourcePaths.getStringTableFilePath(fqName), bytes)
}
}
val stream = ByteArrayOutputStream()
with(DataOutputStream(stream)) {
val version = JsBinaryVersion.INSTANCE.toArray()
writeInt(version.size)
version.forEach(this::writeInt)
}
override fun writeClassNames(fqName: FqName, classNames: List<Name>, stringTable: StringTableImpl) {
val builder = JsProtoBuf.Classes.newBuilder()
builder.addAllClassName(classNames.map(stringTable::getSimpleNameIndex))
val classesProto = builder.build()
if (classesProto.classNameCount > 0) {
val bytes = ByteArrayOutputStream().apply(classesProto::writeTo).toByteArray()
writeFile(KotlinJavascriptSerializedResourcePaths.getClassesInPackageFilePath(fqName), bytes)
}
}
})
header.build().writeDelimitedTo(stream)
part.writeTo(stream)
contentMap[JsSerializerProtocol.getKjsmFilePath(fqName)] = stream.toByteArray()
}
return contentMap
@@ -1,72 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.serialization.js
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
object KotlinJavascriptSerializedResourcePaths {
private val CLASSES_FILE_EXTENSION = "kotlin_classes"
private val STRING_TABLE_FILE_EXTENSION = "kotlin_string_table"
private val FILE_LIST_FILE_EXTENSION = "kotlin_file_table"
fun getClassesInPackageFilePath(fqName: FqName): String =
fqName.toPath().withSepIfNotEmpty() + shortName(fqName) + "." + CLASSES_FILE_EXTENSION
fun getClassMetadataPath(classId: ClassId): String {
return classId.packageFqName.toPath().withSepIfNotEmpty() + classId.relativeClassName.asString() +
"." + KotlinJavascriptSerializationUtil.CLASS_METADATA_FILE_EXTENSION
}
fun getPackageFilePath(fqName: FqName): String =
getPackageClassFqName(fqName).toPath() + "." + KotlinJavascriptSerializationUtil.CLASS_METADATA_FILE_EXTENSION
fun getStringTableFilePath(fqName: FqName): String =
fqName.toPath().withSepIfNotEmpty() + shortName(fqName) + "." + STRING_TABLE_FILE_EXTENSION
fun getFileListFilePath(fqName: FqName) = fqName.toPath().withSepIfNotEmpty() + shortName(fqName) + "." + FILE_LIST_FILE_EXTENSION
private fun FqName.toPath() = this.asString().replace('.', '/')
private fun String.withSepIfNotEmpty() = if (this.isEmpty()) this else this + "/"
private fun shortName(fqName: FqName): String =
if (fqName.isRoot) "default-package" else fqName.shortName().asString()
}
private val PACKAGE_CLASS_NAME_SUFFIX: String = "Package"
private val DEFAULT_PACKAGE_CLASS_NAME: String = "_Default" + PACKAGE_CLASS_NAME_SUFFIX
private val DEFAULT_PACKAGE_METAFILE_NAME: String = DEFAULT_PACKAGE_CLASS_NAME + "." + KotlinJavascriptSerializationUtil.CLASS_METADATA_FILE_EXTENSION
fun FqName.isPackageClassFqName(): Boolean = !this.isRoot && getPackageClassFqName(this.parent()) == this
fun isDefaultPackageMetafile(fileName: String): Boolean = fileName == DEFAULT_PACKAGE_METAFILE_NAME
private fun getPackageClassFqName(packageFQN: FqName): FqName {
return packageFQN.child(Name.identifier(getPackageClassName(packageFQN)))
}
private fun getPackageClassName(packageFQN: FqName): String {
return if (packageFQN.isRoot) DEFAULT_PACKAGE_CLASS_NAME else capitalizeNonEmptyString(packageFQN.shortName().asString()) + PACKAGE_CLASS_NAME_SUFFIX
}
private fun capitalizeNonEmptyString(s: String): String {
return if (s[0].isUpperCase()) s else s[0].toUpperCase() + s.substring(1)
}
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.serialization.js
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.protobuf.ExtensionRegistryLite
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.DescriptorUtils
@@ -82,4 +83,12 @@ object JsSerializerProtocol : SerializerExtensionProtocol(
JsProtoBuf.constructorAnnotation, JsProtoBuf.classAnnotation, JsProtoBuf.functionAnnotation, JsProtoBuf.propertyAnnotation,
JsProtoBuf.enumEntryAnnotation, JsProtoBuf.compileTimeValue, JsProtoBuf.parameterAnnotation, JsProtoBuf.typeAnnotation,
JsProtoBuf.typeParameterAnnotation
)
) {
fun getKjsmFilePath(packageFqName: FqName): String {
val shortName = if (packageFqName.isRoot) Name.identifier("root-package") else packageFqName.shortName()
return packageFqName.child(shortName).asString().replace('.', '/') +
"." +
KotlinJavascriptSerializationUtil.CLASS_METADATA_FILE_EXTENSION
}
}