diff --git a/packages/rspack/src/ChunkGraph.ts b/packages/rspack/src/ChunkGraph.ts index 9c80c0d308b..6858b016e74 100644 --- a/packages/rspack/src/ChunkGraph.ts +++ b/packages/rspack/src/ChunkGraph.ts @@ -8,47 +8,40 @@ import { import { Chunk } from "./Chunk"; import type { Compilation } from "./Compilation"; import { Module } from "./Module"; -import type { Lifecycle } from "./util/lifecycle"; export class ChunkGraph { #compilation: Compilation; - #lifecycle: Lifecycle; - constructor(compilation: Compilation, lifecycle: Lifecycle) { + constructor(compilation: Compilation) { this.#compilation = compilation; - this.#lifecycle = lifecycle; } getChunkModules(chunk: Chunk): Readonly { - this.#lifecycle.ensureActive(); return __chunk_graph_inner_get_chunk_modules( chunk.__internal__innerUkey(), this.#compilation.__internal_getInner() - ).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation)); + ).map(m => Module.__from_binding(m, this.#compilation)); } getChunkModulesIterable(chunk: Chunk): Iterable { - this.#lifecycle.ensureActive(); return new Set( __chunk_graph_inner_get_chunk_modules( chunk.__internal__innerUkey(), this.#compilation.__internal_getInner() - ).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation)) + ).map(m => Module.__from_binding(m, this.#compilation)) ); } getChunkEntryModulesIterable(chunk: Chunk): Iterable { - this.#lifecycle.ensureActive(); return new Set( __chunk_graph_inner_get_chunk_entry_modules( chunk.__internal__innerUkey(), this.#compilation.__internal_getInner() - ).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation)) + ).map(m => Module.__from_binding(m, this.#compilation)) ); } getChunkEntryDependentChunksIterable(chunk: Chunk): Iterable { - this.#lifecycle.ensureActive(); return new Set( __chunk_graph_inner_get_chunk_entry_dependent_chunks_iterable( chunk.__internal__innerUkey(), @@ -63,13 +56,12 @@ export class ChunkGraph { chunk: Chunk, sourceType: string ): Iterable { - this.#lifecycle.ensureActive(); return new Set( __chunk_graph_inner_get_chunk_modules_iterable_by_source_type( chunk.__internal__innerUkey(), sourceType, this.#compilation.__internal_getInner() - ).map(m => Module.__from_binding(m, this.#lifecycle, this.#compilation)) + ).map(m => Module.__from_binding(m, this.#compilation)) ); } } diff --git a/packages/rspack/src/Compilation.ts b/packages/rspack/src/Compilation.ts index 9ec7f30792f..1127d47bb5b 100644 --- a/packages/rspack/src/Compilation.ts +++ b/packages/rspack/src/Compilation.ts @@ -54,7 +54,6 @@ import { createReadonlyMap } from "./util/createReadonlyMap"; import { createFakeCompilationDependencies } from "./util/fake"; import type { InputFileSystem } from "./util/fs"; import type Hash from "./util/hash"; -import type { Lifecycle } from "./util/lifecycle"; import { memoizeValue } from "./util/memoize"; import { JsSource } from "./util/source"; export { type AssetInfo } from "./util/AssetInfo"; @@ -253,9 +252,7 @@ export class Compilation { } >; - #lifecycle: Lifecycle; - - constructor(compiler: Compiler, inner: JsCompilation, lifecycle: Lifecycle) { + constructor(compiler: Compiler, inner: JsCompilation) { this.#inner = inner; this.#customModules = {}; @@ -368,7 +365,6 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si this.childrenCounters = {}; this.children = []; this.chunkGraph = new ChunkGraph(this); - this.#lifecycle = lifecycle; } get hash(): Readonly { diff --git a/packages/rspack/src/Compiler.ts b/packages/rspack/src/Compiler.ts index 34a6947585b..25eb42ef6e8 100644 --- a/packages/rspack/src/Compiler.ts +++ b/packages/rspack/src/Compiler.ts @@ -72,7 +72,6 @@ import type { OutputFileSystem, WatchFileSystem } from "./util/fs"; -import { Lifecycle } from "./util/lifecycle"; export interface AssetEmittedInfo { content: Buffer; @@ -86,7 +85,6 @@ class Compiler { #instance?: binding.Rspack; #initial: boolean; - #compilationLifecycle?: Lifecycle; #compilation?: Compilation; #compilationParams?: CompilationParams; @@ -708,23 +706,13 @@ class Compiler { } #createCompilation(native: binding.JsCompilation): Compilation { - this.#compilationLifecycle = new Lifecycle( - "The associated Compilation has exceeded the lifetime of its internal Rust instance, making this object inaccessible" - ); - const compilation = new Compilation( - this, - native, - this.#compilationLifecycle - ); + const compilation = new Compilation(this, native); compilation.name = this.name; this.#compilation = compilation; return compilation; } #resetThisCompilation() { - if (this.#compilationLifecycle) { - this.#compilationLifecycle.drop(); - } // reassign new compilation in thisCompilation this.#compilation = undefined; // ensure thisCompilation must call @@ -1178,9 +1166,6 @@ class Compiler { ) : false; const result = await queried.promise(data); - if (data) { - ContextModuleFactoryBeforeResolveData.__drop(data); - } return result ? ContextModuleFactoryBeforeResolveData.__to_binding(result) : false; @@ -1197,17 +1182,12 @@ class Compiler { | false | binding.JsContextModuleFactoryAfterResolveData ) => { - const lifecycle = new Lifecycle( - "The current object can only be used during the ContextModuleFactory afterResolve phase and is inaccessible beyond this lifecycle" - ); const data = bindingData ? ContextModuleFactoryAfterResolveData.__from_binding( - bindingData, - lifecycle + bindingData ) : false; const result = await queried.promise(data); - lifecycle.drop(); return result ? ContextModuleFactoryAfterResolveData.__to_binding(result) : false; diff --git a/packages/rspack/src/DependenciesBlock.ts b/packages/rspack/src/DependenciesBlock.ts index 9e55dcd3cfc..7879c43ebd7 100644 --- a/packages/rspack/src/DependenciesBlock.ts +++ b/packages/rspack/src/DependenciesBlock.ts @@ -1,27 +1,18 @@ import type { DependenciesBlockDTO } from "@rspack/binding"; import { Dependency } from "./Dependency"; -import type { Lifecycle } from "./util/lifecycle"; export class DependenciesBlock { #binding: DependenciesBlockDTO; - #lifecycle: Lifecycle; - constructor(binding: DependenciesBlockDTO, lifecycle: Lifecycle) { + constructor(binding: DependenciesBlockDTO) { this.#binding = binding; - this.#lifecycle = lifecycle; } get dependencies(): Dependency[] { - this.#lifecycle.ensureActive(); - return this.#binding.dependencies.map(d => - Dependency.__from_binding(d, this.#lifecycle) - ); + return this.#binding.dependencies.map(d => Dependency.__from_binding(d)); } get blocks(): DependenciesBlock[] { - this.#lifecycle.ensureActive(); - return this.#binding.blocks.map( - b => new DependenciesBlock(b, this.#lifecycle) - ); + return this.#binding.blocks.map(b => new DependenciesBlock(b)); } } diff --git a/packages/rspack/src/Dependency.ts b/packages/rspack/src/Dependency.ts index 02c4192d798..80f40b144e6 100644 --- a/packages/rspack/src/Dependency.ts +++ b/packages/rspack/src/Dependency.ts @@ -1,47 +1,33 @@ import { type JsDependency, JsDependencyMut } from "@rspack/binding"; -import type { Lifecycle } from "./util/lifecycle"; export class Dependency { #binding: JsDependencyMut | JsDependency; - #lifecycle: Lifecycle; - static __from_binding( - binding: JsDependencyMut | JsDependency, - lifecycle: Lifecycle - ): Dependency { - return new Dependency(binding, lifecycle); + static __from_binding(binding: JsDependencyMut | JsDependency): Dependency { + return new Dependency(binding); } - private constructor( - binding: JsDependencyMut | JsDependency, - lifecycle: Lifecycle - ) { + private constructor(binding: JsDependencyMut | JsDependency) { this.#binding = binding; - this.#lifecycle = lifecycle; } get type(): string { - this.#lifecycle.ensureActive(); return this.#binding.type; } get category(): string { - this.#lifecycle.ensureActive(); return this.#binding.category; } get request(): string | undefined { - this.#lifecycle.ensureActive(); return this.#binding.request; } get critital(): boolean { - this.#lifecycle.ensureActive(); return this.#binding.critical; } set critital(critital: boolean) { - this.#lifecycle.ensureActive(); if ( typeof critital === "boolean" && this.#binding instanceof JsDependencyMut diff --git a/packages/rspack/src/Module.ts b/packages/rspack/src/Module.ts index 6beb5b862ef..fbdb6153212 100644 --- a/packages/rspack/src/Module.ts +++ b/packages/rspack/src/Module.ts @@ -12,7 +12,6 @@ import type { Source } from "webpack-sources"; import type { Compilation } from "./Compilation"; import { DependenciesBlock } from "./DependenciesBlock"; import { Dependency } from "./Dependency"; -import type { Lifecycle } from "./util/lifecycle"; import { JsSource } from "./util/source"; export type ResourceData = { @@ -40,7 +39,6 @@ export type ResolveData = { export class ContextModuleFactoryBeforeResolveData { #inner: JsContextModuleFactoryBeforeResolveData; - #dropped = false; static __from_binding(binding: JsContextModuleFactoryBeforeResolveData) { return new ContextModuleFactoryBeforeResolveData(binding); @@ -52,70 +50,58 @@ export class ContextModuleFactoryBeforeResolveData { return data.#inner; } - static __drop(data: ContextModuleFactoryBeforeResolveData) { - data.#dropped = true; - } - private constructor(binding: JsContextModuleFactoryBeforeResolveData) { this.#inner = binding; - } - - private ensureValidLifecycle() { - if (this.#dropped) { - throw new Error( - "The ContextModuleFactoryBeforeResolveData has exceeded its lifecycle and has been dropped by Rust." - ); - } - } - get context(): string { - this.ensureValidLifecycle(); - return this.#inner.context; - } - - set context(val: string) { - this.ensureValidLifecycle(); - this.#inner.context = val; - } - - get request(): string { - this.ensureValidLifecycle(); - return this.#inner.request; - } - - set request(val: string) { - this.#inner.request = val; - } - - get regExp(): RegExp | undefined { - this.ensureValidLifecycle(); - if (!this.#inner.regExp) { - return undefined; - } - const { source, flags } = this.#inner.regExp; - return new RegExp(source, flags); - } - - set regExp(val: RegExp | undefined) { - this.ensureValidLifecycle(); - if (!val) { - this.#inner.regExp = undefined; - return; - } - this.#inner.regExp = { - source: val.source, - flags: val.flags - }; - } - - get recursive(): boolean { - this.ensureValidLifecycle(); - return this.#inner.recursive; - } - - set recursive(val: boolean) { - this.ensureValidLifecycle(); - this.#inner.recursive = val; + Object.defineProperties(this, { + context: { + enumerable: true, + get(): string { + return binding.context; + }, + set(val: string) { + binding.context = val; + } + }, + request: { + enumerable: true, + get(): string { + return binding.request; + }, + set(val: string) { + binding.request = val; + } + }, + regExp: { + enumerable: true, + get(): RegExp | undefined { + if (!binding.regExp) { + return undefined; + } + const { source, flags } = binding.regExp; + return new RegExp(source, flags); + }, + set(val: RegExp | undefined) { + if (!val) { + binding.regExp = undefined; + return; + } + binding.regExp = { + source: val.source, + flags: val.flags + }; + } + }, + recursive: { + enumerable: true, + get(this: ContextModuleFactoryAfterResolveData): boolean { + return binding.recursive; + }, + set(val: boolean) { + binding.recursive = val; + } + } + }); } } @@ -125,13 +111,16 @@ export type ContextModuleFactoryBeforeResolveResult = export class ContextModuleFactoryAfterResolveData { #inner: JsContextModuleFactoryAfterResolveData; - #lifecycle: Lifecycle; - static __from_binding( - binding: JsContextModuleFactoryAfterResolveData, - lifecycle: Lifecycle - ) { - return new ContextModuleFactoryAfterResolveData(binding, lifecycle); + declare resource: number; + declare context: string; + declare request: string; + declare regExp: RegExp | undefined; + declare recursive: boolean; + declare readonly dependencies: Dependency[]; + + static __from_binding(binding: JsContextModuleFactoryAfterResolveData) { + return new ContextModuleFactoryAfterResolveData(binding); } static __to_binding( @@ -140,87 +129,75 @@ export class ContextModuleFactoryAfterResolveData { return data.#inner; } - // private ensureValidLifecycle() { - // if (this.#dropped) { - // throw new Error( - // "The ContextModuleFactoryAfterResolveData has exceeded its lifecycle and has been dropped by Rust." - // ); - // } - // } - - private constructor( - data: JsContextModuleFactoryAfterResolveData, - lifecycle: Lifecycle - ) { - this.#inner = data; - this.#lifecycle = lifecycle; - } - - get resource(): string { - this.#lifecycle.ensureActive(); - return this.#inner.resource; - } - - set resource(val: string) { - this.#lifecycle.ensureActive(); - this.#inner.resource = val; - } - - get context(): string { - this.#lifecycle.ensureActive(); - return this.#inner.context; - } - - set context(val: string) { - this.#lifecycle.ensureActive(); - this.#inner.context = val; - } - - get request(): string { - this.#lifecycle.ensureActive(); - return this.#inner.request; - } - - set request(val: string) { - this.#inner.request = val; - } - - get regExp(): RegExp | undefined { - this.#lifecycle.ensureActive(); - if (!this.#inner.regExp) { - return undefined; - } - const { source, flags } = this.#inner.regExp; - return new RegExp(source, flags); - } - - set regExp(val: RegExp | undefined) { - this.#lifecycle.ensureActive(); - if (!val) { - this.#inner.regExp = undefined; - return; - } - this.#inner.regExp = { - source: val.source, - flags: val.flags - }; - } - - get recursive(): boolean { - this.#lifecycle.ensureActive(); - return this.#inner.recursive; - } - - set recursive(val: boolean) { - this.#lifecycle.ensureActive(); - this.#inner.recursive = val; - } + private constructor(binding: JsContextModuleFactoryAfterResolveData) { + this.#inner = binding; - get dependencies(): Dependency[] { - this.#lifecycle.ensureActive(); - return this.#inner.dependencies.map(dep => - Dependency.__from_binding(dep, this.#lifecycle) - ); + Object.defineProperties(this, { + resource: { + enumerable: true, + get(): string { + return binding.resource; + }, + set(val: string) { + binding.resource = val; + } + }, + context: { + enumerable: true, + get(): string { + return binding.context; + }, + set(val: string) { + binding.context = val; + } + }, + request: { + enumerable: true, + get(): string { + return binding.request; + }, + set(val: string) { + binding.request = val; + } + }, + regExp: { + enumerable: true, + get(): RegExp | undefined { + if (!binding.regExp) { + return undefined; + } + const { source, flags } = binding.regExp; + return new RegExp(source, flags); + }, + set(val: RegExp | undefined) { + if (!val) { + binding.regExp = undefined; + return; + } + binding.regExp = { + source: val.source, + flags: val.flags + }; + } + }, + recursive: { + enumerable: true, + get(): boolean { + return binding.recursive; + }, + set(val: boolean) { + binding.recursive = val; + } + }, + dependencies: { + enumerable: true, + get(): Dependency[] { + return binding.dependencies.map(dep => + Dependency.__from_binding(dep) + ); + } + } + }); } } @@ -231,7 +208,6 @@ export type ContextModuleFactoryAfterResolveResult = export class Module { #inner: JsModule | ModuleDTO; #originalSource?: Source; - #lifecycle: Lifecycle; context?: Readonly; resource?: Readonly; @@ -258,17 +234,12 @@ export class Module { static __from_binding( module: JsModule | ModuleDTO, - lifecycle: Lifecycle, compilation?: Compilation ) { - return new Module(module, lifecycle, compilation); + return new Module(module, compilation); } - constructor( - module: JsModule | ModuleDTO, - lifecycle: Lifecycle, - compilation?: Compilation - ) { + constructor(module: JsModule | ModuleDTO, compilation?: Compilation) { this.#inner = module; this.type = module.type; this.layer = module.layer ?? null; @@ -284,11 +255,9 @@ export class Module { ); this.buildInfo = customModule?.buildInfo || {}; this.buildMeta = customModule?.buildMeta || {}; - this.#lifecycle = lifecycle; } originalSource(): Source | null { - this.#lifecycle.ensureActive(); if (this.#originalSource) return this.#originalSource; if (this.#inner.originalSource) { this.#originalSource = JsSource.__from_binding( @@ -300,12 +269,10 @@ export class Module { } identifier(): string { - this.#lifecycle.ensureActive(); return this.#inner.moduleIdentifier; } nameForCondition(): string | null { - this.#lifecycle.ensureActive(); if (typeof this.#inner.nameForCondition === "string") { return this.#inner.nameForCondition; } @@ -313,17 +280,13 @@ export class Module { } get blocks(): DependenciesBlock[] { - this.#lifecycle.ensureActive(); if ("blocks" in this.#inner) { - return this.#inner.blocks.map( - b => new DependenciesBlock(b, this.#lifecycle) - ); + return this.#inner.blocks.map(b => new DependenciesBlock(b)); } return []; } size(type?: string): number { - this.#lifecycle.ensureActive(); if ("size" in this.#inner) { return this.#inner.size(type); } diff --git a/packages/rspack/src/util/lifecycle.ts b/packages/rspack/src/util/lifecycle.ts deleted file mode 100644 index 5cac3d7e7e9..00000000000 --- a/packages/rspack/src/util/lifecycle.ts +++ /dev/null @@ -1,20 +0,0 @@ -export class Lifecycle { - #dropped = false; - #errorMessage: string; - - constructor( - errorMessage = "This object has exceeded the lifecycle of its internal Rust instance and is now inaccessible" - ) { - this.#errorMessage = errorMessage; - } - - drop() { - this.#dropped = true; - } - - ensureActive() { - if (this.#dropped) { - throw new Error(this.#errorMessage); - } - } -}