Add a 'dir' function

parent a9e26b95
function (args) {
var env = args.environ;
var argv = args.argv
return argv
var result = Object.keys(env);
var argv = args.argv;
var modules = args.modules;
var result = modules.dir('./src');
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);
// Construct array
duk_idx_t dir_idx = duk_push_array(ctx);
// Shove filenames into array
struct dirent *dir;
int i = 0;
DIR *d = opendir(dirname);
if (d) {
while (i < 100) {
dir = readdir(d);
if (dir == NULL) break;
duk_push_string(ctx, dir->d_name);
duk_put_prop_index(ctx, dir_idx, i);
i++;
}
closedir(d);
}
return 1;
}
......@@ -13,6 +13,7 @@
#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"
......@@ -53,7 +54,13 @@ int main(int argc, char *argv[] /* char *environ[] */) {
// Create environment variables object
ret = app_push_environ(ctx);
duk_put_prop_string(ctx, argument_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");
// Run top-level module
ret = duk_pcall(ctx, 1);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
......
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