Skip to content

Commit ecf52d5

Browse files
committed
Solve issues on ruby port and loader.
1 parent 19ee89f commit ecf52d5

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

source/loaders/rb_loader/source/rb_loader_impl.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,17 +1252,13 @@ loader_handle rb_loader_impl_load_from_file(loader_impl impl, const loader_path
12521252
goto load_error;
12531253
}
12541254

1255-
if (result == Qnil)
1255+
/* Define module name */
12561256
{
12571257
loader_path name;
12581258
size_t size = portability_path_get_name(paths[0], strnlen(paths[0], LOADER_PATH_SIZE), name, LOADER_PATH_SIZE);
12591259

12601260
module_name = rb_str_new(name, size);
12611261
}
1262-
else
1263-
{
1264-
module_name = rb_funcall(result, rb_intern("name"), 0);
1265-
}
12661262

12671263
rb_module = rb_loader_impl_create_module(module_name, result, module_data, result);
12681264

source/loaders/rb_loader/source/rb_loader_port.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ VALUE rb_loader_port_metacall(int argc, VALUE *argv, VALUE self)
175175
/* Convert the arguments into MetaCall values */
176176
for (iterator = 0; iterator < args_size; ++iterator)
177177
{
178-
(void)rb_type_deserialize(rb_loader_impl, argv[iterator], &args[iterator]);
178+
(void)rb_type_deserialize(rb_loader_impl, argv[iterator + 1], &args[iterator]);
179179
}
180180

181181
/* Execute the call */
@@ -195,6 +195,43 @@ VALUE rb_loader_port_metacall(int argc, VALUE *argv, VALUE self)
195195
return rb_type_serialize(result);
196196
}
197197

198+
VALUE rb_loader_port_inspect(VALUE self)
199+
{
200+
VALUE result;
201+
size_t size = 0;
202+
char *result_str = NULL, *inspect_str = NULL;
203+
struct metacall_allocator_std_type std_ctx = { &malloc, &realloc, &free };
204+
205+
/* Create the allocator */
206+
void *allocator = metacall_allocator_create(METACALL_ALLOCATOR_STD, (void *)&std_ctx);
207+
208+
(void)self;
209+
210+
/* Retrieve inspect data */
211+
result_str = inspect_str = metacall_inspect(&size, allocator);
212+
213+
if (inspect_str == NULL || size == 0)
214+
{
215+
static const char empty[] = "{}";
216+
217+
result_str = (char *)empty;
218+
size = sizeof(empty);
219+
220+
rb_raise(rb_eArgError, "Inspect returned an invalid size or string");
221+
}
222+
223+
result = rb_str_new(result_str, size - 1);
224+
225+
if (inspect_str != NULL && size > 0)
226+
{
227+
metacall_allocator_free(allocator, inspect_str);
228+
}
229+
230+
metacall_allocator_destroy(allocator);
231+
232+
return result;
233+
}
234+
198235
int rb_loader_port_initialize(loader_impl impl)
199236
{
200237
VALUE rb_loader_port;
@@ -213,6 +250,7 @@ int rb_loader_port_initialize(loader_impl impl)
213250
rb_define_module_function(rb_loader_port, "metacall_load_from_file", rb_loader_port_load_from_file, 2);
214251
rb_define_module_function(rb_loader_port, "metacall_load_from_memory", rb_loader_port_load_from_memory, 2);
215252
rb_define_module_function(rb_loader_port, "metacall", rb_loader_port_metacall, -1);
253+
rb_define_module_function(rb_loader_port, "metacall_inspect", rb_loader_port_inspect, 0);
216254

217255
rb_loader_impl = impl;
218256

source/ports/rb_port/package/lib/metacall.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,25 @@ def metacall_module_load
105105
end
106106
end
107107

108+
# Initialize the MetaCall Ruby Port
109+
metacall_module_load
110+
108111
public
109112

110113
def metacall_load_from_file(tag, paths)
111-
metacall_module_load
112-
113114
MetaCallRbLoaderPort.metacall_load_from_file(tag, paths)
114115
end
115116

116117
def metacall_load_from_memory(tag, script)
117-
metacall_module_load
118-
119118
MetaCallRbLoaderPort.metacall_load_from_memory(tag, script)
120119
end
121120

122121
def metacall(function_name, *args)
123-
metacall_module_load
124-
125122
MetaCallRbLoaderPort.metacall(function_name, *args)
126123
end
127124

125+
def metacall_inspect()
126+
MetaCallRbLoaderPort.metacall_inspect()
127+
end
128+
128129
end

source/ports/rb_port/test/run.rb

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ class RbPortTest < Test::Unit::TestCase
77

88
# MetaCall (Python from memory)
99
def test_python_memory
10-
script = '#!/usr/bin/env python3\n' \
11-
'def inline_multiply_mem(left: int, right: int) -> int:\n' \
12-
' return left * right;\n'
13-
'def inline_hello(left: int, right: int) -> int:\n' \
14-
' print(\'Helloo\', left, \' \', right);\n'
15-
' return;\n'
10+
script = <<~SCRIPT
11+
def inline_multiply_mem(left: int, right: int) -> int:
12+
return left * right
13+
def inline_hello(left: int, right: int) -> int:
14+
print('Helloo', left, ' ', right)
15+
return left * right
16+
SCRIPT
1617

17-
assert_equal(0, MetaCall.metacall_load_from_memory('py', script))
18+
assert_equal(0, MetaCall::metacall_load_from_memory('py', script))
1819

19-
# TODO
20-
# assert_equal(4, MetaCall::metacall('inline_multiply_mem', 2, 2))
20+
assert_equal(4, MetaCall::metacall('inline_multiply_mem', 2, 2))
2121

22-
assert_equal(nil, MetaCall::metacall('inline_hello', 10, 20))
22+
assert_equal(200, MetaCall::metacall('inline_hello', 10, 20))
2323
end
2424

2525
# MetaCall (Python)
@@ -28,8 +28,7 @@ def test_python
2828

2929
assert_equal(nil, MetaCall::metacall('hello'))
3030

31-
# TODO
32-
# assert_equal(35, MetaCall::metacall('multiply', 5, 7))
31+
assert_equal(35, MetaCall::metacall('multiply', 5, 7))
3332
end
3433

3534
# MetaCall (Ruby)
@@ -38,11 +37,9 @@ def test_ruby
3837

3938
assert_equal(nil, MetaCall::metacall('say_null'))
4039

41-
# TODO
42-
# assert_equal(12, MetaCall::metacall('say_multiply', 3, 4))
40+
assert_equal(12, MetaCall::metacall('say_multiply', 3, 4))
4341

44-
# TODO
45-
# assert_equal('Hello world!', MetaCall::metacall('say_hello', 'world'))
42+
assert_equal('Hello world!', MetaCall::metacall('say_hello', 'world'))
4643
end
4744

4845
end

0 commit comments

Comments
 (0)