From c4fbdf55c03a42be9440c73ec0bbf262606f74af Mon Sep 17 00:00:00 2001 From: Pelanyo Kamara Date: Fri, 5 Nov 2021 18:34:38 +0000 Subject: [PATCH] flesh out webrepl base --- .gitignore | 1 + CLI/Repl.cpp | 33 +- CLI/html/index.html | 1011 +++++++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 56 ++- Sources.cmake | 10 + 5 files changed, 1098 insertions(+), 13 deletions(-) create mode 100644 CLI/html/index.html diff --git a/.gitignore b/.gitignore index 0b2422ce..fa11b45b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ ^default.prof* ^fuzz-* ^luau$ +/.vs diff --git a/CLI/Repl.cpp b/CLI/Repl.cpp index 4968d080..2fb7b135 100644 --- a/CLI/Repl.cpp +++ b/CLI/Repl.cpp @@ -249,6 +249,34 @@ static void completeRepl(lua_State* L, const char* editBuffer, std::vector globalState(luaL_newstate(), lua_close); + lua_State* L = globalState.get(); + + setupState(L); + + luaL_sandboxthread(L); + + linenoise::SetCompletionCallback([L](const char* editBuffer, std::vector& completions) { + completeRepl(L, editBuffer, completions); + }); + + std::string error = runCode(L, source); + + if (error.length()) + { + fprintf(stdout, "%s\n", error.c_str()); + } + + return error.c_str(); + } +} +#endif + static void runRepl() { std::unique_ptr globalState(luaL_newstate(), lua_close); @@ -420,6 +448,7 @@ static int assertionHandler(const char* expr, const char* file, int line) return 1; } +#ifndef LUAU_WEB_REPL int main(int argc, char** argv) { Luau::assertHandler() = assertionHandler; @@ -428,6 +457,7 @@ int main(int argc, char** argv) if (strncmp(flag->name, "Luau", 4) == 0) flag->value = true; + executeCode("print'asd'"); if (argc == 1) { runRepl(); @@ -511,5 +541,4 @@ int main(int argc, char** argv) return failed; } } - - +#endif \ No newline at end of file diff --git a/CLI/html/index.html b/CLI/html/index.html new file mode 100644 index 00000000..bf982507 --- /dev/null +++ b/CLI/html/index.html @@ -0,0 +1,1011 @@ + + + + + + Emscripten-Generated Code + + + + + image/svg+xml + + +
+
Downloading...
+ + + Resize canvas + Lock/hide mouse pointer     + + + + + +
+ +
+ + +
+ +
+ + + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index d6598f2a..d8240c00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,10 @@ endif() cmake_minimum_required(VERSION 3.0) project(Luau LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 17) option(LUAU_BUILD_CLI "Build CLI" ON) +option(LUAU_BUILD_WEB_REPL "Build Web CLI" ON) option(LUAU_BUILD_TESTS "Build tests" ON) add_library(Luau.Ast STATIC) @@ -15,19 +17,50 @@ add_library(Luau.Compiler STATIC) add_library(Luau.Analysis STATIC) add_library(Luau.VM STATIC) -if(LUAU_BUILD_CLI) - add_executable(Luau.Repl.CLI) - add_executable(Luau.Analyze.CLI) +if(LUAU_BUILD_WEB_REPL AND EMSCRIPTEN) + # this is a hack, disable cli and tests since we're compiling using emscripten + set(LUAU_BUILD_CLI OFF) + set(LUAU_BUILD_TESTS OFF) - # This also adds target `name` on Linux/macOS and `name.exe` on Windows - set_target_properties(Luau.Repl.CLI PROPERTIES OUTPUT_NAME luau) - set_target_properties(Luau.Analyze.CLI PROPERTIES OUTPUT_NAME luau-analyze) + add_definitions(-DLUAU_WEB_REPL) + + # luau web repl + add_executable(Luau.Repl.Web) + + # configuration below + target_compile_options(Luau.Repl.Web PRIVATE ${LUAU_OPTIONS}) + + target_include_directories(Luau.Repl.Web PRIVATE extern) + target_link_libraries(Luau.Repl.Web PRIVATE Luau.Compiler Luau.VM) + + if(UNIX) + target_link_libraries(Luau.Repl.Web PRIVATE pthread) + endif() + + # declare exported functions to emscripten + target_link_options(Luau.Repl.Web PRIVATE -sEXPORTED_FUNCTIONS=['_executeOnce'] -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap']) + + # copy html directory + add_custom_command(TARGET Luau.Repl.Web PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/CLI/html ${CMAKE_BINARY_DIR}/webrepl) + + # custom output directory for webm + js file + set_target_properties(Luau.Repl.Web PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/webrepl/luau) +else() + if(LUAU_BUILD_CLI) + add_executable(Luau.Repl.CLI) + add_executable(Luau.Analyze.CLI) + + # This also adds target `name` on Linux/macOS and `name.exe` on Windows + set_target_properties(Luau.Repl.CLI PROPERTIES OUTPUT_NAME luau) + set_target_properties(Luau.Analyze.CLI PROPERTIES OUTPUT_NAME luau-analyze) + endif() + + if(LUAU_BUILD_TESTS) + add_executable(Luau.UnitTest) + add_executable(Luau.Conformance) + endif() endif() -if(LUAU_BUILD_TESTS) - add_executable(Luau.UnitTest) - add_executable(Luau.Conformance) -endif() include(Sources.cmake) target_compile_features(Luau.Ast PUBLIC cxx_std_17) @@ -54,7 +87,8 @@ else() list(APPEND LUAU_OPTIONS -Wall) # All warnings list(APPEND LUAU_OPTIONS -Werror) # Warnings are errors - if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + # temporary hot fix (removes need for commenting out line above when compiling with Clang) + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") list(APPEND LUAU_OPTIONS -Wno-unused) # GCC considers variables declared/checked in if() as unused endif() endif() diff --git a/Sources.cmake b/Sources.cmake index 83ed5230..76e33e20 100644 --- a/Sources.cmake +++ b/Sources.cmake @@ -157,6 +157,16 @@ if(TARGET Luau.Repl.CLI) CLI/Repl.cpp) endif() +if (TARGET Luau.Repl.Web) + # Luau.Repl.Web Sources + target_sources(Luau.Repl.Web PRIVATE + CLI/FileUtils.h + CLI/FileUtils.cpp + CLI/Profiler.h + CLI/Profiler.cpp + CLI/Repl.cpp) +endif() + if(TARGET Luau.Analyze.CLI) # Luau.Analyze.CLI Sources target_sources(Luau.Analyze.CLI PRIVATE