-
Notifications
You must be signed in to change notification settings - Fork 120
Дешевле обёртка вызова методов контекста #1747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
EvilBeaver
merged 1 commit into
EvilBeaver:develop
from
sfaqer:feature/context-call-marshalling
Sep 27, 2026
+211
−43
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/Tests/OneScript.Core.Tests/ContextValuesMarshallerTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /*---------------------------------------------------------- | ||
| This Source Code Form is subject to the terms of the | ||
| Mozilla Public License, v.2.0. If a copy of the MPL | ||
| was not distributed with this file, You can obtain one | ||
| at http://mozilla.org/MPL/2.0/. | ||
| ----------------------------------------------------------*/ | ||
|
|
||
| using FluentAssertions; | ||
| using OneScript.Contexts; | ||
| using OneScript.Exceptions; | ||
| using OneScript.Execution; | ||
| using OneScript.Values; | ||
| using ScriptEngine.Machine; | ||
| using ScriptEngine.Machine.Contexts; | ||
| using Xunit; | ||
|
|
||
| namespace OneScript.Core.Tests | ||
| { | ||
| public class ContextValuesMarshallerTest | ||
| { | ||
| [Fact] | ||
| public void ConvertParam_Int() | ||
| { | ||
| ContextValuesMarshaller.ConvertParam(ValueFactory.Create(5), 0).Should().Be(5); | ||
| ContextValuesMarshaller.ConvertParam(ValueFactory.Create("12"), 0).Should().Be(12); | ||
| ContextValuesMarshaller.ConvertParam(Variable.Create(ValueFactory.Create(3), "x"), 0).Should().Be(3); | ||
| ContextValuesMarshaller.ConvertParam(BslSkippedParameterValue.Instance, 7).Should().Be(7); | ||
| ContextValuesMarshaller.ConvertParam(ValueFactory.Create(), 7).Should().Be(7); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ConvertParam_Int_Overflow() | ||
| { | ||
| var huge = ValueFactory.Create(100_000_000_000m); | ||
|
|
||
| Assert.Throws<RuntimeException>(() => ContextValuesMarshaller.ConvertParam(huge, 0)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ConvertParam_Primitives() | ||
| { | ||
| ContextValuesMarshaller.ConvertParam(ValueFactory.Create(1.5m), 0m).Should().Be(1.5m); | ||
| ContextValuesMarshaller.ConvertParam(ValueFactory.Create(true), false).Should().BeTrue(); | ||
| ContextValuesMarshaller.ConvertParam(ValueFactory.Create(1), false).Should().BeTrue(); | ||
| ContextValuesMarshaller.ConvertParam(ValueFactory.Create("строка"), "").Should().Be("строка"); | ||
| ContextValuesMarshaller.ConvertParam(ValueFactory.Create(5), "").Should().Be("5"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ConvertReturnValue_Primitives() | ||
| { | ||
| ContextValuesMarshaller.ConvertReturnValue(true).Should().BeSameAs(BslBooleanValue.True); | ||
| ContextValuesMarshaller.ConvertReturnValue(5).AsNumber().Should().Be(5); | ||
| ContextValuesMarshaller.ConvertReturnValue(1.5m).AsNumber().Should().Be(1.5m); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ContextMethod_CallThroughWrapper() | ||
| { | ||
| Call("Повторить", ValueFactory.Create("ab"), ValueFactory.Create(2)).AsString(ForbiddenBslProcess.Instance).Should().Be("abab"); | ||
| Call("Повторить", ValueFactory.Create(5), BslSkippedParameterValue.Instance).AsString(ForbiddenBslProcess.Instance).Should().Be("5"); | ||
|
|
||
| Call("Удвоить", ValueFactory.Create(5)).AsNumber().Should().Be(10); | ||
| Call("Удвоить", ValueFactory.Create("6")).AsNumber().Should().Be(12); | ||
| Call("Удвоить", Variable.Create(ValueFactory.Create(3), "x")).AsNumber().Should().Be(6); | ||
| Call("Удвоить", ValueFactory.Create()).AsNumber().Should().Be(14); | ||
| Call("Удвоить", BslSkippedParameterValue.Instance).AsNumber().Should().Be(14); | ||
|
|
||
| Call("Половина", ValueFactory.Create(3m)).AsNumber().Should().Be(1.5m); | ||
|
|
||
| Call("Не", ValueFactory.Create(true)).Should().BeSameAs(BslBooleanValue.False); | ||
| Call("Не", ValueFactory.Create(0)).Should().BeSameAs(BslBooleanValue.True); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ContextMethod_CallThroughWrapper_IntOverflow() | ||
| { | ||
| Assert.Throws<RuntimeException>(() => Call("Удвоить", ValueFactory.Create(100_000_000_000m))); | ||
| } | ||
|
|
||
| private static IValue Call(string methodName, params IValue[] arguments) | ||
| { | ||
| var context = new MarshallingTestContext(); | ||
| context.CallAsFunction(context.GetMethodNumber(methodName), arguments, out var result, ForbiddenBslProcess.Instance); | ||
| return result; | ||
| } | ||
|
|
||
| [ContextClass("ТестМаршаллинга", "MarshallingTest")] | ||
| private class MarshallingTestContext : AutoContext<MarshallingTestContext> | ||
| { | ||
| [ContextMethod("Повторить", "Repeat")] | ||
| public string Repeat(string text, int count = 1) | ||
| { | ||
| return string.Concat(System.Linq.Enumerable.Repeat(text, count)); | ||
| } | ||
|
|
||
| [ContextMethod("Удвоить", "Twice")] | ||
| public int Twice(int value = 7) | ||
| { | ||
| return value * 2; | ||
| } | ||
|
|
||
| [ContextMethod("Половина", "Half")] | ||
| public decimal Half(decimal value) | ||
| { | ||
| return value / 2; | ||
| } | ||
|
|
||
| [ContextMethod("Не", "Not")] | ||
| public bool Not(bool value) | ||
| { | ||
| return !value; | ||
| } | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
обычный switch не будет тут выгоднее Dictionary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Выбор делается один раз — при построении обёртки метода, а не на вызове, так что скорость тут не важна. Словарь — ради точного совпадения типа: switch по
Type.GetTypeCodeотнёс бы кintи перечисления с базовымint, а цепочкаifпоtypeofвышла бы длиннее. Если читается хуже — переделаю наif.