remove sdl from os.c
This commit is contained in:
@@ -684,6 +684,8 @@ function guid(bits = 256)
|
||||
{
|
||||
var guid = new blob(bits, os.random)
|
||||
stone(guid)
|
||||
log.console(os.random())
|
||||
log.console("guid: " + text(guid,'h'))
|
||||
return text(guid,'h')
|
||||
}
|
||||
|
||||
|
||||
107
scripts/os.c
107
scripts/os.c
@@ -1,7 +1,5 @@
|
||||
#include "cell.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
@@ -10,6 +8,7 @@
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
@@ -47,34 +46,91 @@ static JSClassDef js_dylib_class = {
|
||||
.finalizer = js_dylib_finalizer,
|
||||
};
|
||||
|
||||
JSC_CCALL(os_now, return number2js(js, (double)SDL_GetTicksNS()/1000000000.0))
|
||||
#include <mach/mach_time.h>
|
||||
|
||||
JSC_CCALL(os_now,
|
||||
#ifdef _WIN32
|
||||
LARGE_INTEGER frequency, counter;
|
||||
QueryPerformanceFrequency(&frequency);
|
||||
QueryPerformanceCounter(&counter);
|
||||
return number2js(js, (double)counter.QuadPart / (double)frequency.QuadPart);
|
||||
#elif defined(__APPLE__)
|
||||
static mach_timebase_info_data_t timebase = {0, 0};
|
||||
if (timebase.denom == 0) {
|
||||
mach_timebase_info(&timebase);
|
||||
}
|
||||
uint64_t time = mach_absolute_time();
|
||||
return number2js(js, (double)time * timebase.numer / timebase.denom / 1000000000.0);
|
||||
#else
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return number2js(js, (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0);
|
||||
#endif
|
||||
)
|
||||
|
||||
JSC_CCALL(os_sleep,
|
||||
double secs = js2number(js,argv[0]);
|
||||
int ms = secs*1000;
|
||||
SDL_Delay(ms);
|
||||
#ifdef _WIN32
|
||||
Sleep((DWORD)(secs * 1000));
|
||||
#else
|
||||
struct timespec ts;
|
||||
ts.tv_sec = (time_t)secs;
|
||||
ts.tv_nsec = (long)((secs - ts.tv_sec) * 1000000000);
|
||||
nanosleep(&ts, NULL);
|
||||
#endif
|
||||
)
|
||||
|
||||
JSC_CCALL(os_power_state,
|
||||
int pct, secs;
|
||||
SDL_PowerState state = SDL_GetPowerInfo(&secs, &pct);
|
||||
const char *statestr = "unknown";
|
||||
switch(state) {
|
||||
case SDL_POWERSTATE_ON_BATTERY: statestr = "battery"; break;
|
||||
case SDL_POWERSTATE_NO_BATTERY: statestr = "no battery"; break;
|
||||
case SDL_POWERSTATE_CHARGING: statestr = "charging"; break;
|
||||
case SDL_POWERSTATE_CHARGED: statestr = "charged"; break;
|
||||
case SDL_POWERSTATE_ERROR: statestr = "error"; break;
|
||||
case SDL_POWERSTATE_UNKNOWN: statestr = "unknown"; break;
|
||||
}
|
||||
ret = JS_NewObject(js);
|
||||
JS_SetPropertyStr(js,ret,"state",JS_NewString(js,statestr));
|
||||
JS_SetPropertyStr(js,ret,"percent",number2js(js,pct));
|
||||
JS_SetPropertyStr(js,ret,"seconds",number2js(js,secs));
|
||||
)
|
||||
static JSValue js_os_totalmem(JSContext *js, JSValue self, int argc, JSValue *argv) {
|
||||
JSValue ret = JS_NULL;
|
||||
#ifdef _WIN32
|
||||
MEMORYSTATUSEX statex;
|
||||
statex.dwLength = sizeof(statex);
|
||||
if (!GlobalMemoryStatusEx(&statex)) return JS_ThrowInternalError(js,"GlobalMemoryStatusEx failed");
|
||||
return JS_NewInt64(js,(int64_t)(statex.ullTotalPhys / (1024 * 1024)));
|
||||
#elif defined(__linux__)
|
||||
struct sysinfo info;
|
||||
if (sysinfo(&info) == 0)
|
||||
return JS_NewInt64(js,(int64_t)((info.totalram * info.mem_unit) / (1024 * 1024)));
|
||||
return JS_NewInt64(js,0);
|
||||
#elif defined(__APPLE__)
|
||||
int mib[2] = {CTL_HW, HW_MEMSIZE};
|
||||
int64_t memsize;
|
||||
size_t len = sizeof(memsize);
|
||||
if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0)
|
||||
return JS_NewInt64(js, memsize / (1024 * 1024));
|
||||
return JS_NewInt64(js,0);
|
||||
#else
|
||||
long pages = sysconf(_SC_PHYS_PAGES);
|
||||
long page_size = sysconf(_SC_PAGE_SIZE);
|
||||
if (pages < 0 || page_size < 0) return JS_NewInt64(js,0);
|
||||
return JS_NewInt64(js,(int64_t)((pages * page_size) / (1024 * 1024)));
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
JSC_CCALL(os_totalmem, return number2js(js, SDL_GetSystemRAM()))
|
||||
JSC_CCALL(os_platform, return JS_NewString(js,SDL_GetPlatform()))
|
||||
static JSValue js_os_platform(JSContext *js, JSValue self, int argc, JSValue *argv) {
|
||||
JSValue ret = JS_NULL;
|
||||
#ifdef _WIN32
|
||||
return JS_NewString(js,"Windows");
|
||||
#elif defined(__linux__)
|
||||
return JS_NewString(js,"Linux");
|
||||
#elif defined(__APPLE__)
|
||||
#ifdef TARGET_OS_IPHONE
|
||||
return JS_NewString(js,"iOS");
|
||||
#else
|
||||
return JS_NewString(js,"macOS");
|
||||
#endif
|
||||
#elif defined(__FreeBSD__)
|
||||
return JS_NewString(js,"FreeBSD");
|
||||
#elif defined(__OpenBSD__)
|
||||
return JS_NewString(js,"OpenBSD");
|
||||
#elif defined(__NetBSD__)
|
||||
return JS_NewString(js,"NetBSD");
|
||||
#else
|
||||
return JS_NewString(js,"Unknown");
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
static JSValue js_os_hostname(JSContext *js, JSValue self, int argc, JSValue *argv) {
|
||||
JSValue ret = JS_NULL;
|
||||
@@ -447,7 +503,7 @@ JSC_CCALL(os_random,
|
||||
if (randombytes(buf, sizeof(double)) != 0) {
|
||||
return JS_ThrowInternalError(js, "failed to generate random bytes");
|
||||
}
|
||||
return JS_NewFloat64(js, random_double);
|
||||
return JS_NewInt32(js, random_double);
|
||||
)
|
||||
|
||||
static const JSCFunctionListEntry js_os_funcs[] = {
|
||||
@@ -458,7 +514,6 @@ static const JSCFunctionListEntry js_os_funcs[] = {
|
||||
MIST_FUNC_DEF(os, hostname, 0),
|
||||
MIST_FUNC_DEF(os, version, 0),
|
||||
MIST_FUNC_DEF(os, now, 0),
|
||||
MIST_FUNC_DEF(os, power_state, 0),
|
||||
MIST_FUNC_DEF(os, rusage, 0),
|
||||
MIST_FUNC_DEF(os, mallinfo, 0),
|
||||
MIST_FUNC_DEF(os, buffer2string, 1),
|
||||
|
||||
@@ -3,4 +3,4 @@ $_.start(e => {
|
||||
log.console(`Original sender got message back: ${json.encode(msg)}. Stopping!`)
|
||||
$_.stop()
|
||||
})
|
||||
}, "reply")
|
||||
}, "tests/reply")
|
||||
|
||||
Reference in New Issue
Block a user