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

Add macros for static class method calls #190

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion program/cpp/api/syscalls_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" __attribute__((noreturn)) void fast_exit();
return operator() (#name, std::forward<Args>(args)...); \
}

#define METHOD(Type, name, ...) \
#define METHOD(Type, name) \
template <typename... Args> \
inline Type name(Args&&... args) { \
if constexpr (std::is_same_v<Type, void>) { \
Expand All @@ -36,3 +36,7 @@ extern "C" __attribute__((noreturn)) void fast_exit();
return operator() (#name, std::forward<Args>(args)...); \
} \
}

// Helpers for static method calls
#define SMETHOD(Type, name) METHOD(Type, name)
#define SVMETHOD(name) VMETHOD(name)
14 changes: 11 additions & 3 deletions src/sandbox_generated_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ static String emit_class(ClassDBSingleton *class_db, const HashSet<String> &cpp_
// eg. if it's a Node, we need to inherit from Node.
// If it's a Node2D, we need to inherit from Node2D. etc.
api += " using " + parent_name + "::" + parent_name + ";\n";
// Add a constructor from Object
api += " constexpr " + class_name + "(const Object &obj) : " + parent_name + "(obj.address()) {}\n";
// We just need the names of the properties and methods.
TypedArray<Dictionary> properties = class_db->class_get_property_list(class_name, true);
for (int j = 0; j < properties.size(); j++) {
Expand Down Expand Up @@ -167,9 +169,15 @@ static String emit_class(ClassDBSingleton *class_db, const HashSet<String> &cpp_
}

const int flags = int(method["flags"]);
if (false && (flags & METHOD_FLAG_STATIC)) {
// TODO: Implement static calls.
continue;
if (flags & METHOD_FLAG_STATIC) {
// Static class methods.
if (type == 0) {
// Variant return type.
api += String(" SVMETHOD(") + method_name + ");\n";
} else {
// Typed return type.
api += String(" SMETHOD(") + cpp_compatible_variant_type(type) + ", " + method_name + ");\n";
}
} else {
// Variant::NIL is a special case, as it's a void return type.
// Sometimes it's a Variant return type, other times it's a void return type.
Expand Down