Skip to content

Commit c60db00

Browse files
committed
PR review fixes
Signed-off-by: Fred Dushin <fred@dushin.net>
1 parent 11ace3b commit c60db00

File tree

6 files changed

+427
-338
lines changed

6 files changed

+427
-338
lines changed

src/libAtomVM/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ set(HEADER_FILES
5050
port.h
5151
refc_binary.h
5252
scheduler.h
53+
stacktrace.h
5354
sys.h
5455
term_typedef.h
5556
term.h
@@ -81,6 +82,7 @@ set(SOURCE_FILES
8182
port.c
8283
refc_binary.c
8384
scheduler.c
85+
stacktrace.c
8486
term.c
8587
timer_wheel.c
8688
valueshashtable.c

src/libAtomVM/module.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,41 @@ void module_get_imported_function_module_and_name(const Module *this_module, int
155155
}
156156
#endif
157157

158+
bool module_get_fun_from_label(Module *this_module, int label, AtomString *function_name, int *arity)
159+
{
160+
int best_label = -1;
161+
const uint8_t *table_data = (const uint8_t *) this_module->export_table;
162+
int exports_count = READ_32_ALIGNED(table_data + 8);
163+
for (int export_index = exports_count - 1; export_index >= 0; export_index--) {
164+
int fun_atom_index = READ_32_ALIGNED(table_data + (export_index * 12) + 12);
165+
int fun_arity = READ_32_ALIGNED(table_data + (export_index * 12) + 4 + 12);
166+
int fun_label = READ_32_ALIGNED(table_data + (export_index * 12) + 8 + 12);
167+
if (fun_label <= label && best_label < fun_label) {
168+
best_label = fun_label;
169+
*arity = fun_arity;
170+
*function_name = module_get_atom_string_by_id(this_module, fun_atom_index);
171+
}
172+
}
173+
174+
table_data = (const uint8_t *) this_module->local_table;
175+
int locals_count = READ_32_ALIGNED(table_data + 8);
176+
for (int local_index = locals_count - 1; local_index >= 0; local_index--) {
177+
int fun_atom_index = READ_32_ALIGNED(table_data + (local_index * 12) + 12);
178+
int fun_arity = READ_32_ALIGNED(table_data + (local_index * 12) + 4 + 12);
179+
int fun_label = READ_32_ALIGNED(table_data + (local_index * 12) + 8 + 12);
180+
if (fun_label <= label && best_label < fun_label) {
181+
best_label = fun_label;
182+
*arity = fun_arity;
183+
*function_name = module_get_atom_string_by_id(this_module, fun_atom_index);
184+
}
185+
}
186+
if (UNLIKELY(best_label == -1)) {
187+
// Couldn't find the function.
188+
return false;
189+
}
190+
return true;
191+
}
192+
158193
uint32_t module_search_exported_function(Module *this_module, AtomString func_name, int func_arity)
159194
{
160195
const uint8_t *table_data = (const uint8_t *) this_module->export_table;

src/libAtomVM/module.h

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ struct LineRefOffset
7272
{
7373
struct ListHead head;
7474
uint16_t line_ref;
75-
unsigned offset;
75+
unsigned int offset;
7676
};
7777

7878
struct Module
@@ -293,39 +293,7 @@ static inline const uint8_t *module_get_str(Module *mod, size_t offset, size_t *
293293
* @param function_name (output) the function name, as an AtomString.
294294
* @param arity (output) the function arity
295295
*/
296-
static inline bool module_get_fun_from_label(Module *this_module, int label, AtomString *function_name, int *arity) {
297-
int best_label = -1;
298-
const uint8_t *table_data = (const uint8_t *) this_module->export_table;
299-
int exports_count = READ_32_ALIGNED(table_data + 8);
300-
for (int export_index = exports_count - 1; export_index >= 0; export_index--) {
301-
int fun_atom_index = READ_32_ALIGNED(table_data + (export_index * 12) + 12);
302-
int fun_arity = READ_32_ALIGNED(table_data + (export_index * 12) + 4 + 12);
303-
int fun_label = READ_32_ALIGNED(table_data + (export_index * 12) + 8 + 12);
304-
if (fun_label <= label && best_label < fun_label) {
305-
best_label = fun_label;
306-
*arity = fun_arity;
307-
*function_name = module_get_atom_string_by_id(this_module, fun_atom_index);
308-
}
309-
}
310-
311-
table_data = (const uint8_t *) this_module->local_table;
312-
int locals_count = READ_32_ALIGNED(table_data + 8);
313-
for (int local_index = locals_count - 1; local_index >= 0; local_index--) {
314-
int fun_atom_index = READ_32_ALIGNED(table_data + (local_index * 12) + 12);
315-
int fun_arity = READ_32_ALIGNED(table_data + (local_index * 12) + 4 + 12);
316-
int fun_label = READ_32_ALIGNED(table_data + (local_index * 12) + 8 + 12);
317-
if (fun_label <= label && best_label < fun_label) {
318-
best_label = fun_label;
319-
*arity = fun_arity;
320-
*function_name = module_get_atom_string_by_id(this_module, fun_atom_index);
321-
}
322-
}
323-
if (UNLIKELY(best_label == -1)) {
324-
// Couldn't find the function.
325-
return false;
326-
}
327-
return true;
328-
}
296+
bool module_get_fun_from_label(Module *this_module, int label, AtomString *function_name, int *arity);
329297

330298
/*
331299
* @brief Insert the instruction offset for a given module at a line reference instruction.
@@ -362,7 +330,7 @@ int module_find_line(Module *mod, int offset);
362330
*/
363331
static inline bool module_has_line_chunk(Module *mod)
364332
{
365-
return !IS_NULL_PTR(mod->line_refs);
333+
return mod->line_refs != NULL;
366334
}
367335

368336
#ifdef __cplusplus

0 commit comments

Comments
 (0)