protected override ExpressionSyntax GenerateAllocationExpression(
     TypePositionInfo info,
     StubCodeContext context,
     SyntaxToken byteLengthIdentifier,
     out bool allocationRequiresByteLength)
 {
     allocationRequiresByteLength = false;
     return(CastExpression(
                AsNativeType(info),
                StringMarshaller.AllocationExpression(CharEncoding.Utf16, context.GetIdentifiers(info).managed)));
 }
Beispiel #2
0
        public void TryPackUnpack_NullString()
        {
            var m    = new StringMarshaller();
            var data = new string[]
            {
                null,
                Constants.NULL_VALUE,
                Constants.NULL_VALUE.ToLowerInvariant(),
                Constants.NULL_VALUE.ToUpperInvariant(),
                Constants.NULL_VALUE + "Hello",
                " " + Constants.NULL_VALUE,
                " " + Constants.NULL_VALUE + "Hello",
                Constants.NULL_VALUE_ESCAPE_PERFIX + Constants.NULL_VALUE,
                Constants.NULL_VALUE_ESCAPE_PERFIX.ToString(),
                "SomeString " + Constants.NULL_VALUE,
            };

            test(m, data);
        }
Beispiel #3
0
        public override IEnumerable <StatementSyntax> Generate(TypePositionInfo info, StubCodeContext context)
        {
            (string managedIdentifier, string nativeIdentifier) = context.GetIdentifiers(info);
            switch (context.CurrentStage)
            {
            case StubCodeContext.Stage.Setup:
                if (TryGenerateSetupSyntax(info, context, out StatementSyntax conditionalAllocSetup))
                {
                    yield return(conditionalAllocSetup);
                }

                break;

            case StubCodeContext.Stage.Marshal:
                if (info.RefKind != RefKind.Out)
                {
                    // <native> = (byte*)Marshal.StringToCoTaskMemAnsi(<managed>);
                    BlockSyntax windowsBlock = Block(
                        ExpressionStatement(
                            AssignmentExpression(
                                SyntaxKind.SimpleAssignmentExpression,
                                IdentifierName(nativeIdentifier),
                                CastExpression(
                                    AsNativeType(info),
                                    StringMarshaller.AllocationExpression(CharEncoding.Ansi, managedIdentifier)))));

                    // Set the allocation marker to true if it is being used
                    if (UsesConditionalStackAlloc(info, context))
                    {
                        // <allocationMarker> = true
                        windowsBlock = windowsBlock.AddStatements(
                            ExpressionStatement(
                                AssignmentExpression(
                                    SyntaxKind.SimpleAssignmentExpression,
                                    IdentifierName(GetAllocationMarkerIdentifier(info, context)),
                                    LiteralExpression(SyntaxKind.TrueLiteralExpression))));
                    }

                    // [Compat] The generated source for ANSI string marshalling does not optimize for
                    // allocating on the stack based on the string length. It always uses AllocCoTaskMem.
                    // if (OperatingSystem.IsWindows())
                    // {
                    //     <native> = (byte*)Marshal.StringToCoTaskMemAnsi(<managed>);
                    // }
                    // else
                    // {
                    //     << marshal as UTF-8 >>
                    // }
                    yield return(IfStatement(IsWindows,
                                             windowsBlock,
                                             ElseClause(
                                                 Block(_utf8StringMarshaller.Generate(info, context)))));
                }
                break;

            case StubCodeContext.Stage.Unmarshal:
                if (info.IsManagedReturnPosition || (info.IsByRef && info.RefKind != RefKind.In))
                {
                    // if (OperatingSystem.IsWindows())
                    // {
                    //     <managed> = <native> == null ? null : new string((sbyte*)<native>);
                    // }
                    // else
                    // {
                    //     << unmarshal as UTF-8 >>
                    // }
                    yield return(IfStatement(IsWindows,
                                             Block(
                                                 ExpressionStatement(
                                                     AssignmentExpression(
                                                         SyntaxKind.SimpleAssignmentExpression,
                                                         IdentifierName(managedIdentifier),
                                                         ConditionalExpression(
                                                             BinaryExpression(
                                                                 SyntaxKind.EqualsExpression,
                                                                 IdentifierName(nativeIdentifier),
                                                                 LiteralExpression(SyntaxKind.DefaultLiteralExpression)),
                                                             LiteralExpression(SyntaxKind.NullLiteralExpression),
                                                             ObjectCreationExpression(
                                                                 PredefinedType(Token(SyntaxKind.StringKeyword)),
                                                                 ArgumentList(SingletonSeparatedList(
                                                                                  Argument(
                                                                                      CastExpression(
                                                                                          PointerType(PredefinedType(Token(SyntaxKind.SByteKeyword))),
                                                                                          IdentifierName(nativeIdentifier))))),
                                                                 initializer: null))))),
                                             ElseClause(
                                                 Block(_utf8StringMarshaller.Generate(info, context)))));
                }
                break;

            case StubCodeContext.Stage.Cleanup:
                yield return(GenerateConditionalAllocationFreeSyntax(info, context));

                break;
            }
        }
Beispiel #4
0
 protected override ExpressionSyntax GenerateFreeExpression(TypePositionInfo info, StubCodeContext context)
 {
     return(StringMarshaller.FreeExpression(context.GetIdentifiers(info).native));
 }
Beispiel #5
0
        private IEnumerable <string> GetAssociatedImageNames()
        {
            var imageNames = StringMarshaller.Marshal(OpenSlideDll.openslide_get_associated_image_names(osr));

            return(imageNames);
        }
Beispiel #6
0
        private IEnumerable <string> GetPropertyNames()
        {
            var propertyNames = StringMarshaller.Marshal(OpenSlideDll.openslide_get_property_names(osr));

            return(propertyNames);
        }
Beispiel #7
0
        public void TryPack_StringArray()
        {
            var m = new StringMarshaller();

            Assert.Throws <ArrayPackingNotSupportedException>(() => m.TryPack(new string[] { "one", "two!" }, out ConfigValue result));
        }
 private void SetText(TASKDIALOG_ELEMENTS tde, string text, bool relayout)
 {
     using (StringMarshaller sm = new StringMarshaller(text))
     {
         SendMessage(
             relayout ? TASKDIALOG_MESSAGES.TDM_SET_ELEMENT_TEXT : TASKDIALOG_MESSAGES.TDM_UPDATE_ELEMENT_TEXT,
             (int)tde,
             sm.Value.ToInt32());
     }
 }