-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[22_2] add api to load shared library (#320)
TODO: add proper implementation on wasm
- Loading branch information
1 parent
f782710
commit 122d03f
Showing
11 changed files
with
180 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
/** \file shared_lib.cpp | ||
* \copyright GPLv3 | ||
* \details dynamic (shared) library related routines | ||
* \author jingkaimori | ||
* \date 2024 | ||
*/ | ||
|
||
#include "shared_lib.hpp" | ||
|
||
#ifndef OS_WASM | ||
namespace lolly { | ||
namespace system { | ||
|
||
shared_lib_rep::shared_lib_rep (string dynamic_name, url path) | ||
: dynamic_ref (tb_dynamic_init (c_string (as_system_string (path)))), | ||
rep (dynamic_name) { | ||
if (dynamic_ref == nullptr) { | ||
TM_FAILED ("error occurs during loading of library") | ||
} | ||
}; | ||
shared_lib_rep::~shared_lib_rep () { | ||
tb_dynamic_exit (dynamic_ref); | ||
shared_lib::instances->reset (res_name); | ||
}; | ||
|
||
shared_lib | ||
load_shared_library (string name, url path) { | ||
return make (shared_lib, name, tm_new<shared_lib_rep> (name, path)); | ||
} | ||
|
||
} // namespace system | ||
} // namespace lolly | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
/** \file shared_lib.hpp | ||
* \copyright GPLv3 | ||
* \details dynamic (shared) library related routines | ||
* \author jingkaimori | ||
* \date 2024 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "resource.hpp" | ||
#include "url.hpp" | ||
#include <stdint.h> | ||
#include <tbox/tbox.h> | ||
|
||
#ifndef OS_WASM | ||
namespace lolly { | ||
namespace system { | ||
|
||
RESOURCE (shared_lib); | ||
|
||
struct shared_lib_rep : rep<shared_lib> { | ||
private: | ||
tb_dynamic_ref_t dynamic_ref; | ||
|
||
public: | ||
shared_lib_rep (string dynamic_name, url path); | ||
virtual ~shared_lib_rep (); | ||
template <typename Ret, typename... Args> | ||
auto get_function (string function_name) -> Ret (*) (Args...) { | ||
return (Ret (*) (Args...)) tb_dynamic_func (dynamic_ref, | ||
c_string (function_name)); | ||
} | ||
friend struct shared_lib; | ||
}; | ||
|
||
shared_lib load_shared_library (string name, url path); | ||
|
||
} // namespace system | ||
} // namespace lolly | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
/****************************************************************************** | ||
* MODULE : example_dynamic_library.cpp | ||
* DESCRIPTION: an example dynamic library for testing | ||
* COPYRIGHT : (C) 2024 jingkaimori | ||
******************************************************************************* | ||
* This software falls under the GNU general public license version 3 or later. | ||
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE | ||
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
******************************************************************************/ | ||
|
||
extern "C" { | ||
double | ||
square_div_2 (int arg) { | ||
return arg * arg / 2.0; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
/****************************************************************************** | ||
* MODULE : shared_lib_test.cpp | ||
* DESCRIPTION: tests on dynamic library loading | ||
* COPYRIGHT : (C) 2024 jingkaimori | ||
******************************************************************************* | ||
* This software falls under the GNU general public license version 3 or later. | ||
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE | ||
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
******************************************************************************/ | ||
|
||
#include "a_lolly_test.hpp" | ||
#include "lolly/system/shared_lib.hpp" | ||
#include "sys_utils.hpp" | ||
|
||
#ifndef OS_WASM | ||
using lolly::system::load_shared_library; | ||
using lolly::system::shared_lib; | ||
#endif | ||
|
||
TEST_CASE ("load_shared_library") { | ||
#ifndef OS_WASM | ||
url lib_path; | ||
if (os_win ()) { | ||
lib_path= url_pwd () * "example_dynamic_library.dll"; | ||
} | ||
else if (os_macos ()) { | ||
lib_path= url_pwd () * "libexample_dynamic_library.dylib"; | ||
} | ||
else { | ||
lib_path= url_pwd () * "libexample_dynamic_library.so"; | ||
} | ||
shared_lib lib= load_shared_library ("example_dynamic_library", lib_path); | ||
double (*func) (int)= lib->get_function<double, int> ("square_div_2"); | ||
double res = func (5); | ||
CHECK_EQ (res, 12.5); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters