Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for hack lang #337

Merged
merged 15 commits into from
Jan 28, 2019
23 changes: 23 additions & 0 deletions examples/hack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
This examples directory shows some examples written in [Hack](https://hacklang.org).

### Requirements :

- [HHVM](https://github.com/facebook/hhvm) 3.30+
- [HSL ( Hack Standard Library )](https://github.com/hhvm/hsl) 3.30+
- [HSL Experimental](https://github.com/hhvm/hsl-experimental) 3.30+

You can also test the command files by running from the command line :

```
$ hhvm count.hh
1
2
3
4
5
6
7
8
9
10
```
26 changes: 26 additions & 0 deletions examples/hack/count.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/hhvm
<?hh // strict

use namespace HH\Lib\Str;
use function HH\Lib\Experimental\IO\request_output;
use function usleep;

// Simple example script that counts to 10 at ~2Hz, then stops.

<<__EntryPoint>>
azjezz marked this conversation as resolved.
Show resolved Hide resolved
async function count(): Awaitable<noreturn> {
$output = request_output();
for ($count = 1; $count <= 10; $count++) {
await $output->writeAsync(
Str\format("%d\n",$count)
);

// usleep is builtin, it is not an async builtin - so it also must block the main request thread
usleep(500000);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HH\Asio\usleep()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a side note, async is not multithreading. Some async operations may use threading as an implementation detail, but in general (and for IO specifically), it's single-threaded.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed here : #340

}

// flush output
await $output->flushAsync();

exit(0);
}
59 changes: 59 additions & 0 deletions examples/hack/dump-env.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/hhvm
<?hh // strict

use namespace HH\Lib\Str;
use function HH\Lib\Experimental\IO\request_output;

<<__EntryPoint>>
async function dumpEnv(): Awaitable<noreturn> {
// Standard CGI(ish) environment variables, as defined in
// http://tools.ietf.org/html/rfc3875
$names = keyset[
'AUTH_TYPE',
'CONTENT_LENGTH',
'CONTENT_TYPE',
'GATEWAY_INTERFACE',
'PATH_INFO',
'PATH_TRANSLATED',
'QUERY_STRING',
'REMOTE_ADDR',
'REMOTE_HOST',
'REMOTE_IDENT',
'REMOTE_PORT',
'REMOTE_USER',
'REQUEST_METHOD',
'REQUEST_URI',
'SCRIPT_NAME',
'SERVER_NAME',
'SERVER_PORT',
'SERVER_PROTOCOL',
'SERVER_SOFTWARE',
'UNIQUE_ID',
'HTTPS'
];

/* HH_IGNORE_ERROR[2050] using global variable */
$server = dict($_SERVER);

$ouput = request_output();

foreach($names as $name) {
await $output->writeAsync(
Str\format("%s = %s\n", $name, $server[$name] ?? '<unset>')
);
}

// Additional HTTP headers
foreach($server as $k => $v) {
if ($k is string && Str\starts_with($k, 'HTTP_')) {
await $output->writeAsync(
Str\format("%s = %s\n", $k, $v as string)
);
}
}

// flush output
await $output->flushAsync();

exit(0);
}
22 changes: 22 additions & 0 deletions examples/hack/greeter.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/hhvm
<?hh // strict

use namespace HH\Lib\Str;
use namespace HH\Lib\Experimental\IO;

<<__EntryPoint>>
async function greeter(): Awaitable<noreturn> {
// For each line FOO received on STDIN, respond with "Hello FOO!".
$input = IO\request_input();
$output = IO\request_output();
while(!$input->isEndOfFile()) {
await $ouput->writeAsync(
Str\format("Hello %s!\n", await $input->readLineAsync())
);
}

// flush output
await $output->flushAsync();

exit(0);
}