| 2639 | } |
| 2640 | |
| 2641 | return importModule(resourceIdResult.value(), jsEntry); |
| 2642 | } |
| 2643 | |
| 2644 | Shared<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 | |
| 2685 | void 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()); |
nothing calls this directly
no test coverage detected