Ejemplo n.º 1
0
        public JSValue ResolveModule(ScriptContext context, string parent_module_id, string module_id)
        {
            for (int i = 0, count = _moduleResolvers.Count; i < count; i++)
            {
                var    resolver = _moduleResolvers[i];
                string resolved_id;
                if (resolver.ResolveModule(_fileSystem, _pathResolver, parent_module_id, module_id, out resolved_id))
                {
                    // 如果目标模块在 reloading 列表中, 直接进入重载逻辑
                    JSValue module_obj;
                    if (context.TryGetModuleForReloading(resolved_id, out module_obj))
                    {
                        JSValue exports_obj;
                        RaiseScriptReloadingEvent_nothrow(context, resolved_id);
                        if (resolver.ReloadModule(context, resolved_id, module_obj, out exports_obj))
                        {
                            RaiseScriptReloadedEvent_nothrow(context, resolved_id);
                            JSApi.JS_FreeValue(context, module_obj);
                            return(exports_obj);
                        }

                        JSApi.JS_FreeValue(context, module_obj);
                    }

                    // 如果已经在模块缓存中, 直接返回
                    if (context.LoadModuleCache(resolved_id, out module_obj))
                    {
                        var ctx         = (JSContext)context;
                        var exports_obj = JSApi.JS_GetProperty(ctx, module_obj, context.GetAtom("exports"));
                        JSApi.JS_FreeValue(ctx, module_obj);
                        return(exports_obj);
                    }

                    // 载入新模块
                    return(resolver.LoadModule(context, parent_module_id, resolved_id));
                }
            }

            return(JSApi.JS_ThrowInternalError(context, $"module can not be resolved ({module_id})"));
        }
Ejemplo n.º 2
0
        public JSValue ResolveModule(ScriptContext context, string parent_module_id, string module_id)
        {
            for (int i = 0, count = _moduleResolvers.Count; i < count; i++)
            {
                var    resolver = _moduleResolvers[i];
                string resolved_id;
                if (resolver.ResolveModule(_fileSystem, _pathResolver, parent_module_id, module_id, out resolved_id))
                {
                    JSValue mod;
                    if (context.LoadModuleCache(resolved_id, out mod))
                    {
                        var ctx     = (JSContext)context;
                        var exports = JSApi.JS_GetProperty(ctx, mod, context.GetAtom("exports"));
                        JSApi.JS_FreeValue(ctx, mod);
                        return(exports);
                    }

                    return(resolver.LoadModule(context, resolved_id));
                }
            }

            return(JSApi.JS_ThrowInternalError(context, $"module can not be resolved ({module_id})"));
        }