LoginSignup
3
1

More than 5 years have passed since last update.

HHVM/Hackの開発環境を構築する(MacOS)

Last updated at Posted at 2018-08-12

HHVM/Hackへの入門です。HHVM/Hackの開発環境を構築します。
Hackとは、FacebookよりOSSとして公開された言語で、https://hacklang.org/ にて詳細を確認することができます。

目次

  • Installation
  • Setup Editor
  • Getting Started Hack

Installation

https://docs.hhvm.com/hhvm/installation/macを参照して、MacへHHVMをinstallします。

installation
$ brew tap hhvm/hhvm
$ brew install hhvm

これだけで完了です。

動作確認

Installが完了したら、 https://docs.hhvm.com/hhvm/getting-started/getting-started#test-hhvmの内容を進めていきます。

Run Server

下記のコマンドを実行して8080ポートをlistenするサーバーが立ち上がることを確認しましょう。

start_server
$ hhvm -m server -p 8080
curl
$ curl -i http://localhost:8080
HTTP/1.1 404 Not Found
Vary: Accept-Encoding
Content-Type: text/html
X-Powered-By: HHVM/3.27.1-0brew
Date: Sun, 12 Aug 2018 05:21:54 UTC
Connection: keep-alive
Content-Length: 18

404 File Not Found

X-Powered-Byヘッダーに、HHVMと設定されていることが確認できます。

Run PHP Script

次に、HHVM上でPHPのファイルを動かしてみます。

直下のディレクトリに、hello.phpを作成します。

hello.php
<?php
echo "Hello Hack World!";
確認
$ hhvm -m server -p 8080
$ curl http://localhost:8080/hello.php
Hello Hack World!

以上の内容にて、HackのInstallと動作確認ができました。

Setup Editor

Hackで開発するためのエディター環境を整えていきます。
普段、PHPStormを使っているのですがHackの公式サポートプラグインは提供されていないようです。

jetbrains社のPHP Storm Blog: Hack Language Support in PhpStorm Postponedでは、Facebook製のNuclideというAtom editorのPackageが紹介されています。

Some time ago Facebook announced Nuclide, a unified IDE for Hack/React/Flow. Nuclide is described as a set of packages for Atom editor

今回整えるエディター環境としてもそちらを採用していきます。

Install Atom

未installの場合は、https://atom.io/ からAtomをinstallしてください。

Install Nuclide

https://nuclide.io/docs/quick-start/getting-started/ の内容に沿って進めていきます。
AtomのSettingsからInstall Packagesにすすんで、nuclideと検索すると今回対象のNuclideが出てきます。こちらをInstallします。

“screenshot” 2018-08-13 at 04.50.52.png

Installが完了したら終了です。

Getting Started Hack

開発環境が構築できたことを確認するために、実際にHackのコードを書いて動かしてみます。
https://docs.hhvm.com/hack/getting-started/getting-started の内容をここから進めていきます。

まず、これからHackのコードを書くディレクトリの直下に空の.hhconfigというファイルを作ります。これがHackのTypecheckerを動かすための準備になります。

create_hhconfig
$ touch .hhconfig

次に、簡単なHackのコードを書いていきます。

first.php
<?hh
/**
 * Created by PhpStorm.
 * User: higashiguchi0kazuki
 * Date: 8/13/18
 * Time: 04:27
 */
namespace Quckstart\Examples\First;

class Box<T> {
    public function __construct(private T $elem) {
    }

  public function get(): T {
    return $this->elem;
  }
}

function get_int(): int {
  return 42;
}

function use_box(): void {
  $box = new Box(get_int());
  $i = $box->get();
  \var_dump($i);
}

use_box();

コードを書けたらTypecheckerにかけて、エラーがないことを確認してみましょう。

check
$ hh_client
No errors!

ここで、整備したエディター環境でコードを書いていると得られる利点として、typecheckerが自動で動いてエラーとして表示してくれます。

例えば、上記のコードの、get_int関数の戻り値をboolに変えた場合、エディターは次のような表示をしてくれます。

“screenshot” 2018-08-13 at 05.24.00.png

最後に、書いたコードを実行してみましょう。

run_script
hhvm first.php
int(42)

以上でHHVM/Hackの開発環境を一通り構築することができました。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1