コード例 #1
0
        /// <inheritdoc />
        protected override EvaluationResult DoEval(Context context, ModuleLiteral env, EvaluationStackFrame frame)
        {
            var moduleCandidate = TargetExpression.Eval(context, env, frame);

            if (moduleCandidate.IsErrorValue || moduleCandidate.IsUndefined)
            {
                return(moduleCandidate);
            }

            // Qualifier type coercion could happen on both namespaces and files when importFrom is used.
            var module = moduleCandidate.Value as ModuleLiteral;

            // Moving Assert into the if block to avoid relatively expensive message computation for non-error case.
            if (module == null)
            {
                Contract.Assert(
                    false,
                    I($"TargetExpression '{TargetExpression.ToDisplayString(context)}' should produce 'ModuleLiteral' but produced '{moduleCandidate.Value.GetType()}'"));
            }

            var pathTable = context.FrontEndContext.PathTable;

            // TODO: Consider if it is possible to use QualifierUtilities.CoerceQualifierValue instead.
            QualifierValue oldQualifierValue = module.Qualifier;

            if (
                !oldQualifierValue.TryCoerce(
                    TargetQualifierSpaceId,
                    context.FrontEndContext.QualifierTable,
                    context.QualifierValueCache,
                    pathTable,
                    context.FrontEndContext.StringTable,
                    context.FrontEndContext.LoggingContext,
                    out QualifierValue coercedQualifier,
                    Location,
                    ShouldUseDefaultsOnCoercion,
                    context.LastActiveUsedPath))
            {
                context.Errors.ReportQualifierCannotBeCoarcedToQualifierSpaceWithProvenance(
                    oldQualifierValue.QualifierId,
                    TargetQualifierSpaceId,
                    ReferencedLocation.AsLoggingLocation(),
                    Location.AsLoggingLocation(env, context));

                return(EvaluationResult.Error);
            }

            return(EvaluationResult.Create(module.Instantiate(context.ModuleRegistry, coercedQualifier)));
        }