Skip to content

Commit

Permalink
Split bare_run() into bare_run() and bare_load()
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Jun 20, 2024
1 parent d9138f6 commit 411b113
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 22 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ Bare can easily be embedded using the C API defined in [`include/bare.h`](includ
bare_t *bare;
bare_setup(uv_default_loop(), platform, &env, argc, argv, options, &bare);

bare_run(bare, filename, source);
bare_load(bare, filename, source);

bare_run(bare);

int exit_code;
bare_teardown(bare, &exit_code);
Expand Down
5 changes: 4 additions & 1 deletion bin/bare.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ main (int argc, char *argv[]) {

uv_buf_t source = uv_buf_init((char *) bare_bundle, bare_bundle_len);

bare_run(bare, "/bare.bundle", &source);
bare_load(bare, "/bare.bundle", &source);

err = bare_run(bare);
assert(err == 0);

int exit_code;
err = bare_teardown(bare, &exit_code);
Expand Down
10 changes: 8 additions & 2 deletions include/bare.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ int
bare_teardown (bare_t *bare, int *exit_code);

/**
* Run the module identified by `filename`, which may be any of the formats
* Load the module identified by `filename`, which may be any of the formats
* supported by the module system. Unless `source` is provided, the contents
* of `filename` will be read from disk.
*
* See https://github.com/holepunchto/bare-module for more information on the
* supported module formats.
*/
int
bare_run (bare_t *bare, const char *filename, const uv_buf_t *source);
bare_load (bare_t *bare, const char *filename, const uv_buf_t *source);

/**
* Run the I/O event loop.
*/
int
bare_run (bare_t *bare);

/**
* Suspend the process as soon as possible. Once the process has suspended
Expand Down
15 changes: 13 additions & 2 deletions src/bare.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ bare_teardown (bare_t *bare, int *exit_code) {
}

int
bare_run (bare_t *bare, const char *filename, const uv_buf_t *source) {
bare_load (bare_t *bare, const char *filename, const uv_buf_t *source) {
int err;

bare_runtime_t *runtime = bare->process.runtime;

err = bare_runtime_run(
err = bare_runtime_load(
runtime,
filename,
(bare_source_t){
Expand All @@ -96,6 +96,17 @@ bare_run (bare_t *bare, const char *filename, const uv_buf_t *source) {
return err;
}

int
bare_run (bare_t *bare) {
int err;

bare_runtime_t *runtime = bare->process.runtime;

err = bare_runtime_run(runtime);

return err;
}

int
bare_suspend (bare_t *bare) {
return uv_async_send(&bare->process.runtime->signals.suspend);
Expand Down
4 changes: 2 additions & 2 deletions src/bare.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ bare.onthread = exports._onthread.bind(exports)

/**
* Step 8:
* Register the main entry function used by `bare_run()`.
* Register the main entry function used by `bare_load()`.
*/

const Module = require('bare-module')
const url = require('bare-url')

bare.run = function run (filename, source) {
bare.load = function load (filename, source) {
Module.load(url.pathToFileURL(filename), source ? Buffer.from(source) : null)
}
27 changes: 17 additions & 10 deletions src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ bare_runtime_teardown (bare_runtime_t *runtime, int *exit_code) {
}

int
bare_runtime_run (bare_runtime_t *runtime, const char *filename, bare_source_t source) {
bare_runtime_load (bare_runtime_t *runtime, const char *filename, bare_source_t source) {
int err;

js_env_t *env = runtime->env;
Expand All @@ -1099,8 +1099,8 @@ bare_runtime_run (bare_runtime_t *runtime, const char *filename, bare_source_t s
err = js_get_reference_value(env, runtime->exports, &exports);
assert(err == 0);

js_value_t *run;
err = js_get_named_property(env, exports, "run", &run);
js_value_t *load;
err = js_get_named_property(env, exports, "load", &load);
assert(err == 0);

js_value_t *args[2];
Expand Down Expand Up @@ -1133,11 +1133,24 @@ bare_runtime_run (bare_runtime_t *runtime, const char *filename, bare_source_t s
err = js_get_global(env, &global);
assert(err == 0);

js_call_function(env, global, run, 2, args, NULL);
js_call_function(env, global, load, 2, args, NULL);

err = js_close_handle_scope(env, scope);
assert(err == 0);

return 0;

err:
err = js_close_handle_scope(env, scope);
assert(err == 0);

return -1;
}

int
bare_runtime_run (bare_runtime_t *runtime) {
int err;

do {
err = uv_run(runtime->loop, UV_RUN_DEFAULT);

Expand All @@ -1162,10 +1175,4 @@ bare_runtime_run (bare_runtime_t *runtime, const char *filename, bare_source_t s
assert(err == 0);

return 0;

err:
err = js_close_handle_scope(env, scope);
assert(err == 0);

return -1;
}
5 changes: 4 additions & 1 deletion src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ int
bare_runtime_teardown (bare_runtime_t *runtime, int *exit_code);

int
bare_runtime_run (bare_runtime_t *runtime, const char *filename, bare_source_t source);
bare_runtime_load (bare_runtime_t *runtime, const char *filename, bare_source_t source);

int
bare_runtime_run (bare_runtime_t *runtime);

#endif // BARE_RUNTIME_H
5 changes: 4 additions & 1 deletion src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ bare_thread_entry (void *opaque) {
runtime->process->on_thread((bare_t *) runtime->process, env);
}

bare_runtime_run(runtime, thread->filename, source);
bare_runtime_load(runtime, thread->filename, source);

err = bare_runtime_run(runtime);
assert(err == 0);

free(thread->filename);

Expand Down
5 changes: 4 additions & 1 deletion test/argv-empty.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ main (int argc, char *argv[]) {

uv_buf_t source = uv_buf_init("", 0);

bare_run(bare, "/test.js", &source);
bare_load(bare, "/test.js", &source);

e = bare_run(bare);
assert(e == 0);

int exit_code;
e = bare_teardown(bare, &exit_code);
Expand Down
5 changes: 4 additions & 1 deletion test/restart.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ main (int argc, char *argv[]) {

uv_buf_t source = uv_buf_init(code, strlen(code));

bare_run(bare, "/test.js", &source);
bare_load(bare, "/test.js", &source);

e = bare_run(bare);
assert(e == 0);

e = bare_teardown(bare, &exit_code);
assert(e == 0);
Expand Down

0 comments on commit 411b113

Please sign in to comment.