MCPcopy Create free account
hub / github.com/Snapchat/Valdi / loadJsModule

Method loadJsModule

valdi/src/valdi/runtime/JavaScript/JavaScriptRuntime.cpp:1364–1474  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1362
1363 auto jsPaths = bundle->getAllJsPaths();
1364 auto output = callContext.getContext().newArray(jsPaths.size(), callContext.getExceptionTracker());
1365 CHECK_CALL_CONTEXT(callContext);
1366
1367 size_t index = 0;
1368 for (const auto& jsPath : jsPaths) {
1369 auto jsPathJs =
1370 callContext.getContext().newStringUTF8(jsPath.toStringView(), callContext.getExceptionTracker());
1371 CHECK_CALL_CONTEXT(callContext);
1372 callContext.getContext().setObjectPropertyIndex(
1373 output.get(), index++, jsPathJs.get(), callContext.getExceptionTracker());
1374 CHECK_CALL_CONTEXT(callContext);
1375 }
1376
1377 return output;
1378}
1379
1380JSValueRef JavaScriptRuntime::runtimeLoadModule(JSFunctionNativeCallContext& callContext) {
1381 auto moduleName = callContext.getParameterAsString(0);
1382 CHECK_CALL_CONTEXT(callContext);
1383
1384 Shared<JSValueRefHolder> callback;
1385 auto callbackParam = callContext.getParameter(1);
1386 if (callContext.getContext().isValueUndefined(callbackParam)) {
1387 if (!_resourceManager.isLazyModulePreloadingEnabled()) {
1388 // When not providing a callback, we consider it as a preload operation, which
1389 // we dont need if eager module preloading is enabled
1390 return callContext.getContext().newUndefined();
1391 }
1392 } else {
1393 callback = JSValueRefHolder::makeRetainedCallback(
1394 callContext.getContext(),
1395 callbackParam,
1396 ReferenceInfoBuilder(callContext.getReferenceInfo()).withParameter(1),
1397 callContext.getExceptionTracker());
1398 }
1399
1400 _resourceManager.loadModuleAsync(
1401 moduleName, ResourceManagerLoadModuleType::Sources, [this, callback](const Result<Void>& result) {
1402 if (callback == nullptr) {
1403 return;
1404 }
1405
1406 this->dispatchOnJsThreadAsync(callback->getContext(), [callback, result](JavaScriptEntryParameters& entry) {
1407 auto jsValue = callback->getJsValue(entry.jsContext, entry.exceptionTracker);
1408 if (!entry.exceptionTracker) {
1409 return;
1410 }
1411
1412 if (result) {
1413 JSFunctionCallContext callContext(entry.jsContext, nullptr, 0, entry.exceptionTracker);
1414 entry.jsContext.callObjectAsFunction(jsValue, callContext);
1415 } else {
1416 auto errorString = entry.jsContext.newStringUTF8(result.error().toString(), entry.exceptionTracker);
1417 if (!entry.exceptionTracker) {
1418 return;
1419 }
1420
1421 JSFunctionCallContext callContext(entry.jsContext, &errorString, 1, entry.exceptionTracker);

Callers

nothing calls this directly

Calls 15

getGlobalFunction · 0.85
getMemoryUsageBytesFunction · 0.85
getCurrentTimestampMsFunction · 0.85
moduleLoadModeToStringFunction · 0.85
newUndefinedMethod · 0.80
getBundleMethod · 0.80
makeStringMethod · 0.80
getJsMethod · 0.80
startMethod · 0.65
onErrorMethod · 0.65
valueMethod · 0.65

Tested by

no test coverage detected