Skip to content

Commit 151de09

Browse files
authored
Merge pull request #736 from sogaiu/new-style-core-fn-decl-for-debug
Update debug.c with new style core function declarations.
2 parents cc13e45 + 59aab2e commit 151de09

File tree

1 file changed

+69
-88
lines changed

1 file changed

+69
-88
lines changed

src/core/debug.c

Lines changed: 69 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -195,39 +195,60 @@ static void helper_find_fun(int32_t argc, Janet *argv, JanetFuncDef **def, int32
195195
*bytecode_offset = offset;
196196
}
197197

198-
static Janet cfun_debug_break(int32_t argc, Janet *argv) {
198+
JANET_CORE_FN(cfun_debug_break,
199+
"(debug/break source line col)",
200+
"Sets a breakpoint in `source` at a given line and column. "
201+
"Will throw an error if the breakpoint location "
202+
"cannot be found. For example\n\n"
203+
"\t(debug/break \"core.janet\" 10 4)\n\n"
204+
"will set a breakpoint at line 10, 4th column of the file core.janet.") {
199205
JanetFuncDef *def;
200206
int32_t offset;
201207
helper_find(argc, argv, &def, &offset);
202208
janet_debug_break(def, offset);
203209
return janet_wrap_nil();
204210
}
205211

206-
static Janet cfun_debug_unbreak(int32_t argc, Janet *argv) {
212+
JANET_CORE_FN(cfun_debug_unbreak,
213+
"(debug/unbreak source line column)",
214+
"Remove a breakpoint with a source key at a given line and column. "
215+
"Will throw an error if the breakpoint "
216+
"cannot be found.") {
207217
JanetFuncDef *def;
208218
int32_t offset = 0;
209219
helper_find(argc, argv, &def, &offset);
210220
janet_debug_unbreak(def, offset);
211221
return janet_wrap_nil();
212222
}
213223

214-
static Janet cfun_debug_fbreak(int32_t argc, Janet *argv) {
224+
JANET_CORE_FN(cfun_debug_fbreak,
225+
"(debug/fbreak fun &opt pc)",
226+
"Set a breakpoint in a given function. pc is an optional offset, which "
227+
"is in bytecode instructions. fun is a function value. Will throw an error "
228+
"if the offset is too large or negative.") {
215229
JanetFuncDef *def;
216230
int32_t offset = 0;
217231
helper_find_fun(argc, argv, &def, &offset);
218232
janet_debug_break(def, offset);
219233
return janet_wrap_nil();
220234
}
221235

222-
static Janet cfun_debug_unfbreak(int32_t argc, Janet *argv) {
236+
JANET_CORE_FN(cfun_debug_unfbreak,
237+
"(debug/unfbreak fun &opt pc)",
238+
"Unset a breakpoint set with debug/fbreak.") {
223239
JanetFuncDef *def;
224240
int32_t offset;
225241
helper_find_fun(argc, argv, &def, &offset);
226242
janet_debug_unbreak(def, offset);
227243
return janet_wrap_nil();
228244
}
229245

230-
static Janet cfun_debug_lineage(int32_t argc, Janet *argv) {
246+
JANET_CORE_FN(cfun_debug_lineage,
247+
"(debug/lineage fib)",
248+
"Returns an array of all child fibers from a root fiber. This function "
249+
"is useful when a fiber signals or errors to an ancestor fiber. Using this function, "
250+
"the fiber handling the error can see which fiber raised the signal. This function should "
251+
"be used mostly for debugging purposes.") {
231252
janet_fixarity(argc, 1);
232253
JanetFiber *fiber = janet_getfiber(argv, 0);
233254
JanetArray *array = janet_array(0);
@@ -284,7 +305,21 @@ static Janet doframe(JanetStackFrame *frame) {
284305
return janet_wrap_table(t);
285306
}
286307

287-
static Janet cfun_debug_stack(int32_t argc, Janet *argv) {
308+
JANET_CORE_FN(cfun_debug_stack,
309+
"(debug/stack fib)",
310+
"Gets information about the stack as an array of tables. Each table "
311+
"in the array contains information about a stack frame. The top-most, current "
312+
"stack frame is the first table in the array, and the bottom-most stack frame "
313+
"is the last value. Each stack frame contains some of the following attributes:\n\n"
314+
"* :c - true if the stack frame is a c function invocation\n\n"
315+
"* :column - the current source column of the stack frame\n\n"
316+
"* :function - the function that the stack frame represents\n\n"
317+
"* :line - the current source line of the stack frame\n\n"
318+
"* :name - the human-friendly name of the function\n\n"
319+
"* :pc - integer indicating the location of the program counter\n\n"
320+
"* :source - string with the file path or other identifier for the source code\n\n"
321+
"* :slots - array of all values in each slot\n\n"
322+
"* :tail - boolean indicating a tail call") {
288323
janet_fixarity(argc, 1);
289324
JanetFiber *fiber = janet_getfiber(argv, 0);
290325
JanetArray *array = janet_array(0);
@@ -300,15 +335,23 @@ static Janet cfun_debug_stack(int32_t argc, Janet *argv) {
300335
return janet_wrap_array(array);
301336
}
302337

303-
static Janet cfun_debug_stacktrace(int32_t argc, Janet *argv) {
338+
JANET_CORE_FN(cfun_debug_stacktrace,
339+
"(debug/stacktrace fiber &opt err)",
340+
"Prints a nice looking stacktrace for a fiber. Can optionally provide "
341+
"an error value to print the stack trace with. If `err` is nil or not "
342+
"provided, will skip the error line. Returns the fiber.") {
304343
janet_arity(argc, 1, 2);
305344
JanetFiber *fiber = janet_getfiber(argv, 0);
306345
Janet x = argc == 1 ? janet_wrap_nil() : argv[1];
307346
janet_stacktrace(fiber, x);
308347
return argv[0];
309348
}
310349

311-
static Janet cfun_debug_argstack(int32_t argc, Janet *argv) {
350+
JANET_CORE_FN(cfun_debug_argstack,
351+
"(debug/arg-stack fiber)",
352+
"Gets all values currently on the fiber's argument stack. Normally, "
353+
"this should be empty unless the fiber signals while pushing arguments "
354+
"to make a function call. Returns a new array.") {
312355
janet_fixarity(argc, 1);
313356
JanetFiber *fiber = janet_getfiber(argv, 0);
314357
JanetArray *array = janet_array(fiber->stacktop - fiber->stackstart);
@@ -317,93 +360,31 @@ static Janet cfun_debug_argstack(int32_t argc, Janet *argv) {
317360
return janet_wrap_array(array);
318361
}
319362

320-
static Janet cfun_debug_step(int32_t argc, Janet *argv) {
363+
JANET_CORE_FN(cfun_debug_step,
364+
"(debug/step fiber &opt x)",
365+
"Run a fiber for one virtual instruction of the Janet machine. Can optionally "
366+
"pass in a value that will be passed as the resuming value. Returns the signal value, "
367+
"which will usually be nil, as breakpoints raise nil signals.") {
321368
janet_arity(argc, 1, 2);
322369
JanetFiber *fiber = janet_getfiber(argv, 0);
323370
Janet out = janet_wrap_nil();
324371
janet_step(fiber, argc == 1 ? janet_wrap_nil() : argv[1], &out);
325372
return out;
326373
}
327374

328-
static const JanetReg debug_cfuns[] = {
329-
{
330-
"debug/break", cfun_debug_break,
331-
JDOC("(debug/break source line col)\n\n"
332-
"Sets a breakpoint in `source` at a given line and column. "
333-
"Will throw an error if the breakpoint location "
334-
"cannot be found. For example\n\n"
335-
"\t(debug/break \"core.janet\" 10 4)\n\n"
336-
"will set a breakpoint at line 10, 4th column of the file core.janet.")
337-
},
338-
{
339-
"debug/unbreak", cfun_debug_unbreak,
340-
JDOC("(debug/unbreak source line column)\n\n"
341-
"Remove a breakpoint with a source key at a given line and column. "
342-
"Will throw an error if the breakpoint "
343-
"cannot be found.")
344-
},
345-
{
346-
"debug/fbreak", cfun_debug_fbreak,
347-
JDOC("(debug/fbreak fun &opt pc)\n\n"
348-
"Set a breakpoint in a given function. pc is an optional offset, which "
349-
"is in bytecode instructions. fun is a function value. Will throw an error "
350-
"if the offset is too large or negative.")
351-
},
352-
{
353-
"debug/unfbreak", cfun_debug_unfbreak,
354-
JDOC("(debug/unfbreak fun &opt pc)\n\n"
355-
"Unset a breakpoint set with debug/fbreak.")
356-
},
357-
{
358-
"debug/arg-stack", cfun_debug_argstack,
359-
JDOC("(debug/arg-stack fiber)\n\n"
360-
"Gets all values currently on the fiber's argument stack. Normally, "
361-
"this should be empty unless the fiber signals while pushing arguments "
362-
"to make a function call. Returns a new array.")
363-
},
364-
{
365-
"debug/stack", cfun_debug_stack,
366-
JDOC("(debug/stack fib)\n\n"
367-
"Gets information about the stack as an array of tables. Each table "
368-
"in the array contains information about a stack frame. The top-most, current "
369-
"stack frame is the first table in the array, and the bottom-most stack frame "
370-
"is the last value. Each stack frame contains some of the following attributes:\n\n"
371-
"* :c - true if the stack frame is a c function invocation\n\n"
372-
"* :column - the current source column of the stack frame\n\n"
373-
"* :function - the function that the stack frame represents\n\n"
374-
"* :line - the current source line of the stack frame\n\n"
375-
"* :name - the human-friendly name of the function\n\n"
376-
"* :pc - integer indicating the location of the program counter\n\n"
377-
"* :source - string with the file path or other identifier for the source code\n\n"
378-
"* :slots - array of all values in each slot\n\n"
379-
"* :tail - boolean indicating a tail call")
380-
},
381-
{
382-
"debug/stacktrace", cfun_debug_stacktrace,
383-
JDOC("(debug/stacktrace fiber &opt err)\n\n"
384-
"Prints a nice looking stacktrace for a fiber. Can optionally provide "
385-
"an error value to print the stack trace with. If `err` is nil or not "
386-
"provided, will skip the error line. Returns the fiber.")
387-
},
388-
{
389-
"debug/lineage", cfun_debug_lineage,
390-
JDOC("(debug/lineage fib)\n\n"
391-
"Returns an array of all child fibers from a root fiber. This function "
392-
"is useful when a fiber signals or errors to an ancestor fiber. Using this function, "
393-
"the fiber handling the error can see which fiber raised the signal. This function should "
394-
"be used mostly for debugging purposes.")
395-
},
396-
{
397-
"debug/step", cfun_debug_step,
398-
JDOC("(debug/step fiber &opt x)\n\n"
399-
"Run a fiber for one virtual instruction of the Janet machine. Can optionally "
400-
"pass in a value that will be passed as the resuming value. Returns the signal value, "
401-
"which will usually be nil, as breakpoints raise nil signals.")
402-
},
403-
{NULL, NULL, NULL}
404-
};
405-
406375
/* Module entry point */
407376
void janet_lib_debug(JanetTable *env) {
408-
janet_core_cfuns(env, NULL, debug_cfuns);
377+
JanetRegExt debug_cfuns[] = {
378+
JANET_CORE_REG("debug/break", cfun_debug_break),
379+
JANET_CORE_REG("debug/unbreak", cfun_debug_unbreak),
380+
JANET_CORE_REG("debug/fbreak", cfun_debug_fbreak),
381+
JANET_CORE_REG("debug/unfbreak", cfun_debug_unfbreak),
382+
JANET_CORE_REG("debug/arg-stack", cfun_debug_argstack),
383+
JANET_CORE_REG("debug/stack", cfun_debug_stack),
384+
JANET_CORE_REG("debug/stacktrace", cfun_debug_stacktrace),
385+
JANET_CORE_REG("debug/lineage", cfun_debug_lineage),
386+
JANET_CORE_REG("debug/step", cfun_debug_step),
387+
JANET_REG_END
388+
};
389+
janet_core_cfuns_ext(env, NULL, debug_cfuns);
409390
}

0 commit comments

Comments
 (0)