Skip to content

Commit 4f68ac2

Browse files
committed
Solve issues with reflection module.
1 parent e3f98a3 commit 4f68ac2

File tree

3 files changed

+201
-59
lines changed

3 files changed

+201
-59
lines changed

source/reflect/source/reflect_value_type.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -525,15 +525,29 @@ value value_from_double(value v, double d)
525525

526526
value value_from_string(value v, const char *str, size_t length)
527527
{
528-
if (v != NULL && str != NULL && length > 0)
528+
if (v != NULL)
529529
{
530-
size_t current_size = value_size(v);
530+
if (str == NULL || length == 0)
531+
{
532+
return value_from(v, NULL, 1);
533+
}
534+
else
535+
{
536+
size_t current_size = value_type_size(v);
531537

532-
size_t bytes = length + 1;
538+
size_t bytes = length + 1;
533539

534-
size_t size = (bytes <= current_size) ? bytes : current_size;
540+
size_t size = (bytes <= current_size) ? bytes : current_size;
535541

536-
return value_from(v, str, size);
542+
value_from(v, str, size);
543+
544+
if (bytes > current_size)
545+
{
546+
char *str = value_to_string(v);
547+
548+
str[size - 1] = '\0';
549+
}
550+
}
537551
}
538552

539553
return v;
@@ -543,7 +557,7 @@ value value_from_buffer(value v, const void *buffer, size_t size)
543557
{
544558
if (v != NULL && buffer != NULL && size > 0)
545559
{
546-
size_t current_size = value_size(v);
560+
size_t current_size = value_type_size(v);
547561

548562
size_t bytes = sizeof(char) * size;
549563

@@ -557,7 +571,7 @@ value value_from_array(value v, const value *values, size_t size)
557571
{
558572
if (v != NULL && values != NULL && size > 0)
559573
{
560-
size_t current_size = value_size(v);
574+
size_t current_size = value_type_size(v);
561575

562576
size_t bytes = sizeof(const value) * size;
563577

@@ -571,7 +585,7 @@ value value_from_map(value v, const value *tuples, size_t size)
571585
{
572586
if (v != NULL && tuples != NULL && size > 0)
573587
{
574-
size_t current_size = value_size(v);
588+
size_t current_size = value_type_size(v);
575589

576590
size_t bytes = sizeof(const value) * size;
577591

source/scripts/c/compiled/source/compiled.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,34 @@ void free_data(data_ptr_t ptr)
7979

8080
// TODO: When calling from NodeJS it does not work,
8181
// NodeJS emmits double as a call, and this expects long, it needs a casting
82-
/*
8382
void modify_int_ptr(long *l)
8483
{
8584
printf("l %p\n", l);
8685
printf("value %d\n", *l);
8786
assert(*l == 324444L);
8887
*l = 111L;
8988
}
90-
*/
9189

92-
void modify_int_ptr(double *d)
90+
void modify_double_ptr(double *d)
9391
{
9492
printf("d %p\n", d);
9593
printf("value %f\n", *d);
9694
assert(*d == 324444.0);
9795
*d = 111.0;
9896
}
97+
98+
void modify_str_ptr(char **str_ptr)
99+
{
100+
static char new_str[] = "yeet";
101+
printf("(C) pointer %p\n", str_ptr);
102+
printf("(C) string %p\n", (*str_ptr));
103+
printf("(C) string value %s\n", *str_ptr);
104+
fflush(stdout);
105+
assert(strcmp("asd", *str_ptr) == 0);
106+
*str_ptr = new_str;
107+
printf("(C) pointer %p\n", str_ptr);
108+
printf("(C) string %p\n", (*str_ptr));
109+
printf("(C) string value %s\n", *str_ptr);
110+
fflush(stdout);
111+
assert(strcmp("yeet", *str_ptr) == 0);
112+
}

source/tests/metacall_c_test/source/metacall_c_test.cpp

Lines changed: 162 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -41,96 +41,210 @@ void *sum_callback(size_t argc, void *args[], void *data)
4141
return metacall_value_create_int(result);
4242
}
4343

44+
void *test_string_reference(size_t argc, void *args[], void *data)
45+
{
46+
printf("ptr %p\n", args[0]);
47+
fflush(stdout);
48+
49+
void *string_value = metacall_value_to_ptr(args[0]);
50+
51+
printf("string ptr %p\n", string_value);
52+
printf("type id %s\n", metacall_value_type_name(string_value));
53+
fflush(stdout);
54+
55+
char *str = metacall_value_to_string(string_value);
56+
57+
(void)argc;
58+
(void)data;
59+
60+
printf("native string %s\n", str);
61+
62+
EXPECT_STREQ("asd", str);
63+
64+
static const char yeet[] = "yeet";
65+
66+
metacall_value_from_string(string_value, yeet, sizeof(yeet) - 1);
67+
68+
printf("type id %s\n", metacall_value_type_name(string_value));
69+
printf("native string %s\n", str);
70+
fflush(stdout);
71+
72+
return metacall_value_create_null();
73+
}
74+
4475
TEST_F(metacall_c_test, DefaultConstructor)
4576
{
4677
ASSERT_EQ((int)0, (int)metacall_initialize());
4778

79+
void *ret = NULL;
80+
4881
/* File */
49-
const char *c_scripts[] = {
50-
"compiled.c"
51-
};
82+
{
83+
const char *c_scripts[] = {
84+
"compiled.c"
85+
};
5286

53-
EXPECT_EQ((int)0, (int)metacall_load_from_file("c", c_scripts, sizeof(c_scripts) / sizeof(c_scripts[0]), NULL));
87+
EXPECT_EQ((int)0, (int)metacall_load_from_file("c", c_scripts, sizeof(c_scripts) / sizeof(c_scripts[0]), NULL));
5488

55-
void *ret = metacall("compiled_sum", 3, 4);
89+
ret = metacall("compiled_sum", 3, 4);
5690

57-
EXPECT_NE((void *)NULL, (void *)ret);
91+
EXPECT_NE((void *)NULL, (void *)ret);
5892

59-
EXPECT_EQ((long)metacall_value_to_long(ret), (long)7);
93+
EXPECT_EQ((long)metacall_value_to_long(ret), (long)7);
6094

61-
metacall_value_destroy(ret);
95+
metacall_value_destroy(ret);
96+
}
6297

6398
/* File with dependencies */
64-
const char *c_dep_scripts[] = {
65-
"ffi.c",
66-
"ffi.ld"
67-
};
99+
{
100+
const char *c_dep_scripts[] = {
101+
"ffi.c",
102+
"ffi.ld"
103+
};
68104

69-
/* Set dependency paths */
70-
EXPECT_EQ((int)0, (int)metacall_execution_path("c", LIBFFI_INCLUDE_DIR));
71-
EXPECT_EQ((int)0, (int)metacall_execution_path("c", LIBFFI_LIBRARY));
105+
/* Set dependency paths */
106+
EXPECT_EQ((int)0, (int)metacall_execution_path("c", LIBFFI_INCLUDE_DIR));
107+
EXPECT_EQ((int)0, (int)metacall_execution_path("c", LIBFFI_LIBRARY));
72108

73-
EXPECT_EQ((int)0, (int)metacall_load_from_file("c", c_dep_scripts, sizeof(c_dep_scripts) / sizeof(c_dep_scripts[0]), NULL));
109+
EXPECT_EQ((int)0, (int)metacall_load_from_file("c", c_dep_scripts, sizeof(c_dep_scripts) / sizeof(c_dep_scripts[0]), NULL));
74110

75-
ret = metacall("call_fp_address");
111+
ret = metacall("call_fp_address");
76112

77-
EXPECT_NE((void *)NULL, (void *)ret);
113+
EXPECT_NE((void *)NULL, (void *)ret);
78114

79-
EXPECT_EQ((enum metacall_value_id)metacall_value_id(ret), (enum metacall_value_id)METACALL_PTR);
115+
EXPECT_EQ((enum metacall_value_id)metacall_value_id(ret), (enum metacall_value_id)METACALL_PTR);
80116

81-
EXPECT_NE((void *)metacall_value_to_ptr(ret), (void *)NULL);
117+
EXPECT_NE((void *)metacall_value_to_ptr(ret), (void *)NULL);
82118

83-
metacall_value_destroy(ret);
119+
metacall_value_destroy(ret);
84120

85-
ret = metacall("int_type_renaming");
121+
ret = metacall("int_type_renaming");
86122

87-
EXPECT_NE((void *)NULL, (void *)ret);
123+
EXPECT_NE((void *)NULL, (void *)ret);
88124

89-
EXPECT_EQ((enum metacall_value_id)metacall_value_id(ret), (enum metacall_value_id)METACALL_INT);
125+
EXPECT_EQ((enum metacall_value_id)metacall_value_id(ret), (enum metacall_value_id)METACALL_INT);
90126

91-
EXPECT_EQ((int)metacall_value_to_int(ret), (int)345);
127+
EXPECT_EQ((int)metacall_value_to_int(ret), (int)345);
92128

93-
metacall_value_destroy(ret);
129+
metacall_value_destroy(ret);
130+
}
94131

95132
/* Native register */
96-
metacall_register("sum_callback", sum_callback, NULL, METACALL_INT, 2, METACALL_INT, METACALL_INT);
133+
{
134+
metacall_register("sum_callback", sum_callback, NULL, METACALL_INT, 2, METACALL_INT, METACALL_INT);
97135

98-
void *func = metacall_function("sum_callback");
136+
void *func = metacall_function("sum_callback");
99137

100-
EXPECT_NE((void *)NULL, (void *)func);
138+
EXPECT_NE((void *)NULL, (void *)func);
101139

102-
void *args[] = {
103-
metacall_value_create_function(func)
104-
};
140+
void *args[] = {
141+
metacall_value_create_function(func)
142+
};
105143

106-
ret = metacallv_s("c_callback", args, 1);
144+
ret = metacallv_s("c_callback", args, 1);
107145

108-
EXPECT_NE((void *)NULL, (void *)ret);
146+
EXPECT_NE((void *)NULL, (void *)ret);
109147

110-
EXPECT_EQ((enum metacall_value_id)metacall_value_id(ret), (enum metacall_value_id)METACALL_INT);
148+
EXPECT_EQ((enum metacall_value_id)metacall_value_id(ret), (enum metacall_value_id)METACALL_INT);
111149

112-
EXPECT_EQ((int)metacall_value_to_int(ret), (int)7);
150+
EXPECT_EQ((int)metacall_value_to_int(ret), (int)7);
113151

114-
metacall_value_destroy(ret);
152+
metacall_value_destroy(ret);
115153

116-
metacall_value_destroy(args[0]);
154+
metacall_value_destroy(args[0]);
155+
}
117156

118157
/* Memory */
119-
// TODO
120-
// const char c_buffer[] = {
121-
// "int compiled_mult(int a, int b) { return a * b; }"
122-
// };
158+
{
159+
// TODO
160+
// const char c_buffer[] = {
161+
// "int compiled_mult(int a, int b) { return a * b; }"
162+
// };
123163

124-
// EXPECT_EQ((int)0, (int)metacall_load_from_memory("c", c_buffer, sizeof(c_buffer), NULL));
164+
// EXPECT_EQ((int)0, (int)metacall_load_from_memory("c", c_buffer, sizeof(c_buffer), NULL));
125165

126-
// TODO
127-
// void *ret = metacall("compiled_mult", 3, 4);
166+
// TODO
167+
// void *ret = metacall("compiled_mult", 3, 4);
128168

129-
// EXPECT_NE((void *)NULL, (void *)ret);
169+
// EXPECT_NE((void *)NULL, (void *)ret);
130170

131-
// EXPECT_EQ((int)metacall_value_to_int(ret), (int)0);
171+
// EXPECT_EQ((int)metacall_value_to_int(ret), (int)0);
132172

133-
// metacall_value_destroy(ret);
173+
// metacall_value_destroy(ret);
174+
}
175+
176+
/* References (native) */
177+
{
178+
static const char str[] = "asd";
179+
void *str_value = metacall_value_create_string(str, sizeof(str) - 1);
180+
void *str_value_ref = metacall_value_reference(str_value);
181+
182+
printf("ptr %p\n", str_value_ref);
183+
printf("string %p\n", str_value);
184+
printf("string str %s\n", metacall_value_to_string(str_value));
185+
fflush(stdout);
186+
187+
{
188+
void *new_str_value = metacall_value_to_ptr(str_value_ref);
189+
char *new_str = metacall_value_to_string(new_str_value);
190+
191+
EXPECT_STREQ("asd", new_str);
192+
}
193+
194+
void *args[] = {
195+
str_value_ref
196+
};
197+
198+
metacall_register("test_string_reference", test_string_reference, NULL, METACALL_NULL, 1, METACALL_PTR);
199+
200+
ret = metacallv_s("test_string_reference", args, 1);
201+
202+
EXPECT_NE((void *)NULL, (void *)ret);
203+
204+
EXPECT_EQ((enum metacall_value_id)metacall_value_id(ret), (enum metacall_value_id)METACALL_NULL);
205+
206+
metacall_value_destroy(ret);
207+
208+
printf("type id %s\n", metacall_value_type_name(str_value));
209+
fflush(stdout);
210+
211+
// It chops the string because it has a fixed size from 'asd'
212+
EXPECT_STREQ(metacall_value_to_string(str_value), "yee");
213+
214+
metacall_value_destroy(str_value);
215+
metacall_value_destroy(str_value_ref);
216+
}
217+
218+
/* References (c) */
219+
{
220+
static const char str[] = "asd";
221+
void *str_value = metacall_value_create_string(str, sizeof(str) - 1);
222+
void *str_value_ref = metacall_value_reference(str_value);
223+
224+
printf("(R) ptr %p\n", str_value_ref);
225+
printf("(R) string ptr %p\n", str_value);
226+
printf("(R) string str %s\n", metacall_value_to_string(str_value));
227+
fflush(stdout);
228+
229+
void *args[] = {
230+
str_value_ref
231+
};
232+
233+
ret = metacallv_s("modify_str_ptr", args, 1);
234+
235+
EXPECT_NE((void *)NULL, (void *)ret);
236+
237+
EXPECT_EQ((enum metacall_value_id)metacall_value_id(ret), (enum metacall_value_id)METACALL_NULL);
238+
239+
metacall_value_destroy(ret);
240+
241+
char *str_value_deref = static_cast<char *>(metacall_value_dereference(str_value_ref));
242+
243+
EXPECT_STREQ(str_value_deref, "yeet");
244+
245+
metacall_value_destroy(str_value);
246+
metacall_value_destroy(str_value_ref);
247+
}
134248

135249
/* Print inspect information */
136250
{

0 commit comments

Comments
 (0)