diff --git a/examples/hack/README.md b/examples/hack/README.md new file mode 100644 index 00000000..d327674f --- /dev/null +++ b/examples/hack/README.md @@ -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 +``` diff --git a/examples/hack/count.hh b/examples/hack/count.hh new file mode 100644 index 00000000..7b07f59c --- /dev/null +++ b/examples/hack/count.hh @@ -0,0 +1,26 @@ +#!/usr/bin/hhvm +> +async function count_to_ten(): Awaitable { + $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); + } + + // flush output + await $output->flushAsync(); + + exit(0); +} diff --git a/examples/hack/dump-env.hh b/examples/hack/dump-env.hh new file mode 100644 index 00000000..f7fa5d54 --- /dev/null +++ b/examples/hack/dump-env.hh @@ -0,0 +1,59 @@ +#!/usr/bin/hhvm +> +async function dumpEnv(): Awaitable { + // 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] ?? '') + ); + } + + // 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); +} diff --git a/examples/hack/greeter.hh b/examples/hack/greeter.hh new file mode 100644 index 00000000..d3418ef5 --- /dev/null +++ b/examples/hack/greeter.hh @@ -0,0 +1,22 @@ +#!/usr/bin/hhvm +> +async function greeter(): Awaitable { + // 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); +}