Hi,
Looking up the latest release, finally I understood how to create
asynchronous jobs with quickjs, I've come up with this new method in the
quickjs-lib to make it more generic:
*static* *const* JSCFunctionListEntry js_std_funcs[] = {
...
JS_CFUNC_DEF("process", 2, js_std_process )
};
...
*static* JSValue js_std_process(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
*const char* *command, *mode = NULL;
FILE *p;
command = JS_ToCString(ctx, argv[0]);
*if* (!command)
*goto* fail;
mode = JS_ToCString(ctx, argv[1]);
*if* (!mode)
goto fail;
*if* (mode[strspn(mode, "rwa+b")] != '\0') {
js_std_throw_errno(ctx, EINVAL);
*goto* fail;
}
p = popen(command, mode);
JS_FreeCString(ctx, command);
JS_FreeCString(ctx, mode);
*if* (!p)
*return* js_std_throw_errno(ctx, errno);
*return* js_new_std_file(ctx, p, FALSE);
*fail*:
printf("Error");
JS_FreeCString(ctx, command);
JS_FreeCString(ctx, mode);
*return* JS_EXCEPTION;
}
Surely it can be improved, I've tried just to follow the rest of the code,
here is an example:
*import* * *as* std *from* "std"
*import* * *as* os *from* "os"
*function* ls (dir) {
*const* file = std.process(`find ${dir}`, "r")
*const* fd = file.fileno()
os.setReadHandler(fd, *function*() {
*let* line = file.getline()
*if* (line) {
std.out.printf("%s\n", line)
}
*else* {
file.close()
os.setReadHandler(fd, null)
}
})
}
ls('/etc')
ls('/bin')
console.log('This is printed first because our listeners run
asynchronously!')
I've tested by using a *tail -f *on several files and it works perfectly.
I'd like to have a way to add more global stuff to the *std* or *os*
modules without to modify the core, would be nice for having this up to
date.
Regards
*Carlos Alberto Castaño*
Software Developer
Movil: +34 684 275 791
Madrid - España
El mar., 6 ago. 2019 a las 10:41, Sam Chang (<dmarc-noreply@xxxxxxxxxxxxx>)
escribió:
You can try the following steps:
1) make libcurl works asynchronously in your environment.
2) intergate it with Promise in javascript
Regards,
Sam
On Aug 6, 2019, at 4:23 PM, Carlos Alberto Castaño García <calbertts@xxxxxxxxx> wrote:
behave asynchronously, I mean, return and later, invoke the corresponding
Hi,
I've been learning a lot reading the quickjs source code.
I'm trying to implement some C functions (libCURL), but I want them to
callback.
implementing an event loop.
I've seen that Libuv or libev and similars can achieve this by
I want to know if that's the only way or there's another way, I wantsomething similar I saw in the QuickJS API with setReadHandler and timeouts.
Is there any example to take a look on?
Regard