1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-18 14:44:48 +00:00

windows needs a distinct implementation from posix for thread safety.

I must say, the windows solution is a lot simpler.
This commit is contained in:
Calvin Rose 2024-05-18 14:02:20 -05:00
parent 2e2f8abfc0
commit af232ef729
2 changed files with 44 additions and 12 deletions

View File

@ -934,7 +934,7 @@ JanetFuncDef *janetc_pop_funcdef(JanetCompiler *c) {
int32_t slotchunks = (def->slotcount + 31) >> 5;
/* numchunks is min of slotchunks and scope->ua.count */
int32_t numchunks = slotchunks > scope->ua.count ? scope->ua.count : slotchunks;
uint32_t *chunks = janet_calloc(sizeof(uint32_t), slotchunks);
uint32_t *chunks = janet_calloc(1, slotchunks * sizeof(uint32_t));
if (NULL == chunks) {
JANET_OUT_OF_MEMORY;
}

View File

@ -1897,35 +1897,64 @@ JANET_CORE_FN(os_setlocale,
"Set the system locale, which affects how dates, currencies, and numbers are formatted. "
"Passing nil to locale will return the current locale.") {
janet_arity(argc, 1, 2);
const char *locale = janet_optcstring(argv, argc, 1, NULL);
const char *locale_name = janet_optcstring(argv, argc, 1, NULL);
int category_int = 0;
#ifdef JANET_WINDOWS
if (janet_keyeq(argv[0], "all")) {
category_int |= LC_ALL_MASK;
category_int = LC_ALL;
} else if (janet_keyeq(argv[0], "collate")) {
category_int |= LC_COLLATE_MASK;
category_int = LC_COLLATE;
} else if (janet_keyeq(argv[0], "ctype")) {
category_int |= LC_CTYPE_MASK;
category_int = LC_CTYPE;
} else if (janet_keyeq(argv[0], "monetary")) {
category_int |= LC_MONETARY_MASK;
category_int = LC_MONETARY;
} else if (janet_keyeq(argv[0], "numeric")) {
category_int |= LC_NUMERIC_MASK;
category_int = LC_NUMERIC;
} else if (janet_keyeq(argv[0], "time")) {
category_int |= LC_TIME_MASK;
category_int = LC_TIME;
} else {
janet_panicf("expected one of :all, :collate, :ctype, :monetary, :numeric, or :time, got %v", argv[0]);
}
if (locale == NULL) {
const char *old = setlocale(category_int, locale_name);
if (old == NULL) {
janet_panicf("failed to set locale - %s", strerror(errno));
}
return janet_wrap_nil();
#else
if (janet_keyeq(argv[0], "all")) {
category_int = LC_ALL_MASK;
} else if (janet_keyeq(argv[0], "collate")) {
category_int = LC_COLLATE_MASK;
} else if (janet_keyeq(argv[0], "ctype")) {
category_int = LC_CTYPE_MASK;
} else if (janet_keyeq(argv[0], "monetary")) {
category_int = LC_MONETARY_MASK;
} else if (janet_keyeq(argv[0], "numeric")) {
category_int = LC_NUMERIC_MASK;
} else if (janet_keyeq(argv[0], "time")) {
category_int = LC_TIME_MASK;
} else {
janet_panicf("expected one of :all, :collate, :ctype, :monetary, :numeric, or :time, got %v", argv[0]);
}
if (locale_name == NULL) {
const char *old = setlocale(category_int, NULL);
if (old == NULL) return janet_wrap_nil();
return janet_cstringv(old);
}
locale_t loc = newlocale(category_int, locale, 0);
/* Use newlocale instead of setlocale for per-thread behavior */
locale_t loc = newlocale(category_int, locale_name, 0);
if (loc == 0) {
janet_panicf("failed to make locale - %s", strerror(errno));
}
locale_t old_locale = uselocale(loc);
if (old_locale == 0) {
janet_panicf("failed to set locale - %s", strerror(errno));
}
uselocale(loc);
freelocale(loc);
if (old_locale != LC_GLOBAL_LOCALE) {
freelocale(old_locale);
}
return janet_wrap_nil();
#endif
}
JANET_CORE_FN(os_link,
@ -2782,5 +2811,8 @@ void janet_lib_os(JanetTable *env) {
#endif
JANET_REG_END
};
#ifdef JANET_WINDOWS
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
#endif
janet_core_cfuns_ext(env, NULL, os_cfuns);
}