Skip to content

Commit 88a7882

Browse files
committed
Was made refactoring
1 parent d25d1d2 commit 88a7882

File tree

8 files changed

+229
-100
lines changed

8 files changed

+229
-100
lines changed

src/MsieJavaScriptEngine/JsRt/Edge/ChakraEdgeJsRtJsEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,14 +836,14 @@ private JsRuntimeException ConvertJsExceptionToJsRuntimeException(
836836
if (errorValue.HasProperty(linePropertyId))
837837
{
838838
EdgeJsValue linePropertyValue = errorValue.GetProperty(linePropertyId);
839-
lineNumber = (int)linePropertyValue.ConvertToNumber().ToDouble() + 1;
839+
lineNumber = linePropertyValue.ConvertToNumber().ToInt32() + 1;
840840
}
841841

842842
EdgeJsPropertyId columnPropertyId = EdgeJsPropertyId.FromString("column");
843843
if (errorValue.HasProperty(columnPropertyId))
844844
{
845845
EdgeJsValue columnPropertyValue = errorValue.GetProperty(columnPropertyId);
846-
columnNumber = (int)columnPropertyValue.ConvertToNumber().ToDouble() + 1;
846+
columnNumber = columnPropertyValue.ConvertToNumber().ToInt32() + 1;
847847
}
848848

849849
EdgeJsPropertyId sourcePropertyId = EdgeJsPropertyId.FromString("source");

src/MsieJavaScriptEngine/JsRt/Edge/EdgeJsErrorHelpers.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public static void ThrowIfError(JsErrorCode error)
1515
{
1616
switch (error)
1717
{
18+
#region Usage
19+
1820
case JsErrorCode.InvalidArgument:
1921
throw new JsUsageException(error, "Invalid argument.");
2022

@@ -45,9 +47,6 @@ public static void ThrowIfError(JsErrorCode error)
4547
case JsErrorCode.CannotDisableExecution:
4648
throw new JsUsageException(error, "Cannot disable execution.");
4749

48-
case JsErrorCode.AlreadyDebuggingContext:
49-
throw new JsUsageException(error, "Context is already in debug mode.");
50-
5150
case JsErrorCode.HeapEnumInProgress:
5251
throw new JsUsageException(error, "Heap enumeration is in progress.");
5352

@@ -63,15 +62,26 @@ public static void ThrowIfError(JsErrorCode error)
6362
case JsErrorCode.CannotSerializeDebugScript:
6463
throw new JsUsageException(error, "Cannot serialize a debug script.");
6564

65+
case JsErrorCode.AlreadyDebuggingContext:
66+
throw new JsUsageException(error, "Context is already in debug mode.");
67+
6668
case JsErrorCode.AlreadyProfilingContext:
6769
throw new JsUsageException(error, "Already profiling this context.");
6870

6971
case JsErrorCode.IdleNotEnabled:
7072
throw new JsUsageException(error, "Idle is not enabled.");
7173

74+
#endregion
75+
76+
#region Engine
77+
7278
case JsErrorCode.OutOfMemory:
7379
throw new JsEngineException(error, "Out of memory.");
7480

81+
#endregion
82+
83+
#region Script
84+
7585
case JsErrorCode.ScriptException:
7686
{
7787
EdgeJsValue errorObject;
@@ -104,9 +114,15 @@ public static void ThrowIfError(JsErrorCode error)
104114
case JsErrorCode.ScriptEvalDisabled:
105115
throw new EdgeJsScriptException(error, EdgeJsValue.Invalid, "Eval of strings is disabled in this runtime.");
106116

117+
#endregion
118+
119+
#region Fatal
120+
107121
case JsErrorCode.Fatal:
108122
throw new JsFatalException(error);
109123

124+
#endregion
125+
110126
default:
111127
throw new JsFatalException(error);
112128
}

src/MsieJavaScriptEngine/JsRt/Edge/EdgeJsValue.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,27 @@ public double ToDouble()
568568
return value;
569569
}
570570

571+
/// <summary>
572+
/// Retrieves a <c>int</c> value of a <c>Number</c> value
573+
/// </summary>
574+
/// <remarks>
575+
/// <para>
576+
/// This function retrieves the value of a Number value. It will fail with
577+
/// <c>InvalidArgument</c> if the type of the value is not <c>Number</c>.
578+
/// </para>
579+
/// <para>
580+
/// Requires an active script context.
581+
/// </para>
582+
/// </remarks>
583+
/// <returns>The <c>int</c> value</returns>
584+
public int ToInt32()
585+
{
586+
int value;
587+
EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsNumberToInt(this, out value));
588+
589+
return value;
590+
}
591+
571592
/// <summary>
572593
/// Retrieves a string pointer of a <c>String</c> value
573594
/// </summary>

src/MsieJavaScriptEngine/JsRt/Edge/EdgeNativeMethods.cs

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,46 @@ namespace MsieJavaScriptEngine.JsRt.Edge
1010
/// </summary>
1111
internal static class EdgeNativeMethods
1212
{
13+
#region Hosting
14+
15+
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
16+
internal static extern JsErrorCode JsParseScript(string script, JsSourceContext sourceContext,
17+
string sourceUrl, out EdgeJsValue result);
18+
19+
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
20+
internal static extern JsErrorCode JsRunScript(string script, JsSourceContext sourceContext,
21+
string sourceUrl, out EdgeJsValue result);
22+
23+
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
24+
internal static extern JsErrorCode JsSerializeScript(string script, byte[] buffer, ref ulong bufferSize);
25+
26+
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
27+
internal static extern JsErrorCode JsParseSerializedScript(string script, byte[] buffer,
28+
JsSourceContext sourceContext, string sourceUrl, out EdgeJsValue result);
29+
30+
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
31+
internal static extern JsErrorCode JsRunSerializedScript(string script, byte[] buffer,
32+
JsSourceContext sourceContext, string sourceUrl, out EdgeJsValue result);
33+
34+
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
35+
internal static extern JsErrorCode JsGetPropertyIdFromName(string name, out EdgeJsPropertyId propertyId);
36+
37+
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
38+
internal static extern JsErrorCode JsGetPropertyNameFromId(EdgeJsPropertyId propertyId, out IntPtr buffer);
39+
40+
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
41+
internal static extern JsErrorCode JsPointerToString(string value, UIntPtr stringLength,
42+
out EdgeJsValue stringValue);
43+
44+
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
45+
internal static extern JsErrorCode JsStringToPointer(EdgeJsValue value, out IntPtr stringValue,
46+
out UIntPtr stringLength);
47+
48+
#endregion
49+
1350
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
14-
internal static extern JsErrorCode JsCreateRuntime(JsRuntimeAttributes attributes, JsThreadServiceCallback threadService, out EdgeJsRuntime runtime);
51+
internal static extern JsErrorCode JsCreateRuntime(JsRuntimeAttributes attributes,
52+
JsThreadServiceCallback threadService, out EdgeJsRuntime runtime);
1553

1654
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
1755
internal static extern JsErrorCode JsCollectGarbage(EdgeJsRuntime handle);
@@ -29,10 +67,12 @@ internal static class EdgeNativeMethods
2967
internal static extern JsErrorCode JsSetRuntimeMemoryLimit(EdgeJsRuntime runtime, UIntPtr memoryLimit);
3068

3169
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
32-
internal static extern JsErrorCode JsSetRuntimeMemoryAllocationCallback(EdgeJsRuntime runtime, IntPtr callbackState, JsMemoryAllocationCallback allocationCallback);
70+
internal static extern JsErrorCode JsSetRuntimeMemoryAllocationCallback(EdgeJsRuntime runtime,
71+
IntPtr callbackState, JsMemoryAllocationCallback allocationCallback);
3372

3473
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
35-
internal static extern JsErrorCode JsSetRuntimeBeforeCollectCallback(EdgeJsRuntime runtime, IntPtr callbackState, JsBeforeCollectCallback beforeCollectCallback);
74+
internal static extern JsErrorCode JsSetRuntimeBeforeCollectCallback(EdgeJsRuntime runtime,
75+
IntPtr callbackState, JsBeforeCollectCallback beforeCollectCallback);
3676

3777
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode, EntryPoint = "JsAddRef")]
3878
internal static extern JsErrorCode JsContextAddRef(EdgeJsContext reference, out uint count);
@@ -64,27 +104,6 @@ internal static class EdgeNativeMethods
64104
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
65105
internal static extern JsErrorCode JsIdle(out uint nextIdleTick);
66106

67-
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
68-
internal static extern JsErrorCode JsParseScript(string script, JsSourceContext sourceContext, string sourceUrl, out EdgeJsValue result);
69-
70-
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
71-
internal static extern JsErrorCode JsRunScript(string script, JsSourceContext sourceContext, string sourceUrl, out EdgeJsValue result);
72-
73-
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
74-
internal static extern JsErrorCode JsSerializeScript(string script, byte[] buffer, ref ulong bufferSize);
75-
76-
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
77-
internal static extern JsErrorCode JsParseSerializedScript(string script, byte[] buffer, JsSourceContext sourceContext, string sourceUrl, out EdgeJsValue result);
78-
79-
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
80-
internal static extern JsErrorCode JsRunSerializedScript(string script, byte[] buffer, JsSourceContext sourceContext, string sourceUrl, out EdgeJsValue result);
81-
82-
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
83-
internal static extern JsErrorCode JsGetPropertyIdFromName(string name, out EdgeJsPropertyId propertyId);
84-
85-
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
86-
internal static extern JsErrorCode JsGetPropertyNameFromId(EdgeJsPropertyId propertyId, out IntPtr buffer);
87-
88107
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
89108
internal static extern JsErrorCode JsGetUndefinedValue(out EdgeJsValue undefinedValue);
90109

@@ -119,26 +138,25 @@ internal static class EdgeNativeMethods
119138
internal static extern JsErrorCode JsNumberToDouble(EdgeJsValue value, out double doubleValue);
120139

121140
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
122-
internal static extern JsErrorCode JsConvertValueToNumber(EdgeJsValue value, out EdgeJsValue numberValue);
141+
internal static extern JsErrorCode JsNumberToInt(EdgeJsValue value, out int intValue);
123142

124143
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
125-
internal static extern JsErrorCode JsGetStringLength(EdgeJsValue sringValue, out int length);
126-
127-
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
128-
internal static extern JsErrorCode JsPointerToString(string value, UIntPtr stringLength, out EdgeJsValue stringValue);
144+
internal static extern JsErrorCode JsConvertValueToNumber(EdgeJsValue value, out EdgeJsValue numberValue);
129145

130146
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
131-
internal static extern JsErrorCode JsStringToPointer(EdgeJsValue value, out IntPtr stringValue, out UIntPtr stringLength);
147+
internal static extern JsErrorCode JsGetStringLength(EdgeJsValue sringValue, out int length);
132148

133149
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
134150
internal static extern JsErrorCode JsConvertValueToString(EdgeJsValue value, out EdgeJsValue stringValue);
135151
#if !NETSTANDARD1_3
136152

137153
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
138-
internal static extern JsErrorCode JsVariantToValue([MarshalAs(UnmanagedType.Struct)] ref object var, out EdgeJsValue value);
154+
internal static extern JsErrorCode JsVariantToValue([MarshalAs(UnmanagedType.Struct)] ref object var,
155+
out EdgeJsValue value);
139156

140157
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
141-
internal static extern JsErrorCode JsValueToVariant(EdgeJsValue obj, [MarshalAs(UnmanagedType.Struct)] out object var);
158+
internal static extern JsErrorCode JsValueToVariant(EdgeJsValue obj,
159+
[MarshalAs(UnmanagedType.Struct)] out object var);
142160
#endif
143161

144162
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
@@ -148,7 +166,8 @@ internal static class EdgeNativeMethods
148166
internal static extern JsErrorCode JsCreateObject(out EdgeJsValue obj);
149167

150168
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
151-
internal static extern JsErrorCode JsCreateExternalObject(IntPtr data, JsObjectFinalizeCallback finalizeCallback, out EdgeJsValue obj);
169+
internal static extern JsErrorCode JsCreateExternalObject(IntPtr data,
170+
JsObjectFinalizeCallback finalizeCallback, out EdgeJsValue obj);
152171

153172
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
154173
internal static extern JsErrorCode JsConvertValueToObject(EdgeJsValue value, out EdgeJsValue obj);
@@ -166,34 +185,42 @@ internal static class EdgeNativeMethods
166185
internal static extern JsErrorCode JsPreventExtension(EdgeJsValue obj);
167186

168187
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
169-
internal static extern JsErrorCode JsGetProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId, out EdgeJsValue value);
188+
internal static extern JsErrorCode JsGetProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId,
189+
out EdgeJsValue value);
170190

171191
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
172-
internal static extern JsErrorCode JsGetOwnPropertyDescriptor(EdgeJsValue obj, EdgeJsPropertyId propertyId, out EdgeJsValue propertyDescriptor);
192+
internal static extern JsErrorCode JsGetOwnPropertyDescriptor(EdgeJsValue obj, EdgeJsPropertyId propertyId,
193+
out EdgeJsValue propertyDescriptor);
173194

174195
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
175196
internal static extern JsErrorCode JsGetOwnPropertyNames(EdgeJsValue obj, out EdgeJsValue propertyNames);
176197

177198
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
178-
internal static extern JsErrorCode JsSetProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId, EdgeJsValue value, bool useStrictRules);
199+
internal static extern JsErrorCode JsSetProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId,
200+
EdgeJsValue value, bool useStrictRules);
179201

180202
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
181-
internal static extern JsErrorCode JsHasProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId, out bool hasProperty);
203+
internal static extern JsErrorCode JsHasProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId,
204+
out bool hasProperty);
182205

183206
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
184-
internal static extern JsErrorCode JsDeleteProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId, bool useStrictRules, out EdgeJsValue result);
207+
internal static extern JsErrorCode JsDeleteProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId,
208+
bool useStrictRules, out EdgeJsValue result);
185209

186210
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
187-
internal static extern JsErrorCode JsDefineProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId, EdgeJsValue propertyDescriptor, out bool result);
211+
internal static extern JsErrorCode JsDefineProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId,
212+
EdgeJsValue propertyDescriptor, out bool result);
188213

189214
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
190215
internal static extern JsErrorCode JsHasIndexedProperty(EdgeJsValue obj, EdgeJsValue index, out bool result);
191216

192217
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
193-
internal static extern JsErrorCode JsGetIndexedProperty(EdgeJsValue obj, EdgeJsValue index, out EdgeJsValue result);
218+
internal static extern JsErrorCode JsGetIndexedProperty(EdgeJsValue obj, EdgeJsValue index,
219+
out EdgeJsValue result);
194220

195221
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
196-
internal static extern JsErrorCode JsSetIndexedProperty(EdgeJsValue obj, EdgeJsValue index, EdgeJsValue value);
222+
internal static extern JsErrorCode JsSetIndexedProperty(EdgeJsValue obj, EdgeJsValue index,
223+
EdgeJsValue value);
197224

198225
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
199226
internal static extern JsErrorCode JsDeleteIndexedProperty(EdgeJsValue obj, EdgeJsValue index);
@@ -217,13 +244,16 @@ internal static class EdgeNativeMethods
217244
internal static extern JsErrorCode JsCreateArray(uint length, out EdgeJsValue result);
218245

219246
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
220-
internal static extern JsErrorCode JsCallFunction(EdgeJsValue function, EdgeJsValue[] arguments, ushort argumentCount, out EdgeJsValue result);
247+
internal static extern JsErrorCode JsCallFunction(EdgeJsValue function, EdgeJsValue[] arguments,
248+
ushort argumentCount, out EdgeJsValue result);
221249

222250
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
223-
internal static extern JsErrorCode JsConstructObject(EdgeJsValue function, EdgeJsValue[] arguments, ushort argumentCount, out EdgeJsValue result);
251+
internal static extern JsErrorCode JsConstructObject(EdgeJsValue function, EdgeJsValue[] arguments,
252+
ushort argumentCount, out EdgeJsValue result);
224253

225254
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
226-
internal static extern JsErrorCode JsCreateFunction(EdgeJsNativeFunction nativeFunction, IntPtr externalData, out EdgeJsValue function);
255+
internal static extern JsErrorCode JsCreateFunction(EdgeJsNativeFunction nativeFunction,
256+
IntPtr externalData, out EdgeJsValue function);
227257

228258
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
229259
internal static extern JsErrorCode JsCreateError(EdgeJsValue message, out EdgeJsValue error);
@@ -262,7 +292,8 @@ internal static class EdgeNativeMethods
262292
internal static extern JsErrorCode JsIsRuntimeExecutionDisabled(EdgeJsRuntime runtime, out bool isDisabled);
263293

264294
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
265-
internal static extern JsErrorCode JsStartProfiling(IActiveScriptProfilerCallback callback, ProfilerEventMask eventMask, int context);
295+
internal static extern JsErrorCode JsStartProfiling(IActiveScriptProfilerCallback callback,
296+
ProfilerEventMask eventMask, int context);
266297

267298
[DllImport(DllName.Chakra, CharSet = CharSet.Unicode)]
268299
internal static extern JsErrorCode JsStopProfiling(int reason);

src/MsieJavaScriptEngine/JsRt/Ie/IeJsErrorHelpers.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public static void ThrowIfError(JsErrorCode error)
1515
{
1616
switch (error)
1717
{
18+
#region Usage
19+
1820
case JsErrorCode.InvalidArgument:
1921
throw new JsUsageException(error, "Invalid argument.");
2022

@@ -45,9 +47,6 @@ public static void ThrowIfError(JsErrorCode error)
4547
case JsErrorCode.CannotDisableExecution:
4648
throw new JsUsageException(error, "Cannot disable execution.");
4749

48-
case JsErrorCode.AlreadyDebuggingContext:
49-
throw new JsUsageException(error, "Context is already in debug mode.");
50-
5150
case JsErrorCode.HeapEnumInProgress:
5251
throw new JsUsageException(error, "Heap enumeration is in progress.");
5352

@@ -63,15 +62,26 @@ public static void ThrowIfError(JsErrorCode error)
6362
case JsErrorCode.CannotSerializeDebugScript:
6463
throw new JsUsageException(error, "Cannot serialize a debug script.");
6564

65+
case JsErrorCode.AlreadyDebuggingContext:
66+
throw new JsUsageException(error, "Context is already in debug mode.");
67+
6668
case JsErrorCode.AlreadyProfilingContext:
6769
throw new JsUsageException(error, "Already profiling this context.");
6870

6971
case JsErrorCode.IdleNotEnabled:
7072
throw new JsUsageException(error, "Idle is not enabled.");
7173

74+
#endregion
75+
76+
#region Engine
77+
7278
case JsErrorCode.OutOfMemory:
7379
throw new JsEngineException(error, "Out of memory.");
7480

81+
#endregion
82+
83+
#region Script
84+
7585
case JsErrorCode.ScriptException:
7686
{
7787
IeJsValue errorObject;
@@ -104,9 +114,15 @@ public static void ThrowIfError(JsErrorCode error)
104114
case JsErrorCode.ScriptEvalDisabled:
105115
throw new IeJsScriptException(error, IeJsValue.Invalid, "Eval of strings is disabled in this runtime.");
106116

117+
#endregion
118+
119+
#region Fatal
120+
107121
case JsErrorCode.Fatal:
108122
throw new JsFatalException(error);
109123

124+
#endregion
125+
110126
default:
111127
throw new JsFatalException(error);
112128
}

0 commit comments

Comments
 (0)