Ejemplo n.º 1
0
        public virtual string GenerateTypeCheckForParameter(int paramIndex, Type type)
        {
            var typeChecker = new NAPITypeCheckGen(paramIndex);

            type.Visit(typeChecker);

            var condition = typeChecker.Generate();

            if (string.IsNullOrWhiteSpace(condition))
            {
                throw new NotSupportedException();
            }

            return(condition);
        }
Ejemplo n.º 2
0
        private void CheckArgumentsOverload(IList <Function> @group, DFSM stateMachine)
        {
            var typeCheckStates = stateMachine.Q.Except(stateMachine.F).ToList();
            var finalStates     = stateMachine.F;

            // Create a set of unique parameter types.
            var uniqueTypes = @group.SelectMany(method => method.Parameters)
                              .Select(p => p.Type).Distinct().ToList();

            // Type check states.
            for (var i = 0; i < typeCheckStates.Count; i++)
            {
                NewLineIfNeeded();

                if (i > 0)
                {
                    Unindent();
                    WriteLine($"typecheck{i}:");
                    Indent();
                }

                var state       = typeCheckStates[i];
                var transitions = stateMachine.Delta.Where(t => t.StartState == state).ToArray();

                foreach (var transition in transitions)
                {
                    NewLineIfNeeded();

                    var isInitialState = stateMachine.Q0.Contains(state);
                    var paramIndex     = isInitialState ? 0 :
                                         int.Parse(transition.StartState.Split(' ').Last().Split('_').Last()) + 1;

                    var type = uniqueTypes[(int)transition.Symbol];

                    var typeChecker = new NAPITypeCheckGen(paramIndex);
                    type.Visit(typeChecker);

                    var condition = typeChecker.Generate();
                    if (string.IsNullOrWhiteSpace(condition))
                    {
                        throw new NotSupportedException();
                    }

                    WriteLine($"if ({condition})");

                    var nextState = typeCheckStates.Contains(transition.EndState)
                        ? $"typecheck{typeCheckStates.FindIndex(s => s == transition.EndState)}"
                        : $"overload{finalStates.FindIndex(s => s == transition.EndState)}";
                    WriteLineIndent($"goto {nextState};");
                    NewLine();
                }

                WriteLine("goto error;");
                NeedNewLine();

                NeedNewLine();
            }
            NewLineIfNeeded();

            // Error state.
            Unindent();
            WriteLine($"error:");
            Indent();

            WriteLine("status = napi_throw_type_error(env, nullptr, \"Unsupported argument type\");");
            WriteLine("assert(status == napi_ok);");
            NewLine();

            WriteLine("return nullptr;");
            NewLine();
        }