Skip to content

Commit 0fbcec6

Browse files
committed
Implemented simple conversion #26
1 parent 4ef54be commit 0fbcec6

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/AngleSharp.Scripting.JavaScript/Extensions.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Jint.Native;
77
using Jint.Native.Function;
88
using Jint.Native.Object;
9+
using Jint.Native.String;
910
using Jint.Runtime;
1011
using Jint.Runtime.Descriptors;
1112
using Jint.Runtime.Interop;
@@ -113,9 +114,26 @@ public static Object As(this Object value, Type targetType, EngineInstance engin
113114
return (Int32)(Double)value;
114115
}
115116

116-
if (targetType.GetTypeInfo().IsSubclassOf(typeof(Delegate)) && value is FunctionInstance)
117+
if (targetType.GetTypeInfo().IsSubclassOf(typeof(Delegate)))
117118
{
118-
return targetType.ToDelegate((FunctionInstance)value, engine);
119+
var f = value as FunctionInstance;
120+
121+
if (f == null)
122+
{
123+
var b = value as String;
124+
125+
if (b != null)
126+
{
127+
var e = engine.Jint;
128+
var p = new[] { new JsValue(b) };
129+
f = new ClrFunctionInstance(e, (_this, args) => e.Eval.Call(_this, p));
130+
}
131+
}
132+
133+
if (f != null)
134+
{
135+
return targetType.ToDelegate(f, engine);
136+
}
119137
}
120138

121139
var method = sourceType.PrepareConvert(targetType);

0 commit comments

Comments
 (0)