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

Allow passing enum-structs to natives. #987

Merged
merged 1 commit into from
Oct 26, 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
2 changes: 1 addition & 1 deletion compiler/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ static const char* errmsg[] = {
/*132*/ "cannot coerce non-object type %s to object type %s\n",
/*133*/ "cannot coerce unrelated object types %s and %s\n",
/*134*/ "type mismatch (expected \"%s\", got \"%s\")\n",
/*135*/ "cannot use enum struct type \"%s\" in natives\n",
/*135*/ "unused135\n",
/*136*/ "reference is redundant, enum struct types are array-like\n",
/*137*/ "cannot mix reference and array types\n",
/*138*/ "const was specified twice\n",
Expand Down
4 changes: 0 additions & 4 deletions compiler/name-resolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,10 +821,6 @@ FunctionDecl::BindArgs(SemaContext& sc)
}

Type* type = typeinfo.type;
if (type->isEnumStruct()) {
if (is_native_)
report(var->pos(), 135) << type;
}

/* Stack layout:
* base + 0*sizeof(cell) == previous "base"
Expand Down
10 changes: 0 additions & 10 deletions tests/compile-only/fail-enum-struct-in-native.sp

This file was deleted.

1 change: 0 additions & 1 deletion tests/compile-only/fail-enum-struct-in-native.txt

This file was deleted.

2 changes: 2 additions & 0 deletions tests/enum-structs/native-access.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x: 5
y: 10
8 changes: 8 additions & 0 deletions tests/enum-structs/native-access.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <shell>

public void main() {
TestStruct ts;
ts.x = 5;
ts.y = 10;
print_test_struct(ts);
}
7 changes: 7 additions & 0 deletions tests/shell.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include <float>
#include <handles>

enum struct TestStruct {
int x;
int y;
}

native void printnum(int n);
native void writenum(int n);
native void printfloat(float n);
Expand Down Expand Up @@ -45,3 +50,5 @@ native void copy_2d_array_to_callback(const int[] flat_array, int length, int st
Array2dCallback callback);

native void assert_eq(any a1, any a2);

native void print_test_struct(TestStruct x);
20 changes: 20 additions & 0 deletions vm/shell/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,25 @@ static cell_t Copy2dArrayToCallback(IPluginContext* cx, const cell_t* params)
return 0;
}

#pragma pack(push, 1)
struct TestStruct {
cell_t x;
cell_t y;
};
#pragma pack(pop)

static cell_t PrintTestStruct(IPluginContext* cx, const cell_t* params) {
TestStruct* ts;

int err;
if ((err = cx->LocalToPhysAddr(params[1], reinterpret_cast<cell_t**>(&ts))) != SP_ERROR_NONE)
return cx->ThrowNativeErrorEx(err, "Could not read argument 1");

printf("x: %d\n", ts->x);
printf("y: %d\n", ts->y);
return 0;
}

class DynamicNative : public INativeCallback
{
public:
Expand Down Expand Up @@ -366,6 +385,7 @@ static int Execute(const char* file)
BindNative(rt, "call_with_string", CallWithString);
BindNative(rt, "assert_eq", AssertEq);
BindNative(rt, "printf", Printf);
BindNative(rt, "print_test_struct", PrintTestStruct);

IPluginFunction* fun = rt->GetFunctionByName("main");
if (!fun)
Expand Down
Loading