| 990 | } |
| 991 | |
| 992 | void CallbackHandlers::NewThreadCallback(const v8::FunctionCallbackInfo<v8::Value> &args) { |
| 993 | try { |
| 994 | if (!args.IsConstructCall()) { |
| 995 | throw NativeScriptException("Worker should be called as a constructor!"); |
| 996 | } |
| 997 | |
| 998 | if (args.Length() == 0) { |
| 999 | throw NativeScriptException("Not enough arguments."); |
| 1000 | } |
| 1001 | |
| 1002 | if (args.Length() > 2) { |
| 1003 | throw NativeScriptException("Too many arguments passed."); |
| 1004 | } |
| 1005 | |
| 1006 | auto thiz = args.This(); |
| 1007 | auto isolate = thiz->GetIsolate(); |
| 1008 | auto context = isolate->GetCurrentContext(); |
| 1009 | |
| 1010 | std::string workerPath; |
| 1011 | |
| 1012 | // Handle both string URLs and URL objects |
| 1013 | if (args[0]->IsString()) { |
| 1014 | workerPath = ArgConverter::ConvertToString(args[0].As<String>()); |
| 1015 | } else if (args[0]->IsObject()) { |
| 1016 | Local<Object> urlObj = args[0].As<Object>(); |
| 1017 | Local<Value> toStringMethod; |
| 1018 | if (urlObj->Get(context, ArgConverter::ConvertToV8String(isolate, "toString")).ToLocal(&toStringMethod)) { |
| 1019 | if (toStringMethod->IsFunction()) { |
| 1020 | Local<v8::Function> toString = toStringMethod.As<v8::Function>(); |
| 1021 | Local<Value> result; |
| 1022 | if (toString->Call(context, urlObj, 0, nullptr).ToLocal(&result)) { |
| 1023 | if (result->IsString()) { |
| 1024 | std::string stringResult = ArgConverter::ConvertToString(result.As<String>()); |
| 1025 | // Reject plain objects that return "[object Object]" from toString() |
| 1026 | if (stringResult == "[object Object]") { |
| 1027 | throw NativeScriptException("Worker constructor expects a string URL or URL object."); |
| 1028 | } |
| 1029 | workerPath = stringResult; |
| 1030 | } else { |
| 1031 | throw NativeScriptException("Worker URL object toString() must return a string."); |
| 1032 | } |
| 1033 | } else { |
| 1034 | throw NativeScriptException("Error calling toString() on Worker URL object."); |
| 1035 | } |
| 1036 | } else { |
| 1037 | throw NativeScriptException("Worker URL object must have a toString() method."); |
| 1038 | } |
| 1039 | } else { |
| 1040 | throw NativeScriptException("Worker URL object must have a toString() method."); |
| 1041 | } |
| 1042 | } else { |
| 1043 | throw NativeScriptException("Worker constructor expects a string URL or URL object."); |
| 1044 | } |
| 1045 | |
| 1046 | // TODO: Handle options parameter (args[1]) if provided |
| 1047 | // For now, we ignore the options parameter to maintain compatibility |
| 1048 | // TODO: Validate worker path and call worker.onerror if the script does not exist |
| 1049 |
nothing calls this directly
no test coverage detected