/// <summary>
 /// Nominal implementation to retrieve a module.
 /// </summary>
 /// <param name="moduleName"></param>
 /// <param name="modulePriority"></param>
 /// <param name="moduleLoader"></param>
 /// <returns></returns>
 private Task <IBaristaModule> InitializeAndReturnModule(string moduleName, int modulePriority, IBaristaModuleLoader moduleLoader)
 {
     return(moduleLoader.GetModule(moduleName));
 }
Пример #2
0
        private bool FetchImportedModule(JavaScriptModuleRecord jsReferencingModuleRecord, JavaScriptValueSafeHandle specifier, out IntPtr dependentModule)
        {
            var moduleName = Context.CreateValue(specifier).ToString();
            var referencingModuleRecord = m_moduleRecordFactory.GetBaristaModuleRecord(jsReferencingModuleRecord);

            //If the current module name is equal to the fetching module name, return this value.
            if (Name == moduleName || referencingModuleRecord != null && referencingModuleRecord.Name == moduleName)
            {
                //Top-level self-referencing module. Reference itself.
                dependentModule = jsReferencingModuleRecord.DangerousGetHandle();
                return(false);
            }
            else if (m_importedModules.ContainsKey(moduleName))
            {
                //The module has already been imported, return the existing JavaScriptModuleRecord
                dependentModule = m_importedModules[moduleName].Handle.DangerousGetHandle();
                return(false);
            }
            else if (m_moduleLoader != null)
            {
                Task <IBaristaModule> moduleLoaderTask = null;
                try
                {
                    moduleLoaderTask = m_moduleLoader.GetModule(moduleName);
                }
                catch (Exception ex)
                {
                    var error = Context.CreateError($"An error occurred while attempting to load a module named {moduleName}: {ex.Message}");
                    Engine.JsSetException(error.Handle);
                    dependentModule = jsReferencingModuleRecord.DangerousGetHandle();
                    return(true);
                }

                if (moduleLoaderTask != null)
                {
                    IBaristaModule module;
                    try
                    {
                        module = moduleLoaderTask.GetAwaiter().GetResult();
                    }
                    catch (Exception ex)
                    {
                        var error = Context.CreateError($"An error occurred while attempting to load a module named {moduleName}: {ex.Message}");
                        Engine.JsSetException(error.Handle);
                        dependentModule = jsReferencingModuleRecord.DangerousGetHandle();
                        return(true);
                    }

                    if (module != null)
                    {
                        var newModuleRecord = m_moduleRecordFactory.CreateBaristaModuleRecord(Context, specifier, this, false);
                        m_importedModules.Add(moduleName, newModuleRecord);
                        dependentModule = newModuleRecord.Handle.DangerousGetHandle();

                        switch (module)
                        {
                        //For the built-in Script Module type, parse the string returned by ExportDefault and install it as a module.
                        case IBaristaScriptModule scriptModule:
                            var script = scriptModule.ExportDefault(Context, newModuleRecord) as JsString;
                            if (script == null)
                            {
                                var error = Context.CreateError($"The module {moduleName} implements IBaristaScriptModule and is expected to return a string based module that exports a default value.");
                                Engine.JsSetException(error.Handle);
                                return(true);
                            }

                            newModuleRecord.ParseModuleSource(script.ToString());
                            return(false);

                        //Otherwise, install the module.
                        default:
                            var result = InstallModule(newModuleRecord, referencingModuleRecord, module, specifier);
                            return(result);
                        }
                    }
                }
            }

            dependentModule = jsReferencingModuleRecord.DangerousGetHandle();
            return(true);
        }