Skip to content

Start with init:boot/1 #1752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libs/estdlib/src/init.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@
-module(init).

-export([
boot/1,
get_argument/1,
get_plain_arguments/0,
notify_when_started/1
]).

%%-----------------------------------------------------------------------------
%% @param Args command line arguments
%% @doc Entry point.
%% @end
%%-----------------------------------------------------------------------------
-spec boot([binary() | atom()]) -> any().
boot([<<"-s">>, StartupModule]) when is_atom(StartupModule) ->
% Until we have boot scripts, we just start kernel application.
{ok, _KernelPid} = kernel:start(boot, []),
StartupModule:start().

%%-----------------------------------------------------------------------------
%% @param Flag flag to get values for
%% @return `error' if no value is associated with provided flag or values in
Expand Down
2 changes: 2 additions & 0 deletions src/libAtomVM/defaultatoms.def
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,5 @@ X(BREAK_IGNORED_ATOM, "\xD", "break_ignored")

X(SCOPE_ATOM, "\x5", "scope")
X(NOMATCH_ATOM, "\x7", "nomatch")

X(INIT_ATOM, "\x4", "init")
37 changes: 37 additions & 0 deletions src/libAtomVM/globalcontext.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,40 @@ Module *globalcontext_get_module_by_index(GlobalContext *global, int index)
SMP_RWLOCK_UNLOCK(global->modules_lock);
return result;
}

enum RunResult globalcontext_run(GlobalContext *glb, Module *startup_module)
{
Context *ctx = context_new(glb);
ctx->leader = 1;
Module *init_module = globalcontext_get_module(glb, INIT_ATOM_INDEX);
if (IS_NULL_PTR(init_module)) {
context_execute_loop(ctx, startup_module, "start", 0);
} else {
if (UNLIKELY(memory_ensure_free(ctx, term_binary_heap_size(2) + LIST_SIZE(2, 0)) != MEMORY_GC_OK)) {
fprintf(stderr, "Unable to allocate arguments.\n");
return RUN_MEMORY_FAILURE;
}
term s_opt = term_from_literal_binary("-s", 2, &ctx->heap, glb);
term list = term_list_prepend(module_get_name(startup_module), term_nil(), &ctx->heap);
ctx->x[0] = term_list_prepend(s_opt, list, &ctx->heap);

context_execute_loop(ctx, init_module, "boot", 1);
}

term ret_value = ctx->x[0];
fprintf(stderr, "Return value: ");
term_display(stderr, ret_value, ctx);
fprintf(stderr, "\n");

enum RunResult result;
// ok or 0. 0 is required for running tests for emscripten
if (ret_value == OK_ATOM || ret_value == term_from_int(0)) {
result = RUN_SUCCESS;
} else {
result = RUN_RESULT_NOT_OK;
}

context_destroy(ctx);

return result;
}
21 changes: 21 additions & 0 deletions src/libAtomVM/globalcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ struct RefcBinaryQueueItem
struct RefcBinary *refc;
};

enum RunResult
{
RUN_SUCCESS = 0,
RUN_MEMORY_FAILURE = 1,
RUN_RESULT_NOT_OK = 2,
};

struct GlobalContext
{
struct ListHead ready_processes;
Expand Down Expand Up @@ -512,6 +519,20 @@ Module *globalcontext_get_module(GlobalContext *global, atom_index_t module_name
*/
Module *globalcontext_load_module_from_avm(GlobalContext *global, const char *module_name);

/**
* @brief Run a given start module.
*
* @details This function will create a new context and call init:boot/1
* to execute the passed start module. If init module is not found, it will
* fallback to calling start module:start/0 directly. It will also
* print the result to stdout. It will return RUN_SUCCESS if result is ok,
* an error code otherwise.
* @param global the global context
* @param start_module the start module
* @returns RUN_SUCCESS or an error code
*/
enum RunResult globalcontext_run(GlobalContext *global, Module *start_module);

#ifndef __cplusplus
static inline uint64_t globalcontext_get_ref_ticks(GlobalContext *global)
{
Expand Down
13 changes: 3 additions & 10 deletions src/platforms/emscripten/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,16 @@ static int start(void)
fprintf(stderr, "main module not loaded\n");
return EXIT_FAILURE;
}
Context *ctx = context_new(global);
ctx->leader = 1;
context_execute_loop(ctx, main_module, "start", 0);
term ret_value = ctx->x[0];
fprintf(stdout, "Return value: ");
term_display(stdout, ret_value, ctx);
fprintf(stdout, "\n");

enum RunResult result = globalcontext_run(global, main_module);

int status;
if (ret_value == OK_ATOM || ret_value == term_from_int(0)) {
if (ret_value == RUN_SUCCESS) {
status = EXIT_SUCCESS;
} else {
status = EXIT_FAILURE;
}

context_destroy(ctx);

return status;
}

Expand Down
11 changes: 2 additions & 9 deletions src/platforms/esp32/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,19 @@ void app_main()
AVM_ABORT();
}
globalcontext_insert_module(glb, mod);
Context *ctx = context_new(glb);
ctx->leader = 1;

ESP_LOGI(TAG, "Starting %s...", startup_module_name);
fprintf(stdout, "---\n");

context_execute_loop(ctx, mod, "start", 0);
term ret_value = ctx->x[0];

fprintf(stdout, "AtomVM finished with return value: ");
term_display(stdout, ret_value, ctx);
fprintf(stdout, "\n");
enum RunResult result = globalcontext_run(glb, mod);

bool reboot_on_not_ok =
#if defined(CONFIG_REBOOT_ON_NOT_OK)
CONFIG_REBOOT_ON_NOT_OK ? true : false;
#else
false;
#endif
if (reboot_on_not_ok && ret_value != OK_ATOM) {
if (reboot_on_not_ok && result != RUN_SUCCESS) {
ESP_LOGE(TAG, "AtomVM application terminated with non-ok return value. Rebooting ...");
esp_restart();
} else {
Expand Down
13 changes: 2 additions & 11 deletions src/platforms/generic_unix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,24 +146,15 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}

Context *ctx = context_new(glb);
ctx->leader = 1;

context_execute_loop(ctx, startup_module, "start", 0);

term ret_value = ctx->x[0];
fprintf(stderr, "Return value: ");
term_display(stderr, ret_value, ctx);
fprintf(stderr, "\n");
enum RunResult result = globalcontext_run(glb, startup_module);

int status;
if (ret_value == OK_ATOM) {
if (result == RUN_SUCCESS) {
status = EXIT_SUCCESS;
} else {
status = EXIT_FAILURE;
}

context_destroy(ctx);
globalcontext_destroy(glb);

return status;
Expand Down
13 changes: 1 addition & 12 deletions src/platforms/rp2/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,8 @@ static int app_main()

globalcontext_insert_module(glb, mod);
mod->module_platform_data = NULL;
Context *ctx = context_new(glb);
ctx->leader = 1;

fprintf(stderr, "Starting %s...", startup_module_name);
fprintf(stdout, "---\n");

context_execute_loop(ctx, mod, "start", 0);
term ret_value = ctx->x[0];

fprintf(stdout, "AtomVM finished with return value: ");
term_display(stdout, ret_value, ctx);
fprintf(stdout, "\n");
int result = ret_value != OK_ATOM;
enum RunResult result = globalcontext_run(glb, mod);

context_destroy(ctx);

Expand Down
15 changes: 2 additions & 13 deletions src/platforms/stm32/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,30 +267,19 @@ int main()

Module *mod = module_new_from_iff_binary(glb, startup_beam, startup_beam_size);
globalcontext_insert_module(glb, mod);
Context *ctx = context_new(glb);
ctx->leader = 1;

AVM_LOGI(TAG, "Starting: %s...\n", startup_module_name);
fprintf(stdout, "---\n");

context_execute_loop(ctx, mod, "start", 0);

term ret_value = ctx->x[0];
char *ret_atom_string = interop_atom_to_string(ctx, ret_value);
if (ret_atom_string != NULL) {
AVM_LOGI(TAG, "Exited with return: %s", ret_atom_string);
} else {
AVM_LOGI(TAG, "Exited with return value: %lx", (long) term_to_int32(ret_value));
}
free(ret_atom_string);
enum RunResult result = globalcontext_run(glb, mod);

bool reboot_on_not_ok =
#if defined(CONFIG_REBOOT_ON_NOT_OK)
CONFIG_REBOOT_ON_NOT_OK ? true : false;
#else
false;
#endif
if (reboot_on_not_ok && ret_value != OK_ATOM) {
if (reboot_on_not_ok && result != RUN_SUCCESS) {
AVM_LOGE(TAG, "AtomVM application terminated with non-ok return value. Rebooting ...");
scb_reset_system();
} else {
Expand Down
Loading