java inline_inline-java: 实现从Haskell调用任何JVM函数

inline-java: Call any JVM function from Haskell

68747470733a2f2f636972636c6563692e636f6d2f67682f74776561672f696e6c696e652d6a6176612e7376673f7374796c653d73766768747470733a2f2f62616467652e6275696c646b6974652e636f6d2f31343364373762316565633036626238363564363934646265363835663265643737313263616131323835326338383038652e737667

The Haskell standard includes a native foreign function interface (FFI). Using it can be a bit involved and only C support is implemented in GHC. inline-java lets you call any JVM function directly, from Haskell, without the need to write your own foreign import declarations using the FFI. In the style of inline-c for C and inline-r for calling R, inline-java lets you name any function to call inline in your code. It is implemented on top of the jni and jvm packages using a GHC Core plugin to orchestrate compilation and loading of the inlined Java snippets.

Example

Graphical Hello World using Java Swing:

{-# LANGUAGE DataKinds #-}

{-# LANGUAGE QuasiQuotes #-}

{-# LANGUAGE OverloadedStrings #-}

module Main where

import Data.Text (Text)

import Language.Java

import Language.Java.Inline

main :: IO ()

main = withJVM [] $ do

message

[java| {

javax.swing.JOptionPane.showMessageDialog(null, $message);

} |]

Building it

Requirements:

the Stack build tool (version 1.2 or above);

either, the Nix package manager,

or, OpenJDK and Gradle installed from your distro.

On OS X, you'll need to install the Legacy Java SE 6 runtime when prompted, even when using Nix.

To build:

$ stack build

You can optionally get Stack to download a JDK in a local sandbox (using Nix) for good build results reproducibility. This is the recommended way to build inline-java. Alternatively, you'll need it installed through your OS distribution's package manager for the next steps (and you'll need to tell Stack how to find the JVM header files and shared libraries).

To use Nix, set the following in your ~/.stack/config.yaml (or pass --nix to all Stack commands, see the Stack manual for more):

nix:

enable: true

Building the safe interface

There is [an experimental interface][safe-interface] which catches common memory management mistakes at compile time. This interface currently needs a fork of GHC which supports the LinearTypes language extension. Both the GHC fork and the safe interface can be built with:

$ stack --nix --stack-yaml stack-linear.yaml build inline-java

For examples of how to use the safe interface you can check the directory server or the wizzardo-http benchmark.

Further reading

Check the tutorial on how to use inline-java. If you want to know more about how it is implemented, look at our post on the plugin implementation.

Debugging

The generated java output can be dumped to stderr by passing to GHC

-fplugin-opt=Language.Java.Inline.Plugin:dump-java

If -ddump-to-file is in effect (as when using stack), the java code is dumped to .dump-java instead.

License

Copyright (c) 2015-2016 EURL Tweag.

All rights reserved.

inline-java is free software, and may be redistributed under the terms specified in the LICENSE file.

Sponsors

68747470733a2f2f7777772e74776561672e696f2f696d672f74776561672d6d65642e706e67              164ef910fd9e487781571e3c3f5e903c.png

inline-java is maintained by Tweag I/O.

Have questions? Need help? Tweet at @tweagio.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值