Skip to content

Commit

Permalink
Solve bugs from cobol loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Feb 12, 2021
1 parent d227fcd commit 7c55580
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
23 changes: 12 additions & 11 deletions source/loaders/cob_loader/source/cob_loader_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <libcob.h>

#include <string>
#include <map>

typedef struct loader_impl_cob_handle_type
Expand Down Expand Up @@ -123,7 +124,7 @@ loader_impl_data cob_loader_impl_initialize(loader_impl impl, configuration conf
(void)impl;
(void)config;

loader_copy(host->log);
loader_copy(host);

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

loader_handle cob_loader_impl_load_from_file(loader_impl impl, const loader_naming_path paths[], size_t size)
{
loader_impl_cob_handle cob_handle = static_cast<loader_impl_cob_handle>(malloc(sizeof(struct loader_impl_cob_handle_type)));
loader_impl_cob_handle cob_handle = new loader_impl_cob_handle_type();

(void)impl;

if (cob_handle == NULL)
if (cob_handle == nullptr)
{
return NULL;
}

cob_handle->funcs = std::map<std::string, void *>();

for (size_t path_count = 0; path_count < size; ++path_count)
{
loader_naming_name module_name;
Expand All @@ -188,14 +187,14 @@ loader_handle cob_loader_impl_load_from_file(loader_impl impl, const loader_nami
}
else
{
cob_handle->funcs[module_name] = func;
cob_handle->funcs.insert(std::pair<std::string, void *>(std::string(module_name), func));
}
}
}

if (cob_handle->funcs.size() == 0)
{
free(cob_handle);
delete cob_handle;
return NULL;
}

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

(void)impl;

if (cob_handle != NULL)
if (cob_handle != nullptr)
{
free(cob_handle);
// TODO: Is there any cob_resolve inverse function?

delete cob_handle;
}

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

(void)impl;

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

scope_define(sp, function_name(f), value_create_function(f));
}
Expand Down
14 changes: 13 additions & 1 deletion source/tests/metacall_cobol_test/source/metacall_cobol_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ TEST_F(metacall_cobol_test, DefaultConstructor)
METACALL_STRING, METACALL_STRING
};

static const char tag[] = "cob";

void * ret = NULL;

ASSERT_EQ((int) 0, (int) metacall_load_from_file("cob", cob_scripts, sizeof(cob_scripts) / sizeof(cob_scripts[0]), NULL));
ASSERT_EQ((int) 0, (int) metacall_load_from_file(tag, cob_scripts, sizeof(cob_scripts) / sizeof(cob_scripts[0]), NULL));

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

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

metacall_value_destroy(ret);

/* This is a Python script on purpose, in order to test Cobol when it fails */
static const char buffer[] =
"#!/usr/bin/env python3\n"
"def multmem(left: int, right: int) -> int:\n"
"\tresult = left * right;\n"
"\tprint(left, ' * ', right, ' = ', result);\n"
"\treturn result;";

EXPECT_EQ((int) 1, (int) metacall_load_from_memory(tag, buffer, sizeof(buffer), NULL));
}
#endif /* OPTION_BUILD_LOADERS_COB */

Expand Down

0 comments on commit 7c55580

Please sign in to comment.