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

Method onModuleLoaded

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

Source from the content-addressed store, hash-verified

2639 }
2640
2641 return importModule(resourceIdResult.value(), jsEntry);
2642}
2643
2644Shared<JavaScriptModuleContainer> JavaScriptRuntime::importModule(const ResourceId& resourceId,
2645 JavaScriptEntryParameters& jsEntry) {
2646 const auto& it = _modules.find(resourceId);
2647 if (it != _modules.end()) {
2648 return it->second;
2649 }
2650
2651 auto absoluteModulePath = resourceId.toAbsolutePath();
2652
2653 VALDI_TRACE_META("Valdi.importModule", absoluteModulePath);
2654 JavaScriptContextEntry contextEntry(_globalContext);
2655
2656 auto bundle = _resourceManager.getBundle(resourceId.bundleName);
2657
2658 auto path = jsEntry.jsContext.newStringUTF8(absoluteModulePath, jsEntry.exceptionTracker);
2659 if (!jsEntry.exceptionTracker) {
2660 return nullptr;
2661 }
2662
2663 std::initializer_list<JSValueRef> params = {
2664 std::move(path),
2665 jsEntry.jsContext.newBool(true), // disableProxy
2666 };
2667
2668 JSFunctionCallContext callContext(jsEntry.jsContext, params.begin(), params.size(), jsEntry.exceptionTracker);
2669
2670 auto result = jsEntry.jsContext.callObjectProperty(
2671 _moduleLoader.get(), _propertyNameIndex.getJsName(kLoadPropertyName), callContext);
2672 if (!jsEntry.exceptionTracker) {
2673 return nullptr;
2674 }
2675
2676 auto jsModuleContainer = Valdi::makeShared<JavaScriptModuleContainer>(
2677 std::move(bundle), jsEntry.jsContext, result.get(), jsEntry.exceptionTracker);
2678 _modules[resourceId] = jsModuleContainer;
2679
2680 onModuleLoaded(jsEntry.jsContext, resourceId, result.get(), /* isFromJsEval */ false, jsEntry.exceptionTracker);
2681
2682 return jsModuleContainer;
2683}
2684
2685void JavaScriptRuntime::onModuleLoaded(IJavaScriptContext& jsContext,
2686 const ResourceId& resourceId,
2687 const JSValue& value,
2688 bool isFromJsEval,
2689 JSExceptionTracker& exceptionTracker) {
2690 if (!exceptionTracker) {
2691 return;
2692 }
2693
2694 if (resourceId == valdiModuleResourceId()) {
2695 _onDaemonClientEventFunction = exceptionTracker.toRetainedResult(
2696 jsContext.getObjectProperty(value, "onDaemonClientEvent", exceptionTracker));
2697 if (!_onDaemonClientEventFunction) {
2698 onRecoverableError("onModuleLoaded", _onDaemonClientEventFunction.error());

Callers

nothing calls this directly

Calls 14

toRetainedResultMethod · 0.80
toResultMethod · 0.80
setJsContextHandlerMethod · 0.80
withObjectMethod · 0.80
extractErrorMethod · 0.80
JSValueClass · 0.70
errorMethod · 0.65
getMethod · 0.65
valueMethod · 0.65
getObjectPropertyMethod · 0.45
callObjectAsFunctionMethod · 0.45

Tested by

no test coverage detected