| 4148 | } |
| 4149 | |
| 4150 | static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target, |
| 4151 | int argc, JSValueConst *argv) |
| 4152 | { |
| 4153 | JSRuntime *rt = JS_GetRuntime(ctx); |
| 4154 | WorkerFuncArgs *args = NULL; |
| 4155 | js_thread_t thr; |
| 4156 | JSValue obj = JS_UNDEFINED; |
| 4157 | int ret; |
| 4158 | const char *filename = NULL, *basename; |
| 4159 | JSAtom basename_atom; |
| 4160 | |
| 4161 | /* XXX: in order to avoid problems with resource liberation, we |
| 4162 | don't support creating workers inside workers */ |
| 4163 | if (!is_main_thread(rt)) |
| 4164 | return JS_ThrowTypeError(ctx, "cannot create a worker inside a worker"); |
| 4165 | |
| 4166 | /* base name, assuming the calling function is a normal JS |
| 4167 | function */ |
| 4168 | basename_atom = JS_GetScriptOrModuleName(ctx, 1); |
| 4169 | if (basename_atom == JS_ATOM_NULL) { |
| 4170 | return JS_ThrowTypeError(ctx, "could not determine calling script or module name"); |
| 4171 | } |
| 4172 | basename = JS_AtomToCString(ctx, basename_atom); |
| 4173 | JS_FreeAtom(ctx, basename_atom); |
| 4174 | if (!basename) |
| 4175 | goto fail; |
| 4176 | |
| 4177 | /* module name */ |
| 4178 | filename = JS_ToCString(ctx, argv[0]); |
| 4179 | if (!filename) |
| 4180 | goto fail; |
| 4181 | |
| 4182 | args = malloc(sizeof(*args)); |
| 4183 | if (!args) |
| 4184 | goto oom_fail; |
| 4185 | memset(args, 0, sizeof(*args)); |
| 4186 | args->filename = strdup(filename); |
| 4187 | args->basename = strdup(basename); |
| 4188 | |
| 4189 | /* ports */ |
| 4190 | args->recv_pipe = js_new_message_pipe(); |
| 4191 | if (!args->recv_pipe) |
| 4192 | goto oom_fail; |
| 4193 | args->send_pipe = js_new_message_pipe(); |
| 4194 | if (!args->send_pipe) |
| 4195 | goto oom_fail; |
| 4196 | |
| 4197 | obj = js_worker_ctor_internal(ctx, new_target, |
| 4198 | args->send_pipe, args->recv_pipe); |
| 4199 | if (JS_IsException(obj)) |
| 4200 | goto fail; |
| 4201 | |
| 4202 | ret = js_thread_create(&thr, worker_func, args, JS_THREAD_CREATE_DETACHED); |
| 4203 | if (ret != 0) { |
| 4204 | JS_ThrowTypeError(ctx, "could not create worker"); |
| 4205 | goto fail; |
| 4206 | } |
| 4207 | JS_FreeCString(ctx, basename); |
nothing calls this directly
no test coverage detected