Skip to content

Commit fe73520

Browse files
Update libcxx to work with emscripten.
1 parent aa0108c commit fe73520

5 files changed

Lines changed: 42 additions & 16 deletions

File tree

system/include/libcxx/__locale

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
#include <locale.h>
2222
#ifdef _WIN32
2323
# include <support/win32/locale_win32.h>
24-
#elif (defined(__GLIBC__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun__))
24+
#elif (defined(__GLIBC__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun__)) || defined(EMSCRIPTEN)
2525
# include <xlocale.h>
26-
#endif // _WIN32 || __GLIBC__ || __APPLE__ || __FreeBSD_
26+
#endif // _WIN32 || __GLIBC__ || __APPLE__ || __FreeBSD__ || __sun__ || EMSCRIPTEN
2727

2828
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2929
#pragma GCC system_header
@@ -339,11 +339,13 @@ public:
339339
static const mask punct = _PUNCT;
340340
static const mask xdigit = _HEX;
341341
static const mask blank = _BLANK;
342-
#elif (defined(__APPLE__) || defined(__FreeBSD__))
342+
#elif (defined(__APPLE__) || defined(__FreeBSD__)) || defined(EMSCRIPTEN)
343343
#ifdef __APPLE__
344344
typedef __uint32_t mask;
345345
#elif defined(__FreeBSD__)
346346
typedef unsigned long mask;
347+
#elif defined(EMSCRIPTEN)
348+
typedef unsigned short mask;
347349
#endif
348350
static const mask space = _CTYPE_S;
349351
static const mask print = _CTYPE_R;
@@ -367,7 +369,7 @@ public:
367369
static const mask punct = _ISPUNCT;
368370
static const mask xdigit = _ISXDIGIT;
369371
static const mask blank = _ISBLANK;
370-
#else // __GLIBC__ || _WIN32 || __APPLE__ || __FreeBSD__ || __sun__
372+
#else // __GLIBC__ || _WIN32 || __APPLE__ || __FreeBSD__ || EMSCRIPTEN || __sun__
371373
typedef unsigned long mask;
372374
static const mask space = 1<<0;
373375
static const mask print = 1<<1;
@@ -590,7 +592,7 @@ public:
590592
#endif
591593
_LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;}
592594
static const mask* classic_table() _NOEXCEPT;
593-
#if defined(__GLIBC__)
595+
#if defined(__GLIBC__) || defined(EMSCRIPTEN)
594596
static const int* __classic_upper_table() _NOEXCEPT;
595597
static const int* __classic_lower_table() _NOEXCEPT;
596598
#endif

system/include/libcxx/locale

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ typedef _VSTD::unique_ptr<__locale_struct, decltype(&uselocale)> __locale_raii;
222222
// OSX has nice foo_l() functions that let you turn off use of the global
223223
// locale. Linux, not so much. The following functions avoid the locale when
224224
// that's possible and otherwise do the wrong thing. FIXME.
225-
#ifdef __linux__
225+
#if defined(__linux__) || defined(EMSCRIPTEN)
226226

227227
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
228228
decltype(MB_CUR_MAX_L(_VSTD::declval<locale_t>()))

system/lib/libcxx/exception.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ get_terminate() _NOEXCEPT
7777
return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0);
7878
}
7979

80+
#ifndef EMSCRIPTEN // We provide this in JS
8081
_LIBCPP_NORETURN
8182
void
8283
terminate() _NOEXCEPT
@@ -97,9 +98,10 @@ terminate() _NOEXCEPT
9798
}
9899
#endif // _LIBCPP_NO_EXCEPTIONS
99100
}
101+
#endif // !EMSCRIPTEN
100102
#endif // !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION)
101103

102-
#if !defined(LIBCXXRT) && !defined(__GLIBCXX__)
104+
#if !defined(LIBCXXRT) && !defined(__GLIBCXX__) && !defined(EMSCRIPTEN)
103105
bool uncaught_exception() _NOEXCEPT
104106
{
105107
#if defined(__APPLE__) || defined(_LIBCPPABI_VERSION)

system/lib/libcxx/locale.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ ctype<wchar_t>::do_toupper(char_type c) const
786786
{
787787
#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
788788
return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c;
789-
#elif defined(__GLIBC__)
789+
#elif defined(__GLIBC__) || defined(EMSCRIPTEN)
790790
return isascii(c) ? ctype<char>::__classic_upper_table()[c] : c;
791791
#else
792792
return (isascii(c) && iswlower_l(c, __cloc())) ? c-L'a'+L'A' : c;
@@ -799,7 +799,7 @@ ctype<wchar_t>::do_toupper(char_type* low, const char_type* high) const
799799
for (; low != high; ++low)
800800
#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
801801
*low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low;
802-
#elif defined(__GLIBC__)
802+
#elif defined(__GLIBC__) || defined(EMSCRIPTEN)
803803
*low = isascii(*low) ? ctype<char>::__classic_upper_table()[*low]
804804
: *low;
805805
#else
@@ -813,7 +813,7 @@ ctype<wchar_t>::do_tolower(char_type c) const
813813
{
814814
#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
815815
return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c;
816-
#elif defined(__GLIBC__)
816+
#elif defined(__GLIBC__) || defined(EMSCRIPTEN)
817817
return isascii(c) ? ctype<char>::__classic_lower_table()[c] : c;
818818
#else
819819
return (isascii(c) && isupper_l(c, __cloc())) ? c-L'A'+'a' : c;
@@ -826,7 +826,7 @@ ctype<wchar_t>::do_tolower(char_type* low, const char_type* high) const
826826
for (; low != high; ++low)
827827
#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
828828
*low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low;
829-
#elif defined(__GLIBC__)
829+
#elif defined(__GLIBC__) || defined(EMSCRIPTEN)
830830
*low = isascii(*low) ? ctype<char>::__classic_lower_table()[*low]
831831
: *low;
832832
#else
@@ -893,7 +893,7 @@ ctype<char>::do_toupper(char_type c) const
893893
#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
894894
return isascii(c) ?
895895
static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(c)]) : c;
896-
#elif defined(__GLIBC__)
896+
#elif defined(__GLIBC__) || defined(EMSCRIPTEN)
897897
return isascii(c) ?
898898
static_cast<char>(__classic_upper_table()[static_cast<size_t>(c)]) : c;
899899
#else
@@ -908,7 +908,7 @@ ctype<char>::do_toupper(char_type* low, const char_type* high) const
908908
#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
909909
*low = isascii(*low) ?
910910
static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(*low)]) : *low;
911-
#elif defined(__GLIBC__)
911+
#elif defined(__GLIBC__) || defined(EMSCRIPTEN)
912912
*low = isascii(*low) ?
913913
static_cast<char>(__classic_upper_table()[static_cast<size_t>(*low)]) : *low;
914914
#else
@@ -923,7 +923,7 @@ ctype<char>::do_tolower(char_type c) const
923923
#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
924924
return isascii(c) ?
925925
static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(c)]) : c;
926-
#elif defined(__GLIBC__)
926+
#elif defined(__GLIBC__) || defined(EMSCRIPTEN)
927927
return isascii(c) ?
928928
static_cast<char>(__classic_lower_table()[static_cast<size_t>(c)]) : c;
929929
#else
@@ -937,7 +937,7 @@ ctype<char>::do_tolower(char_type* low, const char_type* high) const
937937
for (; low != high; ++low)
938938
#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
939939
*low = isascii(*low) ? static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(*low)]) : *low;
940-
#elif defined(__GLIBC__)
940+
#elif defined(__GLIBC__) || defined(EMSCRIPTEN)
941941
*low = isascii(*low) ? static_cast<char>(__classic_lower_table()[static_cast<size_t>(*low)]) : *low;
942942
#else
943943
*low = (isascii(*low) && isupper_l(*low, __cloc())) ? *low-'A'+'a' : *low;
@@ -978,6 +978,12 @@ ctype<char>::do_narrow(const char_type* low, const char_type* high, char dfault,
978978
return low;
979979
}
980980

981+
#ifdef EMSCRIPTEN
982+
extern "C" const unsigned short ** __ctype_b_loc();
983+
extern "C" const int ** __ctype_tolower_loc();
984+
extern "C" const int ** __ctype_toupper_loc();
985+
#endif
986+
981987
const ctype<char>::mask*
982988
ctype<char>::classic_table() _NOEXCEPT
983989
{
@@ -991,6 +997,8 @@ ctype<char>::classic_table() _NOEXCEPT
991997
return _ctype+1; // internal ctype mask table defined in msvcrt.dll
992998
// This is assumed to be safe, which is a nonsense assumption because we're
993999
// going to end up dereferencing it later...
1000+
#elif defined(EMSCRIPTEN)
1001+
return *__ctype_b_loc();
9941002
#else
9951003
// Platform not supported: abort so the person doing the port knows what to
9961004
// fix
@@ -1014,6 +1022,20 @@ ctype<char>::__classic_upper_table() _NOEXCEPT
10141022
}
10151023
#endif // __GLIBC__
10161024

1025+
#if defined(EMSCRIPTEN)
1026+
const int*
1027+
ctype<char>::__classic_lower_table() _NOEXCEPT
1028+
{
1029+
return *__ctype_tolower_loc();
1030+
}
1031+
1032+
const int*
1033+
ctype<char>::__classic_upper_table() _NOEXCEPT
1034+
{
1035+
return *__ctype_toupper_loc();
1036+
}
1037+
#endif // EMSCRIPTEN
1038+
10171039
// template <> class ctype_byname<char>
10181040

10191041
ctype_byname<char>::ctype_byname(const char* name, size_t refs)

system/lib/libcxx/thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ thread::hardware_concurrency() _NOEXCEPT
6565
std::size_t s = sizeof(n);
6666
sysctl(mib, 2, &n, &s, 0, 0);
6767
return n;
68-
#elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && defined(_SC_NPROCESSORS_ONLN)
68+
#elif (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && defined(_SC_NPROCESSORS_ONLN)) || defined(EMSCRIPTEN)
6969
long result = sysconf(_SC_NPROCESSORS_ONLN);
7070
// sysconf returns -1 if the name is invalid, the option does not exist or
7171
// does not have a definite limit.

0 commit comments

Comments
 (0)