Emscripten: Implement more CRT stuff

Namely:

 - Platform::OpenURI
 - Platform::DoRestart
 - Platform::ExecutableName
This commit is contained in:
Tamás Bálint Misius 2023-06-21 19:23:05 +02:00
parent fd50f2dc9a
commit 648bc08377
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
4 changed files with 113 additions and 70 deletions

View File

@ -17,7 +17,16 @@ namespace Platform
{ {
void OpenURI(ByteString uri) void OpenURI(ByteString uri)
{ {
fprintf(stderr, "cannot open URI: not implemented\n"); EM_ASM({
open(UTF8ToString($0));
}, uri.c_str());
}
void DoRestart()
{
EM_ASM({
location.reload();
});
} }
long unsigned int GetTime() long unsigned int GetTime()
@ -29,7 +38,7 @@ long unsigned int GetTime()
ByteString ExecutableNameFirstApprox() ByteString ExecutableNameFirstApprox()
{ {
return ""; return "powder.wasm"; // bogus
} }
bool CanUpdate() bool CanUpdate()
@ -81,6 +90,25 @@ void MaybeTriggerSyncFs()
}); });
} }
} }
ByteString ExecutableName()
{
return DefaultDdir() + "/" + ExecutableNameFirstApprox(); // bogus
}
bool UpdateStart(const std::vector<char> &data)
{
return false;
}
bool UpdateFinish()
{
return false;
}
void UpdateCleanup()
{
}
} }
EMSCRIPTEN_KEEPALIVE extern "C" int MainJs(int argc, char *argv[]) EMSCRIPTEN_KEEPALIVE extern "C" int MainJs(int argc, char *argv[])

View File

@ -150,72 +150,4 @@ std::vector<ByteString> DirectoryList(ByteString directory)
closedir(directoryHandle); closedir(directoryHandle);
return directoryList; return directoryList;
} }
ByteString ExecutableName()
{
auto firstApproximation = ExecutableNameFirstApprox();
auto rp = std::unique_ptr<char, decltype(std::free) *>(realpath(&firstApproximation[0], NULL), std::free);
if (!rp)
{
std::cerr << "realpath: " << errno << std::endl;
return "";
}
return rp.get();
}
void DoRestart()
{
ByteString exename = ExecutableName();
if (exename.length())
{
execl(exename.c_str(), exename.c_str(), NULL);
int ret = errno;
fprintf(stderr, "cannot restart: execl(...) failed: code %i\n", ret);
}
else
{
fprintf(stderr, "cannot restart: no executable name???\n");
}
Exit(-1);
}
bool UpdateStart(const std::vector<char> &data)
{
ByteString exeName = Platform::ExecutableName();
if (!exeName.length())
return false;
auto updName = exeName + "-update";
if (!WriteFile(data, updName))
{
RemoveFile(updName);
return false;
}
if (chmod(updName.c_str(), 0755))
{
RemoveFile(updName);
return false;
}
if (!RenameFile(updName, exeName, true))
{
RemoveFile(updName);
return false;
}
execl(exeName.c_str(), "powder-update", NULL);
return false; // execl returned, we failed
}
bool UpdateFinish()
{
return true;
}
void UpdateCleanup()
{
}
} }

View File

@ -0,0 +1,79 @@
#include "Platform.h"
#include <iostream>
#include <memory>
#include <sys/stat.h>
#include <unistd.h>
#include <ctime>
#include <sys/time.h>
#include <dirent.h>
namespace Platform
{
void DoRestart()
{
ByteString exename = ExecutableName();
if (exename.length())
{
execl(exename.c_str(), exename.c_str(), NULL);
int ret = errno;
fprintf(stderr, "cannot restart: execl(...) failed: code %i\n", ret);
}
else
{
fprintf(stderr, "cannot restart: no executable name???\n");
}
Exit(-1);
}
ByteString ExecutableName()
{
auto firstApproximation = ExecutableNameFirstApprox();
auto rp = std::unique_ptr<char, decltype(std::free) *>(realpath(&firstApproximation[0], NULL), std::free);
if (!rp)
{
std::cerr << "realpath: " << errno << std::endl;
return "";
}
return rp.get();
}
bool UpdateStart(const std::vector<char> &data)
{
ByteString exeName = Platform::ExecutableName();
if (!exeName.length())
return false;
auto updName = exeName + "-update";
if (!WriteFile(data, updName))
{
RemoveFile(updName);
return false;
}
if (chmod(updName.c_str(), 0755))
{
RemoveFile(updName);
return false;
}
if (!RenameFile(updName, exeName, true))
{
RemoveFile(updName);
return false;
}
execl(exeName.c_str(), "powder-update", NULL);
return false; // execl returned, we failed
}
bool UpdateFinish()
{
return true;
}
void UpdateCleanup()
{
}
}

View File

@ -21,6 +21,7 @@ elif host_platform == 'darwin'
common_files += files( common_files += files(
'Darwin.cpp', 'Darwin.cpp',
'Posix.cpp', 'Posix.cpp',
'PosixProc.cpp',
'ExitCommon.cpp', 'ExitCommon.cpp',
) )
powder_files += files( powder_files += files(
@ -32,6 +33,7 @@ elif host_platform == 'android'
common_files += files( common_files += files(
'Android.cpp', 'Android.cpp',
'Posix.cpp', 'Posix.cpp',
'PosixProc.cpp',
'ExitCommon.cpp', 'ExitCommon.cpp',
) )
powder_files += files( powder_files += files(
@ -51,6 +53,7 @@ elif host_platform == 'linux'
common_files += files( common_files += files(
'Linux.cpp', 'Linux.cpp',
'Posix.cpp', 'Posix.cpp',
'PosixProc.cpp',
'ExitCommon.cpp', 'ExitCommon.cpp',
) )
powder_files += files( powder_files += files(
@ -62,6 +65,7 @@ else
common_files += files( common_files += files(
'Null.cpp', 'Null.cpp',
'Posix.cpp', 'Posix.cpp',
'PosixProc.cpp',
'ExitCommon.cpp', 'ExitCommon.cpp',
) )
powder_files += files( powder_files += files(