Skip to content

Instantly share code, notes, and snippets.

Avatar

Mike Hearn mikehearn

View GitHub Profile
@mikehearn
mikehearn / espresso-notes.md
Created Jan 20, 2021
Espresso architecture notes
View espresso-notes.md

Here are some rough notes on things observed in the codebase - not by the Espresso authors so may all be totally wrong.

Native components

Espresso is mostly written in Java but has some C components as well. These serve two purposes:

  1. The espresso engine can be compiled with native-image down to a shared library so it no longer needs hotspot and runs AOT. This is called “libespresso”
  2. Native code components from OpenJDK that use internal HotSpot APIs can be loaded. This allows a high degree of code re-use and means that unlike most JVM implementations, this one doesn’t need to implement the JDK class library, not even tricky parts in java.base or internal modules - in fact the classes you’d find in the java.base module in a regular OpenJDK should work as long as it’s of the right versions.

OpenJDK native code components include libjava, libjimage etc. These can be loaded as genuinely native components and also (it appears) via Sulong, which on GraalVM EE would give you sandboxing of these compon

View build-jfx-jdk.sh
#!/usr/bin/env bash
#
# To use, unpack a regular JDK somewhere and set JDK_HOME to point to it. The default here expects a JDK 15 early access
# in the same directory but you can set JDK_HOME to anything.
#
JDK_HOME=${JDK_HOME:-$PWD/jdk-15.jdk/Contents/Home}
#
View nodejvm1.java
import net.plan99.nodejs.NodeJS;
public class Demo {
public static void main(String[] args) {
int result = NodeJS.runJS(() ->
NodeJS.eval("return 2 + 3 + 4").asInt()
);
System.out.println(result);
}
}
@mikehearn
mikehearn / kotin-native-win32.kt
Created Sep 28, 2018
Example of using Kotlin/Native on Windows
View kotin-native-win32.kt
typealias WSTR = CPointer<ShortVar>
private fun WSTR.toKString(): String = memScoped {
// Figure out how much memory we need after UTF-8 conversion.
val sz = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, this@toKString, -1, null, 0, null, null)
// Now convert to UTF-8 and from there, a String.
val utf8 = allocArray<ByteVar>(sz)
val r = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, this@toKString, -1, utf8, sz, null, null)
if (r == 0) throw RuntimeException("Could not convert to UTF-8")
utf8.toKString()
@mikehearn
mikehearn / di-helpers.kt
Created Dec 21, 2017
Simple dependency helpers
View di-helpers.kt
package net.corda.node.internal
import java.time.Clock
import java.time.Duration
import java.time.Instant
import java.time.ZoneId
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
/**
@mikehearn
mikehearn / hello.html
Last active Sep 30, 2017
HTML version of the JavaFX JavaScript hello world sample
View hello.html
<html>
<head>
<title>Hello World!</title>
<style>
div.centered {
padding: 1em;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
@mikehearn
mikehearn / hello.js
Last active Sep 30, 2017
Hello World in JavaFX using Nashorn
View hello.js
// Run like this: jjs -fx hello.js
load("fx:base.js");
load("fx:controls.js");
load("fx:graphics.js");
// Set the title bar.
$STAGE.title = "Hello World!";
// Create a button and place it in a layout that will center it.
var button = new Button();
button.text = "Say 'Hello World'";
button.onAction = function() print("Hello World!");
View AmazonSearch.kt
enum SortBy {
Featured,
Relevance,
PriceLowToHigh,
PriceHighToLow,
Reviews
}
@PermazenType
class AmazonSearch(
@mikehearn
mikehearn / jlink --list-plugins
Created Jun 19, 2017
Output of jlink --list-plugins in Java 9 EA build 172
View jlink --list-plugins
View tinyram-opcodes.cpp
enum tinyram_opcode {
tinyram_opcode_AND = 0b00000,
tinyram_opcode_OR = 0b00001,
tinyram_opcode_XOR = 0b00010,
tinyram_opcode_NOT = 0b00011,
tinyram_opcode_ADD = 0b00100,
tinyram_opcode_SUB = 0b00101,
// Multiplications
tinyram_opcode_MULL = 0b00110,