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

Move plugins to Sol. #5622

Merged
merged 88 commits into from
Oct 20, 2024
Merged

Move plugins to Sol. #5622

merged 88 commits into from
Oct 20, 2024

Conversation

Mm2PL
Copy link
Collaborator

@Mm2PL Mm2PL commented Oct 3, 2024

This PR moves the existing plugin implementation to sol2 from using the raw Lua API.

TODO:

  • Channel
  • all the enums
  • commands
  • HTTP
  • IO wrapper

github-actions[bot]

This comment was marked as spam.

CMakeLists.txt Outdated
@@ -212,6 +212,7 @@ endif()
if (CHATTERINO_PLUGINS)
set(LUA_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/lib/lua/src")
add_subdirectory(lib/lua)
add_definitions(-DSOL_ALL_SAFETIES_ON=1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done in FindSol2.cmake already. Afaik, these INTERFACE definitions propagate to all targets that (transitively) use them (but I haven't checked).

github-actions[bot]

This comment was marked as spam.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 25 out of 34. Check the log or trigger a new build to see more.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 25 out of 32. Check the log or trigger a new build to see more.

@Mm2PL Mm2PL force-pushed the chore/move-everything-to-sol branch from d2a86e4 to df915d4 Compare October 7, 2024 22:13
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 25 out of 33. Check the log or trigger a new build to see more.

@Mm2PL Mm2PL force-pushed the chore/move-everything-to-sol branch from df915d4 to 19cb8db Compare October 7, 2024 22:31
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 25 out of 33. Check the log or trigger a new build to see more.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 25 out of 36. Check the log or trigger a new build to see more.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 25 out of 36. Check the log or trigger a new build to see more.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 25 out of 37. Check the log or trigger a new build to see more.

Mm2PL and others added 18 commits October 9, 2024 23:12
Co-authored-by: Nerixyz <nerixdev@outlook.de>
Co-authored-by: Nerixyz <nerixdev@outlook.de>
Co-authored-by: Nerixyz <nerixdev@outlook.de>
Co-authored-by: Nerixyz <nerixdev@outlook.de>
Co-authored-by: Nerixyz <nerixdev@outlook.de>
Co-authored-by: Nerixyz <nerixdev@outlook.de>
Co-authored-by: Nerixyz <nerixdev@outlook.de>
Co-authored-by: Nerixyz <nerixdev@outlook.de>
Co-authored-by: Nerixyz <nerixdev@outlook.de>
For #include reasons
including tab completion
@Mm2PL Mm2PL requested review from pajlada and Nerixyz October 15, 2024 15:01
)lua"));
}

TEST_F(PluginTest, requireNoData)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no test for a successful require (could be in a followup).

Comment on lines +155 to +160
// If you're modifying this PLEASE verify it works, Sol is very annoying about serialization
// - Mm2PL
sol::state_view lua(s);
auto load = lua.registry()["real_load"];
sol::protected_function_result ret = load(data, "=(load)", "t");
return ret;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have this comment, we should test it. Maybe a test that's only in debug mode?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment on the function for this

Comment on lines +247 to +253
vtypes = []
for variant in variants:
vtype = f'{name}.{variant}'
vtypes.append(vtype)
out.write(f'---@alias {vtype} "{vtype}"\n')

out.write(f"---@alias {name} {'|'.join(vtypes)}\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this done? ---@alias c2.LogLevel.Debug "c2.LogLevel.Debug" seems wrong. It defines a type c2.LogLevel.Debug to be the literal string c2.LogLevel.Debug.

Copy link
Collaborator Author

@Mm2PL Mm2PL Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is so the language server doesn't go number == number and instead will show the right overload.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work:

Seems like what we really want is ---@enum <name>.

Diff
diff --git a/scripts/make_luals_meta.py b/scripts/make_luals_meta.py
index b1420e780..cb7b3cdf4 100644
--- a/scripts/make_luals_meta.py
+++ b/scripts/make_luals_meta.py
@@ -242,25 +242,16 @@ def read_file(path: Path, out: TextIOWrapper):
                 )
             name = header[0].split(" ", 1)[1]
             printmsg(path, reader.line_no(), f"enum {name}")
-            variants = reader.read_enum_variants()
-
-            vtypes = []
-            for variant in variants:
-                vtype = f'{name}.{variant}'
-                vtypes.append(vtype)
-                out.write(f'---@alias {vtype} "{vtype}"\n')
-
-            out.write(f"---@alias {name} {'|'.join(vtypes)}\n")
             if header_comment:
                 out.write(f"--- {header_comment}\n")
-            out.write("---@type { ")
+            out.write(f"---@enum {name}\n")
+            out.write(f"{name} = {{\n")
             out.write(
-                ", ".join(
-                    [f"{variant}: {typ}" for variant, typ in zip(variants,vtypes)]
+                "\n".join(
+                    [f"    {variant} = 0," for variant in reader.read_enum_variants()]
                 )
             )
-            out.write(" }\n")
-            out.write(f"{name} = {{}}\n\n")
+            out.write("\n}\n\n")
             continue
 
         # class

Regardless, this isn't related to the SOL migration.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch you've sent makes luals fail to typecheck a simple example:

c2.register_callback(c2.EventType.CompletionRequested, function(ev)end)
--[[
Cannot assign `integer` to parameter `c2.EventType.CompletionRequested`.
- `integer` cannot match `c2.EventType.CompletionRequested`
- Type `integer` cannot match `c2.EventType.CompletionRequested`
- Type `number` cannot match `c2.EventType.CompletionRequested`",
]]

For me it also suggests using raw enum values which are all zero which feels even worse than getting strings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I thought we used numbers for enums. Generating strings here shouldn't be too hard, though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually currently can pick whichever representation suits us, the documentation states that the values may be changed any time and accessing through the enum table is the recommended option.

/**
* @brief Dumps the Lua stack into qCDebug(chatterinoLua)
*
* @param tag is a string to let you know which dump is which when browsing logs
*/
void stackDump(lua_State *L, const QString &tag);

// This is for calling stackDump out of gdb as it's not easy to create a QString there
const QString GDB_DUMMY = "GDB_DUMMY";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be in the source file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be in a header so it's accessible. It could have a default value assigned in a source file although that might have linking issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a null-string? That would be constexpr + all null. I guess we can't have it be inline because we don't use it.

One should be able to debug the lua stack without stackDump though. For GDB there's https://github.com/mkottman/lua-gdb-helper (might be a bit outdated). And for VS there's https://github.com/huoyang11/lua_debug_natvis (that one is a bit outdated and incompatible with qt, but not too hard to fix). There's probably something for LLDB as well.

@Nerixyz
Copy link
Contributor

Nerixyz commented Oct 17, 2024

Before I forget this, plugins are not enabled in the macOS and Windows tests.

@Nerixyz
Copy link
Contributor

Nerixyz commented Oct 19, 2024

Quick patch to be able to compile with clang-cl:

diff --git a/lib/lua/CMakeLists.txt b/lib/lua/CMakeLists.txt
index 9d7aaef90..70a4033b5 100644
--- a/lib/lua/CMakeLists.txt
+++ b/lib/lua/CMakeLists.txt
@@ -52,5 +52,8 @@ set_target_properties(${liblua} PROPERTIES
     CXX_STANDARD 98
     CXX_EXTENSIONS TRUE
 )
-target_compile_options(lua PRIVATE -w)  # this makes clang shut up about c-as-c++
+target_compile_options(lua PRIVATE 
+    -w # this makes clang shut up about c-as-c++
+    $<$<AND:$<BOOL:${MSVC}>,$<CXX_COMPILER_ID:Clang>>:/EHsc> # enable exceptions in clang-cl
+)
 set_source_files_properties(${LUA_SRC} PROPERTIES LANGUAGE CXX)

pajlada and others added 2 commits October 19, 2024 14:06
Co-authored-by: Nerixyz <nerixdev@outlook.de>
@Nerixyz
Copy link
Contributor

Nerixyz commented Oct 19, 2024

  1. #warning is a non-standard extension until C++ 23
  2. Not sure why windows with 6.7.1 fails there exactly (maybe too many callbacks get queued), but let's make sure we only call done once.
Diff
diff --git a/tests/src/Plugins.cpp b/tests/src/Plugins.cpp
index e7951d4d0..156b442d5 100644
--- a/tests/src/Plugins.cpp
+++ b/tests/src/Plugins.cpp
@@ -375,8 +375,10 @@ TEST_F(PluginTest, testHttp)
         {"/status/200", true, false, 200, "200"},
         {"/delay/2", false, true, 0, "TimeoutError"},
 #    ifdef CHATTERINO_TEST_USE_PUBLIC_HTTPBIN
-#        warning \
-            "Cannot test data returned from httpbin. The public one does not return data verbatim."
+#        if __cplusplus >= 202302L || defined(Q_CC_GNU) || defined(Q_CC_CLANG)
+#            warning \
+                "Cannot test data returned from httpbin. The public one does not return data verbatim."
+#        endif
 #    else
         {"/post", true, false, 200, "200", NetworkRequestType::Post,
          "Example data"},
@@ -543,13 +545,15 @@ TEST_F(PluginTest, testTimerRec)
 
     sol::protected_function fn = lua->script(R"lua(
         local i = 0
+        local called_done = false
         f = function()
             i = i + 1
             c2.log(c2.LogLevel.Debug, "cb", i)
             if i < 1024 then
                 c2.later(f, 1)
-            else
+            elseif not called_done then
                 done()
+                called_done = true
             end
         end
         c2.later(f, 1)

@pajlada pajlada merged commit 352a4ec into master Oct 20, 2024
18 checks passed
@pajlada pajlada deleted the chore/move-everything-to-sol branch October 20, 2024 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip-clang-tidy Skips clang-tidy workflow. Useful if you have way too many comments.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants