@@ -98,78 +98,41 @@ public static Object FromJsValue(this JsValue val)
98
98
return val . ToObject ( ) ;
99
99
}
100
100
101
- public static Object As ( this Object value , Type targetType , EngineInstance engine )
101
+ public static Object As ( this JsValue value , Type targetType , EngineInstance engine )
102
102
{
103
- if ( value != null )
103
+ if ( value != JsValue . Null )
104
104
{
105
- var sourceType = value . GetType ( ) ;
106
-
107
- if ( sourceType == targetType || sourceType . GetTypeInfo ( ) . IsSubclassOf ( targetType ) || targetType . GetTypeInfo ( ) . IsAssignableFrom ( sourceType . GetTypeInfo ( ) ) )
105
+ if ( targetType == typeof ( Int32 ) )
108
106
{
109
- return value ;
110
- }
111
- else if ( targetType == typeof ( Int32 ) )
112
- {
113
- if ( sourceType != typeof ( Double ) )
114
- {
115
- var v = value . ToJsValue ( engine ) ;
116
- return TypeConverter . ToInt32 ( v ) ;
117
- }
118
-
119
- return ( Int32 ) ( Double ) value ;
107
+ return TypeConverter . ToInt32 ( value ) ;
120
108
}
121
109
else if ( targetType == typeof ( Double ) )
122
110
{
123
- var v = value . ToJsValue ( engine ) ;
124
- return TypeConverter . ToNumber ( v ) ;
111
+ return TypeConverter . ToNumber ( value ) ;
125
112
}
126
113
else if ( targetType == typeof ( String ) )
127
114
{
128
- var v = value . ToJsValue ( engine ) ;
129
-
130
- if ( v . IsPrimitive ( ) )
131
- {
132
- return TypeConverter . ToString ( v ) ;
133
- }
134
-
135
- return v . ToString ( ) ;
115
+ return value . IsPrimitive ( ) ? TypeConverter . ToString ( value ) : value . ToString ( ) ;
136
116
}
137
117
else if ( targetType == typeof ( Boolean ) )
138
118
{
139
- var v = value . ToJsValue ( engine ) ;
140
- return TypeConverter . ToBoolean ( v ) ;
119
+ return TypeConverter . ToBoolean ( value ) ;
141
120
}
142
- else if ( targetType . GetTypeInfo ( ) . IsSubclassOf ( typeof ( Delegate ) ) )
121
+ else if ( targetType == typeof ( UInt32 ) )
143
122
{
144
- var f = value as FunctionInstance ;
145
-
146
- if ( f == null )
147
- {
148
- var b = value as String ;
149
-
150
- if ( b != null )
151
- {
152
- var e = engine . Jint ;
153
- var p = new [ ] { new JsValue ( b ) } ;
154
- f = new ClrFunctionInstance ( e , ( _this , args ) => e . Eval . Call ( _this , p ) ) ;
155
- }
156
- }
157
-
158
- if ( f != null )
159
- {
160
- return targetType . ToDelegate ( f , engine ) ;
161
- }
123
+ return TypeConverter . ToUint32 ( value ) ;
124
+ }
125
+ else if ( targetType == typeof ( UInt16 ) )
126
+ {
127
+ return TypeConverter . ToUint16 ( value ) ;
128
+ }
129
+ else
130
+ {
131
+ return value . AsComplex ( targetType , engine ) ;
162
132
}
163
-
164
- var method = sourceType . PrepareConvert ( targetType ) ;
165
-
166
- if ( method == null )
167
- throw new JavaScriptException ( "[Internal] Could not find corresponding cast target." ) ;
168
-
169
- return method . Invoke ( value , null ) ;
170
133
}
171
134
172
- return value ;
135
+ return null ;
173
136
}
174
137
175
138
public static Object GetDefaultValue ( this Type type )
@@ -218,7 +181,7 @@ public static Object[] BuildArgs(this EngineInstance context, MethodBase method,
218
181
}
219
182
else
220
183
{
221
- args [ i + offset ] = arguments [ i ] . FromJsValue ( ) . As ( parameters [ i ] . ParameterType , context ) ;
184
+ args [ i + offset ] = arguments [ i ] . As ( parameters [ i ] . ParameterType , context ) ;
222
185
}
223
186
}
224
187
@@ -255,11 +218,16 @@ public static String[] GetParameterNames(this MethodInfo method)
255
218
return method != null ? method . GetParameters ( ) . Select ( m => m . Name ) . ToArray ( ) : null ;
256
219
}
257
220
258
- public static void AddConstructors ( this EngineInstance engine , ObjectInstance obj , Type type )
221
+ public static Assembly GetAssembly ( this Type type )
259
222
{
260
- foreach ( var exportedType in type . GetTypeInfo ( ) . Assembly . ExportedTypes )
223
+ return type . GetTypeInfo ( ) . Assembly ;
224
+ }
225
+
226
+ public static void AddConstructors ( this EngineInstance engine , ObjectInstance ctx , Assembly assembly )
227
+ {
228
+ foreach ( var exportedType in assembly . ExportedTypes )
261
229
{
262
- engine . AddConstructor ( obj , exportedType ) ;
230
+ engine . AddConstructor ( ctx , exportedType ) ;
263
231
}
264
232
}
265
233
@@ -273,14 +241,15 @@ public static void AddInstances(this EngineInstance engine, ObjectInstance obj,
273
241
274
242
public static void AddConstructor ( this EngineInstance engine , ObjectInstance obj , Type type )
275
243
{
276
- var info = type . GetTypeInfo ( ) . DeclaredConstructors . FirstOrDefault ( m =>
277
- m . GetCustomAttributes < DomConstructorAttribute > ( ) . Any ( ) ) ;
244
+ var ti = type . GetTypeInfo ( ) ;
245
+ var names = ti . GetCustomAttributes < DomNameAttribute > ( ) ;
246
+ var name = names . FirstOrDefault ( ) ;
278
247
279
- if ( info != null )
248
+ if ( name != null )
280
249
{
281
- var name = type . GetTypeInfo ( ) . GetOfficialName ( ) ;
282
- var constructor = new DomConstructorInstance ( engine , info ) ;
283
- obj . FastSetProperty ( name , new PropertyDescriptor ( constructor , false , true , false ) ) ;
250
+ var info = ti . DeclaredConstructors . FirstOrDefault ( m => m . GetCustomAttributes < DomConstructorAttribute > ( ) . Any ( ) ) ;
251
+ var constructor = info != null ? new DomConstructorInstance ( engine , info ) : new DomConstructorInstance ( engine , type ) ;
252
+ obj . FastSetProperty ( name . OfficialName , new PropertyDescriptor ( constructor , false , true , false ) ) ;
284
253
}
285
254
}
286
255
@@ -328,7 +297,16 @@ public static String GetOfficialName(this Type currentType, Type baseType)
328
297
329
298
if ( name == null )
330
299
{
331
- foreach ( var impl in ti . ImplementedInterfaces . Except ( baseType . GetTypeInfo ( ) . ImplementedInterfaces ) )
300
+ var interfaces = ti . ImplementedInterfaces ;
301
+
302
+ if ( baseType != null )
303
+ {
304
+ var bi = baseType . GetTypeInfo ( ) ;
305
+ var exclude = bi . ImplementedInterfaces ;
306
+ interfaces = interfaces . Except ( exclude ) ;
307
+ }
308
+
309
+ foreach ( var impl in interfaces )
332
310
{
333
311
name = impl . GetTypeInfo ( ) . GetCustomAttribute < DomNameAttribute > ( false ) ? . OfficialName ;
334
312
@@ -337,7 +315,46 @@ public static String GetOfficialName(this Type currentType, Type baseType)
337
315
}
338
316
}
339
317
340
- return name ?? currentType . Name ;
318
+ return name ;
319
+ }
320
+
321
+ private static Object AsComplex ( this JsValue value , Type targetType , EngineInstance engine )
322
+ {
323
+ var obj = value . FromJsValue ( ) ;
324
+ var sourceType = obj . GetType ( ) ;
325
+
326
+ if ( sourceType == targetType || sourceType . GetTypeInfo ( ) . IsSubclassOf ( targetType ) || targetType . GetTypeInfo ( ) . IsAssignableFrom ( sourceType . GetTypeInfo ( ) ) )
327
+ {
328
+ return obj ;
329
+ }
330
+ else if ( targetType . GetTypeInfo ( ) . IsSubclassOf ( typeof ( Delegate ) ) )
331
+ {
332
+ var f = obj as FunctionInstance ;
333
+
334
+ if ( f == null )
335
+ {
336
+ var b = obj as String ;
337
+
338
+ if ( b != null )
339
+ {
340
+ var e = engine . Jint ;
341
+ var p = new [ ] { new JsValue ( b ) } ;
342
+ f = new ClrFunctionInstance ( e , ( _this , args ) => e . Eval . Call ( _this , p ) ) ;
343
+ }
344
+ }
345
+
346
+ if ( f != null )
347
+ {
348
+ return targetType . ToDelegate ( f , engine ) ;
349
+ }
350
+ }
351
+
352
+ var method = sourceType . PrepareConvert ( targetType ) ;
353
+
354
+ if ( method == null )
355
+ throw new JavaScriptException ( "[Internal] Could not find corresponding cast target." ) ;
356
+
357
+ return method . Invoke ( obj , null ) ;
341
358
}
342
359
}
343
360
}
0 commit comments