mirror of
https://github.com/luau-lang/luau.git
synced 2025-05-04 10:33:46 +01:00
Split Web source into a separate file as well
This means Emscripten build is now completely decoupled which should make future changes easier.
This commit is contained in:
parent
2665ffcd5e
commit
609f68492a
4 changed files with 111 additions and 45 deletions
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
|
@ -100,5 +100,5 @@ jobs:
|
||||||
- name: make
|
- name: make
|
||||||
run: |
|
run: |
|
||||||
source emsdk/emsdk_env.sh
|
source emsdk/emsdk_env.sh
|
||||||
emcmake cmake . -DCMAKE_BUILD_TYPE=Release
|
emcmake cmake . -DLUAU_BUILD_WEB=ON -DCMAKE_BUILD_TYPE=Release
|
||||||
make -j2 Luau.Web
|
make -j2 Luau.Web
|
||||||
|
|
39
CLI/Repl.cpp
39
CLI/Repl.cpp
|
@ -198,11 +198,6 @@ static std::string runCode(lua_State* L, const std::string& source)
|
||||||
error += "\nstack backtrace:\n";
|
error += "\nstack backtrace:\n";
|
||||||
error += lua_debugtrace(T);
|
error += lua_debugtrace(T);
|
||||||
|
|
||||||
#ifdef __EMSCRIPTEN__
|
|
||||||
// nicer formatting for errors in web repl
|
|
||||||
error = "Error:" + error;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
fprintf(stdout, "%s", error.c_str());
|
fprintf(stdout, "%s", error.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,39 +205,6 @@ static std::string runCode(lua_State* L, const std::string& source)
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __EMSCRIPTEN__
|
|
||||||
extern "C"
|
|
||||||
{
|
|
||||||
const char* executeScript(const char* source)
|
|
||||||
{
|
|
||||||
// setup flags
|
|
||||||
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next)
|
|
||||||
if (strncmp(flag->name, "Luau", 4) == 0)
|
|
||||||
flag->value = true;
|
|
||||||
|
|
||||||
// create new state
|
|
||||||
std::unique_ptr<lua_State, void (*)(lua_State*)> globalState(luaL_newstate(), lua_close);
|
|
||||||
lua_State* L = globalState.get();
|
|
||||||
|
|
||||||
// setup state
|
|
||||||
setupState(L);
|
|
||||||
|
|
||||||
// sandbox thread
|
|
||||||
luaL_sandboxthread(L);
|
|
||||||
|
|
||||||
// static string for caching result (prevents dangling ptr on function exit)
|
|
||||||
static std::string result;
|
|
||||||
|
|
||||||
// run code + collect error
|
|
||||||
result = runCode(L, source);
|
|
||||||
|
|
||||||
return result.empty() ? NULL : result.c_str();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Excluded from emscripten compilation to avoid -Wunused-function errors.
|
|
||||||
#ifndef __EMSCRIPTEN__
|
|
||||||
static void completeIndexer(lua_State* L, const char* editBuffer, size_t start, std::vector<std::string>& completions)
|
static void completeIndexer(lua_State* L, const char* editBuffer, size_t start, std::vector<std::string>& completions)
|
||||||
{
|
{
|
||||||
std::string_view lookup = editBuffer + start;
|
std::string_view lookup = editBuffer + start;
|
||||||
|
@ -564,5 +526,4 @@ int main(int argc, char** argv)
|
||||||
return failed;
|
return failed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
109
CLI/Web.cpp
Normal file
109
CLI/Web.cpp
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
||||||
|
#include "lua.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
#include "luacode.h"
|
||||||
|
|
||||||
|
#include "Luau/Common.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static void setupState(lua_State* L)
|
||||||
|
{
|
||||||
|
luaL_openlibs(L);
|
||||||
|
|
||||||
|
luaL_sandbox(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string runCode(lua_State* L, const std::string& source)
|
||||||
|
{
|
||||||
|
size_t bytecodeSize = 0;
|
||||||
|
char* bytecode = luau_compile(source.data(), source.length(), nullptr, &bytecodeSize);
|
||||||
|
int result = luau_load(L, "=stdin", bytecode, bytecodeSize, 0);
|
||||||
|
free(bytecode);
|
||||||
|
|
||||||
|
if (result != 0)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
const char* msg = lua_tolstring(L, -1, &len);
|
||||||
|
|
||||||
|
std::string error(msg, len);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_State* T = lua_newthread(L);
|
||||||
|
|
||||||
|
lua_pushvalue(L, -2);
|
||||||
|
lua_remove(L, -3);
|
||||||
|
lua_xmove(L, T, 1);
|
||||||
|
|
||||||
|
int status = lua_resume(T, NULL, 0);
|
||||||
|
|
||||||
|
if (status == 0)
|
||||||
|
{
|
||||||
|
int n = lua_gettop(T);
|
||||||
|
|
||||||
|
if (n)
|
||||||
|
{
|
||||||
|
luaL_checkstack(T, LUA_MINSTACK, "too many results to print");
|
||||||
|
lua_getglobal(T, "print");
|
||||||
|
lua_insert(T, 1);
|
||||||
|
lua_pcall(T, n, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::string error;
|
||||||
|
|
||||||
|
if (status == LUA_YIELD)
|
||||||
|
{
|
||||||
|
error = "thread yielded unexpectedly";
|
||||||
|
}
|
||||||
|
else if (const char* str = lua_tostring(T, -1))
|
||||||
|
{
|
||||||
|
error = str;
|
||||||
|
}
|
||||||
|
|
||||||
|
error += "\nstack backtrace:\n";
|
||||||
|
error += lua_debugtrace(T);
|
||||||
|
|
||||||
|
error = "Error:" + error;
|
||||||
|
|
||||||
|
fprintf(stdout, "%s", error.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pop(L, 1);
|
||||||
|
return std::string();
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
const char* executeScript(const char* source)
|
||||||
|
{
|
||||||
|
// setup flags
|
||||||
|
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next)
|
||||||
|
if (strncmp(flag->name, "Luau", 4) == 0)
|
||||||
|
flag->value = true;
|
||||||
|
|
||||||
|
// create new state
|
||||||
|
std::unique_ptr<lua_State, void (*)(lua_State*)> globalState(luaL_newstate(), lua_close);
|
||||||
|
lua_State* L = globalState.get();
|
||||||
|
|
||||||
|
// setup state
|
||||||
|
setupState(L);
|
||||||
|
|
||||||
|
// sandbox thread
|
||||||
|
luaL_sandboxthread(L);
|
||||||
|
|
||||||
|
// static string for caching result (prevents dangling ptr on function exit)
|
||||||
|
static std::string result;
|
||||||
|
|
||||||
|
// run code + collect error
|
||||||
|
result = runCode(L, source);
|
||||||
|
|
||||||
|
return result.empty() ? NULL : result.c_str();
|
||||||
|
}
|
||||||
|
}
|
|
@ -228,9 +228,5 @@ endif()
|
||||||
if(TARGET Luau.Web)
|
if(TARGET Luau.Web)
|
||||||
# Luau.Web Sources
|
# Luau.Web Sources
|
||||||
target_sources(Luau.Web PRIVATE
|
target_sources(Luau.Web PRIVATE
|
||||||
CLI/FileUtils.h
|
CLI/Web.cpp)
|
||||||
CLI/FileUtils.cpp
|
|
||||||
CLI/Profiler.h
|
|
||||||
CLI/Profiler.cpp
|
|
||||||
CLI/Repl.cpp)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
Loading…
Add table
Reference in a new issue