Skip to content

Commit 5c11ca2

Browse files
azjezzasergeyev
authored andcommitted
Add examples for hack lang (#337)
Thanks @azjezz and @usox
1 parent 9017566 commit 5c11ca2

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

examples/hack/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
This examples directory shows some examples written in [Hack](https://hacklang.org).
2+
3+
### Requirements :
4+
5+
- [HHVM](https://github.com/facebook/hhvm) 3.30+
6+
- [HSL ( Hack Standard Library )](https://github.com/hhvm/hsl) 3.30+
7+
- [HSL Experimental](https://github.com/hhvm/hsl-experimental) 3.30+
8+
9+
You can also test the command files by running from the command line :
10+
11+
```
12+
$ hhvm count.hh
13+
1
14+
2
15+
3
16+
4
17+
5
18+
6
19+
7
20+
8
21+
9
22+
10
23+
```

examples/hack/count.hh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/hhvm
2+
<?hh // strict
3+
4+
use namespace HH\Lib\Str;
5+
use function HH\Lib\Experimental\IO\request_output;
6+
use function usleep;
7+
8+
// Simple example script that counts to 10 at ~2Hz, then stops.
9+
10+
<<__EntryPoint>>
11+
async function count_to_ten(): Awaitable<noreturn> {
12+
$output = request_output();
13+
for ($count = 1; $count <= 10; $count++) {
14+
await $output->writeAsync(
15+
Str\format("%d\n",$count)
16+
);
17+
18+
// usleep is builtin, it is not an async builtin - so it also must block the main request thread
19+
usleep(500000);
20+
}
21+
22+
// flush output
23+
await $output->flushAsync();
24+
25+
exit(0);
26+
}

examples/hack/dump-env.hh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/hhvm
2+
<?hh // strict
3+
4+
use namespace HH\Lib\Str;
5+
use function HH\Lib\Experimental\IO\request_output;
6+
7+
<<__EntryPoint>>
8+
async function dumpEnv(): Awaitable<noreturn> {
9+
// Standard CGI(ish) environment variables, as defined in
10+
// http://tools.ietf.org/html/rfc3875
11+
$names = keyset[
12+
'AUTH_TYPE',
13+
'CONTENT_LENGTH',
14+
'CONTENT_TYPE',
15+
'GATEWAY_INTERFACE',
16+
'PATH_INFO',
17+
'PATH_TRANSLATED',
18+
'QUERY_STRING',
19+
'REMOTE_ADDR',
20+
'REMOTE_HOST',
21+
'REMOTE_IDENT',
22+
'REMOTE_PORT',
23+
'REMOTE_USER',
24+
'REQUEST_METHOD',
25+
'REQUEST_URI',
26+
'SCRIPT_NAME',
27+
'SERVER_NAME',
28+
'SERVER_PORT',
29+
'SERVER_PROTOCOL',
30+
'SERVER_SOFTWARE',
31+
'UNIQUE_ID',
32+
'HTTPS'
33+
];
34+
35+
/* HH_IGNORE_ERROR[2050] using global variable */
36+
$server = dict($_SERVER);
37+
38+
$ouput = request_output();
39+
40+
foreach($names as $name) {
41+
await $output->writeAsync(
42+
Str\format("%s = %s\n", $name, $server[$name] ?? '<unset>')
43+
);
44+
}
45+
46+
// Additional HTTP headers
47+
foreach($server as $k => $v) {
48+
if ($k is string && Str\starts_with($k, 'HTTP_')) {
49+
await $output->writeAsync(
50+
Str\format("%s = %s\n", $k, $v as string)
51+
);
52+
}
53+
}
54+
55+
// flush output
56+
await $output->flushAsync();
57+
58+
exit(0);
59+
}

examples/hack/greeter.hh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/hhvm
2+
<?hh // strict
3+
4+
use namespace HH\Lib\Str;
5+
use namespace HH\Lib\Experimental\IO;
6+
7+
<<__EntryPoint>>
8+
async function greeter(): Awaitable<noreturn> {
9+
// For each line FOO received on STDIN, respond with "Hello FOO!".
10+
$input = IO\request_input();
11+
$output = IO\request_output();
12+
while(!$input->isEndOfFile()) {
13+
await $ouput->writeAsync(
14+
Str\format("Hello %s!\n", await $input->readLineAsync())
15+
);
16+
}
17+
18+
// flush output
19+
await $output->flushAsync();
20+
21+
exit(0);
22+
}

0 commit comments

Comments
 (0)