loader: Return function pointer instead of EVM instance

This commit is contained in:
Paweł Bylica 2018-07-05 14:51:03 +02:00
parent 4a929e248a
commit 721f2b3083
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
3 changed files with 27 additions and 33 deletions

View File

@ -9,6 +9,9 @@
extern "C" {
#endif
/** The function pointer type for EVMC create functions. */
typedef struct evmc_instance* (*evmc_create_fn)(void);
/** Error codes for the EVMC loader. */
enum evmc_loader_error_code
{
@ -44,7 +47,7 @@ enum evmc_loader_error_code
* - the name "evmc_create_" + _short name_ is checked in the library:
* "evmc_create_interpreter".
*
* If the create function is found in the library, the EVM instance is create and returned.
* If the create function is found in the library, the pointer to the function is returned.
* Otherwise, the ::EVMC_ERRC_SYMBOL_NOT_FOUND error code is signaled and NULL is returned.
*
* @param filename The null terminated path (absolute or relative) to the shared library
@ -53,9 +56,9 @@ enum evmc_loader_error_code
* signaled.
* @param error_code The pointer to the error code. If not NULL the value is set to
* ::EVMC_ERRC_SUCCESS on success or any other error code as described above.
* @return The pointer to EVM instance if loaded successfully or NULL.
* @return The pointer to the EVM create function or NULL.
*/
struct evmc_instance* evmc_load(const char* filename, enum evmc_loader_error_code* error_code);
evmc_create_fn evmc_load(const char* filename, enum evmc_loader_error_code* error_code);
#if __cplusplus
}

View File

@ -37,12 +37,11 @@ static void strcpy_s(char* dest, size_t destsz, const char* src)
}
#endif
typedef struct evmc_instance* (*evmc_create_fn)();
struct evmc_instance* evmc_load(const char* filename, enum evmc_loader_error_code* error_code)
evmc_create_fn evmc_load(const char* filename, enum evmc_loader_error_code* error_code)
{
enum evmc_loader_error_code ec = EVMC_LOADER_SUCCESS;
struct evmc_instance* instance = NULL;
evmc_create_fn create_fn = NULL;
if (!filename)
{
@ -93,7 +92,7 @@ struct evmc_instance* evmc_load(const char* filename, enum evmc_loader_error_cod
*dash_pos++ = '_';
// Search for the "full name" based function name.
evmc_create_fn create_fn = DLL_GET_CREATE_FN(handle, name);
create_fn = DLL_GET_CREATE_FN(handle, name);
if (!create_fn)
{
// Try the "short name" based function name.
@ -106,11 +105,7 @@ struct evmc_instance* evmc_load(const char* filename, enum evmc_loader_error_cod
}
}
if (create_fn)
{
instance = create_fn();
}
else
if (!create_fn)
{
DLL_CLOSE(handle);
ec = EVMC_LOADER_SYMBOL_NOT_FOUND;
@ -119,5 +114,5 @@ struct evmc_instance* evmc_load(const char* filename, enum evmc_loader_error_cod
exit:
if (error_code)
*error_code = ec;
return instance;
return create_fn;
}

View File

@ -71,12 +71,14 @@ TEST(loader, aaa)
auto path = "unittests/libaaa.so";
evmc_loader_error_code ec;
auto x = (uintptr_t)evmc_load(path, &ec);
auto fn = evmc_load(path, &ec);
ASSERT_NE(fn, nullptr);
EXPECT_EQ(ec, EVMC_LOADER_SUCCESS);
EXPECT_EQ(x, 0xaaa);
EXPECT_EQ((uintptr_t)fn(), 0xaaa);
x = (uintptr_t)evmc_load(path, nullptr);
EXPECT_EQ(x, 0xaaa);
fn = evmc_load(path, nullptr);
ASSERT_NE(fn, nullptr);
EXPECT_EQ((uintptr_t)fn(), 0xaaa);
}
TEST(loader, prefix_aaa)
@ -86,12 +88,10 @@ TEST(loader, prefix_aaa)
for (auto& path : paths)
{
evmc_loader_error_code ec;
auto x = (uintptr_t)evmc_load(path, &ec);
auto fn = evmc_load(path, &ec);
ASSERT_NE(fn, nullptr);
EXPECT_EQ(ec, EVMC_LOADER_SUCCESS);
EXPECT_EQ(x, 0xaaa);
x = (uintptr_t)evmc_load(path, nullptr);
EXPECT_EQ(x, 0xaaa);
EXPECT_EQ((uintptr_t)fn(), 0xaaa);
}
}
@ -100,12 +100,10 @@ TEST(loader, eee_bbb)
auto path = "unittests/eee-bbb.dll";
evmc_loader_error_code ec;
auto x = (uintptr_t)evmc_load(path, &ec);
auto fn = evmc_load(path, &ec);
ASSERT_NE(fn, nullptr);
EXPECT_EQ(ec, EVMC_LOADER_SUCCESS);
EXPECT_EQ(x, 0xeeebbb);
x = (uintptr_t)evmc_load(path, nullptr);
EXPECT_EQ(x, 0xeeebbb);
EXPECT_EQ((uintptr_t)fn(), 0xeeebbb);
}
#if _WIN32
@ -116,12 +114,10 @@ TEST(loader, nextto)
auto path = "aaa.evm";
evmc_loader_error_code ec;
auto x = (uintptr_t)evmc_load(path, &ec);
auto fn = evmc_load(path, &ec);
ASSERT_NE(fn, nullptr);
EXPECT_EQ(ec, EVMC_LOADER_SUCCESS);
EXPECT_EQ(x, 0xaaa);
x = (uintptr_t)evmc_load(path, nullptr);
EXPECT_EQ(x, 0xaaa);
EXPECT_EQ((uintptr_t)fn(), 0xaaa);
}
#endif
@ -167,7 +163,7 @@ TEST(loader, eee3)
#if !_WIN32
TEST(loader, eee4)
{
// Windows is not loading DLLs without extensions.
// Windows is not loading DLLs without extensions.
auto path = "unittests/eee4";
evmc_loader_error_code ec;