Skip to content

Commit

Permalink
Apply isInheritanceFromInterface patch to kotlin.js
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Feb 23, 2019
1 parent 6ddfe8d commit 89b7096
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
Expand Up @@ -5,14 +5,22 @@ import com.soywiz.korge.gradle.KorgeGradlePlugin
import com.soywiz.korge.gradle.util.encodePNG
import com.soywiz.korge.gradle.util.getScaledInstance
import com.soywiz.korge.gradle.util.toBufferedImage
import com.soywiz.korio.lang.*
import javax.imageio.ImageIO

val ICON_SIZES = listOf(20, 29, 40, 44, 48, 50, 55, 57, 58, 60, 72, 76, 80, 87, 88, 100, 114, 120, 144, 152, 167, 172, 180, 196, 1024)

fun tryGetResourceBytes(path: String): ByteArray? {
return KorgeGradlePlugin::class.java.getResource("/" + path.trim('/'))?.readBytes()
}

fun getResourceBytes(path: String): ByteArray = tryGetResourceBytes(path) ?: error("Can't find resource '$path'")
fun getResourceString(path: String): String = getResourceBytes(path).toString(UTF8)

fun KorgeExtension.getIconBytes(): ByteArray {
return when {
icon?.exists() == true -> icon!!.readBytes()
else -> KorgeGradlePlugin::class.java.getResource("/icons/korge.png").readBytes()
else -> getResourceBytes("/icons/korge.png")
}
}

Expand Down
@@ -1,7 +1,7 @@
package com.soywiz.korge.gradle.targets.js

import com.moowork.gradle.node.*
import com.moowork.gradle.node.exec.NodeExecRunner
import com.moowork.gradle.node.exec.*
import com.moowork.gradle.node.npm.*
import com.moowork.gradle.node.task.*
import com.soywiz.korge.gradle.*
Expand All @@ -11,13 +11,11 @@ import groovy.text.*
import org.gradle.api.*
import org.gradle.api.artifacts.repositories.*
import org.gradle.api.file.*
import org.gradle.process.ExecResult
import org.gradle.process.*
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.tasks.*
import java.awt.Desktop
import java.io.File
import java.net.URI
import java.io.*

val Project.node_modules get() = korgeCacheDir["node_modules"]
val Project.webMinFolder get() = buildDir["web-min"]
Expand Down Expand Up @@ -212,6 +210,10 @@ private fun Project.addWeb() {
}
//task.exclude(*excludesNormal)
task.into(task.targetDir)
task.doLast {
val (src, dst) = getResourceString("/patches/isInheritanceFromInterface.kotlin.js.patch").split("--------------------------------")
task.targetDir["kotlin.js"].writeText(task.targetDir["kotlin.js"].readText().replace(src, dst))
}
}

task.doLast {
Expand Down
@@ -0,0 +1,47 @@
function isInheritanceFromInterface(ctor, iface) {
if (ctor === iface)
return true;
var metadata = ctor.$metadata$;
if (metadata != null) {
var interfaces = metadata.interfaces;
for (var i = 0; i < interfaces.length; i++) {
if (isInheritanceFromInterface(interfaces[i], iface)) {
return true;
}
}
}
var superPrototype = ctor.prototype != null ? Object.getPrototypeOf(ctor.prototype) : null;
var superConstructor = superPrototype != null ? superPrototype.constructor : null;
return superConstructor != null && isInheritanceFromInterface(superConstructor, iface);
}
--------------------------------
function getAllInterfaces(ctor) {
if (ctor.$metadata$ == null) ctor.$metadata$ = {};
var metadata = ctor.$metadata$;
if (metadata._allInterfaces === undefined) {
var allInterfaces = metadata._allInterfaces = [];

allInterfaces.push(ctor);

var interfaces = metadata.interfaces;
if (interfaces !== undefined) {
for (var i = 0; i < interfaces.length; i++) {
allInterfaces.push.apply(allInterfaces, getAllInterfaces(interfaces[i]));
}
}

var superPrototype = ctor.prototype != null ? Object.getPrototypeOf(ctor.prototype) : null;
var superConstructor = superPrototype != null ? superPrototype.constructor : null;
if (superConstructor != null) {
allInterfaces.push.apply(allInterfaces, getAllInterfaces(superConstructor));
}
// @TODO: faster duplicate removal
metadata._allInterfaces = metadata._allInterfaces.filter(function(item, pos, self) { return self.indexOf(item) === pos; });
}
return metadata._allInterfaces;
}

function isInheritanceFromInterface(ctor, iface) {
if (ctor === iface) return true;
return getAllInterfaces(ctor).indexOf(iface) >= 0;
}

0 comments on commit 89b7096

Please sign in to comment.