Esempio n. 1
0
        private JavaScriptModuleRecord createModule(JavaScriptModuleRecord?parent, string name, Func <string, string> loadModuleCallback)
        {
            bool isCreateFromSourceCode = string.IsNullOrEmpty(name);

            //if module is cached, return cached value
            if (!isCreateFromSourceCode && moduleCache.ContainsKey(name))
            {
                return(moduleCache[name]);
            }

            IRuntimeDebuggingService debugService = CurrentNode.GetService <IRuntimeDebuggingService>();
            var result = JavaScriptModuleRecord.Create(parent, name);

            #region init moudle callback delegates
            FetchImportedModuleDelegate fetchImported = (JavaScriptModuleRecord reference, JavaScriptValue scriptName, out JavaScriptModuleRecord output) =>
            {
                output = createModule(reference, scriptName.ToString(), loadModuleCallback);

                return(JavaScriptErrorCode.NoError);
            };

            FetchImportedModuleFromScriptDelegate fetchImportedFromScript = (JavaScriptSourceContext sourceContext, JavaScriptValue scriptName, out JavaScriptModuleRecord output) =>
            {
                output = createModule(null, scriptName.ToString(), loadModuleCallback);
                return(JavaScriptErrorCode.NoError);
            };

            NotifyModuleReadyCallbackDelegate notifyReady = (module, jsvalue) =>
            {
                moduleReadyEvent.Set();
                if (jsvalue.IsValid)
                {
                    var valueService = CurrentNode.GetService <IJSValueService>();
                    valueService.ThrowIfErrorValue(jsvalue);
                }
                debugService.NotifyScriptReady();
                return(JavaScriptErrorCode.NoError);
            };
            #endregion


            Action parseModule = () =>
            {
                string script = loadModuleCallback(name);
                JavaScriptModuleRecord.ParseScript(result, script, debugService.GetScriptContext(name, script));
                //debugService.AddScriptSource(name, script);
                System.Diagnostics.Debug.WriteLine($"module {name} Parsed");
            };

            JavaScriptModuleRecord.SetFetchModuleCallback(result, fetchImported);
            JavaScriptModuleRecord.SetFetchModuleScriptCallback(result, fetchImportedFromScript);
            JavaScriptModuleRecord.SetNotifyReady(result, notifyReady);
            if (!string.IsNullOrEmpty(name))
            {
                JavaScriptModuleRecord.SetHostUrl(result, name);
            }
            else
            {
                JavaScriptModuleRecord.SetHostUrl(result, "<Generated_ModuleRoot>");
            }


            moduleParseQueue.Add(new moduleItem(
                                     parseModule,
                                     fetchImported,
                                     fetchImportedFromScript,
                                     notifyReady));

            if (!isCreateFromSourceCode)
            {
                moduleCache.Add(name, result);//cache the module if it's not directly from RunModule function
            }


            System.Diagnostics.Debug.WriteLine($"{name} module created");
            return(result);
        }