Skip to content
Open
74 changes: 72 additions & 2 deletions source/loaders/cs_loader/include/cs_loader/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
*/

#ifndef _DEFS_H_
#define _DEFS_H_
Expand Down Expand Up @@ -52,6 +52,76 @@ typedef struct
reflect_param pars[10];
} reflect_function;

typedef struct
{
char name[100];
} reflect_class;

typedef struct
{
char class_name[100];
int param_count;
reflect_param pars[10];
} reflect_constructor;

typedef struct
{
char class_name[100];
short return_type;
int param_count;
char name[100];
int is_static;
reflect_param pars[10];
} reflect_method;

typedef struct
{
char class_name[100];
short type;
char name[100];
int is_static;
} reflect_attribute;

typedef void(get_classes_c)(int *, void *);

typedef void(get_constructors_c)(int *, void *);

typedef void(get_methods_c)(int *, void *);

typedef void(get_attributes_c)(int *, void *);

typedef void *(create_object_c)(const char *,
parameters *,
long);

typedef execution_result *(invoke_object_c)(void *,
const char *,
parameters *,
long);

typedef execution_result *(invoke_static_c)(const char *,
const char *,
parameters *,
long);

typedef execution_result *(get_object_attribute_c)(void *,
const char *);

typedef int(set_object_attribute_c)(
void *,
const char *,
parameters *);

typedef execution_result *(get_static_attribute_c)(const char *,
const char *);

typedef int(set_static_attribute_c)(
const char *,
const char *,
parameters *);

typedef void(destroy_object_c)(void *);

typedef char(execution_path_w)(const wchar_t *source);
typedef char(execution_path_c)(const char *source);

Expand All @@ -75,7 +145,7 @@ typedef void(destroy_clr)(void);

#if defined(__linux) || defined(linux) || defined(__APPLE__) || defined(__MACH__)
/* On Linux and macOS, CoreCLR uses narrow char APIs (const char *).
* Only Windows uses wide char (wchar_t) for its CLR host API. */
* Only Windows uses wide char (wchar_t) for its CLR host API. */
#define W(str) str
typedef char CHARSTRING;
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
*/

#ifndef _HOST_ENVIRONMENT_H_
#define _HOST_ENVIRONMENT_H_
Expand Down
68 changes: 67 additions & 1 deletion source/loaders/cs_loader/include/cs_loader/netcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
*/

#ifndef _NETCORE_H_
#define _NETCORE_H_
Expand All @@ -43,6 +43,19 @@ class netcore
protected:
reflect_function functions[100]; // TODO: Improve this, make it dynamic

Copy link
Copy Markdown
Member

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:

int functions_count;

reflect_class classes[100];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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;
Expand All @@ -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;
Expand All @@ -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");

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion source/loaders/cs_loader/include/cs_loader/netcore_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
*/

#ifndef _NETCORE_WIN_H_
#define _NETCORE_WIN_H_
Expand Down
26 changes: 25 additions & 1 deletion source/loaders/cs_loader/include/cs_loader/simple_netcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
*/

#ifndef _SIMPLE_NETCORE_H_
#define _SIMPLE_NETCORE_H_
Expand All @@ -38,6 +38,30 @@ netcore_handle simple_netcore_create(char *dotnet_root, char *dotnet_loader_asse

reflect_function *simple_netcore_get_functions(netcore_handle handle, int *count);

reflect_class *simple_netcore_get_classes(netcore_handle handle, int *count);

reflect_constructor *simple_netcore_get_constructors(netcore_handle handle, int *count);

reflect_method *simple_netcore_get_methods(netcore_handle handle, int *count);

reflect_attribute *simple_netcore_get_attributes(netcore_handle handle, int *count);

void *simple_netcore_create_object(netcore_handle handle, const char *class_name, parameters *params, long size);

execution_result *simple_netcore_invoke_object(netcore_handle handle, void *object, const char *method, parameters *params, long size);

execution_result *simple_netcore_invoke_static(netcore_handle handle, const char *class_name, const char *method, parameters *params, long size);

execution_result *simple_netcore_get_object_attribute(netcore_handle handle, void *object, const char *attribute);

int simple_netcore_set_object_attribute(netcore_handle handle, void *object, const char *attribute, parameters *param);

execution_result *simple_netcore_get_static_attribute(netcore_handle handle, const char *class_name, const char *attribute);

int simple_netcore_set_static_attribute(netcore_handle handle, const char *class_name, const char *attribute, parameters *param);

void simple_netcore_destroy_object(netcore_handle handle, void *object);

int simple_netcore_load_script_from_files(netcore_handle handle, const char *files[MAX_FILES], size_t size);

int simple_netcore_execution_path(netcore_handle handle, const char *path);
Expand Down
2 changes: 1 addition & 1 deletion source/loaders/cs_loader/include/cs_loader/string_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
*/

#ifndef _STRING_BUFFER_H_
#define _STRING_BUFFER_H_
Expand Down
103 changes: 103 additions & 0 deletions source/loaders/cs_loader/netcore/source/ClassContainer.cs
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();
}
}
}
8 changes: 8 additions & 0 deletions source/loaders/cs_loader/netcore/source/Contracts/ILoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ namespace CSLoader.Contracts

public interface ILoader
{
ReflectClass[] Classes();

ReflectConstructor[] Constructors();

ReflectMethod[] Methods();

ReflectAttribute[] Attributes();

ReflectFunction[] Functions();

string LoadFromSourceFunctions(string[] source);
Expand Down
Loading