Code Monkey home page Code Monkey logo

mjolnir's Introduction

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
  }

mjolnir's People

Contributors

forax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

funcguy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.