Skip to content

Commit 56aa07e

Browse files
committed
runtime: allow URL in worker constructor
1 parent d85eb9c commit 56aa07e

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

test-app/runtime/src/main/cpp/runtime/callbackhandlers/CallbackHandlers.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,11 +1271,28 @@ napi_value CallbackHandlers::NewThreadCallback(napi_env env, napi_callback_info
12711271
throw NativeScriptException("Worker should be called as a constructor!");
12721272
}
12731273

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) ) {
12751278
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];
12771292
}
12781293

1294+
1295+
12791296
napi_value global;
12801297
napi_get_global(env, &global);
12811298

@@ -1290,9 +1307,18 @@ napi_value CallbackHandlers::NewThreadCallback(napi_env env, napi_callback_info
12901307
currentDir = currentDir.substr(fileSchema.length());
12911308
}
12921309

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+
12941318
DEBUG_WRITE("Worker Path: %s, Current Dir: %s", workerPath.c_str(), currentDir.c_str());
12951319

1320+
1321+
12961322
// Will throw if path is invalid or doesn't exist
12971323
ModuleInternal::CheckFileExists(env, workerPath, currentDir);
12981324

@@ -1306,7 +1332,7 @@ napi_value CallbackHandlers::NewThreadCallback(napi_env env, napi_callback_info
13061332
DEBUG_WRITE("Called Worker constructor id=%d", workerId);
13071333

13081334
JEnv jEnv;
1309-
JniLocalRef filePath(ArgConverter::ConvertToJavaString(env, argv[0]));
1335+
JniLocalRef filePath(jEnv.NewStringUTF(workerPath.c_str()));
13101336
JniLocalRef dirPath(jEnv.NewStringUTF(currentDir.c_str()));
13111337
jEnv.CallStaticVoidMethod(RUNTIME_CLASS, INIT_WORKER_METHOD_ID, (jstring) filePath,
13121338
(jstring) dirPath, workerId);

0 commit comments

Comments
 (0)