Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged blaugold's WASM branch, plus fixes #2069

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions C/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ elseif(APPLE)
include("${CMAKE_CURRENT_LIST_DIR}/cmake/platform_apple.cmake")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
include("${CMAKE_CURRENT_LIST_DIR}/cmake/platform_linux.cmake")
elseif(EMSCRIPTEN)
include("${CMAKE_CURRENT_LIST_DIR}/cmake/platform_emscripten.cmake")
else()
message(FATAL_ERROR "Unsupported platform ${CMAKE_SYSTEM_NAME}")
endif()
Expand All @@ -56,18 +58,27 @@ add_executable(
# EE tests:
c4DatabaseEncryptionTest.cc
c4CertificateTest.cc

${TOP}LiteCore/tests/main.cpp
${TOP}Crypto/SecureRandomize.cc
${TOP}LiteCore/Support/FilePath.cc
${TOP}LiteCore/Support/LogDecoder.cc
${TOP}LiteCore/Support/Logging_Stub.cc
${TOP}LiteCore/Support/StringUtil.cc
${TOP}LiteCore/Support/TestsCommon.cc
${TOP}LiteCore/Support/Error.cc
${TOP}vendor/fleece/ObjC/slice+CoreFoundation.cc
)

if (NOT EMSCRIPTEN)
# These files contain internal functionality that isn't exported from the LiteCore shared lib.
# But C4Tests use these, so we have to compile them into its binary.
# This doesn't apply to Emscripten, bc WASM doesn't have shared libs; all linking is static.
target_sources(
C4Tests PRIVATE
${TOP}Crypto/SecureRandomize.cc
${TOP}LiteCore/Support/Error.cc
${TOP}LiteCore/Support/FilePath.cc
${TOP}LiteCore/Support/LogDecoder.cc
${TOP}LiteCore/Support/Logging_Stub.cc
${TOP}LiteCore/Support/StringUtil.cc
)
endif()

get_directory_property(this_targets DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} BUILDSYSTEM_TARGETS)
set(LITECORE_TARGETS ${LITECORE_TARGETS} ${this_targets} PARENT_SCOPE)
setup_build()
Expand Down Expand Up @@ -112,6 +123,7 @@ target_include_directories(
${TOP}vendor/fleece/API
${TOP}vendor/fleece/Fleece/Support
${TOP}C
${TOP}C/include
${TOP}Crypto
${TOP}Replicator
${TOP}Replicator/tests
Expand Down
4 changes: 4 additions & 0 deletions C/tests/c4DatabaseTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ N_WAY_TEST_CASE_METHOD(C4DatabaseTest, "Database ErrorMessages", "[Database][Err
assertMessage(SQLiteDomain, SQLITE_IOERR_ACCESS, "SQLite error 3338", "disk I/O error (3338)");
assertMessage(SQLiteDomain, SQLITE_IOERR, "SQLite error 10", "disk I/O error");
assertMessage(LiteCoreDomain, 15, "LiteCore CorruptData", "data is corrupted");
#ifdef __EMSCRIPTEN__
assertMessage(POSIXDomain, ENOENT, "POSIX error 44", "No such file or directory");
#else
assertMessage(POSIXDomain, ENOENT, "POSIX error 2", "No such file or directory");
#endif
assertMessage(LiteCoreDomain, kC4ErrorTransactionNotClosed, "LiteCore TransactionNotClosed",
"transaction not closed");
assertMessage(SQLiteDomain, -1234, "SQLite error -1234", "unknown error (-1234)");
Expand Down
2 changes: 2 additions & 0 deletions C/tests/c4PerfTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ class PerfTest : public C4Test {
input += "_helium_macos";
#elif defined(__linux__)
input += "_helium_linux";
#elif defined(__EMSCRIPTEN__)
input += "_helium_emscripten";
#else
# error "Unknown platform"
#endif
Expand Down
31 changes: 31 additions & 0 deletions C/tests/cmake/platform_emscripten.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function(setup_build)
target_sources(
C4Tests PRIVATE
${TOP}LiteCore/Unix/strlcat.c
)

target_link_libraries(
C4Tests PRIVATE
)

target_compile_options(
C4Tests PRIVATE
${EMSCRIPTEN_COMPILE_FLAGS}
)

target_link_options(
C4Tests PRIVATE
${EMSCRIPTEN_LINK_FLAGS}
"SHELL:-s EXIT_RUNTIME=1"
"SHELL:-s PTHREAD_POOL_SIZE=24"
"SHELL:-s ALLOW_MEMORY_GROWTH=1"
"SHELL:-s WASM_BIGINT=1"
"-lnodefs.js"
"-lnoderawfs.js"
)

target_include_directories(
C4Tests PRIVATE
${TOP}LiteCore/Unix
)
endfunction()
111 changes: 60 additions & 51 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,14 @@ if(BUILD_ENTERPRISE)
)
endif()

if(MSVC)
if(EMSCRIPTEN)
# Emscripten does not actually support shared libraries and instead
# just builds a static library for compatibility with existing
# build setups. We just set this property to suppress a CMake warning.
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)

include("${PROJECT_SOURCE_DIR}/cmake/platform_emscripten.cmake")
elseif(MSVC)
add_definitions(-DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0A00)
if(WINDOWS_STORE)
message(FATAL_ERROR "UWP no longer supported")
Expand All @@ -143,7 +150,7 @@ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
include("${PROJECT_SOURCE_DIR}/cmake/platform_linux_desktop.cmake")
else()
message(FATAL_ERROR "Unable to determine a supported platform from ${CMAKE_SYSTEM_NAME}")
endif(MSVC)
endif()

check_threading()
setup_globals()
Expand Down Expand Up @@ -333,60 +340,62 @@ install(FILES ${FLEECE_HEADERS} DESTINATION include/fleece)

### Support Libraries (Add functionality, but add nothing to official API)

add_subdirectory(REST EXCLUDE_FROM_ALL)

set(
LC_WEBSOCKET_SRC
Networking/HTTP/HTTPTypes.cc
Networking/HTTP/HTTPLogic.cc
Networking/NetworkInterfaces.cc
Networking/Poller.cc
Networking/TCPSocket.cc
Networking/TLSContext.cc
Networking/WebSockets/BuiltInWebSocket.cc
vendor/sockpp/src/acceptor.cpp
vendor/sockpp/src/connector.cpp
vendor/sockpp/src/datagram_socket.cpp
vendor/sockpp/src/exception.cpp
vendor/sockpp/src/inet_address.cpp
vendor/sockpp/src/inet6_address.cpp
vendor/sockpp/src/mbedtls_context.cpp
vendor/sockpp/src/socket.cpp
vendor/sockpp/src/stream_socket.cpp
)
if (NOT EMSCRIPTEN)
add_subdirectory(REST EXCLUDE_FROM_ALL)

add_library(LiteCoreWebSocket STATIC EXCLUDE_FROM_ALL ${LC_WEBSOCKET_SRC})
target_include_directories(
LiteCoreWebSocket PRIVATE
C
C/include
C/Cpp_include
Crypto
LiteCore/Support
Networking
Networking/BLIP/
Networking/HTTP
Networking/WebSockets
Replicator
REST
vendor/fleece/Fleece/Support
vendor/fleece/API
vendor/sockpp/include
vendor/mbedtls/include
vendor/mbedtls/crypto/include
)
set(
LC_WEBSOCKET_SRC
Networking/HTTP/HTTPTypes.cc
Networking/HTTP/HTTPLogic.cc
Networking/NetworkInterfaces.cc
Networking/Poller.cc
Networking/TCPSocket.cc
Networking/TLSContext.cc
Networking/WebSockets/BuiltInWebSocket.cc
vendor/sockpp/src/acceptor.cpp
vendor/sockpp/src/connector.cpp
vendor/sockpp/src/datagram_socket.cpp
vendor/sockpp/src/exception.cpp
vendor/sockpp/src/inet_address.cpp
vendor/sockpp/src/inet6_address.cpp
vendor/sockpp/src/mbedtls_context.cpp
vendor/sockpp/src/socket.cpp
vendor/sockpp/src/stream_socket.cpp
)

target_link_libraries(
LiteCoreWebSocket PUBLIC
LiteCoreObjects
)
add_library(LiteCoreWebSocket STATIC EXCLUDE_FROM_ALL ${LC_WEBSOCKET_SRC})
target_include_directories(
LiteCoreWebSocket PRIVATE
C
C/include
C/Cpp_include
Crypto
LiteCore/Support
Networking
Networking/BLIP/
Networking/HTTP
Networking/WebSockets
Replicator
REST
vendor/fleece/Fleece/Support
vendor/fleece/API
vendor/sockpp/include
vendor/mbedtls/include
vendor/mbedtls/crypto/include
)

if(LITECORE_PERF_TESTING_MODE)
target_compile_definitions(
target_link_libraries(
LiteCoreWebSocket PUBLIC
LITECORE_PERF_TESTING_MODE
LiteCoreObjects
)
endif()

if(LITECORE_PERF_TESTING_MODE)
target_compile_definitions(
LiteCoreWebSocket PUBLIC
LITECORE_PERF_TESTING_MODE
)
endif()
endif() # NOT EMSCRIPTEN

### TESTS:

Expand Down
98 changes: 98 additions & 0 deletions LiteCore/Storage/UnicodeCollator_JS.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// UnicodeCollator_JS.cc
//
// Copyright 2017-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
//

// This is an UnicodeCollaction implementation based on the JS Intl.Collator API.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator

#include "UnicodeCollator.hh"
#include "Error.hh"
#include "emscripten/val.h"
#include "SQLiteCpp/Exception.h"
#include <sqlite3.h>

namespace litecore {

using namespace std;
using namespace emscripten;
using namespace fleece;

class JSCollationContext : public CollationContext {
public:
val collator = val::undefined();

JSCollationContext(const Collation& collation) : CollationContext(collation) {
auto locale = val::undefined();
auto options = val::object();

if ( collation.localeName ) { locale = val(collation.localeName.asString()); }

if ( collation.diacriticSensitive ) {
if ( collation.caseSensitive ) {
options.set("sensitivity", "variant");
} else {
options.set("sensitivity", "accent");
}
} else {
if ( collation.caseSensitive ) {
options.set("sensitivity", "case");
} else {
options.set("sensitivity", "base");
}
}

collator = val::global("Intl")["Collator"].new_(locale, options);
}
};

unique_ptr<CollationContext> CollationContext::create(const Collation& coll) {
return make_unique<JSCollationContext>(coll);
}

static inline int compareStringsUnicode(int len1, const void* chars1, int len2, const void* chars2,
const JSCollationContext& ctx) {
return ctx.collator.call<int>("compare", string((const char*)chars1, len1), string((const char*)chars2, len2));
}

static int collateUnicodeCallback(void* context, int len1, const void* chars1, int len2, const void* chars2) {
auto& coll = *(JSCollationContext*)context;
if ( coll.canCompareASCII ) {
int result = CompareASCII(len1, (const uint8_t*)chars1, len2, (const uint8_t*)chars2, coll.caseSensitive);
if ( result != kCompareASCIIGaveUp ) return result;
}
return compareStringsUnicode(len1, chars1, len2, chars2, coll);
}

int CompareUTF8(slice str1, slice str2, const Collation& coll) {
return CompareUTF8(str1, str2, JSCollationContext(coll));
}

int CompareUTF8(slice str1, slice str2, const CollationContext& ctx) {
return collateUnicodeCallback((void*)&ctx, (int)str1.size, str1.buf, (int)str2.size, str2.buf);
}

int LikeUTF8(slice str1, slice str2, const Collation& coll) {
return LikeUTF8(str1, str2, JSCollationContext(coll));
}

bool ContainsUTF8(slice str, slice substr, const CollationContext& ctx) {
return ContainsUTF8_Slow(str, substr, ctx);
}

unique_ptr<CollationContext> RegisterSQLiteUnicodeCollation(sqlite3* dbHandle, const Collation& coll) {
unique_ptr<CollationContext> context(new JSCollationContext(coll));
int rc = sqlite3_create_collation(dbHandle, coll.sqliteName().c_str(), SQLITE_UTF8, (void*)context.get(),
collateUnicodeCallback);
if ( rc != SQLITE_OK ) throw SQLite::Exception(dbHandle, rc);
return context;
}

} // namespace litecore
4 changes: 2 additions & 2 deletions LiteCore/Support/Error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
# include <android/log.h>
#endif

#if defined(__clang__) && !defined(__ANDROID__) // For logBacktrace:
# include <execinfo.h> // Not available in Windows?
#if defined(__clang__) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) // For logBacktrace:
# include <execinfo.h> // Not available in Windows?
# include <cxxabi.h>
#endif

Expand Down
27 changes: 26 additions & 1 deletion LiteCore/Support/FilePath.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ using namespace std;
using namespace fleece;
using namespace litecore;

#ifdef __linux__
#if defined(__linux__) || defined(__EMSCRIPTEN__)
static int copyfile(const char* from, const char* to) {
int read_fd, write_fd;
off_t offset = 0;
Expand All @@ -73,6 +73,30 @@ static int copyfile(const char* from, const char* to) {
return write_fd;
}

# ifdef __EMSCRIPTEN__
static const size_t kBufSize = 1024;
uint8_t buf[kBufSize];
while ( offset < stat_buf.st_size ) {
auto bytes_left_to_read = (size_t)(stat_buf.st_size - offset);
auto max_bytes_to_read = std::min(bytes_left_to_read, kBufSize);
ssize_t bytes_read;
if ( (bytes_read = read(read_fd, &buf, max_bytes_to_read)) < 0 ) {
int e = errno;
close(read_fd);
close(write_fd);
errno = e;
return -1;
}
offset = offset + bytes_read;
if ( write(write_fd, &buf, bytes_read) < 0 ) {
int e = errno;
close(read_fd);
close(write_fd);
errno = e;
return -1;
}
}
# else
size_t expected = stat_buf.st_size;
ssize_t bytes = 0;
while ( bytes < expected ) {
Expand All @@ -98,6 +122,7 @@ static int copyfile(const char* from, const char* to) {
return -1;
}
}
# endif

if ( close(read_fd) < 0 ) {
int e = errno;
Expand Down
Loading
Loading