@@ -98,78 +98,41 @@ public static Object FromJsValue(this JsValue val)
9898 return val . ToObject ( ) ;
9999 }
100100
101- public static Object As ( this Object value , Type targetType , EngineInstance engine )
101+ public static Object As ( this JsValue value , Type targetType , EngineInstance engine )
102102 {
103- if ( value != null )
103+ if ( value != JsValue . Null )
104104 {
105- var sourceType = value . GetType ( ) ;
106-
107- if ( sourceType == targetType || sourceType . GetTypeInfo ( ) . IsSubclassOf ( targetType ) || targetType . GetTypeInfo ( ) . IsAssignableFrom ( sourceType . GetTypeInfo ( ) ) )
105+ if ( targetType == typeof ( Int32 ) )
108106 {
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 ) ;
120108 }
121109 else if ( targetType == typeof ( Double ) )
122110 {
123- var v = value . ToJsValue ( engine ) ;
124- return TypeConverter . ToNumber ( v ) ;
111+ return TypeConverter . ToNumber ( value ) ;
125112 }
126113 else if ( targetType == typeof ( String ) )
127114 {
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 ( ) ;
136116 }
137117 else if ( targetType == typeof ( Boolean ) )
138118 {
139- var v = value . ToJsValue ( engine ) ;
140- return TypeConverter . ToBoolean ( v ) ;
119+ return TypeConverter . ToBoolean ( value ) ;
141120 }
142- else if ( targetType . GetTypeInfo ( ) . IsSubclassOf ( typeof ( Delegate ) ) )
121+ else if ( targetType == typeof ( UInt32 ) )
143122 {
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 ) ;
162132 }
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 ) ;
170133 }
171134
172- return value ;
135+ return null ;
173136 }
174137
175138 public static Object GetDefaultValue ( this Type type )
@@ -218,7 +181,7 @@ public static Object[] BuildArgs(this EngineInstance context, MethodBase method,
218181 }
219182 else
220183 {
221- args [ i + offset ] = arguments [ i ] . FromJsValue ( ) . As ( parameters [ i ] . ParameterType , context ) ;
184+ args [ i + offset ] = arguments [ i ] . As ( parameters [ i ] . ParameterType , context ) ;
222185 }
223186 }
224187
@@ -255,11 +218,16 @@ public static String[] GetParameterNames(this MethodInfo method)
255218 return method != null ? method . GetParameters ( ) . Select ( m => m . Name ) . ToArray ( ) : null ;
256219 }
257220
258- public static void AddConstructors ( this EngineInstance engine , ObjectInstance obj , Type type )
221+ public static Assembly GetAssembly ( this Type type )
259222 {
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 )
261229 {
262- engine . AddConstructor ( obj , exportedType ) ;
230+ engine . AddConstructor ( ctx , exportedType ) ;
263231 }
264232 }
265233
@@ -273,14 +241,15 @@ public static void AddInstances(this EngineInstance engine, ObjectInstance obj,
273241
274242 public static void AddConstructor ( this EngineInstance engine , ObjectInstance obj , Type type )
275243 {
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 ( ) ;
278247
279- if ( info != null )
248+ if ( name != null )
280249 {
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 ) ) ;
284253 }
285254 }
286255
@@ -328,7 +297,16 @@ public static String GetOfficialName(this Type currentType, Type baseType)
328297
329298 if ( name == null )
330299 {
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 )
332310 {
333311 name = impl . GetTypeInfo ( ) . GetCustomAttribute < DomNameAttribute > ( false ) ? . OfficialName ;
334312
@@ -337,7 +315,46 @@ public static String GetOfficialName(this Type currentType, Type baseType)
337315 }
338316 }
339317
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 ) ;
341358 }
342359 }
343360}
0 commit comments