-
Notifications
You must be signed in to change notification settings - Fork 1
Implement dynamic library loading #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
EdwardPalmer99
wants to merge
1
commit into
master
Choose a base branch
from
EdwardPalmer99/dlopen
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,127 @@ | ||
| /** | ||
| * @file DynamicLibraryLoader.cpp | ||
| * @author Edward Palmer | ||
| * @date 2025-04-08 | ||
| * | ||
| * @copyright Copyright (c) 2025 | ||
| * | ||
| */ | ||
|
|
||
| #include "DynamicLibraryLoader.hpp" | ||
| #include "Exceptions.hpp" | ||
| #include "FloatObject.hpp" | ||
| #include "IntObject.hpp" | ||
| #include "LibraryFunctionObject.hpp" | ||
| #include "Logger.hpp" | ||
| #include "Stringify.hpp" | ||
|
|
||
| /* TODO: - add GTests and move to Bazel to test operation */ | ||
|
|
||
| DynamicLibraryLoader::DynamicLibraryLoader(std::string libPath, std::initializer_list<FuncDefinition> funcDefinitions) | ||
| { | ||
| /* Useful typedefs */ | ||
| typedef double (*DoubleFuncDoublePtr)(double); | ||
| typedef double (*DoubleFuncDoubleDoublePtr)(double, double); | ||
| typedef double (*DoubleFuncIntPtr)(int); | ||
| typedef int (*IntFuncIntPtr)(int); | ||
| typedef int (*IntFuncIntIntPtr)(int, int); | ||
|
|
||
| _closures.clear(); | ||
| _closures.reserve(funcDefinitions.size()); | ||
|
|
||
| _handle = dlopen(libPath.c_str(), RTLD_LAZY); | ||
| if (!_handle) | ||
| { | ||
| ThrowException("failed to load shared library with path: " + libPath + " with error: " + eucleia::stringify(dlerror())); | ||
| } | ||
|
|
||
| for (auto &[funcName, funcSignature] : funcDefinitions) | ||
| { | ||
| void *ptr = dlsym(_handle, funcName.c_str()); | ||
| if (!ptr) | ||
| { | ||
| ThrowException("failed to load function with name " + funcName); | ||
| } | ||
|
|
||
| /* Create each closure */ | ||
| auto closure = [ptr, funcSignature](ProgramNode &callArgs, Scope &scope) -> BaseObject * | ||
| { | ||
| switch (funcSignature) | ||
| { | ||
| case DoubleFuncDouble: | ||
| { | ||
| auto func = reinterpret_cast<DoubleFuncDoublePtr>(ptr); | ||
|
|
||
| double returnValue = func(callArgs[0]->evaluate(scope)->castObject<FloatObject>().value); | ||
|
|
||
| return scope.createManagedObject<FloatObject>(returnValue); | ||
| } | ||
| case DoubleFuncDoubleDouble: | ||
| { | ||
| auto func = reinterpret_cast<DoubleFuncDoubleDoublePtr>(ptr); | ||
|
|
||
| double returnValue = func(callArgs[0]->evaluate(scope)->castObject<FloatObject>().value, | ||
| callArgs[1]->evaluate(scope)->castObject<FloatObject>().value); | ||
|
|
||
| return scope.createManagedObject<FloatObject>(returnValue); | ||
| } | ||
| case DoubleFuncInt: | ||
| { | ||
| auto func = reinterpret_cast<DoubleFuncIntPtr>(ptr); | ||
|
|
||
| double returnValue = func(callArgs[0]->evaluate(scope)->castObject<IntObject>().value); | ||
|
|
||
| return scope.createManagedObject<FloatObject>(returnValue); | ||
| } | ||
| case IntFuncInt: | ||
| { | ||
| auto func = reinterpret_cast<IntFuncIntPtr>(ptr); | ||
|
|
||
| int returnValue = func(callArgs[0]->evaluate(scope)->castObject<IntObject>().value); | ||
|
|
||
| return scope.createManagedObject<IntObject>(returnValue); | ||
| } | ||
| case IntFuncIntInt: | ||
| { | ||
| auto func = reinterpret_cast<IntFuncIntIntPtr>(ptr); | ||
|
|
||
| int returnValue = func(callArgs[0]->evaluate(scope)->castObject<IntObject>().value, | ||
| callArgs[1]->evaluate(scope)->castObject<IntObject>().value); | ||
|
|
||
| return scope.createManagedObject<IntObject>(returnValue); | ||
| } | ||
| default: | ||
| { | ||
| ThrowException("unsupported function signature"); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /* Store closures for when evaluate() is called */ | ||
| _closures.emplace_back(funcName, closure); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| BaseObject *DynamicLibraryLoader::evaluate(Scope &scope) | ||
| { | ||
| for (const auto &[name, closure] : _closures) | ||
| { | ||
| Logger::debug("adding dynamic function " + name + " to scope"); | ||
| LibraryFunctionObject *object = scope.createManagedObject<LibraryFunctionObject>(closure); | ||
| scope.linkObject(name, object); | ||
| } | ||
|
|
||
| return nullptr; | ||
| } | ||
|
|
||
|
|
||
| DynamicLibraryLoader::~DynamicLibraryLoader() | ||
| { | ||
| if (!_handle) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| dlclose(_handle); | ||
| } | ||
This file contains hidden or 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,55 @@ | ||
| /** | ||
| * @file DynamicLibraryLoader.hpp | ||
| * @author Edward Palmer | ||
| * @date 2025-04-08 | ||
| * | ||
| * @copyright Copyright (c) 2025 | ||
| * | ||
| */ | ||
|
|
||
| #pragma once | ||
| #include "BaseObject.hpp" | ||
| #include "ProgramNode.hpp" | ||
| #include "Scope.hpp" | ||
| #include <dlfcn.h> | ||
| #include <functional> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| /** | ||
| * TODO: - an improvement idea: instead of having to add all functions to the scope, would be better to instead call | ||
| * something like [module].func and then we'd realize that it's a function call. | ||
| */ | ||
|
|
||
|
|
||
| class DynamicLibraryLoader : public BaseNode | ||
| { | ||
| public: | ||
| using FuncName = std::string; | ||
| using Function = std::function<BaseObject *(ProgramNode &callArgs, Scope &scope)>; | ||
|
|
||
|
|
||
| enum FuncSignature | ||
| { | ||
| DoubleFuncDouble, | ||
| DoubleFuncDoubleDouble, | ||
| DoubleFuncInt, | ||
| IntFuncInt, | ||
| IntFuncIntInt | ||
| }; | ||
|
|
||
| using FuncDefinition = std::pair<FuncName, FuncSignature>; | ||
|
|
||
| DynamicLibraryLoader() = delete; | ||
|
|
||
| DynamicLibraryLoader(std::string libPath, std::initializer_list<FuncDefinition> funcDefinitions); | ||
| ~DynamicLibraryLoader() override; | ||
|
|
||
| /* Add all functions to the scope this is evaluated in */ | ||
| BaseObject *evaluate(Scope &scope) final; | ||
|
|
||
| protected: | ||
| std::vector<std::pair<FuncName, Function>> _closures; | ||
|
|
||
| void *_handle{nullptr}; | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| // | ||
|
|
||
| #include "EucleiaParser.hpp" | ||
| #include "DynamicLibraryLoader.hpp" | ||
| #include "EucleiaModules.hpp" | ||
| #include "Exceptions.hpp" | ||
| #include "Grammar.hpp" | ||
|
|
@@ -115,7 +116,7 @@ FileNode *Parser::parseFileImport() | |
| /// This is for importing functions from a stdlib as opposed to user-defined functions | ||
| /// into this scope. | ||
|
|
||
| ModuleNode *Parser::parseLibraryImport() | ||
| BaseNode *Parser::parseLibraryImport() | ||
| { | ||
| skipOperator("<"); | ||
|
|
||
|
|
@@ -884,7 +885,8 @@ void Parser::skipSemicolonLineEndingIfRequired(const BaseNode &node) | |
| node.isNodeType<WhileNode>() || | ||
| node.isNodeType<DoWhileNode>() || | ||
| node.isNodeType<ForLoopNode>() || | ||
| node.isNodeType<FunctionNode>()); | ||
| node.isNodeType<FunctionNode>() || | ||
| node.isNodeType<DynamicLibraryLoader>()); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create some custom way for user to load dynamic libs: i.e. import "some path" as dylib dylib.someFunction(arg1, arg2, ...) -> lookup in dylib for "someFunction" and attempt to perform cast based on arg types and expected return type |
||
|
|
||
| if (!doSkipPunctuation) | ||
| skipPunctuation(";"); | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we create a template class where T = some function type to handle this better?