Skip to content

Commit e31725a

Browse files
committed
Merge pull request #1752 from pguyot/w28/start-with-init-boot
Start with init:boot/1 These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents c5eb07f + cc5b44e commit e31725a

File tree

14 files changed

+93
-72
lines changed

14 files changed

+93
-72
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6060
- Release images for ESP32 chips are built with ESP-IDF v5.4
6161
- ESP32: SPI peripheral defaults to `"spi2"` instead of deprecated `hspi`
6262
- Added `zlib:compress/1`
63+
- Entry point now is `init:boot/1` if it exists. It starts the kernel application and calls `start/0` from the
64+
identified startup module. Users who started kernel application (typically for distribution) must no longer
65+
do it. Startint `net_kernel` is still required.
6366

6467
### Fixed
6568

doc/src/programmers-guide.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,7 @@ For more information about deploying the AtomVM image and AtomVM applications to
176176
An AtomVM application is a collection of BEAM files, aggregated into an AtomVM "Packbeam" (`.avm`) file, and typically deployed (flashed) to some device. These BEAM files be be compiled from Erlang, Elixir, or any other language that targets the Erlang VM.
177177

178178
```{attention}
179-
The return value from the `start/0` function is ignored on the the `generic_unix` platform, most MCU platforms have
180-
the option of rebooting the device if the `start/0` function returns a value other than `ok`. Consult the
181-
[Build Instructions](./build-instructions.md#platform-specific-build-instructions) for your device to see how this
182-
is configured.
179+
The return value from the `start/0` function is printed, ok means success (0 is also accepted for historical reasons), and any other value means failure. The processing of this results depend on platforms, on generic unix for example it determines the exit code. Most MCU platforms have the option of rebooting the device if the `start/0` function returns a value other than `ok`. Consult the [Build Instructions](./build-instructions.md#platform-specific-build-instructions) for your device to see how this is configured.
183180
```
184181

185182
Here, for example is one of the smallest AtomVM applications you can write:
@@ -205,6 +202,8 @@ wait_forever() ->
205202
receive X -> X end.
206203
```
207204

205+
The start function actually isn't the first function evaluated by the virtual machine. If the `init` module exists, `init:boot/1` is called and this function calls `start/0`. The `init` module is part of atomvmlib.
206+
208207
### Packbeam files
209208

210209
AtomVM applications are packaged into Packbeam (`.avm`) files, which contain collections of files, typically BEAM (`.beam`) files that have been generated by the Erlang or Elixir compiler.

examples/erlang/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ include(BuildErlang)
2525
add_subdirectory(esp32)
2626
add_subdirectory(rp2)
2727

28-
pack_runnable(hello_world hello_world)
28+
pack_runnable(hello_world hello_world estdlib)
2929
pack_runnable(udp_server udp_server estdlib eavmlib)
3030
pack_runnable(udp_client udp_client estdlib eavmlib)
3131
pack_runnable(tcp_client tcp_client estdlib eavmlib)

libs/estdlib/src/init.erl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,23 @@
2828
-module(init).
2929

3030
-export([
31+
boot/1,
3132
get_argument/1,
3233
get_plain_arguments/0,
3334
notify_when_started/1
3435
]).
3536

37+
%%-----------------------------------------------------------------------------
38+
%% @param Args command line arguments
39+
%% @doc Entry point.
40+
%% @end
41+
%%-----------------------------------------------------------------------------
42+
-spec boot([binary() | atom()]) -> any().
43+
boot([<<"-s">>, StartupModule]) when is_atom(StartupModule) ->
44+
% Until we have boot scripts, we just start kernel application.
45+
{ok, _KernelPid} = kernel:start(boot, []),
46+
StartupModule:start().
47+
3648
%%-----------------------------------------------------------------------------
3749
%% @param Flag flag to get values for
3850
%% @return `error' if no value is associated with provided flag or values in

src/libAtomVM/defaultatoms.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,5 @@ X(BREAK_IGNORED_ATOM, "\xD", "break_ignored")
188188

189189
X(SCOPE_ATOM, "\x5", "scope")
190190
X(NOMATCH_ATOM, "\x7", "nomatch")
191+
192+
X(INIT_ATOM, "\x4", "init")

src/libAtomVM/globalcontext.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,3 +719,42 @@ Module *globalcontext_get_module_by_index(GlobalContext *global, int index)
719719
SMP_RWLOCK_UNLOCK(global->modules_lock);
720720
return result;
721721
}
722+
723+
run_result_t globalcontext_run(GlobalContext *glb, Module *startup_module, FILE *out_f)
724+
{
725+
Context *ctx = context_new(glb);
726+
ctx->leader = 1;
727+
Module *init_module = globalcontext_get_module(glb, INIT_ATOM_INDEX);
728+
if (IS_NULL_PTR(init_module)) {
729+
context_execute_loop(ctx, startup_module, "start", 0);
730+
} else {
731+
if (UNLIKELY(memory_ensure_free(ctx, term_binary_heap_size(2) + LIST_SIZE(2, 0)) != MEMORY_GC_OK)) {
732+
fprintf(stderr, "Unable to allocate arguments.\n");
733+
return RUN_MEMORY_FAILURE;
734+
}
735+
term s_opt = term_from_literal_binary("-s", strlen("-s"), &ctx->heap, glb);
736+
term list = term_list_prepend(module_get_name(startup_module), term_nil(), &ctx->heap);
737+
ctx->x[0] = term_list_prepend(s_opt, list, &ctx->heap);
738+
739+
context_execute_loop(ctx, init_module, "boot", 1);
740+
}
741+
742+
term ret_value = ctx->x[0];
743+
if (out_f) {
744+
fprintf(out_f, "Return value: ");
745+
term_display(out_f, ret_value, ctx);
746+
fprintf(out_f, "\n");
747+
}
748+
749+
run_result_t result;
750+
// ok or 0. 0 is required for running tests for emscripten
751+
if (ret_value == OK_ATOM || ret_value == term_from_int(0)) {
752+
result = RUN_SUCCESS;
753+
} else {
754+
result = RUN_RESULT_NOT_OK;
755+
}
756+
757+
context_destroy(ctx);
758+
759+
return result;
760+
}

src/libAtomVM/globalcontext.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ struct RefcBinaryQueueItem
9090
struct RefcBinary *refc;
9191
};
9292

93+
typedef enum run_result_t
94+
{
95+
RUN_SUCCESS = 0,
96+
RUN_MEMORY_FAILURE = 1,
97+
RUN_RESULT_NOT_OK = 2,
98+
} run_result_t;
99+
93100
struct GlobalContext
94101
{
95102
struct ListHead ready_processes;
@@ -512,6 +519,21 @@ Module *globalcontext_get_module(GlobalContext *global, atom_index_t module_name
512519
*/
513520
Module *globalcontext_load_module_from_avm(GlobalContext *global, const char *module_name);
514521

522+
/**
523+
* @brief Run a given start module.
524+
*
525+
* @details This function will create a new context and call init:boot/1
526+
* to execute the passed start module. If init module is not found, it will
527+
* fallback to calling start module:start/0 directly. It will also
528+
* print the result to out_f, typically stdout. It will return RUN_SUCCESS if
529+
* result is ok or 0, an error code otherwise.
530+
* @param global the global context
531+
* @param start_module the start module
532+
* @param out_f file to print the result to, or NULL
533+
* @returns RUN_SUCCESS or an error code
534+
*/
535+
run_result_t globalcontext_run(GlobalContext *global, Module *start_module, FILE *out_f);
536+
515537
#ifndef __cplusplus
516538
static inline uint64_t globalcontext_get_ref_ticks(GlobalContext *global)
517539
{

src/platforms/emscripten/src/main.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,16 @@ static int start(void)
9595
fprintf(stderr, "main module not loaded\n");
9696
return EXIT_FAILURE;
9797
}
98-
Context *ctx = context_new(global);
99-
ctx->leader = 1;
100-
context_execute_loop(ctx, main_module, "start", 0);
101-
term ret_value = ctx->x[0];
102-
fprintf(stdout, "Return value: ");
103-
term_display(stdout, ret_value, ctx);
104-
fprintf(stdout, "\n");
98+
99+
run_result_t ret_value = globalcontext_run(global, main_module, stdout);
105100

106101
int status;
107-
if (ret_value == OK_ATOM || ret_value == term_from_int(0)) {
102+
if (ret_value == RUN_SUCCESS) {
108103
status = EXIT_SUCCESS;
109104
} else {
110105
status = EXIT_FAILURE;
111106
}
112107

113-
context_destroy(ctx);
114-
115108
return status;
116109
}
117110

src/platforms/esp32/components/avm_sys/include/platform_defaultatoms.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ X(BACKLOG_ATOM, "\x7", "backlog")
4949
X(ACCEPT_ATOM, "\x6", "accept")
5050
X(FD_ATOM, "\x2", "fd")
5151

52-
X(INIT_ATOM, "\x4", "init")
5352
X(GET_PORT_ATOM, "\x8", "get_port")
5453
X(SOCKNAME_ATOM, "\x8", "sockname")
5554
X(PEERNAME_ATOM, "\x8", "peername")

src/platforms/esp32/main/main.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,19 @@ void app_main()
119119
AVM_ABORT();
120120
}
121121
globalcontext_insert_module(glb, mod);
122-
Context *ctx = context_new(glb);
123-
ctx->leader = 1;
124122

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

128-
context_execute_loop(ctx, mod, "start", 0);
129-
term ret_value = ctx->x[0];
130-
131-
fprintf(stdout, "AtomVM finished with return value: ");
132-
term_display(stdout, ret_value, ctx);
133-
fprintf(stdout, "\n");
126+
run_result_t result = globalcontext_run(glb, mod, stdout);
134127

135128
bool reboot_on_not_ok =
136129
#if defined(CONFIG_REBOOT_ON_NOT_OK)
137130
CONFIG_REBOOT_ON_NOT_OK ? true : false;
138131
#else
139132
false;
140133
#endif
141-
if (reboot_on_not_ok && ret_value != OK_ATOM) {
134+
if (reboot_on_not_ok && result != RUN_SUCCESS) {
142135
ESP_LOGE(TAG, "AtomVM application terminated with non-ok return value. Rebooting ...");
143136
esp_restart();
144137
} else {

0 commit comments

Comments
 (0)