public static string TraceCallStack(this CFlat self)
        {
            var vm = self.vm;
            var sb = new StringBuilder();

            sb.AppendLine("callstack:");
            for (var i = vm.callFrameStack.count - 1; i >= 0; i--)
            {
                var callframe = vm.callFrameStack.buffer[i];

                switch (callframe.type)
                {
                case CallFrame.Type.EntryPoint:
                    break;

                case CallFrame.Type.Function:
                {
                    var codeIndex   = System.Math.Max(callframe.codeIndex - 1, 0);
                    var sourceIndex = vm.chunk.sourceSlices.buffer[codeIndex].index;
                    var source      = self.compiler.compiledSources.buffer[vm.chunk.FindSourceIndex(codeIndex)];

                    var pos = FormattingHelper.GetLineAndColumn(
                        source.content,
                        sourceIndex
                        );
                    sb.Append("[line ");
                    sb.Append(pos.lineIndex + 1);
                    sb.Append("] ");

                    vm.chunk.FormatFunction(callframe.functionIndex, sb);

                    sb.Append(" => ");
                    var slice = FormattingHelper.GetLineSlice(source.content, pos.lineIndex);
                    slice = FormattingHelper.Trim(source.content, slice);
                    sb.Append(source.content, slice.index, slice.length);
                    sb.AppendLine();
                    break;
                }

                case CallFrame.Type.NativeFunction:
                    sb.Append("[native] ");
                    vm.chunk.FormatNativeFunction(callframe.functionIndex, sb);
                    sb.AppendLine();
                    break;
                }
            }

            return(sb.ToString());
        }
Exemplo n.º 2
0
        public void OnDebugHook()
        {
            if (onBreak == null)
            {
                return;
            }

            var codeIndex = vm.callFrameStack.buffer[vm.callFrameStack.count - 1].codeIndex;

            if (codeIndex < 0)
            {
                return;
            }
            var sourceIndex = vm.chunk.FindSourceIndex(codeIndex);

            if (sourceIndex < 0)
            {
                return;
            }

            var source      = sources.buffer[sourceIndex];
            var sourceSlice = vm.chunk.sourceSlices.buffer[codeIndex];
            var line        = (ushort)(FormattingHelper.GetLineAndColumn(source.content, sourceSlice.index).lineIndex + 1);
            var position    = new SourcePosition(source.uri, line);

            for (var i = 0; i < breakpoints.count; i++)
            {
                var breakpoint = breakpoints.buffer[i];
                if (
                    (lastPosition.uri.value != position.uri.value ||
                     lastPosition.line != breakpoint.line) &&
                    position.line == breakpoint.line
                    )
                {
                    var localVariables = GetLocalVariables(vm);
                    onBreak(breakpoint, localVariables);
                    break;
                }
            }

            lastPosition = position;
        }
Exemplo n.º 3
0
        private static void PrintLineNumber(ByteCodeChunk self, string source, int index, StringBuilder sb)
        {
            var currentSourceIndex = self.sourceSlices.buffer[index].index;
            var currentPosition    = FormattingHelper.GetLineAndColumn(source, currentSourceIndex);
            var lastLineIndex      = -1;

            if (index > 0)
            {
                var lastSourceIndex = self.sourceSlices.buffer[index - 1].index;
                lastLineIndex = FormattingHelper.GetLineAndColumn(source, lastSourceIndex).lineIndex;
            }

            if (currentPosition.lineIndex == lastLineIndex)
            {
                sb.Append("   | ");
            }
            else
            {
                sb.AppendFormat("{0,4} ", currentPosition.lineIndex);
            }
        }