Skip to content

Commit 8dc0cae

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore fixed a wrong implementation of destruction of the embedded delegates
1 parent 9a71448 commit 8dc0cae

File tree

6 files changed

+30
-39
lines changed

6 files changed

+30
-39
lines changed

Diff for: src/JavaScriptEngineSwitcher.ChakraCore/Helpers/ReflectionHelpers.cs

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ public static BindingFlags GetDefaultBindingFlags(bool instance)
2727
return bindingFlags;
2828
}
2929

30+
public static bool IsFullyFledgedMethod(MethodInfo method)
31+
{
32+
if (!method.Attributes.HasFlag(MethodAttributes.SpecialName))
33+
{
34+
return true;
35+
}
36+
37+
string name = method.Name;
38+
bool isFullyFledged = !(name.StartsWith("get_", StringComparison.Ordinal)
39+
|| name.StartsWith("set_", StringComparison.Ordinal));
40+
41+
return isFullyFledged;
42+
}
43+
3044
public static void FixFieldValueType(ref object value, FieldInfo field)
3145
{
3246
Type valueType = value.GetType();

Diff for: src/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ This package does not contain the native implementations of ChakraCore. Therefor
2222
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</Description>
2323
<PackageIconUrl>https://raw.githubusercontent.com/Taritsyn/JavaScriptEngineSwitcher/master/Icons/JavaScriptEngineSwitcher_ChakraCore_Logo128x128.png</PackageIconUrl>
2424
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript;ChakraCore</PackageTags>
25-
<PackageReleaseNotes>Reduced a memory consumption in cases, where not used the embedding of objects and types.</PackageReleaseNotes>
25+
<PackageReleaseNotes>1. Reduced a memory consumption in cases, where not used the embedding of objects and types;
26+
2. Fixed a wrong implementation of destruction of the embedded delegates.</PackageReleaseNotes>
2627
</PropertyGroup>
2728

2829
<Import Project="../../build/common.props" />

Diff for: src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsValue.cs

-18
Original file line numberDiff line numberDiff line change
@@ -393,24 +393,6 @@ public static JsValue CreateExternalObject(IntPtr data, JsFinalizeCallback final
393393
return reference;
394394
}
395395

396-
/// <summary>
397-
/// Creates a new <c>Object</c> (with prototype) that stores some external data
398-
/// </summary>
399-
/// <remarks>
400-
/// Requires an active script context.
401-
/// </remarks>
402-
/// <param name="data">External data that the object will represent. May be null.</param>
403-
/// <param name="finalizer">A callback for when the object is finalized. May be null.</param>
404-
/// <param name="prototype">Prototype object or null</param>
405-
/// <returns>The new <c>Object</c></returns>
406-
public static JsValue CreateExternalObjectWithPrototype(IntPtr data, JsFinalizeCallback finalizer, JsValue prototype)
407-
{
408-
JsValue reference;
409-
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateExternalObjectWithPrototype(data, finalizer, prototype, out reference));
410-
411-
return reference;
412-
}
413-
414396
/// <summary>
415397
/// Creates a new JavaScript function
416398
/// </summary>

Diff for: src/JavaScriptEngineSwitcher.ChakraCore/JsRt/NativeMethods.cs

-4
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ internal static extern JsErrorCode JsGetPropertyIdType(JsPropertyId propertyId,
148148
internal static extern JsErrorCode JsCreateExternalObject(IntPtr data,
149149
JsFinalizeCallback finalizeCallback, out JsValue obj);
150150

151-
[DllImport(DllName.Universal)]
152-
internal static extern JsErrorCode JsCreateExternalObjectWithPrototype(IntPtr data,
153-
JsFinalizeCallback finalizeCallback, JsValue prototype, out JsValue obj);
154-
155151
[DllImport(DllName.Universal)]
156152
internal static extern JsErrorCode JsConvertValueToObject(JsValue value, out JsValue obj);
157153

Diff for: src/JavaScriptEngineSwitcher.ChakraCore/JsRt/TypeMapper.cs

+11-14
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,12 @@ private EmbeddedObject CreateEmbeddedFunction(Delegate del)
313313
return resultValue;
314314
};
315315

316-
JsValue functionValue = JsValue.CreateFunction(nativeFunction);
316+
GCHandle delHandle = GCHandle.Alloc(del);
317+
IntPtr delPtr = GCHandle.ToIntPtr(delHandle);
318+
JsValue prototypeValue = JsValue.CreateExternalObject(delPtr, _embeddedObjectFinalizeCallback);
317319

318-
GCHandle objHandle = GCHandle.Alloc(del);
319-
IntPtr objPtr = GCHandle.ToIntPtr(objHandle);
320-
JsValue objValue = JsValue.CreateExternalObjectWithPrototype(objPtr,
321-
_embeddedObjectFinalizeCallback, functionValue);
320+
JsValue functionValue = JsValue.CreateFunction(nativeFunction);
321+
functionValue.Prototype = prototypeValue;
322322

323323
var embeddedObject = new EmbeddedObject(del, functionValue,
324324
new List<JsNativeFunction> { nativeFunction });
@@ -419,23 +419,22 @@ private EmbeddedType CreateEmbeddedType(Type type)
419419
return resultValue;
420420
};
421421

422-
JsValue constructorValue = JsValue.CreateFunction(nativeConstructorFunction);
423-
424422
string embeddedTypeKey = type.AssemblyQualifiedName;
425423
GCHandle embeddedTypeKeyHandle = GCHandle.Alloc(embeddedTypeKey);
426424
IntPtr embeddedTypeKeyPtr = GCHandle.ToIntPtr(embeddedTypeKeyHandle);
427425
JsValue prototypeValue = JsValue.CreateExternalObject(embeddedTypeKeyPtr,
428426
_embeddedTypeFinalizeCallback);
429-
constructorValue.Prototype = prototypeValue;
430-
prototypeValue.SetProperty("constructor", constructorValue, true);
431427

432-
var embeddedType = new EmbeddedType(type, constructorValue,
428+
JsValue typeValue = JsValue.CreateFunction(nativeConstructorFunction);
429+
typeValue.Prototype = prototypeValue;
430+
431+
var embeddedType = new EmbeddedType(type, typeValue,
433432
new List<JsNativeFunction> { nativeConstructorFunction });
434433

435434
ProjectFields(embeddedType);
436435
ProjectProperties(embeddedType);
437436
ProjectMethods(embeddedType);
438-
FreezeObject(constructorValue);
437+
FreezeObject(typeValue);
439438

440439
return embeddedType;
441440
}
@@ -697,9 +696,7 @@ private void ProjectMethods(EmbeddedItem externalItem)
697696
string typeName = type.FullName;
698697
BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);
699698
IEnumerable<MethodInfo> methods = type.GetMethods(defaultBindingFlags)
700-
.Where(m => !(m.Attributes.HasFlag(MethodAttributes.SpecialName)
701-
&& (m.Name.StartsWith("get_") || m.Name.StartsWith("set_"))))
702-
;
699+
.Where(ReflectionHelpers.IsFullyFledgedMethod);
703700
IEnumerable<IGrouping<string, MethodInfo>> methodGroups = methods.GroupBy(m => m.Name);
704701

705702
foreach (IGrouping<string, MethodInfo> methodGroup in methodGroups)

Diff for: src/JavaScriptEngineSwitcher.ChakraCore/readme.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
Reduced a memory consumption in cases, where not used the embedding of objects
34-
and types.
33+
1. Reduced a memory consumption in cases, where not used the embedding of objects
34+
and types;
35+
2. Fixed a wrong implementation of destruction of the embedded delegates.
3536

3637
=============
3738
DOCUMENTATION

0 commit comments

Comments
 (0)