add serialization.js module

This commit is contained in:
Michael Nedzelsky
2015-02-19 14:40:35 +03:00
parent 9be62de964
commit e0c0012a20
9 changed files with 3808 additions and 0 deletions
+1
View File
@@ -48,6 +48,7 @@
<module fileurl="file://$PROJECT_DIR$/core/reflection.jvm/reflection.jvm.iml" filepath="$PROJECT_DIR$/core/reflection.jvm/reflection.jvm.iml" group="core" />
<module fileurl="file://$PROJECT_DIR$/core/runtime.jvm/runtime.jvm.iml" filepath="$PROJECT_DIR$/core/runtime.jvm/runtime.jvm.iml" group="core" />
<module fileurl="file://$PROJECT_DIR$/core/serialization/serialization.iml" filepath="$PROJECT_DIR$/core/serialization/serialization.iml" group="core" />
<module fileurl="file://$PROJECT_DIR$/core/serialization.js/serialization.js.iml" filepath="$PROJECT_DIR$/core/serialization.js/serialization.js.iml" group="core" />
<module fileurl="file://$PROJECT_DIR$/core/serialization.jvm/serialization.jvm.iml" filepath="$PROJECT_DIR$/core/serialization.jvm/serialization.jvm.iml" group="core" />
<module fileurl="file://$PROJECT_DIR$/compiler/util/util.iml" filepath="$PROJECT_DIR$/compiler/util/util.iml" />
<module fileurl="file://$PROJECT_DIR$/core/util.runtime/util.runtime.iml" filepath="$PROJECT_DIR$/core/util.runtime/util.runtime.iml" group="core" />
+3
View File
@@ -99,6 +99,7 @@
<include name="core/descriptors/src"/>
<include name="core/serialization/src"/>
<include name="core/descriptor.loader.java/src"/>
<include name="core/serialization.js/src"/>
<include name="core/serialization.jvm/src"/>
<include name="core/util.runtime/src"/>
<include name="compiler/frontend/src"/>
@@ -125,6 +126,7 @@
<include name="serialization/**"/>
<include name="descriptor.loader.java/**"/>
<include name="frontend.java/**"/>
<include name="serialization.js/**"/>
<include name="serialization.jvm/**"/>
<include name="backend/**"/>
<include name="backend-common/**"/>
@@ -190,6 +192,7 @@
<fileset dir="core/descriptors/src"/>
<fileset dir="core/descriptor.loader.java/src"/>
<fileset dir="core/serialization/src"/>
<fileset dir="core/serialization.js/src"/>
<fileset dir="core/serialization.jvm/src"/>
<fileset dir="core/util.runtime/src"/>
<fileset dir="compiler/frontend/src"/>
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="serialization" />
<orderEntry type="module" module-name="util.runtime" />
</component>
</module>
+30
View File
@@ -0,0 +1,30 @@
/*
* 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;
option java_outer_classname = "JsProtoBuf";
option optimize_for = LITE_RUNTIME;
// For Kotlin/Javascript we reuse builtins serialization code.
message Library {
message FileEntry {
required string path = 1;
required bytes content = 2;
}
repeated FileEntry entry = 1;
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,78 @@
/*
* 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 com.google.protobuf.ByteString
import org.jetbrains.kotlin.builtins.BuiltinsPackageFragment
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.serialization.deserialization.FlexibleTypeCapabilitiesDeserializer
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
import kotlin.platform.platformStatic
public object KotlinJavascriptSerializationUtil {
private val PACKAGE_FILE_SUFFIX = "/.kotlin_package"
platformStatic
public fun getPackageFragmentProviders(moduleDescriptor: ModuleDescriptor, metadata: ByteArray): List<PackageFragmentProvider> {
val gzipInputStream = GZIPInputStream(ByteArrayInputStream(metadata))
val content = JsProtoBuf.Library.parseFrom(gzipInputStream)
gzipInputStream.close()
val contentMap: MutableMap<String, ByteArray> = hashMapOf()
for(index in 0..content.getEntryCount()-1) {
val entry = content.getEntry(index)
contentMap[entry.getPath()] = entry.getContent().toByteArray()
}
val packages = getPackages(contentMap)
val load = { (path: String) -> if (!contentMap.containsKey(path)) null else ByteArrayInputStream(contentMap.get(path)) }
val providers = arrayListOf<PackageFragmentProvider>()
for (packageName in packages) {
val fqName = FqName(packageName)
val packageFragment = BuiltinsPackageFragment(fqName, LockBasedStorageManager(), moduleDescriptor, FlexibleTypeCapabilitiesDeserializer.Dynamic, load)
providers.add(packageFragment.provider)
}
return providers
}
public fun contentMapToByteArray(contentMap: Map<String, ByteArray>): ByteArray {
val contentBuilder = JsProtoBuf.Library.newBuilder()
contentMap forEach {
val entry = JsProtoBuf.Library.FileEntry.newBuilder().setPath(it.getKey()).setContent(ByteString.copyFrom(it.getValue())).build()
contentBuilder.addEntry(entry)
}
val byteStream = ByteArrayOutputStream()
val gzipOutputStream = GZIPOutputStream(byteStream)
contentBuilder.build().writeTo(gzipOutputStream)
gzipOutputStream.close()
return byteStream.toByteArray()
}
private fun getPackages(contentMap: Map<String, ByteArray>): List<String> =
contentMap.keySet().filter { it.endsWith(PACKAGE_FILE_SUFFIX) } map { it.substring(0, it.length() - PACKAGE_FILE_SUFFIX.length()).replace('/', '.') }
}
@@ -51,6 +51,7 @@ public data class ProtoPath(
public val PROTO_PATHS: List<ProtoPath> = listOf(
ProtoPath("core/serialization/src/descriptors.proto", "core/serialization/src"),
ProtoPath("core/serialization/src/builtins.proto", "core/serialization/src"),
ProtoPath("core/serialization.js/src/js.proto", "core/serialization.js/src"),
ProtoPath("core/serialization.jvm/src/jvm_descriptors.proto", "core/serialization.jvm/src")
)