Skip to content
Open
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
27 changes: 23 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "duv.h"
#include "misc.h"
#include <unistd.h>

static uv_loop_t loop;
static char *bootstrap = NULL;

// Sync readfile using libuv APIs as an API function.
static duk_ret_t duv_loadfile(duk_context *ctx) {
Expand Down Expand Up @@ -378,7 +380,6 @@ static duk_ret_t duv_mod_compile(duk_context *ctx) {
}

static duk_ret_t duv_main(duk_context *ctx) {

duk_push_global_object(ctx);
duk_dup(ctx, -1);
duk_put_prop_string(ctx, -2, "global");
Expand Down Expand Up @@ -425,6 +426,12 @@ static duk_ret_t duv_main(duk_context *ctx) {
duk_push_object(ctx);
duk_push_c_function(ctx, duv_cwd, 0);
duk_call(ctx, 0);

if (bootstrap != NULL) {
duk_eval_file_noresult(ctx, bootstrap);
}


duk_push_string(ctx, "/main.c");
duk_concat(ctx, 2);
duk_put_prop_string(ctx, -2, "id");
Expand All @@ -438,12 +445,24 @@ static duk_ret_t duv_main(duk_context *ctx) {

int main(int argc, char *argv[]) {
duk_context *ctx = NULL;
int opt;
//char *bootstrap = NULL;

uv_loop_init(&loop);

uv_setup_args(argc, argv);

if (argc < 2) {
fprintf(stderr, "Usage: dukluv script.js\n");
opt = getopt(argc, argv, "b:");

if (opt == 'b') {
bootstrap = optarg;
} else if (opt == '?' && optopt == 'b') {
fprintf(stderr, "expected bootstrap\n");
exit(1);
}

if (argc < 2 || argc == optind) {
fprintf(stderr, "Usage: dukluv [-b bootstrap] script.js\n");
exit(1);
}

Expand All @@ -456,7 +475,7 @@ int main(int argc, char *argv[]) {
loop.data = ctx;

duk_push_c_function(ctx, duv_main, 1);
duk_push_string(ctx, argv[1]);
duk_push_string(ctx, argv[optind]);
if (duk_pcall(ctx, 1)) {
fprintf(stderr, "\nUncaught Exception:\n");
if (duk_is_object(ctx, -1)) {
Expand Down