Launch 2 JS threads per native thread AND hide global Duktape object from 2nd thread.

parent b342c88a
......@@ -2,6 +2,17 @@ function (args) {
var env = args.environ;
var argv = args.argv;
var modules = args.modules;
if (typeof global === 'undefined') {
(function () {
var global = new Function('return this;')();
Object.defineProperty(global, 'global', {
value: global,
writable: true,
enumerable: false,
configurable: true
});
})();
}
var result = modules.dir('./src');
result.sort();
modules.print(JSON.stringify(result));
......
void app_stash_global_string(duk_context *ctx, char *name) {
duk_push_global_stash(ctx); // summon the magic stash object
duk_get_global_string(ctx, name);
duk_put_prop_string(ctx, -2, name); // save as property on magic stash object
duk_pop(ctx); // Disappear the stash
}
void app_unstash_global_string(duk_context *ctx, char *name) {
duk_push_global_stash(ctx); // summon the magic stash object
// TODO: Throw an error if property not found. (Right now just returns undefined)
duk_get_prop_string(ctx, -1, name); // retrieve property
duk_remove(ctx, -2); // Disappear the stash
}
......@@ -18,6 +18,8 @@ extern char **environ;
#include "app_push_dir.c"
#include "native_print.c"
#include "native_import.c"
#include "app_stash_global_string.c"
#include "app_unstash_global_string.c"
int main(int argc, char *argv[] /* char *environ[] */) {
// Re-open stdin and stdout in binary mode
......@@ -35,11 +37,21 @@ int main(int argc, char *argv[] /* char *environ[] */) {
exit(0);
}
// Fork into native (gcc) threads.
duk_int_t result;
#pragma omp parallel
{
// Create a JS environment
duk_context *ctx = duk_create_heap_default();
// Save a copy of the Duktape object...
app_stash_global_string(ctx, "Duktape");
// ...then make it inaccessible to JS code.
duk_push_undefined(ctx);
duk_put_global_string(ctx, "Duktape");
// Creat a 2nd JS thread (yes, it's possible in Duktape!)
duk_push_thread(ctx); // TODO: Use duk_push_thread_new_globalenv for modules?
duk_context *ctx2 = duk_get_context(ctx, -1);
// Load top-level function module
duk_int_t ret = native_import(ctx, argv[1]);
......@@ -62,6 +74,8 @@ int main(int argc, char *argv[] /* char *environ[] */) {
duk_put_prop_string(ctx, -2, "dir");
duk_push_c_function(ctx, native_print, DUK_VARARGS);
duk_put_prop_string(ctx, -2, "print");
app_unstash_global_string(ctx, "Duktape"); // summon from the magic stash
duk_put_prop_string(ctx, -2, "Duktape");
duk_put_prop_string(ctx, -2, "modules");
// Run top-level module, passing the god arg as the sole argument.
......@@ -75,6 +89,42 @@ int main(int argc, char *argv[] /* char *environ[] */) {
// printf("%s\n", duk_to_string(ctx, -1));
// }
// Load top-level function module
ret = native_import(ctx2, argv[1]);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx2, -1));
goto finally;
}
// Construct the all-powerful god argument
duk_push_object(ctx2);
// Create argv object
app_push_argv(ctx2, argc, argv);
duk_put_prop_string(ctx2, -2, "argv");
// Create environment variables object
app_push_environ(ctx2);
duk_put_prop_string(ctx2, -2, "environ");
// Provide access to native functions
duk_push_object(ctx2);
duk_push_c_function(ctx2, app_push_dir, 1);
duk_put_prop_string(ctx2, -2, "dir");
duk_push_c_function(ctx2, native_print, DUK_VARARGS);
duk_put_prop_string(ctx2, -2, "print");
duk_get_global_string(ctx2, "Duktape");
duk_put_prop_string(ctx2, -2, "Duktape");
duk_put_prop_string(ctx2, -2, "modules");
// Run top-level module, passing the god arg as the sole argument.
ret = duk_pcall(ctx2, 1);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx2, -1));
goto finally;
}
// else {
// duk_push_context_dump(ctx);
// printf("%s\n", duk_to_string(ctx, -1));
// }
finally:
duk_destroy_heap(ctx);
#pragma omp critical
......
......@@ -2,9 +2,9 @@
DUKBOOTS="${1:-./dukboots}"
NUM_CORES="$(grep -c ^processor /proc/cpuinfo)"
echo "$NUM_CORES cores"
EXPECT_ONE='[".","..","app_push_argv.c","app_push_dir.c","app_push_environ.c","main.c","native_import.c","native_print.c"]'
EXPECT_ONE='[".","..","app_push_argv.c","app_push_dir.c","app_push_environ.c","app_stash_global_string.c","app_unstash_global_string.c","main.c","native_import.c","native_print.c"]'
EXPECT="$EXPECT_ONE"
for n in $(seq 2 $NUM_CORES); do
for n in $(seq 2 $((NUM_CORES * 2))); do
EXPECT="$EXPECT
$EXPECT_ONE"
done
......
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