-
Notifications
You must be signed in to change notification settings - Fork 271
C# loader: class support (constructors, instance/static methods and attributes) #123 #858
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
Open
G00dS0ul
wants to merge
8
commits into
metacall:develop
Choose a base branch
from
G00dS0ul:feature/cs-class-support
base: develop
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.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7bbab0b
test(cs_loader): add failing class-support test (TDD red) - #123
G00dS0ul 429a60d
test(cs_loader): cover static attribute in Counter
G00dS0ul 9afebe2
style: clang-format new class-test sources
G00dS0ul 0c780bb
cs_loader: add managed reflection records for classes, constructors, …
G00dS0ul 4ed0bca
cs_loader: add class discovery and GCHandle-backed object runtime
G00dS0ul c45db98
cs_loader: add native reflection records and runtime delegate types
G00dS0ul 578301a
cs_loader: bridge managed metadata and object runtime through netcore
G00dS0ul 98ff4f0
cs_loader: register reflected classes and objects with MetaCall
G00dS0ul 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
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
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 |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
| */ | ||
|
|
||
| #ifndef _NETCORE_H_ | ||
| #define _NETCORE_H_ | ||
|
|
@@ -43,6 +43,19 @@ class netcore | |
| protected: | ||
| reflect_function functions[100]; // TODO: Improve this, make it dynamic | ||
| int functions_count; | ||
|
|
||
| reflect_class classes[100]; | ||
|
Member
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. we shouldn't be using fixed arrays.. this is a very bad pattern,we should use a pointer + a size_t size for defining the size of the array. |
||
| int classes_count; | ||
|
|
||
| reflect_constructor constructors[100]; | ||
| int constructors_count; | ||
|
|
||
| reflect_method methods[100]; | ||
| int methods_count; | ||
|
|
||
| reflect_attribute attributes[100]; | ||
| int attributes_count; | ||
|
|
||
| char *dotnet_root; | ||
| char *dotnet_loader_assembly_path; | ||
| bool initialized; | ||
|
|
@@ -65,6 +78,18 @@ class netcore | |
| execute_function_with_params_w *execute_with_params_w; | ||
| execute_function_with_params_c *execute_with_params_c; | ||
| get_loaded_functions *core_get_functions; | ||
| get_classes_c *core_get_classes; | ||
| get_constructors_c *core_get_constructors; | ||
| get_methods_c *core_get_methods; | ||
| get_attributes_c *core_get_attributes; | ||
| create_object_c *core_create_object; | ||
| invoke_object_c *core_invoke_object; | ||
| invoke_static_c *core_invoke_static; | ||
| get_object_attribute_c *core_get_object_attribute; | ||
| set_object_attribute_c *core_set_object_attribute; | ||
| get_static_attribute_c *core_get_static_attribute; | ||
| set_static_attribute_c *core_set_static_attribute; | ||
| destroy_object_c *core_destroy_object; | ||
| corefunction_destroy_execution_result *core_destroy_execution_result; | ||
|
|
||
| destroy_clr *core_destroy; | ||
|
|
@@ -90,7 +115,23 @@ class netcore | |
| const CHARSTRING *delegate_execute_with_params_w = W("ExecuteWithParamsW"); | ||
| const CHARSTRING *delegate_execute_with_params_c = W("ExecuteWithParamsC"); | ||
| const CHARSTRING *delegate_get_functions = W("GetFunctions"); | ||
| const CHARSTRING *delegate_get_classes = W("GetClasses"); | ||
| const CHARSTRING *delegate_get_constructors = W("GetConstructors"); | ||
| const CHARSTRING *delegate_get_methods = W("GetMethods"); | ||
| const CHARSTRING *delegate_get_attributes = W("GetAttributes"); | ||
| const CHARSTRING *delegate_destroy_execution_result = W("DestroyExecutionResult"); | ||
| const CHARSTRING *delegate_create_object = W("CreateObjectC"); | ||
| const CHARSTRING *delegate_invoke_object = W("InvokeObjectC"); | ||
| const CHARSTRING *delegate_invoke_static = W("InvokeStaticC"); | ||
| const CHARSTRING *delegate_get_object_attribute = | ||
| W("GetObjectAttributeC"); | ||
| const CHARSTRING *delegate_set_object_attribute = | ||
| W("SetObjectAttributeC"); | ||
| const CHARSTRING *delegate_get_static_attribute = | ||
| W("GetStaticAttributeC"); | ||
| const CHARSTRING *delegate_set_static_attribute = | ||
| W("SetStaticAttributeC"); | ||
| const CHARSTRING *delegate_destroy_object = W("DestroyObjectC"); | ||
|
|
||
| const CHARSTRING *delegate_destroy = W("Destroy"); | ||
|
|
||
|
|
@@ -123,6 +164,31 @@ class netcore | |
| virtual bool create_delegate(const CHARSTRING *delegate_name, void **func) = 0; | ||
|
|
||
| reflect_function *get_functions(int *count); | ||
|
|
||
| reflect_class *get_classes(int *count); | ||
|
|
||
| reflect_constructor *get_constructors(int *count); | ||
|
|
||
| reflect_method *get_methods(int *count); | ||
|
|
||
| reflect_attribute *get_attributes(int *count); | ||
|
|
||
| void *create_object(const char *class_name, parameters *params, long size); | ||
|
|
||
| execution_result *invoke_object(void *object, const char *method, parameters *params, long size); | ||
|
|
||
| execution_result *invoke_static(const char *class_name, const char *method, parameters *params, long size); | ||
|
|
||
| execution_result *get_object_attribute(void *object, const char *attribute); | ||
|
|
||
| bool set_object_attribute(void *object, const char *attribute, parameters *param); | ||
|
|
||
| execution_result *get_static_attribute(const char *class_name, const char *attribute); | ||
|
|
||
| bool set_static_attribute(const char *class_name, const char *attribute, parameters *param); | ||
|
|
||
| void destroy_object(void *object); | ||
|
|
||
| void destroy_execution_result(execution_result *er); | ||
|
|
||
| void destroy(void); | ||
|
|
||
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
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
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
103 changes: 103 additions & 0 deletions
103
source/loaders/cs_loader/netcore/source/ClassContainer.cs
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,103 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
| using System.Threading.Tasks; | ||
| using static CSLoader.MetacallDef; | ||
|
|
||
| namespace CSLoader | ||
| { | ||
| public sealed class ClassContainer | ||
| { | ||
| public Type Type { get; } | ||
|
|
||
| public string ScriptHandle { get; } | ||
|
|
||
| public string ClassName => this.Type.Name; | ||
|
|
||
| public ClassContainer(Type type, string scriptHandle) | ||
| { | ||
| this.Type = type; | ||
| this.ScriptHandle = scriptHandle; | ||
| } | ||
|
|
||
| private static ReflectParam[] GetParameters(ParameterInfo[] parameters) | ||
| { | ||
| ReflectParam[] result = new ReflectParam[10]; | ||
|
|
||
| for (int i = 0; i < parameters.Length && i < result.Length; ++i) | ||
| { | ||
| result[i] = new ReflectParam | ||
| { | ||
| type = MetacallDef.Get(parameters[i].ParameterType), | ||
| name = parameters[i].Name ?? string.Empty | ||
| }; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public ReflectClass GetReflectClass() | ||
| { | ||
| return new ReflectClass | ||
| { | ||
| name = this.ClassName | ||
| }; | ||
| } | ||
|
|
||
| public ReflectConstructor[] GetReflectConstructors() | ||
| { | ||
| return this.Type | ||
| .GetConstructors( | ||
| BindingFlags.Public | | ||
| BindingFlags.Instance | | ||
| BindingFlags.DeclaredOnly) | ||
| .Select(constructor => new ReflectConstructor | ||
| { | ||
| className = this.ClassName, | ||
| paramcount = constructor.GetParameters().Length, | ||
| pars = GetParameters(constructor.GetParameters()) | ||
| }) | ||
| .ToArray(); | ||
| } | ||
|
|
||
| public ReflectMethod[] GetReflectMethods() | ||
| { | ||
| return this.Type | ||
| .GetMethods( | ||
| BindingFlags.Public | | ||
| BindingFlags.Instance | | ||
| BindingFlags.Static | | ||
| BindingFlags.DeclaredOnly) | ||
| .Select(method => new ReflectMethod | ||
| { | ||
| className = this.ClassName, | ||
| returnType = MetacallDef.Get(method.ReturnType), | ||
| paramcount = method.GetParameters().Length, | ||
| name = method.Name, | ||
| isStatic = method.IsStatic ? 1 : 0, | ||
| pars = GetParameters(method.GetParameters()) | ||
| }) | ||
| .ToArray(); | ||
| } | ||
|
|
||
| public ReflectAttribute[] GetReflectAttributes() | ||
| { | ||
| return this.Type | ||
| .GetFields( | ||
| BindingFlags.Public | | ||
| BindingFlags.Instance | | ||
| BindingFlags.Static | | ||
| BindingFlags.DeclaredOnly) | ||
| .Select(field => new ReflectAttribute | ||
| { | ||
| className = this.ClassName, | ||
| type = MetacallDef.Get(field.FieldType), | ||
| name = field.Name, | ||
| isStatic = field.IsStatic ? 1 : 0 | ||
| }) | ||
| .ToArray(); | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
this Todo already indicated the issue I commented below: