public JSImportCodeGenerator( StubEnvironment environment, ImmutableArray <TypePositionInfo> argTypes, JSImportData attributeData, JSSignatureContext signatureContext, Action <TypePositionInfo, MarshallingNotSupportedException> marshallingNotSupportedCallback, IMarshallingGeneratorFactory generatorFactory) { _signatureContext = signatureContext; ManagedToNativeStubCodeContext innerContext = new ManagedToNativeStubCodeContext(environment, ReturnIdentifier, ReturnIdentifier); _context = new JSImportCodeContext(attributeData, innerContext); _marshallers = new BoundGenerators(argTypes, CreateGenerator); if (_marshallers.ManagedReturnMarshaller.Generator.UsesNativeIdentifier(_marshallers.ManagedReturnMarshaller.TypeInfo, null)) { // If we need a different native return identifier, then recreate the context with the correct identifier before we generate any code. innerContext = new ManagedToNativeStubCodeContext(environment, ReturnIdentifier, ReturnNativeIdentifier); _context = new JSImportCodeContext(attributeData, innerContext); _marshallers = new BoundGenerators(argTypes, CreateGenerator); } // validate task + span mix if (_marshallers.ManagedReturnMarshaller.TypeInfo.ManagedType is JSTaskTypeInfo) { BoundGenerator spanArg = _marshallers.AllMarshallers.FirstOrDefault(m => m.TypeInfo.ManagedType is JSSpanTypeInfo); if (spanArg != default) { marshallingNotSupportedCallback(spanArg.TypeInfo, new MarshallingNotSupportedException(spanArg.TypeInfo, _context) { NotSupportedDetails = SR.SpanAndTaskNotSupported }); } } IMarshallingGenerator CreateGenerator(TypePositionInfo p) { try { return(generatorFactory.Create(p, _context)); } catch (MarshallingNotSupportedException e) { marshallingNotSupportedCallback(p, e); return(new EmptyJSGenerator()); } } }
public PInvokeStubCodeGenerator( StubEnvironment environment, ImmutableArray <TypePositionInfo> argTypes, bool setLastError, Action <TypePositionInfo, MarshallingNotSupportedException> marshallingNotSupportedCallback, IMarshallingGeneratorFactory generatorFactory) { _setLastError = setLastError; // Support for SetLastError logic requires .NET 6+. Initialize the // supports target framework value with this value. if (_setLastError) { SupportsTargetFramework = environment.TargetFramework == TargetFramework.Net && environment.TargetFrameworkVersion.Major >= 6; } else { SupportsTargetFramework = true; } _context = new ManagedToNativeStubCodeContext(environment, ReturnIdentifier, ReturnIdentifier); _marshallers = new BoundGenerators(argTypes, CreateGenerator); if (_marshallers.ManagedReturnMarshaller.Generator.UsesNativeIdentifier(_marshallers.ManagedReturnMarshaller.TypeInfo, _context)) { // If we need a different native return identifier, then recreate the context with the correct identifier before we generate any code. _context = new ManagedToNativeStubCodeContext(environment, ReturnIdentifier, $"{ReturnIdentifier}{StubCodeContext.GeneratedNativeIdentifierSuffix}"); } bool noMarshallingNeeded = true; foreach (BoundGenerator generator in _marshallers.AllMarshallers) { // Check if marshalling info and generator support the current target framework. SupportsTargetFramework &= generator.TypeInfo.MarshallingAttributeInfo is not MissingSupportMarshallingInfo && generator.Generator.IsSupported(environment.TargetFramework, environment.TargetFrameworkVersion); // Check if generator is either blittable or just a forwarder. noMarshallingNeeded &= generator is { Generator : BlittableMarshaller, TypeInfo.IsByRef : false } or { Generator : Forwarder }; } StubIsBasicForwarder = !setLastError && _marshallers.ManagedNativeSameReturn && // If the managed return has native return position, then it's the return for both. noMarshallingNeeded; IMarshallingGenerator CreateGenerator(TypePositionInfo p) { try { return(generatorFactory.Create(p, _context)); } catch (MarshallingNotSupportedException e) { marshallingNotSupportedCallback(p, e); return(new Forwarder()); } } }