Skip to content

Commit

Permalink
feat(driver): implement subscript and lcurl.safe (#57)
Browse files Browse the repository at this point in the history
fixes #15 

* chore(driver): use emscripten's zlib

* chore(web): footer

* feat(driver): subscript

* chore(driver): subscript

* chore(driver): subscript

* chore(driver): subscript

* chore(driver): fix memory handling

* chore(driver): fix build sharing

* chore(driver): fix content-type
  • Loading branch information
atty303 authored May 21, 2024
1 parent 4dbc234 commit b108bef
Show file tree
Hide file tree
Showing 16 changed files with 1,048 additions and 86 deletions.
19 changes: 7 additions & 12 deletions packages/driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ file(GLOB_RECURSE LUA_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../vendor/lua/*.c)
list(REMOVE_ITEM LUA_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../vendor/lua/lua.c)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../vendor/lua)

set(ZLIB_ENABLE_TESTS OFF)
set(ZLIBNG_ENABLE_TESTS OFF)
set(WITH_GTEST OFF)
set(ZLIB_COMPAT ON)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../vendor/zlib-ng ${CMAKE_CURRENT_BINARY_DIR}/vendor/zlib-ng)

add_compile_options("-flto" "-g" "-gsource-map")
add_compile_options("-flto" "-g" "-gsource-map" "-sUSE_ZLIB")
set(CMAKE_EXECUTABLE_SUFFIX ".mjs")

add_executable(${PROJECT_NAME}
Expand All @@ -43,13 +36,15 @@ add_executable(${PROJECT_NAME}
src/c/wasmfs/nodefs.cpp
src/c/wasmfs/nodefs.h
src/c/wasmfs/nodefs_js.cpp
src/c/sub.c
src/c/sub.h
src/c/lcurl.c
src/c/lcurl.h
)

target_link_libraries(${PROJECT_NAME} PRIVATE $<TARGET_FILE:zlib>)
target_include_directories(${PROJECT_NAME} PRIVATE $<TARGET_PROPERTY:zlib,INCLUDE_DIRECTORIES>)

set(DRIVER_LINK_FLAGS
"-flto"
"-sUSE_ZLIB"
"-sMODULARIZE"
"-sSTACK_SIZE=131072"
"-sASYNCIFY"
Expand All @@ -59,7 +54,7 @@ set(DRIVER_LINK_FLAGS
"-sWASMFS"
"-sSTRICT"
"-sINCOMING_MODULE_JS_API=[print,printErr]"
"-sEXPORTED_FUNCTIONS=[_malloc,_init,_start,_on_frame,_on_key_down,_on_key_up,_on_char,_on_download_page_result]"
"-sEXPORTED_FUNCTIONS=[_malloc,_free,_init,_start,_on_frame,_on_key_down,_on_key_up,_on_char,_on_download_page_result,_on_subscript_finished]"
"-sEXPORTED_RUNTIME_METHODS=cwrap,ccall,ERRNO_CODES,setValue,HEAPU8,Asyncify"
"-sASYNCIFY_IMPORTS=js_wasmfs_node_read"
)
Expand Down
31 changes: 0 additions & 31 deletions packages/driver/boot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ end
function GetWorkDir()
return ""
end
function LaunchSubScript(scriptText, funcList, subList, ...)
error("SubScript is not implemented")
end
function AbortSubScript(ssID)
end
function IsSubScriptRunning(ssID)
end
function LoadModule(fileName, ...)
if not fileName:match("%.lua") then
fileName = fileName .. ".lua"
Expand Down Expand Up @@ -163,27 +156,3 @@ mainObject["OnInit"] = function(self)
return false
end
end

local dkjson = require "dkjson"
local downloadHandle = nil
mainObject["DownloadPage"] = function(self, url, callback, params)
params = params or {}
print(string.format("DownloadPage: url=[%s], header=[%s], body=[%s]", url, params.header, params.body))
if downloadHandle then
error("Download already in progress")
else
DownloadPage(url, params.header, params.body)
downloadHandle = callback
end
end
OnDownloadPageResult = function(resultJson)
print("OnDownloadPageResult")
if downloadHandle then
local callback = downloadHandle
downloadHandle = nil
local result = dkjson.decode(resultJson)
callback({header=result.header, body=result.body}, result.error)
else
error("No download handle")
end
end
2 changes: 0 additions & 2 deletions packages/driver/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Path of Building Web</title>
<link rel="stylesheet" href="https://fonts.cdnfonts.com/css/liberation-sans">
<link rel="stylesheet" href="https://fonts.cdnfonts.com/css/bitstream-vera-sans-mono">
</head>
<body style="margin: 0;">
<div id="app">
Expand Down
39 changes: 39 additions & 0 deletions packages/driver/src/c/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "draw.h"
#include "image.h"
#include "fs.h"
#include "sub.h"

extern backend_t wasmfs_create_nodefs_backend(const char* root);

Expand Down Expand Up @@ -338,6 +339,7 @@ int init() {
image_init(L);
draw_init(L);
fs_init(L);
sub_init(L);

//
lua_pushcclosure(L, GetTime, 0);
Expand Down Expand Up @@ -486,3 +488,40 @@ int on_download_page_result(const char *json) {
}
return 0;
}

EMSCRIPTEN_KEEPALIVE
int on_subscript_finished(int id, const uint8_t *data) {
lua_State *L = GL;

int extra = push_callback(L, "OnSubFinished");
if (extra >= 0) {
lua_pushlightuserdata(L, (void *)id);
int count = sub_lua_deserialize(L, data);
if (lua_pcall(L, extra + 1 + count, 0, 0) != LUA_OK) {
const char *msg = lua_tostring(L, -1);
fprintf(stderr, "on_subscript_finished error: %s\n", msg);
return 1;
}
return 0;
}
return 1;
}

// Call from main worker
EMSCRIPTEN_KEEPALIVE
int on_subscript_error(int id, const char *message) {
lua_State *L = GL;

int extra = push_callback(L, "OnSubError");
if (extra >= 0) {
lua_pushlightuserdata(L, (void *)id);
lua_pushstring(L, message);
if (lua_pcall(L, extra + 2, 0, 0) != LUA_OK) {
const char *msg = lua_tostring(L, -1);
fprintf(stderr, "on_subscript_error error: %s\n", msg);
return 1;
}
return 0;
}
return 1;
}
Loading

0 comments on commit b108bef

Please sign in to comment.