All Projects → jgpc42 → Insn

jgpc42 / Insn

Licence: epl-1.0
Functional JVM bytecode generation for Clojure.

Programming Languages

clojure
4091 projects

Labels

Projects that are alternatives of or similar to Insn

Shellen
🌸 Interactive shellcoding environment to easily craft shellcodes
Stars: ✭ 799 (+544.35%)
Mutual labels:  asm
Module Service Manager
Android模块化/组件化通信框架
Stars: ✭ 58 (-53.23%)
Mutual labels:  asm
Hlslexplorer
See how hardware understands your HLSL
Stars: ✭ 91 (-26.61%)
Mutual labels:  asm
Wxinlineplayer
🤟Super fast H.264/H.265 FLV player
Stars: ✭ 873 (+604.03%)
Mutual labels:  asm
Sjasmplus
SJAsmPlus: Z80 cross-assembler
Stars: ✭ 51 (-58.87%)
Mutual labels:  asm
Asm Module
✈️ ASM 4 教程中的示例代码,用于 ASM 学习,介绍了 ASM 中基本的使用说明。
Stars: ✭ 65 (-47.58%)
Mutual labels:  asm
The holy book of x86
A simple guide to x86 architecture, assembly, memory management, paging, segmentation, SMM, BIOS....
Stars: ✭ 577 (+365.32%)
Mutual labels:  asm
Ass Js
Assembler.js — X86_64 and Ethereum
Stars: ✭ 115 (-7.26%)
Mutual labels:  asm
C2goasm
C to Go Assembly
Stars: ✭ 1,072 (+764.52%)
Mutual labels:  asm
Methodtraceman
用于快速找到高耗时方法,定位解决Android App卡顿问题。通过gradle plugin+ASM实现可配置范围的方法插桩来统计所有方法的耗时,并提供友好的界面展示,支持耗时筛选、线程筛选、方法名筛选等。(A Tool for Discovering High Time-consuming Methods for Android App)
Stars: ✭ 1,258 (+914.52%)
Mutual labels:  asm
Hunter
A fast, incremental, concurrent framework to develop compile plugin for android project to manipulate bytecode
Stars: ✭ 999 (+705.65%)
Mutual labels:  asm
Kanela
The Kamon Instrumentation Agent
Stars: ✭ 45 (-63.71%)
Mutual labels:  asm
Toledo Atomchess
Toledo Atomchess is the world's smallest chess program in x86 assembly code
Stars: ✭ 69 (-44.35%)
Mutual labels:  asm
Rappel
A linux-based assembly REPL for x86, amd64, armv7, and armv8
Stars: ✭ 818 (+559.68%)
Mutual labels:  asm
Tools
Security and Hacking Tools, Exploits, Proof of Concepts, Shellcodes, Scripts.
Stars: ✭ 1,343 (+983.06%)
Mutual labels:  asm
Rgbds
Rednex Game Boy Development System - An assembly toolchain for the Nintendo Game Boy & Game Boy Color
Stars: ✭ 772 (+522.58%)
Mutual labels:  asm
Simplify Core
Simplify 为简化重复的JAVA代码而生,基于JDK8,无其它jar包依赖,提供序列化,json parse/generator,日期处理,asm && jdkproxy 实现动态代理功能 等常见操作。
Stars: ✭ 65 (-47.58%)
Mutual labels:  asm
Jaop
jaop is a gradle plugin base on javassist&asm for android aop
Stars: ✭ 115 (-7.26%)
Mutual labels:  asm
Koala
从 Java 字节码到 ASM 实践
Stars: ✭ 103 (-16.94%)
Mutual labels:  asm
Androidanimationexercise
Android 动画各种实现,包括帧动画、补间动画和属性动画的总结分享
Stars: ✭ 1,254 (+911.29%)
Mutual labels:  asm

Clojars Project

Dependency and version information

Click to show

⚠️ This library uses a recent version of asm which can cause dependency issues. See here for more.

Leiningen

[insn "0.4.0"]

tools.deps

{insn/insn {:mvn/version "0.4.0"}}

Maven

<dependency>
  <groupId>insn</groupId>
  <artifactId>insn</artifactId>
  <version>0.4.0</version>
</dependency>

JDK versions 8 to 14 and Clojure versions 1.7 to 1.10 are currently supported.

What is it?

This library provides a functional abstraction over ASM for generating JVM bytecode. ASM is the library that Clojure itself uses to dynamically compile Clojure code into code that can be run on the JVM.

Quick start

Let's begin by creating a simple class, equivalent to the following Java code.

package my.pkg;

public class Adder {
    public static int VALUE = 42;
    public long add (int n) {
        return (long) (VALUE + n);
    }
}

The class is specified as a map. The class fields and methods are sequences of maps giving the members of said class.

(def class-data
  {:name 'my.pkg.Adder
   :fields [{:flags #{:public :static}, :name "VALUE", :type :int, :value 42}]
   :methods [{:flags #{:public}, :name "add", :desc [:int :long]
              :emit [[:getstatic :this "VALUE" :int]
                     [:iload 1]
                     [:iadd]
                     [:i2l]
                     [:lreturn]]}]})

Above, we described in data the same information expressed by the Java code, except the method body was given as a sequence of bytecode instructions. (Note: unlike Java, the method return value is specified via the :desc key as the last element). If you aren't fluent in JVM bytecode instruction syntax, I would suggest reading chapter 3 of the excellent tutorial pdf from the ASM site.

:emit can also be a fn that is passed the ASM MethodVisitor object to write the method bytecode as shown in this example.

Now to write the bytecode.

(require '[insn.core :as insn])

(def result (insn/visit class-data))

The result is a map containing the generated classes' packaged-prefixed :name and :bytes, the latter being a byte array. This information is all you need to give to a ClassLoader to define your class.

For convenience, we can use insn.core/define to define the class for us.

(def class-object (insn/define class-data)) ;; => my.pkg.Adder
(-> class-object .newInstance (.add 17))    ;; => 59

Note that you can also pass result to define, the class will not be regenerated. Also note, like Java, since we did not define any constructors, a public no-argument constructor that simply calls the superclass constructor was generated for us.

If you are evaluating the code snippets above in the REPL, you can also just do:

(.add (my.pkg.Adder.) 17) ;; => 59

Since, by default, define loads the class using Clojure's own DynamicClassLoader, meaning the class will be first class to subsequent evaluations in the running Clojure environment.

More information

For additional usage examples and topics, see the wiki. For a complete reference, see the docs.

Running the tests

lein test

Or, lein test-all for all supported Clojure versions.

The tests can also be run against all supported Java versions (via docker) with:

./test-all-jdk.sh

Similar libraries

Projects using insn

  • tech.datatype
    • Efficient N-dimensional numerics across a range of primitive datatypes and containers.
  • jmh-clojure
    • Clojure bridge to JMH benchmarking via bytecode generation.

License

Copyright © 2017-2020 Justin Conklin

Distributed under the Eclipse Public License, the same as Clojure.

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].