@@ -18,25 +18,41 @@ static class Extensions
18
18
{
19
19
public static JsValue ToJsValue ( this Object obj , EngineInstance engine )
20
20
{
21
- if ( obj == null )
22
- return JsValue . Null ;
23
-
24
- if ( obj is String )
25
- return new JsValue ( ( String ) obj ) ;
26
- else if ( obj is Int32 )
27
- return new JsValue ( ( Int32 ) obj ) ;
28
- else if ( obj is UInt32 )
29
- return new JsValue ( ( UInt32 ) obj ) ;
30
- else if ( obj is Double )
31
- return new JsValue ( ( Double ) obj ) ;
32
- else if ( obj is Single )
33
- return new JsValue ( ( Single ) obj ) ;
34
- else if ( obj is Boolean )
35
- return new JsValue ( ( Boolean ) obj ) ;
36
- else if ( obj is Enum )
37
- return new JsValue ( Convert . ToInt32 ( obj ) ) ;
38
-
39
- return engine . GetDomNode ( obj ) ;
21
+ if ( obj != null )
22
+ {
23
+ if ( obj is String )
24
+ {
25
+ return new JsValue ( ( String ) obj ) ;
26
+ }
27
+ else if ( obj is Int32 )
28
+ {
29
+ return new JsValue ( ( Int32 ) obj ) ;
30
+ }
31
+ else if ( obj is UInt32 )
32
+ {
33
+ return new JsValue ( ( UInt32 ) obj ) ;
34
+ }
35
+ else if ( obj is Double )
36
+ {
37
+ return new JsValue ( ( Double ) obj ) ;
38
+ }
39
+ else if ( obj is Single )
40
+ {
41
+ return new JsValue ( ( Single ) obj ) ;
42
+ }
43
+ else if ( obj is Boolean )
44
+ {
45
+ return new JsValue ( ( Boolean ) obj ) ;
46
+ }
47
+ else if ( obj is Enum )
48
+ {
49
+ return new JsValue ( Convert . ToInt32 ( obj ) ) ;
50
+ }
51
+
52
+ return engine . GetDomNode ( obj ) ;
53
+ }
54
+
55
+ return JsValue . Null ;
40
56
}
41
57
42
58
public static ClrFunctionInstance AsValue ( this Engine engine , Func < JsValue , JsValue [ ] , JsValue > func )
@@ -74,7 +90,9 @@ public static Object FromJsValue(this JsValue val)
74
90
var node = obj as DomNodeInstance ;
75
91
76
92
if ( node != null )
93
+ {
77
94
return node . Value ;
95
+ }
78
96
79
97
return obj ;
80
98
case Types . Undefined :
@@ -88,25 +106,35 @@ public static Object FromJsValue(this JsValue val)
88
106
89
107
public static Object As ( this Object value , Type targetType , EngineInstance engine )
90
108
{
91
- if ( value == null )
92
- return value ;
109
+ if ( value != null )
110
+ {
111
+ var sourceType = value . GetType ( ) ;
93
112
94
- var sourceType = value . GetType ( ) ;
113
+ if ( sourceType == targetType || sourceType . IsSubclassOf ( targetType ) || targetType . IsInstanceOfType ( value ) || targetType . IsAssignableFrom ( sourceType ) )
114
+ {
115
+ return value ;
116
+ }
117
+ else if ( sourceType == typeof ( Double ) && targetType == typeof ( Int32 ) )
118
+ {
119
+ return ( Int32 ) ( Double ) value ;
120
+ }
95
121
96
- if ( sourceType == targetType || sourceType . IsSubclassOf ( targetType ) || targetType . IsInstanceOfType ( value ) || targetType . IsAssignableFrom ( sourceType ) )
97
- return value ;
98
- else if ( sourceType == typeof ( Double ) && targetType == typeof ( Int32 ) )
99
- return ( Int32 ) ( Double ) value ;
122
+ if ( targetType . IsSubclassOf ( typeof ( Delegate ) ) && value is FunctionInstance )
123
+ {
124
+ return targetType . ToDelegate ( ( FunctionInstance ) value , engine ) ;
125
+ }
100
126
101
- if ( targetType . IsSubclassOf ( typeof ( Delegate ) ) && value is FunctionInstance )
102
- return targetType . ToDelegate ( ( FunctionInstance ) value , engine ) ;
127
+ var method = sourceType . PrepareConvert ( targetType ) ;
103
128
104
- var method = sourceType . PrepareConvert ( targetType ) ;
129
+ if ( method == null )
130
+ {
131
+ throw new JavaScriptException ( "[Internal] Could not find corresponding cast target." ) ;
132
+ }
105
133
106
- if ( method != null )
107
134
return method . Invoke ( value , null ) ;
135
+ }
108
136
109
- throw new JavaScriptException ( "[Internal] Could not find corresponding cast target." ) ;
137
+ return value ;
110
138
}
111
139
112
140
public static Object GetDefaultValue ( this Type type )
@@ -136,36 +164,50 @@ public static Object[] BuildArgs(this EngineInstance context, MethodBase method,
136
164
var offset = 0 ;
137
165
138
166
if ( parameters . Length > 0 && parameters [ 0 ] . ParameterType == typeof ( IWindow ) )
167
+ {
139
168
args [ offset ++ ] = context . Window . Value ;
169
+ }
140
170
141
171
if ( max > 0 && parameters [ max - 1 ] . GetCustomAttribute < ParamArrayAttribute > ( ) != null )
172
+ {
142
173
max -- ;
174
+ }
143
175
144
176
var n = Math . Min ( arguments . Length , max - offset ) ;
145
177
146
- for ( int i = 0 ; i < n ; i ++ )
178
+ for ( var i = 0 ; i < n ; i ++ )
147
179
{
148
180
if ( parameters [ i ] . IsOptional && arguments [ i ] . IsUndefined ( ) )
181
+ {
149
182
args [ i + offset ] = parameters [ i ] . DefaultValue ;
183
+ }
150
184
else
185
+ {
151
186
args [ i + offset ] = arguments [ i ] . FromJsValue ( ) . As ( parameters [ i ] . ParameterType , context ) ;
187
+ }
152
188
}
153
189
154
- for ( int i = n + offset ; i < max ; i ++ )
190
+ for ( var i = n + offset ; i < max ; i ++ )
155
191
{
156
192
if ( parameters [ i ] . IsOptional )
193
+ {
157
194
args [ i ] = parameters [ i ] . DefaultValue ;
195
+ }
158
196
else
197
+ {
159
198
args [ i ] = parameters [ i ] . ParameterType . GetDefaultValue ( ) ;
199
+ }
160
200
}
161
201
162
202
163
203
if ( max != parameters . Length )
164
204
{
165
205
var array = Array . CreateInstance ( parameters [ max ] . ParameterType . GetElementType ( ) , Math . Max ( 0 , arguments . Length - max ) ) ;
166
206
167
- for ( int i = max ; i < arguments . Length ; i ++ )
207
+ for ( var i = max ; i < arguments . Length ; i ++ )
208
+ {
168
209
array . SetValue ( arguments [ i ] . FromJsValue ( ) , i - max ) ;
210
+ }
169
211
170
212
args [ max ] = array ;
171
213
}
@@ -181,13 +223,17 @@ public static String[] GetParameterNames(this MethodInfo method)
181
223
public static void AddConstructors ( this EngineInstance engine , ObjectInstance obj , Type type )
182
224
{
183
225
foreach ( var exportedType in type . Assembly . ExportedTypes )
226
+ {
184
227
engine . AddConstructor ( obj , exportedType ) ;
228
+ }
185
229
}
186
230
187
231
public static void AddInstances ( this EngineInstance engine , ObjectInstance obj , Type type )
188
232
{
189
233
foreach ( var exportedType in type . Assembly . ExportedTypes )
234
+ {
190
235
engine . AddInstance ( obj , exportedType ) ;
236
+ }
191
237
}
192
238
193
239
public static void AddConstructor ( this EngineInstance engine , ObjectInstance obj , Type type )
0 commit comments