Skip to content

Commit 7c55580

Browse files
committed
Solve bugs from cobol loader.
1 parent d227fcd commit 7c55580

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

source/loaders/cob_loader/source/cob_loader_impl.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include <libcob.h>
3636

37+
#include <string>
3738
#include <map>
3839

3940
typedef struct loader_impl_cob_handle_type
@@ -123,7 +124,7 @@ loader_impl_data cob_loader_impl_initialize(loader_impl impl, configuration conf
123124
(void)impl;
124125
(void)config;
125126

126-
loader_copy(host->log);
127+
loader_copy(host);
127128

128129
// Copy environment variables in order to resolve properly the scripts
129130
const char * scripts_path = getenv("LOADER_SCRIPT_PATH");
@@ -156,17 +157,15 @@ int cob_loader_impl_execution_path(loader_impl impl, const loader_naming_path pa
156157

157158
loader_handle cob_loader_impl_load_from_file(loader_impl impl, const loader_naming_path paths[], size_t size)
158159
{
159-
loader_impl_cob_handle cob_handle = static_cast<loader_impl_cob_handle>(malloc(sizeof(struct loader_impl_cob_handle_type)));
160+
loader_impl_cob_handle cob_handle = new loader_impl_cob_handle_type();
160161

161162
(void)impl;
162163

163-
if (cob_handle == NULL)
164+
if (cob_handle == nullptr)
164165
{
165166
return NULL;
166167
}
167168

168-
cob_handle->funcs = std::map<std::string, void *>();
169-
170169
for (size_t path_count = 0; path_count < size; ++path_count)
171170
{
172171
loader_naming_name module_name;
@@ -188,14 +187,14 @@ loader_handle cob_loader_impl_load_from_file(loader_impl impl, const loader_nami
188187
}
189188
else
190189
{
191-
cob_handle->funcs[module_name] = func;
190+
cob_handle->funcs.insert(std::pair<std::string, void *>(std::string(module_name), func));
192191
}
193192
}
194193
}
195194

196195
if (cob_handle->funcs.size() == 0)
197196
{
198-
free(cob_handle);
197+
delete cob_handle;
199198
return NULL;
200199
}
201200

@@ -230,9 +229,11 @@ int cob_loader_impl_clear(loader_impl impl, loader_handle handle)
230229

231230
(void)impl;
232231

233-
if (cob_handle != NULL)
232+
if (cob_handle != nullptr)
234233
{
235-
free(cob_handle);
234+
// TODO: Is there any cob_resolve inverse function?
235+
236+
delete cob_handle;
236237
}
237238

238239
return 0;
@@ -246,9 +247,9 @@ int cob_loader_impl_discover(loader_impl impl, loader_handle handle, context ctx
246247

247248
(void)impl;
248249

249-
for (std::pair<std::string, void *> func : cob_handle->funcs)
250+
for (const auto & func : cob_handle->funcs)
250251
{
251-
function f = function_create(func.first.c_str(), 0, NULL, &function_cob_singleton);
252+
function f = function_create(func.first.c_str(), 0, func.second, &function_cob_singleton);
252253

253254
scope_define(sp, function_name(f), value_create_function(f));
254255
}

source/tests/metacall_cobol_test/source/metacall_cobol_test.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ TEST_F(metacall_cobol_test, DefaultConstructor)
4848
METACALL_STRING, METACALL_STRING
4949
};
5050

51+
static const char tag[] = "cob";
52+
5153
void * ret = NULL;
5254

53-
ASSERT_EQ((int) 0, (int) metacall_load_from_file("cob", cob_scripts, sizeof(cob_scripts) / sizeof(cob_scripts[0]), NULL));
55+
ASSERT_EQ((int) 0, (int) metacall_load_from_file(tag, cob_scripts, sizeof(cob_scripts) / sizeof(cob_scripts[0]), NULL));
5456

5557
ret = metacallt_s("say", hello_string_ids, 2, "hello", "world");
5658

@@ -59,6 +61,16 @@ TEST_F(metacall_cobol_test, DefaultConstructor)
5961
EXPECT_EQ((int) metacall_value_to_int(ret), (int) 0);
6062

6163
metacall_value_destroy(ret);
64+
65+
/* This is a Python script on purpose, in order to test Cobol when it fails */
66+
static const char buffer[] =
67+
"#!/usr/bin/env python3\n"
68+
"def multmem(left: int, right: int) -> int:\n"
69+
"\tresult = left * right;\n"
70+
"\tprint(left, ' * ', right, ' = ', result);\n"
71+
"\treturn result;";
72+
73+
EXPECT_EQ((int) 1, (int) metacall_load_from_memory(tag, buffer, sizeof(buffer), NULL));
6274
}
6375
#endif /* OPTION_BUILD_LOADERS_COB */
6476

0 commit comments

Comments
 (0)