Skip to content

Commit 0a45c4e

Browse files
committed
Implemented more type casts #8
1 parent f1a0c63 commit 0a45c4e

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/AngleSharp.Scripting.JavaScript/Extensions.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Jint.Native;
77
using Jint.Native.Function;
88
using Jint.Native.Object;
9-
using Jint.Native.String;
109
using Jint.Runtime;
1110
using Jint.Runtime.Descriptors;
1211
using Jint.Runtime.Interop;
@@ -91,7 +90,7 @@ public static Object FromJsValue(this JsValue val)
9190
var node = obj as DomNodeInstance;
9291
return node != null ? node.Value : obj;
9392
case Types.Undefined:
94-
return "undefined";
93+
return Undefined.Text;
9594
case Types.Null:
9695
return null;
9796
}
@@ -109,12 +108,38 @@ public static Object As(this Object value, Type targetType, EngineInstance engin
109108
{
110109
return value;
111110
}
112-
else if (sourceType == typeof(Double) && targetType == typeof(Int32))
111+
else if (targetType == typeof(Int32))
113112
{
113+
if (sourceType != typeof(Double))
114+
{
115+
var v = value.ToJsValue(engine);
116+
return TypeConverter.ToInt32(v);
117+
}
118+
114119
return (Int32)(Double)value;
115120
}
121+
else if (targetType == typeof(Double))
122+
{
123+
var v = value.ToJsValue(engine);
124+
return TypeConverter.ToNumber(v);
125+
}
126+
else if (targetType == typeof(String))
127+
{
128+
var v = value.ToJsValue(engine);
116129

117-
if (targetType.GetTypeInfo().IsSubclassOf(typeof(Delegate)))
130+
if (v.IsPrimitive())
131+
{
132+
return TypeConverter.ToString(v);
133+
}
134+
135+
return v.ToString();
136+
}
137+
else if (targetType == typeof(Boolean))
138+
{
139+
var v = value.ToJsValue(engine);
140+
return TypeConverter.ToBoolean(v);
141+
}
142+
else if (targetType.GetTypeInfo().IsSubclassOf(typeof(Delegate)))
118143
{
119144
var f = value as FunctionInstance;
120145

0 commit comments

Comments
 (0)