Code cleanup and fix tests

parent 1dbf6c5e
......@@ -3,6 +3,6 @@ function (args) {
var argv = args.argv;
var modules = args.modules;
var result = modules.dir('./src');
print(JSON.stringify(result));
modules.print(JSON.stringify(result));
return result
}
#include <dirent.h>
#include <stdio.h>
int app_push_dir (duk_context *ctx) {
// Check argument
const char *dirname = duk_require_string(ctx, -1);
......
/*
* Very simple example program
*/
extern char **environ;
// Include dependencies
#include <stdio.h>
#include <stdlib.h> // malloc
#include <stdlib.h>
#include <dirent.h>
#include "duktape.h"
// @see [1]
#ifdef _WIN32
// @see [1]
#include <io.h>
#include <fcntl.h>
#endif
extern char **environ;
// Include helper functions
#include "app_push_argv.c"
#include "app_push_environ.c"
#include "app_push_dir.c"
#include "native_print.c"
#include "native_adder.c"
#include "native_import.c"
#include "register_globals.c"
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");
exit(0);
}
// Re-open stdin and stdout in binary mode
freopen(NULL, "rb", stdin);
freopen(NULL, "wb", stdout);
// @see [1]
#ifdef _WIN32
// @see [1]
_setmode(0, _O_BINARY);
_setmode(1, _O_BINARY);
#endif
// Create JS environment
if (argc < 2) {
printf("Usage: dukboots file-to-eval.js\n");
printf("Yup. Pretty basic API at the moment.\n");
exit(0);
}
// Create a 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]);
......@@ -48,28 +45,31 @@ int main(int argc, char *argv[] /* char *environ[] */) {
goto finally;
}
// Construct the argument
duk_idx_t argument_idx = duk_push_object(ctx);
// Construct the all-powerful god argument
duk_idx_t god_arg_idx = duk_push_object(ctx);
ret = app_push_argv(ctx, argc, argv);
duk_put_prop_string(ctx, argument_idx, "argv");
duk_put_prop_string(ctx, god_arg_idx, "argv");
// Create environment variables object
ret = app_push_environ(ctx);
duk_put_prop_string(ctx, argument_idx, "environ");
duk_put_prop_string(ctx, god_arg_idx, "environ");
// Provide access to native functions
ret = duk_push_object(ctx);
duk_push_c_function(ctx, app_push_dir, 1);
duk_put_prop_string(ctx, ret, "dir");
duk_put_prop_string(ctx, argument_idx, "modules");
duk_push_c_function(ctx, native_print, DUK_VARARGS);
duk_put_prop_string(ctx, ret, "print");
duk_put_prop_string(ctx, god_arg_idx, "modules");
// Run top-level module
// Run top-level module, passing the god arg as the sole argument.
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));
}
// else {
// duk_push_context_dump(ctx);
// printf("%s\n", duk_to_string(ctx, -1));
// }
finally:
duk_destroy_heap(ctx);
......
static duk_ret_t native_adder(duk_context *ctx) {
int i;
int n = duk_get_top(ctx); /* #args */
double res = 0.0;
for (i = 0; i < n; i++) {
res += duk_to_number(ctx, i);
}
duk_push_number(ctx, res);
return 1; /* one return value */
}
// TODO: Can we remove this stuff once we have a global proxy object
// we pass into the top-level function?
void register_globals(duk_context *ctx) {
duk_push_c_function(ctx, native_print, DUK_VARARGS);
duk_put_global_string(ctx, "print");
duk_push_c_function(ctx, native_adder, DUK_VARARGS);
duk_put_global_string(ctx, "adder");
}
#!/bin/sh
DUKBOOTS="${1-:dukboots}"
EXPECT='More hello world!
2+3=5'
DUKBOOTS="${1:-./dukboots}"
EXPECT='[".","..","app_push_argv.c","app_push_dir.c","app_push_environ.c","main.c","native_import.c","native_print.c"]'
RESULT="$($DUKBOOTS example.js)"
echo "$EXPECT"
echo "$RESULT"
test "$RESULT" = "$EXPECT"
\ No newline at end of file
test "$RESULT" = "$EXPECT"
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