CLI args and environment variables!

parent eae9944f
[*]
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
[*.{c,h}]
indent_style = space
indent_size = 2
\ No newline at end of file
indent_size = 2
function () {
print('More hello world!');
print('2+3=' + adder(2, 3));
}
\ No newline at end of file
function (args) {
var env = args.environ;
var argv = args.argv
return argv
var result = Object.keys(env);
print(JSON.stringify(result));
return result
}
int app_push_argv(duk_context *ctx, int argc, char *argv[]) {
// Construct argv array
duk_idx_t argv_idx = duk_push_array(ctx);
int i = 0;
while (i < argc && i < 100) {
duk_push_string(ctx, argv[i]);
duk_put_prop_index(ctx, argv_idx, i);
i++;
}
return 0;
}
int app_push_environ(duk_context *ctx) {
int keylen = 0;
char *val;
char *equalsign;
// Construct environment hash
duk_idx_t environ_idx = duk_push_object(ctx);
int i = 0;
while (environ[i] != NULL && i < 100) {
// strtok actually replaces the delimiter with \0
// so I don't want to use it.
equalsign = strpbrk(environ[i], "=");
val = equalsign + 1;
keylen = equalsign - environ[i]; // yay pointer arithmetic
//printf("%d. %.*s,%s\n", i, keylen, environ[i], val);
duk_push_string(ctx, val);
duk_put_prop_lstring(ctx, environ_idx, environ[i], keylen);
i++;
}
return 0;
}
......@@ -11,12 +11,14 @@
#include <fcntl.h>
#endif
#include "app_push_argv.c"
#include "app_push_environ.c"
#include "native_print.c"
#include "native_adder.c"
#include "native_import.c"
#include "register_globals.c"
int main(int argc, char *argv[]) {
int main(int argc, char *argv[] /* char *environ[] */) {
if (argc < 2) {
printf("Usage: dukboots file-to-eval.js\n");
printf("Yup. Pretty basic API at the moment.\n");
......@@ -32,20 +34,33 @@ int main(int argc, char *argv[]) {
_setmode(1, _O_BINARY);
#endif
// Create JS environment
duk_int_t ret = 0;
duk_context *ctx = duk_create_heap_default();
register_globals(ctx);
// Load top-level function module
ret = native_import(ctx, argv[1]);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
goto finally;
}
ret = duk_pcall(ctx, 0);
// Construct the argument
duk_idx_t argument_idx = duk_push_object(ctx);
ret = app_push_argv(ctx, argc, argv);
duk_put_prop_string(ctx, argument_idx, "argv");
// Create environment variables object
ret = app_push_environ(ctx);
duk_put_prop_string(ctx, argument_idx, "environ");
ret = duk_pcall(ctx, 1);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
goto finally;
} else {
duk_push_context_dump(ctx);
printf("%s\n", duk_to_string(ctx, -1));
}
finally:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment