All Projects → forax → mjolnir

forax / mjolnir

Licence: MIT license
Thor Hammer and a way to express invokedynamic in Java

Programming Languages

java
68154 projects - #9 most used programming language
prolog
421 projects

Projects that are alternatives of or similar to mjolnir

xoom-cluster
The VLINGO XOOM platform SDK cluster management for Reactive, scalable resiliency of JVM tools and applications running on XOOM LATTICE and XOOM ACTORS.
Stars: ✭ 25 (+47.06%)
Mutual labels:  jvm-languages
Retroindy
No description or website provided.
Stars: ✭ 20 (+17.65%)
Mutual labels:  invokedynamic
KValidation
Validation library. Fluent syntax in Java, mini DSL in Kotlin
Stars: ✭ 20 (+17.65%)
Mutual labels:  jvm-languages
jisp
Small Lisp expression interpreter made in Java
Stars: ✭ 19 (+11.76%)
Mutual labels:  jvm-languages
javabeanutil-benchmark
No description or website provided.
Stars: ✭ 42 (+147.06%)
Mutual labels:  invokedynamic
Hammerspoon
Staggeringly powerful macOS desktop automation with Lua
Stars: ✭ 8,540 (+50135.29%)
Mutual labels:  mjolnir
dotfiles
⚡️ Sharpen dat knife.
Stars: ✭ 29 (+70.59%)
Mutual labels:  mjolnir
Aviatorscript
A high performance scripting language hosted on the JVM.
Stars: ✭ 2,780 (+16252.94%)
Mutual labels:  jvm-languages
Scala
Scala 2 compiler and standard library. For bugs, see scala/bug
Stars: ✭ 13,573 (+79741.18%)
Mutual labels:  jvm-languages
Haxe
Haxe - The Cross-Platform Toolkit
Stars: ✭ 4,665 (+27341.18%)
Mutual labels:  jvm-languages
Groovy
Apache Groovy: A powerful multi-faceted programming language for the JVM platform
Stars: ✭ 4,359 (+25541.18%)
Mutual labels:  jvm-languages
JBWAPI
Pure Java BWAPI Client implementation for JVM languages
Stars: ✭ 16 (-5.88%)
Mutual labels:  jvm-languages
CASC
A Go Lang / V Lang / Rust syntax approach in JVM language.
Stars: ✭ 16 (-5.88%)
Mutual labels:  jvm-languages
iokk
International Obfuscated Kotlin Contest
Stars: ✭ 56 (+229.41%)
Mutual labels:  jvm-languages
Jruby
JRuby is an implementation of the Ruby language using the JVM.
Stars: ✭ 3,576 (+20935.29%)
Mutual labels:  invokedynamic

Mjolnir

Thor Hammer and secondarily a way to express invokedynamic in Java

Mjolnir build status

Goal

Mjolnir is a Java class allowing to initialize a stable value by calling a bootstrap method once.

The implementation is optimized so the stable value is very cheap to get. A bytecode rewriter is provided to replace the access to the stable value by an invokedynamic making the call even cheaper (mostly free).

Mjolnir has the following properties:

  • Mjolnir.get().invokeExact() is semantically equivalent to an invokedynamic
  • initialize the constant with a bootstrap method no bootstrap call in the fast path
  • no boxing of arguments
  • no static analysis requires for the bytecode rewriter
    • crawling the bytecode is enough
  • should work without the bytecode rewriter (for testing)

Video

Me presenting Mjolnir at JVM Summit 2017

How to build it

Mjolnir is built using pro which is my own build tool, you can download it from github (amazon S3) like this

sh get_pro.sh

(if you are not on linux, you have to build pro by yourself, sorry)

and build Mjolnir just by running pro.

pro/bin/pro

Examples

The following example implements the equivalent of the macro__LINE__ i.e. it returns the current line number like in C

  private static int boostrap(Lookup lookup) {
    String className = lookup.lookupClass().getName();
    int lineNumber = StackWalker.getInstance()
        .walk(s -> s.skip(1).filter(f -> f.getClassName().equals(className)).findFirst())
        .get()
        .getLineNumber();
    return lineNumber;
  }

  public static void main(String[] args) {
    int __LINE__ = Mjolnir.get(lookup -> boostrap(lookup));
  }

This mechanism can be used to express an invokedynamic in Java, the bootstrap method can return a MethodHandle that will be called with invokeExact.

  private static String hello(String name) {
    return "Hello " + name;
  }
  
  private static MethodHandle initHello(Lookup lookup) throws NoSuchMethodException, IllegalAccessException {
    Class<?> declaringClass = lookup.lookupClass();
    return lookup.findStatic(declaringClass, "hello", methodType(String.class, String.class));
  }

  public static void main(String[] args)  hello() throws Throwable {
    String result = (String)Mjolnir.get(lookup -> initHello(lookup)).invokeExact("Mjolnir");
    System.out.println(result);   // Hello Mjolnir
  }
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].