hhvm / hsl-experimental

Experimental features for the Hack Standard Library
MIT License
23 stars 10 forks source link

Migrate STDIN/STDOUT/STDERR handles from PHP resources to FDs #135

Closed fredemmott closed 4 years ago

fredemmott commented 4 years ago

Non-CLI handles are still using PHP resources, as the replacements are not yet available in FB builds of HHVM.

Test Plan:

use namespace HH\Lib\IO;

<<__EntryPoint>>
async function main(): Awaitable<void> {
  require_once('vendor/autoload.hack');
  \Facebook\AutoloadMap\initialize();

  $in = IO\request_input();
  $out = IO\request_output();
  $err = IO\request_error();

  var_dump($in, $out, $err);

  printf("Read from in: '%s'\n", await $in->readAsync(1024));
  print("Writing to out\n");
  $out->write("Hello,\n");
  print("Writing to error\n");
  $err?->write("world.\n");
}

Standalone CLI

$ echo herpderp | hhvm test.hack
object(HH\Lib\_Private\_IO\StdioReadHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(0)
  }
}
object(HH\Lib\_Private\_IO\StdioWriteHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(1)
  }
}
object(HH\Lib\_Private\_IO\StdioWriteHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(2)
  }
}
Read from in: 'herpderp
'
Writing to out
Hello,
Writing to error
world.

STDOUT vs STDERR

$ echo herpderp | hhvm test.hack 2>/dev/null
object(HH\Lib\_Private\_IO\StdioReadHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(0)
  }
}
object(HH\Lib\_Private\_IO\StdioWriteHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(1)
  }
}
object(HH\Lib\_Private\_IO\StdioWriteHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(2)
  }
}
Read from in: 'herpderp
'
Writing to out
Hello,
Writing to error

CLI Server

$ echo herpderp | hhvm -d hhvm.unix_server_path=/tmp/hhvm.sock -d hhvm.use_remote_unix_server=true test.hack
object(HH\Lib\_Private\_IO\StdioReadHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(29)
  }
}
object(HH\Lib\_Private\_IO\StdioWriteHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(30)
  }
}
object(HH\Lib\_Private\_IO\StdioWriteHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(31)
  }
}
Read from in: 'herpderp
'
Writing to out
Hello,
Writing to error
world.
$ echo herpderp | hhvm -d hhvm.unix_server_path=/tmp/hhvm.sock -d hhvm.use_remote_unix_server=true test.hack 2>/dev/null
object(HH\Lib\_Private\_IO\StdioReadHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(29)
  }
}
object(HH\Lib\_Private\_IO\StdioWriteHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(30)
  }
}
object(HH\Lib\_Private\_IO\StdioWriteHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  object(HH\Lib\OS\FileDescriptor) (2) {
    ["type"]=>
    string(8) "resource"
    ["fd"]=>
    int(31)
  }
}
Read from in: 'herpderp
'
Writing to out
Hello,
Writing to error

HTTP Server

$ curl http://localhost:8080/test.hack
object(HH\Lib\_Private\_IO\RequestReadHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  resource(4) of type (stream)
}
object(HH\Lib\_Private\_IO\ResponseWriteHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  resource(5) of type (stream)
}
NULL
Read from in: ''
Writing to out
Hello,
Writing to error

Request Input

$ curl -d herpderp http://localhost:8080/test.hack
object(HH\Lib\_Private\_IO\RequestReadHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  resource(4) of type (stream)
}
object(HH\Lib\_Private\_IO\ResponseWriteHandle) (2) {
  ["isAwaitable":protected]=>
  bool(true)
  ["impl":protected]=>
  resource(5) of type (stream)
}
NULL
Read from in: 'herpderp'
Writing to out
Hello,
Writing to error
facebook-github-bot commented 4 years ago

@fredemmott merged this pull request in hhvm/hsl-experimental@0a1493cc4a4cb18ef9efd4fcbab2bb0d8d5dcadc.