| 1783 | |
| 1784 | JSValueRef evalResult; |
| 1785 | ModuleLoadMode moduleLoadMode; |
| 1786 | |
| 1787 | { |
| 1788 | VALDI_TRACE_META( |
| 1789 | "Valdi.evalJsModule", |
| 1790 | STRING_FORMAT("importPath: {}, jsModule size: {} bytes", importPath.toStringView(), jsModule.size())); |
| 1791 | auto preCompiledContent = getPreCompiledJsModuleData(jsModule); |
| 1792 | |
| 1793 | if (preCompiledContent) { |
| 1794 | moduleLoadMode = ModuleLoadMode::JS_BYTECODE; |
| 1795 | evalResult = |
| 1796 | jsContext.evaluatePreCompiled(preCompiledContent.value(), importPath.toStringView(), exceptionTracker); |
| 1797 | } else { |
| 1798 | moduleLoadMode = ModuleLoadMode::JS_SOURCE; |
| 1799 | auto moduleSrc = jsModule.asStringView(); |
| 1800 | auto formattedSource = formatJsModule(moduleSrc); |
| 1801 | evalResult = jsContext.evaluate(formattedSource, importPath.toStringView(), exceptionTracker); |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | if (!exceptionTracker) { |
| 1806 | if (outParseFailed != nullptr) { |
| 1807 | *outParseFailed = true; |
| 1808 | } |
| 1809 | return std::make_pair(jsContext.newUndefined(), moduleLoadMode); |
| 1810 | } |
| 1811 | |
| 1812 | JSFunctionCallContext callContext(jsContext, parameters, parametersLength, exceptionTracker); |
| 1813 | return std::make_pair(jsContext.callObjectAsFunction(evalResult.get(), callContext), moduleLoadMode); |
| 1814 | } |
| 1815 | |
| 1816 | ModuleLoadResult JavaScriptRuntime::loadJsModuleFromNative(IJavaScriptContext& jsContext, |
| 1817 | const StringBox& importPath, |
| 1818 | const JSValueRef* parameters, |
| 1819 | size_t parametersLength, |
| 1820 | JSExceptionTracker& exceptionTracker) { |
| 1821 | // Assign global context when loading a JS module |
| 1822 | JavaScriptContextEntry contextEntry(_globalContext); |
| 1823 | |
| 1824 | JSValueRef evalResult; |
| 1825 | |
| 1826 | { |
| 1827 | VALDI_TRACE("Valdi.evalNativeModule"); |
| 1828 | evalResult = jsContext.evaluateNative(importPath.toStringView(), exceptionTracker); |
| 1829 | } |
| 1830 | |
| 1831 | if (!exceptionTracker) { |
| 1832 | return std::make_pair(jsContext.newUndefined(), ModuleLoadMode::NATIVE); |
| 1833 | } |
| 1834 | |
| 1835 | JSFunctionCallContext callContext(jsContext, parameters, parametersLength, exceptionTracker); |
| 1836 | return std::make_pair(jsContext.callObjectAsFunction(evalResult.get(), callContext), ModuleLoadMode::NATIVE); |
| 1837 | } |
| 1838 | |
| 1839 | JSValueRef JavaScriptRuntime::runtimeLoadJsModule(JSFunctionNativeCallContext& callContext) { |
| 1840 | auto importPath = callContext.getParameterAsString(0); |
| 1841 | CHECK_CALL_CONTEXT(callContext); |
| 1842 |
nothing calls this directly
no test coverage detected