http://192.168.99.10/test/ 内网服务器上,有个服务,可以将lua代码压缩后生成zip文件上传生成lfs.img文件。
<?php
ob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS);
print_r($_FILES); // Debug
$dbgErrLog = "web-luac-cross.log";
$zipFile = $_FILES['name']['tmp_name'];
$upLoadName = $_FILES['name']['name'];
$zipName = basename($upLoadName);
$entries = [];
/*
* Clean up and exit on error
*/
function abort($error) {
global $zipFile, $zip, $entry, $tempDir;
if (is_uploaded_file($zipFile)) unlink($zipFile);
if (is_resource($zip)) zip_close($zip);
if (is_resource($entry)) zip_entry_close($entry);
$dbgOP = ob_get_contents();
file_put_contents($dbgErrLog, $dbgOP);
ob_end_clean();
$resp = htmlentities($error, ENT_QUOTES | ENT_IGNORE, "UTF-8");
$resp = <<<EOD
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>Luac.cross compile error</title></head>
<body><pre>$resp</pre></body></html>
EOD;
http_response_code(404);
header('Cache-Control : no-cache, no-store, must-revalidate, max-age=0');
header('Content-type : text/html;charset=UTF-8');
header('Content-Length: ' . strlen($resp));
echo $resp;
exit();
}
// Open uploaded ZIPfile, aborting on error
if (!preg_match('/^([[:alpha:]_]\w{1,32}).zip$/i', $zipName,$matches)) {
abort("Zip file must conform to Lua variable name convention with .zip extention");
} else {
$imageName = $matches[1].'.img';
}
if (!is_uploaded_file($zipFile)) abort("No zip file uploaded");
$zip = zip_open($zipFile);
if (!is_resource($zip)) abort("File is not a valid ZIP format");
// Unpack the uploaded ZIP file into a tmpdir
$tempDir = tempnam(sys_get_temp_dir(), 'luac_dir');
if (file_exists($tempDir)) unlink($tempDir);
mkdir($tempDir);
while (is_resource($entry = zip_read($zip))) {
$name = basename(zip_entry_name($entry));
$entryName = "${tempDir}/${name}";
// Filenames must have a valid Lua name root
if (preg_match('/^[[:alpha:]_]\w*.lua$/', $name)) {
echo "$entryName found\n"; // Debug
$entries[] = $entryName;
$contents = '';
while (($chunk = zip_entry_read($entry,8192))) {
$contents .= $chunk;
}
zip_entry_close($entry); $entry = NULL;
file_put_contents($entryName, $contents);
}
}
zip_close($zip); $zip = NULL;
if (count($entries)==0) abort("ZIP contains no Lua files");
$luaFiles = implode (" ", $entries);
$outLFS = "${tempDir}/lfs.img";
exec("luac.cross -f -o ${outLFS} ${luaFiles}", $output, $status);
if ($status != 0) abort("luac.cross compile error: \n" . implode ("\n", $output) );
$LFSimage = file_get_contents($outLFS);
file_put_contents(time().".img",$LFSimage);
exec("rm -rf ${tempDir}");
file_put_contents($dbgErrLog, ob_get_contents());
ob_end_clean();
http_response_code(200);
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=LFS.img");
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . strlen($LFSimage));
echo $LFSimage;
就是采用luac.cross将lua文件打包。对于esp8266小内存mcu效果良好。