summaryrefslogtreecommitdiff
path: root/Runtime/Scripting/Backend/ScriptingInvocation.cpp
blob: 71ead05391c7d59a815a2b2baa449238ee3fd111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "UnityPrefix.h"

#if ENABLE_SCRIPTING

#include "ScriptingInvocation.h"
#include "Runtime/Utilities/LogAssert.h"
#include "Runtime/Scripting/ScriptingUtility.h"
#include "ScriptingArguments.h"
#include "ScriptingMethodRegistry.h"
#include "Runtime/Scripting/Backend/ScriptingBackendApi.h"
#include "Runtime/Profiler/Profiler.h"
#include "Runtime/Scripting/ScriptingManager.h"

#if ENABLE_MONO
#include "Runtime/Mono/MonoIncludes.h"
#include "Runtime/Mono/MonoManager.h"
#include "Runtime/Mono/MonoUtility.h"
#include "Runtime/Mono/MonoScript.h"
#endif

ScriptingInvocation::ScriptingInvocation()
{
}

ScriptingInvocation::ScriptingInvocation(ScriptingMethodPtr in_method)
	: ScriptingInvocationNoArgs(in_method)
{
}

#if ENABLE_MONO || UNITY_WINRT
ScriptingInvocation::ScriptingInvocation(const char* namespaze, const char* klassName, const char* methodName)
{
	method = GetScriptingMethodRegistry().GetMethod(namespaze, klassName, methodName);
}

ScriptingInvocation::ScriptingInvocation(ScriptingClassPtr klass, const char* methodName)
{
	method = GetScriptingMethodRegistry().GetMethod(klass, methodName);
}

ScriptingInvocation::ScriptingInvocation(BackendNativeMethod monoMethod)
{
	method = GetScriptingMethodRegistry().GetMethod(monoMethod);
}
#endif

bool ScriptingInvocation::Check()
{
#if !ENABLE_MONO
	return true;
#else
	return ScriptingInvocationNoArgs::Check() && arguments.CheckArgumentsAgainstMethod(method);
#endif
}

template<class T>
T ScriptingInvocation::Invoke()
{
	ScriptingExceptionPtr ex = NULL;
	return Invoke<T>(&ex);
}

template<>
ScriptingObjectPtr ScriptingInvocation::Invoke<ScriptingObjectPtr>()
{
	return Invoke();
}

template<>
bool ScriptingInvocation::Invoke<bool>(ScriptingExceptionPtr* exception)
{
	ScriptingObjectPtr o = Invoke(exception);
	if (*exception != NULL)
		return false;
	
	#if ENABLE_MONO
		if (method->fastMonoMethod)
			return (bool)o;
		else
			return ExtractMonoObjectData<char>(o);
	#elif UNITY_FLASH
		bool boolResult;
		__asm __volatile__("%0 = marshallmap.getObjectWithId(%1);" : "=r"(boolResult) : "r"(o));
		return boolResult;
	#elif UNITY_WINRT
		return o != SCRIPTING_NULL ? o.ToBool() : false;
	#endif
}

ScriptingObjectPtr ScriptingInvocation::Invoke()
{
	ScriptingExceptionPtr ex = NULL;
	return Invoke(&ex);
}

ScriptingObjectPtr ScriptingInvocation::Invoke(ScriptingExceptionPtr* exception)
{
	return Invoke(exception, false);
}

ScriptingObjectPtr ScriptingInvocation::Invoke(ScriptingExceptionPtr* exception, bool convertArguments)
{
	ScriptingObjectPtr returnValue;
	
	*exception = NULL;

#if ENABLE_MONO || UNITY_FLASH || UNITY_WINRT
	MONO_PROFILER_BEGIN (method, classContextForProfiler, object)
#if UNITY_WINRT
	ScriptingObjectPtr metro_invoke_method(ScriptingMethodPtr method, ScriptingObjectPtr object, ScriptingArguments* arguments, ScriptingExceptionPtr* exception, bool convertArgs);
	returnValue = metro_invoke_method(method, object, &arguments, exception, convertArguments);
#else
	returnValue = scripting_method_invoke(method, object, arguments, exception);
#endif
	MONO_PROFILER_END
#elif !UNITY_EXTERNAL_TOOL
	ErrorString("Invoke() not implemented on this platform");
#else	
	return NULL;
#endif

	if (! *exception) return returnValue;

	this->exception = *exception;
#if !UNITY_EXTERNAL_TOOL
	if (logException)
		Scripting::LogException(*exception, objectInstanceIDContextForException );
#endif

	return SCRIPTING_NULL;
}

void ScriptingInvocation::AdjustArgumentsToMatchMethod()
{
	arguments.AdjustArgumentsToMatchMethod(method);
}


#if ENABLE_MONO

MonoObject* CallStaticMonoMethod (MonoClass* klass , const char* methodName, void** parameters)
{
	MonoException* exception = NULL;
	return CallStaticMonoMethod(klass, methodName, parameters, &exception);
}


static MonoObject* CallStaticMonoMethod (MonoMethod* method, void** parameters, MonoException** exception)
{
	MonoObject* returnValue = mono_runtime_invoke_profiled (method, NULL, parameters, exception);
	if (! *exception) return returnValue;

	Scripting::LogException(*exception, 0);
	return NULL;
}


static MonoObject* CallStaticMonoMethod (MonoMethod* method, void** parameters)
{
	MonoException* exception = NULL;
	return CallStaticMonoMethod(method, parameters, &exception);
}

MonoObject* CallStaticMonoMethod (const char* className, const char* methodName, void** parameters)
{
	MonoException* exception = NULL;
	return CallStaticMonoMethod(className, methodName, parameters, &exception);
}

MonoObject* CallStaticMonoMethod (MonoClass* klass , const char* methodName, void** parameters, MonoException** exception)
{
	MonoMethod* method = mono_class_get_method_from_name (klass, methodName, -1);

	if (!method)
	{
		ErrorString (Format ("Couldn't call method %s in class %s because it wasn't found.", methodName, mono_class_get_name(klass)));
		return NULL;
	}

	return CallStaticMonoMethod(method, parameters, exception);
}

MonoObject* CallStaticMonoMethod (const char* className, const char* methodName, void** parameters, MonoException** exception)
{
	MonoMethod* m = FindStaticMonoMethod(className, methodName);
	if (!m)
	{
		ErrorString (Format ("Couldn't call method %s because the class %s couldn't be found.", methodName, className));
		return NULL;
	}

	return CallStaticMonoMethod(m, parameters, exception);
}

MonoObject* CallStaticMonoMethod (const char* className, const char* nameSpace, const char* methodName, void** parameters)
{
	MonoException* exception = NULL;
	MonoMethod* m = FindStaticMonoMethod(nameSpace, className, methodName);
	if (!m)
	{
		ErrorString (Format ("Couldn't call method %s because the class %s couldn't be found.", methodName, className));
		return NULL;
	}

	return CallStaticMonoMethod(m, parameters, &exception);
}

MonoObject* CallStaticMonoMethod (const char* className, const char* nameSpace, const char* methodName, void** parameters, MonoException** exception)
{
	MonoMethod* m = FindStaticMonoMethod(nameSpace, className, methodName);
	if (!m)
	{
		ErrorString (Format ("Couldn't call method %s because the class %s couldn't be found.", methodName, className));
		return NULL;
	}

	return CallStaticMonoMethod(m, parameters, exception);
}


#endif

#endif