<commit_before>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include "include/dart_debugger_api.h"
#include "platform/assert.h"
#include "vm/dart_api_impl.h"
#include "vm/unit_test.h"

namespace dart {

// Only ia32 and x64 can run execution tests.
#if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64)

static bool breakpoint_hit = false;
static int  breakpoint_hit_counter = 0;

static const bool verbose = false;

#define EXPECT_NOT_ERROR(handle)                                              \
  if (Dart_IsError(handle)) {                                                 \
    OS::Print("Error: %s\n", Dart_GetError(handle));                          \
  }                                                                           \
  EXPECT(!Dart_IsError(handle));


static Dart_Handle Invoke(Dart_Handle lib, const char* func_name) {
  return  Dart_InvokeStatic(lib,
                            Dart_NewString(""),
                            Dart_NewString(func_name),
                            0,
                            NULL);
}


void TestBreakpointHandler(Dart_Breakpoint bpt, Dart_StackTrace trace) {
  const char* expected_trace[] = {"A.foo", "main"};
  const intptr_t expected_trace_length = 2;
  breakpoint_hit = true;
  breakpoint_hit_counter++;
  intptr_t trace_len;
  Dart_Handle res = Dart_StackTraceLength(trace, &trace_len);
  EXPECT_NOT_ERROR(res);
  EXPECT_EQ(expected_trace_length, trace_len);
  for (int i = 0; i < trace_len; i++) {
    Dart_ActivationFrame frame;
    res = Dart_GetActivationFrame(trace, i, &frame);
    EXPECT_NOT_ERROR(res);
    Dart_Handle func_name;
    res = Dart_ActivationFrameInfo(frame, &func_name, NULL, NULL);
    EXPECT_NOT_ERROR(res);
    EXPECT(Dart_IsString(func_name));
    const char* name_chars;
    Dart_StringToCString(func_name, &name_chars);
    EXPECT_STREQ(expected_trace[i], name_chars);
    if (verbose) printf("  >> %d: %s\n", i, name_chars);
  }
}


TEST_CASE(Debug_Breakpoint) {
  const char* kScriptChars =
      "void moo(s) { }\n"
      "class A {\n"
      "  static void foo() {\n"
      "    moo('good news');\n"
      "  }\n"
      "}\n"
      "void main() {\n"
      "  A.foo();\n"
      "}\n";

  Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
  EXPECT(!Dart_IsError(lib));

  Dart_SetBreakpointHandler(&TestBreakpointHandler);

  Dart_Handle c_name = Dart_NewString("A");
  Dart_Handle f_name = Dart_NewString("foo");
  Dart_Breakpoint bpt;
  Dart_Handle res = Dart_SetBreakpointAtEntry(lib, c_name, f_name, &bpt);
  EXPECT_NOT_ERROR(res);

  breakpoint_hit = false;
  Dart_Handle retval = Invoke(lib, "main");
  EXPECT(!Dart_IsError(retval));
  EXPECT(breakpoint_hit == true);
}


static void DeleteBreakpointHandler(Dart_Breakpoint bpt,
                                    Dart_StackTrace trace) {
  const char* expected_trace[] = {"foo", "main"};
  const intptr_t expected_trace_length = 2;
  breakpoint_hit_counter++;
  intptr_t trace_len;
  Dart_Handle res = Dart_StackTraceLength(trace, &trace_len);
  EXPECT_NOT_ERROR(res);
  EXPECT_EQ(expected_trace_length, trace_len);
  for (int i = 0; i < trace_len; i++) {
    Dart_ActivationFrame frame;
    res = Dart_GetActivationFrame(trace, i, &frame);
    EXPECT_NOT_ERROR(res);
    Dart_Handle func_name;
    res = Dart_ActivationFrameInfo(frame, &func_name, NULL, NULL);
    EXPECT_NOT_ERROR(res);
    EXPECT(Dart_IsString(func_name));
    const char* name_chars;
    Dart_StringToCString(func_name, &name_chars);
    EXPECT_STREQ(expected_trace[i], name_chars);
    if (verbose) printf("  >> %d: %s\n", i, name_chars);
  }
  // Remove the breakpoint after we've hit it twice
  if (breakpoint_hit_counter == 2) {
    if (verbose) printf("uninstalling breakpoint\n");
    Dart_Handle res = Dart_DeleteBreakpoint(bpt);
    EXPECT_NOT_ERROR(res);
  }
}


TEST_CASE(Debug_DeleteBreakpoint) {
  const char* kScriptChars =
      "moo(s) { }\n"
      "\n"
      "foo() {\n"
      "    moo('good news');\n"
      "}\n"
      "\n"
      "void main() {\n"
      "  foo();\n"
      "  foo();\n"
      "  foo();\n"
      "}\n";

  Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
  EXPECT(!Dart_IsError(lib));

  Dart_Handle script_url = Dart_NewString(TestCase::url());
  Dart_Handle line_no = Dart_NewInteger(4);  // In function 'foo'.

  Dart_SetBreakpointHandler(&DeleteBreakpointHandler);

  Dart_Breakpoint bpt;
  Dart_Handle res = Dart_SetBreakpointAtLine(script_url, line_no, &bpt);
  EXPECT_NOT_ERROR(res);

  // Function main() calls foo() 3 times. On the second iteration, the
  // breakpoint is removed by the handler, so we expect the breakpoint
  // to fire twice only.
  breakpoint_hit_counter = 0;
  Dart_Handle retval = Invoke(lib, "main");
  EXPECT(!Dart_IsError(retval));
  EXPECT(breakpoint_hit_counter == 2);
}


TEST_CASE(Debug_InspectObject) {
  const char* kScriptChars =
    " class A { \n"
    "   int a_field = 'a'; \n"
    "   static var bla = 'yada yada yada';\n"
    "   var d = 42.1; \n"
    " } \n"
    " class B extends A { \n"
    "   var oneDay = const Duration(hours: 24); \n"
    "   static var bla = 'blah blah'; \n"
    " } \n"
    " get_b() { return new B(); } \n"
    " get_int() { return 666; } \n";

  // Number of instance fields in an object of class B.
  const intptr_t kNumObjectFields = 3;

  Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
  EXPECT_NOT_ERROR(lib);

  Dart_Handle object_b = Invoke(lib, "get_b");

  EXPECT_NOT_ERROR(object_b);

  Dart_Handle fields = Dart_GetInstanceFields(object_b);
  EXPECT_NOT_ERROR(fields);
  EXPECT(Dart_IsList(fields));
  intptr_t list_length = 0;
  Dart_Handle retval = Dart_ListLength(fields, &list_length);
  EXPECT_NOT_ERROR(retval);
  EXPECT_EQ(2 * kNumObjectFields, list_length);<commit_msg>Fix new test in checked mode<commit_after>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include "include/dart_debugger_api.h"
#include "platform/assert.h"
#include "vm/dart_api_impl.h"
#include "vm/unit_test.h"

namespace dart {

// Only ia32 and x64 can run execution tests.
#if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64)

static bool breakpoint_hit = false;
static int  breakpoint_hit_counter = 0;

static const bool verbose = false;

#define EXPECT_NOT_ERROR(handle)                                              \
  if (Dart_IsError(handle)) {                                                 \
    OS::Print("Error: %s\n", Dart_GetError(handle));                          \
  }                                                                           \
  EXPECT(!Dart_IsError(handle));


static Dart_Handle Invoke(Dart_Handle lib, const char* func_name) {
  return  Dart_InvokeStatic(lib,
                            Dart_NewString(""),
                            Dart_NewString(func_name),
                            0,
                            NULL);
}


void TestBreakpointHandler(Dart_Breakpoint bpt, Dart_StackTrace trace) {
  const char* expected_trace[] = {"A.foo", "main"};
  const intptr_t expected_trace_length = 2;
  breakpoint_hit = true;
  breakpoint_hit_counter++;
  intptr_t trace_len;
  Dart_Handle res = Dart_StackTraceLength(trace, &trace_len);
  EXPECT_NOT_ERROR(res);
  EXPECT_EQ(expected_trace_length, trace_len);
  for (int i = 0; i < trace_len; i++) {
    Dart_ActivationFrame frame;
    res = Dart_GetActivationFrame(trace, i, &frame);
    EXPECT_NOT_ERROR(res);
    Dart_Handle func_name;
    res = Dart_ActivationFrameInfo(frame, &func_name, NULL, NULL);
    EXPECT_NOT_ERROR(res);
    EXPECT(Dart_IsString(func_name));
    const char* name_chars;
    Dart_StringToCString(func_name, &name_chars);
    EXPECT_STREQ(expected_trace[i], name_chars);
    if (verbose) printf("  >> %d: %s\n", i, name_chars);
  }
}


TEST_CASE(Debug_Breakpoint) {
  const char* kScriptChars =
      "void moo(s) { }\n"
      "class A {\n"
      "  static void foo() {\n"
      "    moo('good news');\n"
      "  }\n"
      "}\n"
      "void main() {\n"
      "  A.foo();\n"
      "}\n";

  Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
  EXPECT(!Dart_IsError(lib));

  Dart_SetBreakpointHandler(&TestBreakpointHandler);

  Dart_Handle c_name = Dart_NewString("A");
  Dart_Handle f_name = Dart_NewString("foo");
  Dart_Breakpoint bpt;
  Dart_Handle res = Dart_SetBreakpointAtEntry(lib, c_name, f_name, &bpt);
  EXPECT_NOT_ERROR(res);

  breakpoint_hit = false;
  Dart_Handle retval = Invoke(lib, "main");
  EXPECT(!Dart_IsError(retval));
  EXPECT(breakpoint_hit == true);
}


static void DeleteBreakpointHandler(Dart_Breakpoint bpt,
                                    Dart_StackTrace trace) {
  const char* expected_trace[] = {"foo", "main"};
  const intptr_t expected_trace_length = 2;
  breakpoint_hit_counter++;
  intptr_t trace_len;
  Dart_Handle res = Dart_StackTraceLength(trace, &trace_len);
  EXPECT_NOT_ERROR(res);
  EXPECT_EQ(expected_trace_length, trace_len);
  for (int i = 0; i < trace_len; i++) {
    Dart_ActivationFrame frame;
    res = Dart_GetActivationFrame(trace, i, &frame);
    EXPECT_NOT_ERROR(res);
    Dart_Handle func_name;
    res = Dart_ActivationFrameInfo(frame, &func_name, NULL, NULL);
    EXPECT_NOT_ERROR(res);
    EXPECT(Dart_IsString(func_name));
    const char* name_chars;
    Dart_StringToCString(func_name, &name_chars);
    EXPECT_STREQ(expected_trace[i], name_chars);
    if (verbose) printf("  >> %d: %s\n", i, name_chars);
  }
  // Remove the breakpoint after we've hit it twice
  if (breakpoint_hit_counter == 2) {
    if (verbose) printf("uninstalling breakpoint\n");
    Dart_Handle res = Dart_DeleteBreakpoint(bpt);
    EXPECT_NOT_ERROR(res);
  }
}


TEST_CASE(Debug_DeleteBreakpoint) {
  const char* kScriptChars =
      "moo(s) { }\n"
      "\n"
      "foo() {\n"
      "    moo('good news');\n"
      "}\n"
      "\n"
      "void main() {\n"
      "  foo();\n"
      "  foo();\n"
      "  foo();\n"
      "}\n";

  Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
  EXPECT(!Dart_IsError(lib));

  Dart_Handle script_url = Dart_NewString(TestCase::url());
  Dart_Handle line_no = Dart_NewInteger(4);  // In function 'foo'.

  Dart_SetBreakpointHandler(&DeleteBreakpointHandler);

  Dart_Breakpoint bpt;
  Dart_Handle res = Dart_SetBreakpointAtLine(script_url, line_no, &bpt);
  EXPECT_NOT_ERROR(res);

  // Function main() calls foo() 3 times. On the second iteration, the
  // breakpoint is removed by the handler, so we expect the breakpoint
  // to fire twice only.
  breakpoint_hit_counter = 0;
  Dart_Handle retval = Invoke(lib, "main");
  EXPECT(!Dart_IsError(retval));
  EXPECT(breakpoint_hit_counter == 2);
}


TEST_CASE(Debug_InspectObject) {
  const char* kScriptChars =
    " class A { \n"
    "   var a_field = 'a'; \n"
    "   static var bla = 'yada yada yada';\n"
    "   var d = 42.1; \n"
    " } \n"
    " class B extends A { \n"
    "   var oneDay = const Duration(hours: 24); \n"
    "   static var bla = 'blah blah'; \n"
    " } \n"
    " get_b() { return new B(); } \n"
    " get_int() { return 666; } \n";

  // Number of instance fields in an object of class B.
  const intptr_t kNumObjectFields = 3;

  Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
  EXPECT_NOT_ERROR(lib);

  Dart_Handle object_b = Invoke(lib, "get_b");

  EXPECT_NOT_ERROR(object_b);

  Dart_Handle fields = Dart_GetInstanceFields(object_b);
  EXPECT_NOT_ERROR(fields);
  EXPECT(Dart_IsList(fields));
  intptr_t list_length = 0;
  Dart_Handle retval = Dart_ListLength(fields, &list_length);
  EXPECT_NOT_ERROR(retval);
  EXPECT_EQ(2 * kNumObjectFields, list_length);
{'fraction_non_alphanumeric': 0.12225678970823173, 'fraction_numerical': 0.005717649037248802, 'mean_word_length': 3.078875171467764, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4994828', 'n_tokens_mistral': 4204, 'n_tokens_neox': 4105, 'n_words': 916}