@@ -1271,11 +1271,28 @@ napi_value CallbackHandlers::NewThreadCallback(napi_env env, napi_callback_info
1271
1271
throw NativeScriptException (" Worker should be called as a constructor!" );
1272
1272
}
1273
1273
1274
- if (argc != 1 || !napi_util::is_of_type (env, argv[0 ], napi_string)) {
1274
+ napi_valuetype value_type;
1275
+ napi_typeof (env, argv[0 ], &value_type);
1276
+
1277
+ if (argc != 1 || (value_type != napi_string && value_type != napi_object) ) {
1275
1278
throw NativeScriptException (
1276
- " Worker should be called with one string parameter (name of file to run)!" );
1279
+ " Worker should be called with one parameter (name of file to run) or a URL to the file" );
1280
+ }
1281
+
1282
+ napi_value workerFilePath;
1283
+ std::string baseurl_str;
1284
+ if (value_type == napi_object) {
1285
+ napi_get_named_property (env, argv[0 ], " href" , &workerFilePath);
1286
+ if (napi_util::is_null_or_undefined (env, workerFilePath)) {
1287
+ throw NativeScriptException (
1288
+ " Worker should be called with one parameter (name of file to run) or a URL to the file" );
1289
+ }
1290
+ } else {
1291
+ workerFilePath = argv[0 ];
1277
1292
}
1278
1293
1294
+
1295
+
1279
1296
napi_value global;
1280
1297
napi_get_global (env, &global);
1281
1298
@@ -1290,9 +1307,18 @@ napi_value CallbackHandlers::NewThreadCallback(napi_env env, napi_callback_info
1290
1307
currentDir = currentDir.substr (fileSchema.length ());
1291
1308
}
1292
1309
1293
- std::string workerPath = ArgConverter::ConvertToString (env, argv[0 ]);
1310
+ std::string workerPath = ArgConverter::ConvertToString (env, workerFilePath);
1311
+
1312
+ if (workerPath.compare (0 , fileSchema.length (), fileSchema) == 0 ) {
1313
+ workerPath = workerPath.substr (fileSchema.length ());
1314
+ auto workerPathPrefix = workerPath.substr (0 , 1 ) == " /" ? " ~" : " ~/" ;
1315
+ workerPath = workerPathPrefix + workerPath;
1316
+ }
1317
+
1294
1318
DEBUG_WRITE (" Worker Path: %s, Current Dir: %s" , workerPath.c_str (), currentDir.c_str ());
1295
1319
1320
+
1321
+
1296
1322
// Will throw if path is invalid or doesn't exist
1297
1323
ModuleInternal::CheckFileExists (env, workerPath, currentDir);
1298
1324
@@ -1306,7 +1332,7 @@ napi_value CallbackHandlers::NewThreadCallback(napi_env env, napi_callback_info
1306
1332
DEBUG_WRITE (" Called Worker constructor id=%d" , workerId);
1307
1333
1308
1334
JEnv jEnv;
1309
- JniLocalRef filePath (ArgConverter::ConvertToJavaString (env, argv[ 0 ] ));
1335
+ JniLocalRef filePath (jEnv. NewStringUTF (workerPath. c_str () ));
1310
1336
JniLocalRef dirPath (jEnv.NewStringUTF (currentDir.c_str ()));
1311
1337
jEnv.CallStaticVoidMethod (RUNTIME_CLASS, INIT_WORKER_METHOD_ID, (jstring) filePath,
1312
1338
(jstring) dirPath, workerId);
0 commit comments