MCPcopy Create free account
hub / github.com/buke/quickjs-go / js_os_setReadHandler

Function js_os_setReadHandler

deps/quickjs/quickjs-libc.c:2289–2329  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2287}
2288
2289static JSValue js_os_setReadHandler(JSContext *ctx, JSValueConst this_val,
2290 int argc, JSValueConst *argv, int magic)
2291{
2292 JSRuntime *rt = JS_GetRuntime(ctx);
2293 JSThreadState *ts = js_get_thread_state(rt);
2294 JSOSRWHandler *rh;
2295 int fd;
2296 JSValueConst func;
2297
2298 if (JS_ToInt32(ctx, &fd, argv[0]))
2299 return JS_EXCEPTION;
2300 func = argv[1];
2301 if (JS_IsNull(func)) {
2302 rh = find_rh(ts, fd);
2303 if (rh) {
2304 JS_FreeValue(ctx, rh->rw_func[magic]);
2305 rh->rw_func[magic] = JS_NULL;
2306 if (JS_IsNull(rh->rw_func[0]) &&
2307 JS_IsNull(rh->rw_func[1])) {
2308 /* remove the entry */
2309 free_rw_handler(JS_GetRuntime(ctx), rh);
2310 }
2311 }
2312 } else {
2313 if (!JS_IsFunction(ctx, func))
2314 return JS_ThrowTypeError(ctx, "not a function");
2315 rh = find_rh(ts, fd);
2316 if (!rh) {
2317 rh = js_mallocz(ctx, sizeof(*rh));
2318 if (!rh)
2319 return JS_EXCEPTION;
2320 rh->fd = fd;
2321 rh->rw_func[0] = JS_NULL;
2322 rh->rw_func[1] = JS_NULL;
2323 list_add_tail(&rh->link, &ts->os_rw_handlers);
2324 }
2325 JS_FreeValue(ctx, rh->rw_func[magic]);
2326 rh->rw_func[magic] = JS_DupValue(ctx, func);
2327 }
2328 return JS_UNDEFINED;
2329}
2330
2331static JSOSSignalHandler *find_sh(JSThreadState *ts, int sig_num)
2332{

Callers

nothing calls this directly

Calls 5

js_get_thread_stateFunction · 0.85
JS_IsNullFunction · 0.85
find_rhFunction · 0.85
free_rw_handlerFunction · 0.85
list_add_tailFunction · 0.85

Tested by

no test coverage detected