Skip to content

Commit

Permalink
WASM support (#24)
Browse files Browse the repository at this point in the history
Never compiled with JS and presented on documentation page
Co-authored-by: smaludzi <[email protected]>
  • Loading branch information
kuba-- authored May 15, 2020
1 parent d2a4568 commit fc24998
Show file tree
Hide file tree
Showing 59 changed files with 84,407 additions and 4,730 deletions.
23 changes: 20 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 3.0.2)
project(never)
enable_language(C)

Expand Down Expand Up @@ -29,7 +29,6 @@ if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL
endif(ENABLE_COVERAGE)
endif ()


file(GLOB SRC
"back/*.c"
"front/*.c"
Expand All @@ -42,11 +41,29 @@ add_library(nev STATIC
${FLEX_scanner_OUTPUTS}
${SRC}
)
target_link_libraries(nev m dl ffi)

if (WASM_ONLY OR JS_ONLY)
target_link_libraries(nev m dl)
else()
target_link_libraries(nev m dl ffi)
endif()

add_executable(${CMAKE_PROJECT_NAME} main.c getopt.c)
target_link_libraries(${CMAKE_PROJECT_NAME} nev)

if(WASM_ONLY)
message(STATUS "Setting compilation target to native WASM")
set(CMAKE_EXECUTABLE_SUFFIX ".wasm")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG -DNO_FFI")
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES LINK_FLAGS "-s WASM=1 -s EXPORTED_FUNCTIONS='[_never]' -s EXTRA_EXPORTED_RUNTIME_METHODS='[ccall, cwrap]'")
endif(WASM_ONLY)

if(JS_ONLY)
message(STATUS "Setting compilation target to native JavaScript")
set(CMAKE_EXECUTABLE_SUFFIX ".js")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG -DNO_FFI")
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES LINK_FLAGS "-s WASM=0 -s EXPORTED_FUNCTIONS='[_never]' -s EXTRA_EXPORTED_RUNTIME_METHODS='[ccall, cwrap]' --pre-js ../never-init.js")
endif(JS_ONLY)

# test
enable_testing()
Expand Down
41 changes: 23 additions & 18 deletions back/dlcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dlcache_entry * dlcache_entry_new(unsigned int size)
{
dlcache_entry * entry = (dlcache_entry *)malloc(sizeof(dlcache_entry) * size);
memset(entry, 0, sizeof(dlcache_entry) * size);

return entry;
}

Expand All @@ -21,14 +21,14 @@ void dlcache_entry_delete(dlcache_entry * entries)
}

void dlcache_entry_add_dl(
dlcache_entry * entries,
dlcache_entry * entries,
unsigned int size,
const char * dl_name,
void * handle)
{
unsigned int times = 0;
unsigned int index = 0;

index = hash_string(dl_name) % size;
while (entries[index].dl_name != NULL)
{
Expand All @@ -42,38 +42,38 @@ void dlcache_entry_add_dl(
entries[index].dl_name = dl_name;
entries[index].handle = handle;
}

dlcache_entry * dlcache_entry_lookup(
dlcache_entry * entries,
unsigned int size,
const char * dl_name)
{
unsigned int times = 0;
unsigned int index = 0;

index = hash_string(dl_name) % size;
while (entries[index].dl_name != NULL)
{
if (strcmp(entries[index].dl_name, dl_name) == 0)
{
return &entries[index];
}

index = (index + 1) % size;
if (times++ > size)
{
return NULL;
}
}

return NULL;
}

void dlcache_entry_resize(dlcache_entry * entries, unsigned int size,
dlcache_entry * entries_new, unsigned int size_new)
{
unsigned int i = 0;

for (i = 0; i < size; i++)
{
if (entries[i].dl_name != NULL)
Expand All @@ -96,32 +96,35 @@ dlcache * dlcache_new(unsigned int size)
{
dlcache * cache = (dlcache *)malloc(sizeof(dlcache));
dlcache_entry * entries = dlcache_entry_new(size);
void * host_handle = NULL;


cache->size = size;
cache->count = 0;
cache->entries = entries;

#ifndef NO_FFI
void * host_handle = NULL;
host_handle = ffi_decl_get_handle("host");
dlcache_add_dl(cache, "host", host_handle);

return cache;
#else
return cache;
#endif
}

void dlcache_delete(dlcache * cache)
{
if (cache->entries != NULL)
{
unsigned int i = 0;

for (i = 0; i < cache->size; i++)
{
if (cache->entries[i].handle != NULL)
{
dlclose(cache->entries[i].handle);
}
}

dlcache_entry_delete(cache->entries);
}
free(cache);
Expand All @@ -133,7 +136,7 @@ void dlcache_add_dl(dlcache * cache, const char * dl_name, void * handle)
{
return;
}

dlcache_entry_add_dl(cache->entries, cache->size, dl_name, handle);

cache->count++;
Expand All @@ -160,15 +163,17 @@ void * dlcache_get_handle(dlcache * cache, const char * dl_name)
}
else
{
#ifndef NO_FFI
handle = ffi_decl_get_handle(dl_name);
if (handle == NULL)
{
fprintf(stderr, "cannot open library %s\n", dl_name);
return NULL;
}
dlcache_add_dl(cache, dl_name, handle);
#endif
}

return handle;
}

Expand All @@ -178,10 +183,10 @@ void dlcache_resize(dlcache * cache)
{
unsigned int size_new = cache->size * 2;
dlcache_entry * entries_new = dlcache_entry_new(size_new);

dlcache_entry_resize(cache->entries, cache->size, entries_new, size_new);
dlcache_entry_delete(cache->entries);

cache->size = size_new;
cache->entries = entries_new;
}
Expand All @@ -190,7 +195,7 @@ void dlcache_resize(dlcache * cache)
void dlcache_print(dlcache * cache)
{
unsigned int i = 0;

for (i = 0 ; i < cache->size; i++)
{
dlcache_entry_print(&cache->entries[i]);
Expand Down
Loading

0 comments on commit fc24998

Please sign in to comment.