aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c109
1 files changed, 61 insertions, 48 deletions
diff --git a/src/env.c b/src/env.c
index a854ced..fcf096a 100644
--- a/src/env.c
+++ b/src/env.c
@@ -32,13 +32,13 @@
#define ENV_TM_CURRENT 3198880
#define ENV_TM_TIMEZONE 3198928
-#define ENV_SECONDS_PER_DAY (1000 * 60 * 60 * 24)
+#define ENV_SECONDS_PER_DAY (60 * 60 * 24)
#define ENV_NS_PER_YEAR 31556952000000000
struct w2c_env *pw_env_init(size_t memory) {
size_t bytes = memory << 20;
size_t pages = bytes / wasm2c_pico8_pagesize_env_memory;
- if (bytes < memory || pages < ENV_MIN_PAGES ||
+ if (memory > SIZE_MAX >> 20 || pages < ENV_MIN_PAGES ||
pages > wasm2c_pico8_max_env_memory) {
SDL_InvalidParamError("memory");
return NULL;
@@ -55,7 +55,7 @@ struct w2c_env *pw_env_init(size_t memory) {
}
env->memory.size = pages * wasm2c_pico8_pagesize_env_memory;
env->memory.data_end = env->memory.data + env->memory.size;
- SDL_Log("allocated %llu", env->memory.size);
+ SDL_Log("allocated %" SDL_PRIu64, env->memory.size);
env->table.data =
SDL_calloc(wasm2c_pico8_min_env_table, sizeof(*env->table.data));
@@ -150,9 +150,10 @@ static uint32_t env_ticks(void) {
return (uint32_t)(SDL_GetTicks() & UINT32_MAX);
}
-static uint32_t env_timetm(struct w2c_env *env, uint32_t time,
+static uint32_t env_timetm(struct w2c_env *env, uint32_t timex,
bool local_time) {
- int64_t ns = SDL_SECONDS_TO_NS(time);
+ int32_t t = (int32_t)wasm_rt_memory_load32(&env->memory, timex);
+ int64_t ns = SDL_SECONDS_TO_NS(t);
SDL_DateTime dt;
if (!SDL_TimeToDateTime(ns, &dt, local_time)) {
return 0;
@@ -172,19 +173,20 @@ static uint32_t env_timetm(struct w2c_env *env, uint32_t time,
if (!SDL_DateTimeToTime(&jan, &ns)) {
return 0;
}
- int day_of_year = (time - SDL_NS_TO_SECONDS(ns)) / ENV_SECONDS_PER_DAY;
+ int day_of_year = (t - SDL_NS_TO_SECONDS(ns)) / ENV_SECONDS_PER_DAY;
- bool is_dst;
+ bool has_dst, is_dst;
if (local_time) {
SDL_DateTime jul;
if (!SDL_TimeToDateTime(ns, &jan, true) ||
!SDL_TimeToDateTime(ns + ENV_NS_PER_YEAR / 2, &jul, true)) {
return 0;
}
- is_dst = jan.utc_offset != jul.utc_offset &&
- dt.utc_offset == SDL_min(jan.utc_offset, jul.utc_offset);
+ has_dst = jan.utc_offset != jul.utc_offset;
+ is_dst =
+ has_dst && dt.utc_offset == SDL_max(jan.utc_offset, jul.utc_offset);
} else {
- is_dst = false;
+ has_dst = is_dst = false;
}
uint32_t tm = ENV_TM_CURRENT;
@@ -192,7 +194,7 @@ static uint32_t env_timetm(struct w2c_env *env, uint32_t time,
MEM_WRITE(32, env, tm, dt.minute);
MEM_WRITE(32, env, tm, dt.hour);
MEM_WRITE(32, env, tm, dt.day);
- MEM_WRITE(32, env, tm, dt.month);
+ MEM_WRITE(32, env, tm, dt.month - 1);
MEM_WRITE(32, env, tm, dt.year - 1900);
MEM_WRITE(32, env, tm, dt.day_of_week);
MEM_WRITE(32, env, tm, day_of_year);
@@ -200,22 +202,27 @@ static uint32_t env_timetm(struct w2c_env *env, uint32_t time,
MEM_WRITE(32, env, tm, dt.utc_offset);
if (local_time) {
uint32_t tz = w2c_pico8_0x5F_get_timezone(env->context.instance);
- wasm_rt_memory_store32(&env->memory, tz, dt.utc_offset);
- uint32_t ds = w2c_pico8_0x5F_get_timezone(env->context.instance);
- wasm_rt_memory_store32(&env->memory, ds, dt.utc_offset);
-
- // SDL does not provide an API for getting a timezone name, and we do not
- // want to rely on POSIX for this. Just use a fake `LT` and `LST` for
- // "local time" and "local daylight savings time" respectively.
- uint32_t tn = w2c_pico8_0x5F_get_tzname(env->context.instance);
- if (is_dst) {
- char const name[] = "LST";
- wasm_rt_memory_store(&env->memory, tn, name, sizeof(name));
- } else {
+ wasm_rt_memory_store32(&env->memory, tz, -dt.utc_offset);
+ uint32_t ds = w2c_pico8_0x5F_get_daylight(env->context.instance);
+ wasm_rt_memory_store32(&env->memory, ds, has_dst);
+
+ // SDL does not provide an API for getting a timezone name, and we do
+ // not want to rely on POSIX for this. Just use a fake `LT` and `LST`
+ // for "local time" and "local daylight savings time" respectively.
+ uint32_t tzn = w2c_pico8_0x5F_get_tzname(env->context.instance);
+ if (!wasm_rt_memory_load32(&env->memory, tzn)) {
char const name[] = "LT";
- wasm_rt_memory_store(&env->memory, tn, name, sizeof(name));
- };
+ wasm_rt_memory_store32(&env->memory, tzn,
+ env_strndup(env, name, sizeof(name)));
+ }
+ if (!wasm_rt_memory_load32(&env->memory, tzn + 4)) {
+ char const name[] = "LST";
+ wasm_rt_memory_store32(&env->memory, tzn + 4,
+ env_strndup(env, name, sizeof(name)));
+ }
+ uint32_t tn =
+ wasm_rt_memory_load32(&env->memory, tzn + 4 * (uint32_t)is_dst);
MEM_WRITE(32, env, tm, tn);
} else {
MEM_WRITE(32, env, tm, ENV_TM_TIMEZONE);
@@ -234,7 +241,7 @@ static SDL_NORETURN void env_unimplemented(char const *name) {
}
static SDL_NORETURN void env_badasm(char const *i, uint32_t code) {
- SDL_LogCritical(0, "unknown ASM (%s) code %d", i, code);
+ SDL_LogCritical(0, "unknown ASM (%s) code %" SDL_PRIu32, i, code);
env_abort();
}
@@ -303,8 +310,10 @@ void w2c_env_0x5FSDL_GetWindowSize(struct w2c_env *env, uint32_t window,
uint32_t w2c_env_0x5FSDL_Init(struct w2c_env *env, uint32_t flags) {
(void)flags;
- env->data.errstr =
- w2c_pico8_0x5Fmalloc(env->context.instance, ENV_ERROR_MAX_SIZE);
+ if (!env->data.errstr) {
+ env->data.errstr =
+ w2c_pico8_0x5Fmalloc(env->context.instance, ENV_ERROR_MAX_SIZE);
+ }
return 0;
}
@@ -487,8 +496,8 @@ void w2c_env_0x5F_0x5Flock(struct w2c_env *env, uint32_t ptr) {
}
void w2c_env_0x5F_0x5FsetErrNo(struct w2c_env *env, uint32_t value) {
- uint32_t errno = w2c_pico8_0x5F_0x5Ferrno_location(env->context.instance);
- wasm_rt_memory_store32(&env->memory, errno, value);
+ uint32_t err = w2c_pico8_0x5F_0x5Ferrno_location(env->context.instance);
+ wasm_rt_memory_store32(&env->memory, err, value);
}
uint32_t w2c_env_0x5F_0x5Fsyscall140(struct w2c_env *env, uint32_t which,
@@ -670,12 +679,12 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env,
case 1:
return (uint32_t)env->context.cartdat->count;
case 2:
- // Gets setta byte from the embedded ROM data, note that we need to convert
+ // Gets a byte from the embedded ROM data, note that we need to convert
// the byte position to a cart index and offset within that cart.
{
- uint32_t i = env->data.cartpos / PW_ROM_SIZE;
+ uint32_t i = env->data.cartpos / PW_CART_ROM_SIZE;
if (i < env->context.cartdat->count) {
- uint32_t offset = env->data.cartpos++ % PW_ROM_SIZE;
+ uint32_t offset = env->data.cartpos++ % PW_CART_ROM_SIZE;
return env->context.cartdat->carts[i].rom[offset];
} else {
return 0;
@@ -683,9 +692,9 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env,
}
case 4:
if (env->data.cartname.index < env->context.cartdat->count &&
- env->data.cartname.pos < PW_NAME_MAX) {
+ env->data.cartname.pos < PW_CART_NAME_MAX) {
return env->context.cartdat->carts[env->data.cartname.index]
- .rom[env->data.cartname.pos++];
+ .name[env->data.cartname.pos++];
} else {
return 0;
}
@@ -840,7 +849,7 @@ uint32_t w2c_env_0x5Femscripten_asm_const_iiiiiii(struct w2c_env *env,
(void)e;
(void)f;
switch (code) {
- case 17:
+ case 18:
// PICO-8 can show a an image popup. I am not sure what it is used for
// (maybe for downloading the .p8.png file?), but ignore it.
SDL_LogDebug(0, "ignoring attempt to show image popup");
@@ -878,7 +887,7 @@ void w2c_env_0x5Femscripten_async_wget_data(struct w2c_env *env, uint32_t url,
(void)onload;
char urlstr[256] = {0};
- wasm_rt_memory_load(&env->memory, url, urlstr, sizeof(url) - 1);
+ wasm_rt_memory_loada(&env->memory, url, urlstr, sizeof(urlstr));
SDL_LogDebug(0, "ignoring attempt to fetch %s", urlstr);
w2c_pico8_dynCall_vi(env->context.instance, onerror, userdata);
@@ -922,7 +931,7 @@ void w2c_env_0x5Femscripten_set_main_loop(struct w2c_env *env, uint32_t func,
void w2c_env_0x5Fexit(struct w2c_env *env, uint32_t code) {
// Simulate an exit by signaling that an exit was requested and then trap,
// and handling this in the host.
- SDL_LogDebug(0, "exiting with code: %d", code);
+ SDL_LogDebug(0, "exiting with code: %" SDL_PRIu32, code);
env->data.state.shutdown = true;
wasm_rt_trap(WASM_RT_TRAP_UNREACHABLE);
}
@@ -951,6 +960,10 @@ void w2c_env_0x5Flongjmp(struct w2c_env *env, uint32_t jmpbuf, uint32_t value) {
uint32_t w2c_env_0x5Fstrftime(struct w2c_env *env, uint32_t buf, uint32_t sz,
uint32_t fmt, uint32_t tm) {
+ if (!sz) {
+ return 0;
+ }
+
// AFAICT, PICO-8 only ever formats `struct tm`s with a single format string,
// so instead of relying on `strftime` (which has quite a few portability
// papercuts), just implement the bear minimum of what we need.
@@ -958,8 +971,9 @@ uint32_t w2c_env_0x5Fstrftime(struct w2c_env *env, uint32_t buf, uint32_t sz,
// Check that the format matches the single one we support, and it not, write
// the empty string and return a written length of 0.
- wasm_rt_memory_load(&env->memory, fmt, buffer, sizeof(buffer) - 1);
- if (SDL_strcmp(buffer, "%Y-%m-%d %H:%M:%S")) {
+ size_t fmtlen =
+ wasm_rt_memory_loada(&env->memory, fmt, buffer, sizeof(buffer));
+ if (fmtlen >= sizeof(buffer) || SDL_strcmp(buffer, "%Y-%m-%d %H:%M:%S")) {
wasm_rt_memory_store8(&env->memory, buf, 0);
return 0;
}
@@ -970,22 +984,21 @@ uint32_t w2c_env_0x5Fstrftime(struct w2c_env *env, uint32_t buf, uint32_t sz,
.minute = wasm_rt_memory_load32_unchecked(&env->memory, tm + 4),
.hour = wasm_rt_memory_load32_unchecked(&env->memory, tm + 8),
.day = wasm_rt_memory_load32_unchecked(&env->memory, tm + 12),
- .month = wasm_rt_memory_load32_unchecked(&env->memory, tm + 16),
+ .month = wasm_rt_memory_load32_unchecked(&env->memory, tm + 16) + 1,
.year = wasm_rt_memory_load32_unchecked(&env->memory, tm + 20) + 1900,
};
int n =
SDL_snprintf(buffer, sizeof(buffer), "%d-%02d-%02d %02d:%02d:%02d",
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);
- if (n < 0 || (size_t)n >= sizeof(buffer)) {
+ if (n < 0 || (size_t)n >= sizeof(buffer) || (uint32_t)n >= sz) {
wasm_rt_memory_store8(&env->memory, buf, 0);
return 0;
}
- if (sz) {
- wasm_rt_memory_store(&env->memory, buf, buffer, sz - 1);
- wasm_rt_memory_store8(&env->memory, buf + sz - 1, 0);
- }
- return n;
+ uint32_t nz = (uint32_t)n;
+ wasm_rt_memory_store(&env->memory, buf, buffer, nz + 1);
+
+ return nz;
}
uint32_t w2c_env_0x5Ftime(struct w2c_env *env, uint32_t time) {
@@ -1023,7 +1036,7 @@ void w2c_env_invoke_vii(struct w2c_env *env, uint32_t fn, uint32_t a,
uint32_t sp = w2c_pico8_stackSave(env->context.instance);
env->data.frame = &frame;
- if (setjmp(frame.buf)) {
+ if (!setjmp(frame.buf)) {
w2c_pico8_dynCall_vii(env->context.instance, fn, a, b);
} else {
// Stack unwinding - restore the stack pointer.