54 lines
1.4 KiB
PHP
54 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', '1');
|
|
|
|
if (!extension_loaded('ffi')) {
|
|
http_response_code(500);
|
|
echo "Error: PHP FFI extension is not loaded.\n";
|
|
echo "If you are using the Nix build, run the included server script:\n";
|
|
echo " ./result/bin/tricu-php-server\n";
|
|
exit;
|
|
}
|
|
|
|
require __DIR__ . '/../src/common.php';
|
|
|
|
use function Arboricx\{ctx_init, ctx_free, loadBundleDefault, ofNumber, ofString, app, reduce, decode, findLib, readBundle};
|
|
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
|
|
try {
|
|
if (!isset($_FILES['bundle']) || $_FILES['bundle']['error'] !== UPLOAD_ERR_OK) {
|
|
throw new \RuntimeException('Bundle upload failed.');
|
|
}
|
|
|
|
$args = [];
|
|
for ($i = 0; $i < 5; $i++) {
|
|
$v = $_POST["arg$i"] ?? '';
|
|
if ($v !== '') {
|
|
$args[] = $v;
|
|
}
|
|
}
|
|
|
|
$libPath = findLib();
|
|
$ctx = ctx_init($libPath);
|
|
try {
|
|
$term = loadBundleDefault($ctx, readBundle($_FILES['bundle']['tmp_name']));
|
|
|
|
foreach ($args as $arg) {
|
|
$argTree = preg_match('/^\d+$/', $arg) ? ofNumber($ctx, (int)$arg) : ofString($ctx, $arg);
|
|
$term = app($ctx, $term, $argTree);
|
|
}
|
|
|
|
$result = reduce($ctx, $term, 1_000_000_000);
|
|
echo decode($ctx, $result);
|
|
} finally {
|
|
ctx_free($ctx);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
http_response_code(500);
|
|
echo 'Error: ' . $e->getMessage();
|
|
}
|