Posted on Leave a comment

Building BugVM

One needs a Mac running macOS 10.14. Download and install Java SE JDK 8 from Oracle. Make sure $JAVA_HOME is set properly by editing ~/.bash_profile

export JAVA_HOME=$(/usr/libexec/java_home)

Install Xcode from the Mac App Store.
Use xcode-select command line tool to select correct Xcode if there are multiple versions of Xcode.

xcode-select -s /Applications/Xcode.app
or
xcode-select -s /Applications/OtherVersion/Xcode.app

Installing Xcode Command Line Tools

xcode-select --install

You may need to install cmake, pkg-config, openssl if this is first time.

brew install pkg-config
brew install cmake
brew install openssl

Install any other missing components your build systems may complain about.
Clone GitHub repo.

git clone https://github.com/ibinti/bugvm bvm
cd bvm

BugVM uses Gradle build system. Little or no knowledge of Gradle is required to build BugVM. There is nothing you need to install or configure. Just simple one command line is all you need.

./gradlew all

Note: Gradle will self-configure and download necessary things from the Internet. It takes a few hours to complete all the build processes.

Building BugVM on Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-38-generic x86_64)

One may need:

apt -y install make
apt -y install cmake
apt -y install g++
apt -y install g++-multilib
apt -y install clang
apt -y install libcap-dev
apt -y install libxml2-dev
apt -y install zlib1g-dev
apt -y install openjdk-8-jdk

Clone GitHub and Build

git clone https://github.com/ibinti/bugvm bvm
cd bvm
./gradlew all
./gradlew install

Sample HelloWorld Console App

git clone https://github.com/ibinti/bugvm-console bcon
cd bcon/helloworld
./gradlew

Sample iOS Apps

git clone https://github.com/ibinti/bugvm-ios bios
Button App
cd bios/button
./gradlew sim
WebView App
cd bios/webview
./gradlew sim

Using Gradle Plugin

build.gradle

buildscript {
    ext {
        bugvm_version = "1.2.9"
        kotlin_version = "1.2.40"
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "com.bugvm:bugvm-gradle:$bugvm_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
apply plugin: "bugvm"
apply plugin: "kotlin"
kotlin {
    experimental {
        coroutines "enable"
    }
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
repositories {
    mavenCentral()
}
dependencies {
    compile "com.bugvm:bugvm-rt:$bugvm_version"
    compile "com.bugvm:bugvm-apple:$bugvm_version"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.3"
}
task sim {
    dependsOn launchIPhoneSimulator, build, clean
    launchIPhoneSimulator.mustRunAfter build
    build.mustRunAfter clean
}
task pad {
    dependsOn launchIPadSimulator, build, clean
    launchIPadSimulator.mustRunAfter build
    build.mustRunAfter clean
}
task dev {
    dependsOn launchIOSDevice, build, clean
    launchIOSDevice.mustRunAfter build
    build.mustRunAfter clean
}
task dist {
    dependsOn createIPA
    doLast {
        println "The IPA should be in build/bugvm directory."
    }
}
/*
In order to install to device or to package for the app store, one needs to follow Apple's
signing requirements. Easy way to setup signing and all is to create a sample iOS project
with Xcode. Xcode will advise you if something is missing or not correct.
Once Xcode can build and upload to the iTunes Connect, all setup is good.
*/
gradle.taskGraph.whenReady { graph ->
    if (graph.hasTask(dev)) {
        bugvm.iosSignIdentity = "iPhone Developer"
    } else
    if (graph.hasTask(dist)) {
        bugvm.iosSignIdentity = "iPhone Distribution"
    }
}

Gradle Tasks

Launch to iPhone Simulator

./gradlew sim

Launch to iPad Simulator

./gradlew pad

Install to Device

./gradlew dev

Create App for App Store Distribution

./gradlew dist
Posted on Leave a comment

BugVM

You can build iOS App in Kotlin on macOS that runs on the simulator, on the device, and for the App Store distribution. Console applications in Kotlin on macOS 10.14 and 64-bit Ubuntu 18.04.1 LTS are supported.

Source Code at GitHub
Source Code at GitHub
Posted on Leave a comment

kotlin_4_js_page2

Gradle

build.gradle

buildscript {
    ext {
        kotlin_version = "1.3.11"
    }
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
    }
}

apply plugin: "kotlin2js"

repositories {
    mavenCentral()
}

dependencies {
    compile("org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version")
}

defaultTasks "ko4js"

build.doLast {
    configurations.compile.each { File file ->
        copy {
            includeEmptyDirs = false

            from zipTree(file.absolutePath)
            into "${projectDir}/js"
            include { fileTreeElement ->
                def path = fileTreeElement.path
                path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
            }
        }
    }
}

task uglifyjs(type: Exec) {
    commandLine "uglifyjs", "js/kotlin.js", "js/kotlinx-coroutines-core-js.js", "js/kotlinx-html-js.js", "js/ko4js.js", "-b", "-o", "js/ko4js.core.js"
}

task ko4js {
    dependsOn build, uglifyjs
    uglifyjs.mustRunAfter build
}

compileKotlin2Js {
    kotlinOptions.outputFile = "${projectDir}/js/ko4js.js"
    kotlinOptions.moduleKind = "plain"
    kotlinOptions.sourceMap = false
    kotlinOptions.sourceMapEmbedSources = "never"
    kotlinOptions.metaInfo = false
}

task wrapper(type: Wrapper,overwrite: true) {
    gradleVersion = "5.0"
    distributionType = Wrapper.DistributionType.ALL
}
Posted on Leave a comment

kotlin_4_js_page1

Unified coding model. Kotlin is an interesting programming language that can write codes for both web front-end JavaScript and back-end server side JavaScript as well as server Java Virtual Machine (JVM). Kotlin also does native Android and iOS apps. Kotlin can work things out with existing JavaScript and JVM ecosystems and native C/Objective-C/C++/Swift static, dynamic libraries and frameworks.

In this article, I would like to teach myself on how to target JavaScript with Kotlin.

Official Kotlin documents on the Web: here

Official document recommends to use Gradle build tool. So I will use Gradle to generate JavaScript code from Kotlin code.

Also I would like to use jQuery as an example of existing JavaScript library.

Here is a sample HTML with JavaScript

<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p").click(function(){
    $(this).text("Bless You!");
  });
});
</script>
</head>
<body>

<p>If you click on me, You will be blessed.</p>

</body>
</html>

The JavaScript part of the example above is:

$(document).ready(function(){
  $("p").click(function(){
    $(this).text("Bless You!");
  });
});

I will produce above JavaScript code with Kotlin. Follow me next.

Posted on Leave a comment

The Sun gives off energy for free. Thanks!

How much do we get after all?

The energy rate aka power of sunlight is measured and known to be about 1kW/m^2. In a square area of one meter by one meter, how many people can live? One human consumes about 100W of power for a 2000 Cal diet per day. So if human is caged like a chicken farm, Sun can grow max 10 humans per square meter. How big is the earth normal to sunlight direction? Earth radius is known to be about 64000kM. Simple math of an area of circle is 3.14 * 6400kM * 64000kM = 3.14 x 6.4 x 10 ^6 m x 6.4 x 10^6 m ~ 129 x 10^12 m^2 = 1.29 x 10^14 m^2. Considering non-ideal cases, let us suppose a yield of one human per square meter with 10% efficiency. One million is 10^6, one billion is 10^9, one trillion is 10^12. In a simple calculation, 100 trillion humans can live on earth sustainably. Currently there are less than 100 billion people living on earth. Gee, a thousand times more people still can live with the energy received from generous Sunlight. With 1 dollar for 10kWh rate, 1.29 x 10^14 m^2 x 1kW/m^2 x 1 dollar / 10kWh= 1.29 x 10^15 dollars/h = 1.29 x 10^15 dollars/3600s ~ 358 x 10^9 dollars/s = 358 billion dollars per second. So we seem to receive 358 billion dollars every second from Sun. Everyone on earth should earn 3 dollars per second. The minimum wage should be 10 thousand dollars per hour. Then why are we not rich? Where does all the money go?

Is it really free?

You don’t know until you have to pay for it.

Is it not enough?

If everyone uses a thousand times more energy than needed, it is not enough. We need to find an alternative energy source. One example is well known fossil fuel that captured previously given sunlight and stored in the form of oil, gas and coal, etc. Another is from energy-matter conversion using E=mc^2, the energy captured in the form of matter in the very early stage of our Universe. The fossil fuel is current major energy supplier with a limited inventory. Energy-matter conversion has seen a limited success in the laboratory so far with a promising result to come later. Energy-matter conversion is what the Sun is doing to give us free energy.