Skip to content

Commit 24f0ebc

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore the value of a read-only field in an embedded object or type can no longer be changed
1 parent d021d8e commit 24f0ebc

File tree

4 files changed

+48
-58
lines changed

4 files changed

+48
-58
lines changed

src/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
<PackageIconFullPath>../../Icons/JavaScriptEngineSwitcher_ChakraCore_Logo128x128.png</PackageIconFullPath>
2525
<Description>JavaScriptEngineSwitcher.ChakraCore contains a `ChakraCoreJsEngine` adapter (wrapper for the ChakraCore).</Description>
2626
<PackageTags>$(PackageCommonTags);ChakraCore</PackageTags>
27-
<PackageReleaseNotes>Performed a migration to the modern C# null/not-null checks.</PackageReleaseNotes>
27+
<PackageReleaseNotes>1. Performed a migration to the modern C# null/not-null checks;
28+
2. The value of a read-only field in an embedded object or type can no longer be changed.</PackageReleaseNotes>
2829
</PropertyGroup>
2930

3031
<ItemGroup>

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/TypeMapper.cs

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -543,56 +543,59 @@ private void ProjectFields(EmbeddedItem externalItem)
543543
JsValue getMethodValue = JsValue.CreateFunction(nativeGetFunction);
544544
descriptorValue.SetProperty("get", getMethodValue, true);
545545

546-
JsNativeFunction nativeSetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
546+
if (!field.IsInitOnly)
547547
{
548-
JsValue undefinedValue = JsValue.Undefined;
549-
550-
if (instance && obj is null)
548+
JsNativeFunction nativeSetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
551549
{
552-
CreateAndSetTypeError(string.Format(
553-
Strings.Runtime_InvalidThisContextForHostObjectField, fieldName));
554-
return undefinedValue;
555-
}
550+
JsValue undefinedValue = JsValue.Undefined;
556551

557-
object value = MapToHostType(args[1]);
558-
ReflectionHelpers.FixFieldValueType(ref value, field);
552+
if (instance && obj is null)
553+
{
554+
CreateAndSetTypeError(string.Format(
555+
Strings.Runtime_InvalidThisContextForHostObjectField, fieldName));
556+
return undefinedValue;
557+
}
559558

560-
try
561-
{
562-
field.SetValue(obj, value);
563-
}
564-
catch (Exception e)
565-
{
566-
Exception exception = UnwrapException(e);
567-
var wrapperException = exception as WrapperException;
568-
JsValue errorValue;
559+
object value = MapToHostType(args[1]);
560+
ReflectionHelpers.FixFieldValueType(ref value, field);
569561

570-
if (wrapperException is not null)
562+
try
571563
{
572-
errorValue = CreateErrorFromWrapperException(wrapperException);
564+
field.SetValue(obj, value);
573565
}
574-
else
566+
catch (Exception e)
575567
{
576-
string errorMessage = instance ?
577-
string.Format(Strings.Runtime_HostObjectFieldSettingFailed, fieldName,
578-
exception.Message)
579-
:
580-
string.Format(Strings.Runtime_HostTypeFieldSettingFailed, fieldName, typeName,
581-
exception.Message)
582-
;
583-
errorValue = JsErrorHelpers.CreateError(errorMessage);
568+
Exception exception = UnwrapException(e);
569+
var wrapperException = exception as WrapperException;
570+
JsValue errorValue;
571+
572+
if (wrapperException is not null)
573+
{
574+
errorValue = CreateErrorFromWrapperException(wrapperException);
575+
}
576+
else
577+
{
578+
string errorMessage = instance ?
579+
string.Format(Strings.Runtime_HostObjectFieldSettingFailed, fieldName,
580+
exception.Message)
581+
:
582+
string.Format(Strings.Runtime_HostTypeFieldSettingFailed, fieldName, typeName,
583+
exception.Message)
584+
;
585+
errorValue = JsErrorHelpers.CreateError(errorMessage);
586+
}
587+
JsContext.SetException(errorValue);
588+
589+
return undefinedValue;
584590
}
585-
JsContext.SetException(errorValue);
586591

587592
return undefinedValue;
588-
}
589-
590-
return undefinedValue;
591-
};
592-
nativeFunctions.Add(nativeSetFunction);
593+
};
594+
nativeFunctions.Add(nativeSetFunction);
593595

594-
JsValue setMethodValue = JsValue.CreateFunction(nativeSetFunction);
595-
descriptorValue.SetProperty("set", setMethodValue, true);
596+
JsValue setMethodValue = JsValue.CreateFunction(nativeSetFunction);
597+
descriptorValue.SetProperty("set", setMethodValue, true);
598+
}
596599

597600
typeValue.DefineProperty(fieldName, descriptorValue);
598601
}
@@ -622,7 +625,7 @@ private void ProjectProperties(EmbeddedItem externalItem)
622625
JsValue descriptorValue = JsValue.CreateObject();
623626
descriptorValue.SetProperty("enumerable", JsValue.True, true);
624627

625-
if (property.GetGetMethod() is not null)
628+
if (property.CanRead)
626629
{
627630
JsNativeFunction nativeGetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
628631
{
@@ -677,7 +680,7 @@ private void ProjectProperties(EmbeddedItem externalItem)
677680
descriptorValue.SetProperty("get", getMethodValue, true);
678681
}
679682

680-
if (property.GetSetMethod() is not null)
683+
if (property.CanWrite)
681684
{
682685
JsNativeFunction nativeSetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
683686
{

src/JavaScriptEngineSwitcher.ChakraCore/readme.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
=============
3232
RELEASE NOTES
3333
=============
34-
Performed a migration to the modern C# null/not-null checks.
34+
1. Performed a migration to the modern C# null/not-null checks;
35+
2. The value of a read-only field in an embedded object or type can no longer be
36+
changed.
3537

3638
=============
3739
DOCUMENTATION

test/JavaScriptEngineSwitcher.Tests/ChakraCore/InteropTests.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ private IJsEngine CreateJsEngine(bool allowReflection)
3232

3333
#region Embedding of objects
3434

35-
#region Objects with fields
36-
37-
[Fact]
38-
public override void EmbeddingOfInstanceOfCustomValueTypeWithReadonlyField()
39-
{ }
40-
41-
#endregion
42-
4335
#region Objects with methods
4436

4537
[Fact]
@@ -468,14 +460,6 @@ string TestAllowReflectionSetting(bool allowReflection)
468460

469461
#endregion
470462

471-
#region Types with fields
472-
473-
[Fact]
474-
public override void EmbeddingOfCustomReferenceTypeWithReadonlyFields()
475-
{ }
476-
477-
#endregion
478-
479463
#region Types with methods
480464

481465
[Fact]

0 commit comments

Comments
 (0)