MCPcopy Create free account
hub / github.com/NativeScript/android / NewThreadCallback

Method NewThreadCallback

test-app/runtime/src/main/cpp/CallbackHandlers.cpp:992–1101  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

990}
991
992void 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

Callers

nothing calls this directly

Calls 15

NewFunction · 0.85
IsConstructCallMethod · 0.80
ThisMethod · 0.80
IsStringMethod · 0.80
ToLocalMethod · 0.80
GetFrameMethod · 0.80
emplaceMethod · 0.80
NewStringUTFMethod · 0.80
CallStaticVoidMethodMethod · 0.80
ReThrowToV8Method · 0.80
LengthMethod · 0.45

Tested by

no test coverage detected