Open moar82 opened 5 years ago
If you're referring to the --> undefined
printouts, they are simply the result of eval()
ing the input:
In particular, if the script being eval'd ends with assert(...)
, assert() will return undefined, and thus the script result will be undefined on success. The makefile tests use this assert() technique, throwing an error if some test fails, and that error would then show up in the output: --> TypeError: xxx
.
Sorry, I was expecting a more straightforward test. I apologize my lack of knowledge, but I'm struggling to fix the blanks on the documentation (https://github.com/svaarala/duktape/tree/master/extras/module-node).
Is it too much to ask for a running example? something like runing this script that requires a node.js library?
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
This is my C code:
#include <stdio.h>
#include <string.h>
#include "duktape.h"
#include "duk_module_node.h"
static duk_ret_t cb_resolve_module(duk_context *ctx) {
/*
* Entry stack: [ requested_id parent_id ]
*/
const char *requested_id = duk_get_string(ctx, 0);
const char *parent_id = duk_get_string(ctx, 1); /* calling module */
const char *resolved_id;
/* Arrive at the canonical module ID somehow. */
duk_push_string(ctx, resolved_id);
return 1; /*nrets*/
}
static duk_ret_t cb_load_module(duk_context *ctx) {
const char *filename;
const char *module_id;
module_id = duk_require_string(ctx, 0);
duk_get_prop_string(ctx, 2, "filename");
filename = duk_require_string(ctx, -1);
duk_push_string(ctx, module_id);
return 1;
}
static duk_ret_t handle_print(duk_context *ctx) {
printf("%s\n", duk_safe_to_string(ctx, 0));
return 0;
}
static duk_ret_t handle_assert(duk_context *ctx) {
if (duk_to_boolean(ctx, 0)) {
return 0;
}
(void) duk_generic_error(ctx, "assertion failed: %s", duk_safe_to_string(ctx, 1));
return 0;
}
/* We assume a JS maximum file size of 17kB. */
static void push_file_as_string(duk_context *ctx, const char *filename) {
FILE *f;
size_t len;
char buf[17384];
f = fopen(filename, "rb");
if (f) {
len = fread((void *) buf, 1, sizeof(buf), f);
fclose(f);
duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len);
} else {
duk_push_undefined(ctx);
}
}
int main(int argc, char *argv[]) {
duk_context *ctx;
//int i;
//int exitcode = 0;
ctx = duk_create_heap_default();
if (!ctx) {
return 1;
}
duk_push_c_function(ctx, handle_print, 1);
duk_put_global_string(ctx, "print");
duk_push_c_function(ctx, handle_assert, 2);
duk_put_global_string(ctx, "assert");
duk_push_object(ctx);
duk_push_c_function(ctx, cb_resolve_module, DUK_VARARGS);
duk_put_prop_string(ctx, -2, "resolve");
duk_push_c_function(ctx, cb_load_module, DUK_VARARGS);
duk_put_prop_string(ctx, -2, "load");
duk_module_node_init(ctx);
/* We push to Duktape heap the JS file*/
push_file_as_string(ctx, argv[1]);
if (duk_peval(ctx) != 0) {
printf("Error: %s\n", duk_safe_to_string(ctx, -1));
goto finished;
}
if (duk_pcall(ctx, 0 /*nargs*/) != 0) {
printf("Error: %s\n", duk_safe_to_string(ctx, -1));
}
duk_pop(ctx); /* pop result/error */
finished:
duk_destroy_heap(ctx);
printf("Done\n");
}
Thanks for the support
@moar82 If you’re expecting that require("http")
to work automatically with the node module loader, it won’t unless you provide your own implementation of the http
module...
This is just a module loader framework providing support for Node.js-style CommonJS modules (i.e. require()
itself). You don’t get the Node.js APIs “for free” if that’s what you were expecting.
Hello, I'm using version duktape 2.3.0 and I have issues to load Node.js modules.
In the following web page: https://duktape.org/guide.html there is a link for a Node.js modules compatible loader. However, the following link is broken "See How to use Node.js-like modules for examples."
I'm running the example in duktape-2.3.0/extras/module-node However, I got undefined in all cases. I'm not sure I understand the test, but I see in test.c file the following instruction:
Which is clearly not working.
Any help would be appreciated.
Regards