rspack_core 0.100.0-rc.2

rspack core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// This crate introduces a smart pointer called BindingCell.
//
// When the "napi" feature is disabled, BindingCell uses the sys_binding module as a simple alias for Box.
//
// When the "napi" feature is enabled, BindingCell uses the napi_binding module.
// 1. BindingCell establishes a 1:1 relationship between a Rust instance and a JS Object.
// 2. BindingCell creates a JS Object only when converting a Rust instance to a JS object.
// 3. The JS Object is hold a weak reference to the Rust instance. If the Rust instance is dropped,
//    any attempt to access the JS Object will throw an exception indicating that the Rust instance has been dropped.

#[cfg(feature = "napi")]
mod napi_binding {
  use std::{
    any::{Any, TypeId},
    hash::{Hash, Hasher},
    ops::{Deref, DerefMut},
    sync::{Arc, Weak},
  };

  use derive_more::Debug;
  use napi::{
    Env,
    bindgen_prelude::{Object, ToNapiValue},
  };
  use once_cell::sync::OnceCell;
  use rspack_napi::{ThreadsafeOneShotRef, object_assign};
  use rspack_sources::BoxSource;
  use rustc_hash::FxHashMap;

  use crate::{
    AssetInfo, CodeGenerationResult, CodeGenerationResults, CompilationAsset, SourceType,
    with_thread_local_allocator,
  };

  pub struct WeakBindingCell<T: ?Sized> {
    ptr: *mut T,
    heap: Weak<Heap>,
  }

  impl<T: ?Sized> WeakBindingCell<T> {
    pub fn upgrade(&self) -> Option<BindingCell<T>> {
      self.heap.upgrade().map(|heap| BindingCell {
        ptr: self.ptr,
        heap,
      })
    }
  }

  #[derive(Default, Debug, Clone)]
  pub struct Reflector {
    heap: Weak<Heap>,
  }

  impl Reflector {
    pub fn set_jsobject(&self, env: &Env, object: Object) -> napi::Result<()> {
      let heap = self.heap.upgrade().ok_or_else(|| {
        napi::Error::new(
          napi::Status::GenericFailure,
          "Failed to upgrade weak reference to heap",
        )
      })?;

      heap.jsobject.get_or_try_init(|| match &heap.variant {
        HeapVariant::AssetInfo(_asset_info) => ThreadsafeOneShotRef::new(env.raw(), object),
        _ => unreachable!(),
      })?;
      Ok(())
    }
  }

  impl ToNapiValue for Reflector {
    unsafe fn to_napi_value(
      raw_env: napi::sys::napi_env,
      val: Self,
    ) -> napi::Result<napi::sys::napi_value> {
      with_thread_local_allocator(|allocator| {
        let heap = val.heap.upgrade().ok_or_else(|| {
          napi::Error::new(
            napi::Status::GenericFailure,
            "Failed to upgrade weak reference to heap",
          )
        })?;

        let raw_ref = heap.jsobject.get_or_try_init(|| match &heap.variant {
          HeapVariant::AssetInfo(asset_info) => {
            let binding_cell = BindingCell {
              ptr: asset_info.as_ref() as *const AssetInfo as *mut AssetInfo,
              heap: heap.clone(),
            };
            let napi_val = allocator.allocate_asset_info(raw_env, &binding_cell)?;
            let target = Object::from_raw(raw_env, napi_val);
            ThreadsafeOneShotRef::new(raw_env, target)
          }
          HeapVariant::CodeGenerationResult(code_generation_result) => {
            let binding_cell = BindingCell {
              ptr: code_generation_result.as_ref() as *const CodeGenerationResult
                as *mut CodeGenerationResult,
              heap: heap.clone(),
            };
            let napi_val = allocator.allocate_code_generation_result(raw_env, &binding_cell)?;
            let target = Object::from_raw(raw_env, napi_val);
            ThreadsafeOneShotRef::new(raw_env, target)
          }
          HeapVariant::Sources(sources) => {
            let binding_cell = BindingCell {
              ptr: sources.as_ref() as *const FxHashMap<SourceType, BoxSource>
                as *mut FxHashMap<SourceType, BoxSource>,
              heap: heap.clone(),
            };
            let napi_val = allocator.allocate_sources(raw_env, &binding_cell)?;
            let target = Object::from_raw(raw_env, napi_val);
            ThreadsafeOneShotRef::new(raw_env, target)
          }
          HeapVariant::CodeGenerationResults(code_generation_results) => {
            let binding_cell = BindingCell {
              ptr: code_generation_results.as_ref() as *const CodeGenerationResults
                as *mut CodeGenerationResults,
              heap: heap.clone(),
            };
            let napi_val = allocator.allocate_code_generation_results(raw_env, &binding_cell)?;
            let target = Object::from_raw(raw_env, napi_val);
            ThreadsafeOneShotRef::new(raw_env, target)
          }
          HeapVariant::Assets(assets) => {
            let binding_cell = BindingCell {
              ptr: assets.as_ref() as *const FxHashMap<String, CompilationAsset>
                as *mut FxHashMap<String, CompilationAsset>,
              heap: heap.clone(),
            };
            let napi_val = allocator.allocate_assets(raw_env, &binding_cell)?;
            let target = Object::from_raw(raw_env, napi_val);
            ThreadsafeOneShotRef::new(raw_env, target)
          }
        })?;

        let result = unsafe { ToNapiValue::to_napi_value(raw_env, raw_ref)? };

        match &heap.variant {
          HeapVariant::AssetInfo(asset_info) => {
            let binding_cell = BindingCell {
              ptr: asset_info.as_ref() as *const AssetInfo as *mut AssetInfo,
              heap: heap.clone(),
            };
            let napi_val = allocator.allocate_asset_info(raw_env, &binding_cell)?;
            let new_object = Object::from_raw(raw_env, napi_val);
            let mut original_object = Object::from_raw(raw_env, result);
            object_assign(&mut original_object, &new_object)?;
            Ok(result)
          }
          _ => Ok(result),
        }
      })
    }
  }

  pub trait Reflectable {
    fn reflector(&self) -> Reflector;
  }

  // The reason HeapVariant does not use the generic type T is that for types requiring the implementation of the Reflectable trait,
  // the implementation of Reflectable needs to use the concrete type T, which prevents the trait from inheriting the Reflectable trait.
  #[derive(Debug)]
  enum HeapVariant {
    AssetInfo(Box<AssetInfo>),
    CodeGenerationResults(Box<CodeGenerationResults>),
    CodeGenerationResult(Box<CodeGenerationResult>),
    Sources(Box<FxHashMap<SourceType, BoxSource>>),
    Assets(Box<FxHashMap<String, CompilationAsset>>),
  }

  #[derive(Debug)]
  struct Heap {
    variant: HeapVariant,
    #[debug(skip)]
    jsobject: OnceCell<ThreadsafeOneShotRef>,
  }

  unsafe impl Send for Heap {}
  unsafe impl Sync for Heap {}

  #[derive(Debug)]
  pub struct BindingCell<T: ?Sized> {
    ptr: *mut T,
    heap: Arc<Heap>,
  }

  impl<T: ?Sized> BindingCell<T> {
    pub fn reflector(&self) -> Reflector {
      Reflector {
        heap: Arc::downgrade(&self.heap),
      }
    }
  }

  impl<T: ?Sized> Reflectable for BindingCell<T> {
    fn reflector(&self) -> Reflector {
      Reflector {
        heap: Arc::downgrade(&self.heap),
      }
    }
  }

  unsafe impl<T: ?Sized + Send> Send for BindingCell<T> {}
  unsafe impl<T: ?Sized + Sync> Sync for BindingCell<T> {}

  impl BindingCell<AssetInfo> {
    pub fn new(asset_info: AssetInfo) -> Self {
      let boxed = Box::new(asset_info);
      let ptr = boxed.as_ref() as *const AssetInfo as *mut AssetInfo;
      let heap = Arc::new(Heap {
        variant: HeapVariant::AssetInfo(boxed),
        jsobject: Default::default(),
      });
      Self { ptr, heap }
    }
  }

  impl From<AssetInfo> for BindingCell<AssetInfo> {
    fn from(asset_info: AssetInfo) -> Self {
      Self::new(asset_info)
    }
  }

  impl BindingCell<CodeGenerationResults> {
    pub fn new(code_generation_results: CodeGenerationResults) -> Self {
      let boxed = Box::new(code_generation_results);
      let ptr = boxed.as_ref() as *const CodeGenerationResults as *mut CodeGenerationResults;
      let heap = Arc::new(Heap {
        variant: HeapVariant::CodeGenerationResults(boxed),
        jsobject: Default::default(),
      });
      Self { ptr, heap }
    }
  }

  impl From<CodeGenerationResults> for BindingCell<CodeGenerationResults> {
    fn from(code_generation_results: CodeGenerationResults) -> Self {
      Self::new(code_generation_results)
    }
  }

  impl BindingCell<CodeGenerationResult> {
    pub fn new(code_generation_result: CodeGenerationResult) -> Self {
      let boxed = Box::new(code_generation_result);
      let ptr = boxed.as_ref() as *const CodeGenerationResult as *mut CodeGenerationResult;
      let heap = Arc::new(Heap {
        variant: HeapVariant::CodeGenerationResult(boxed),
        jsobject: Default::default(),
      });
      Self { ptr, heap }
    }
  }

  impl From<CodeGenerationResult> for BindingCell<CodeGenerationResult> {
    fn from(code_generation_result: CodeGenerationResult) -> Self {
      Self::new(code_generation_result)
    }
  }

  impl BindingCell<FxHashMap<SourceType, BoxSource>> {
    pub fn new(sources: FxHashMap<SourceType, BoxSource>) -> Self {
      let boxed = Box::new(sources);
      let ptr = boxed.as_ref() as *const FxHashMap<SourceType, BoxSource>
        as *mut FxHashMap<SourceType, BoxSource>;
      let heap = Arc::new(Heap {
        variant: HeapVariant::Sources(boxed),
        jsobject: Default::default(),
      });
      Self { ptr, heap }
    }
  }

  impl From<FxHashMap<SourceType, BoxSource>> for BindingCell<FxHashMap<SourceType, BoxSource>> {
    fn from(sources: FxHashMap<SourceType, BoxSource>) -> Self {
      Self::new(sources)
    }
  }

  impl BindingCell<FxHashMap<String, CompilationAsset>> {
    pub fn new(assets: FxHashMap<String, CompilationAsset>) -> Self {
      let boxed = Box::new(assets);
      let ptr = boxed.as_ref() as *const FxHashMap<String, CompilationAsset>
        as *mut FxHashMap<String, CompilationAsset>;
      let heap = Arc::new(Heap {
        variant: HeapVariant::Assets(boxed),
        jsobject: Default::default(),
      });
      Self { ptr, heap }
    }
  }

  impl From<FxHashMap<String, CompilationAsset>>
    for BindingCell<FxHashMap<String, CompilationAsset>>
  {
    fn from(assets: FxHashMap<String, CompilationAsset>) -> Self {
      Self::new(assets)
    }
  }

  impl<T: ?Sized> BindingCell<T> {
    pub fn downgrade(&self) -> WeakBindingCell<T> {
      WeakBindingCell {
        ptr: self.ptr,
        heap: Arc::downgrade(&self.heap),
      }
    }
  }

  impl<T: ?Sized> AsRef<T> for BindingCell<T> {
    fn as_ref(&self) -> &T {
      unsafe { &*self.ptr }
    }
  }

  impl<T: ?Sized> AsMut<T> for BindingCell<T> {
    fn as_mut(&mut self) -> &mut T {
      unsafe { &mut *self.ptr }
    }
  }

  impl<T: ?Sized> Deref for BindingCell<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
      unsafe { &*self.ptr }
    }
  }

  impl<T: ?Sized> DerefMut for BindingCell<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
      unsafe { &mut *self.ptr }
    }
  }

  impl<T: Clone + Into<BindingCell<T>>> Clone for BindingCell<T> {
    fn clone(&self) -> Self {
      let val = self.as_ref().clone();
      val.into()
    }
  }

  impl<T: ?Sized + Hash> Hash for BindingCell<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
      self.as_ref().hash(state);
    }
  }

  impl<T: ?Sized + PartialEq> PartialEq for BindingCell<T> {
    fn eq(&self, other: &Self) -> bool {
      self.as_ref() == other.as_ref()
    }
  }

  impl<T: ?Sized + Eq> Eq for BindingCell<T> {}

  impl<T: Default + Into<BindingCell<T>>> Default for BindingCell<T> {
    fn default() -> Self {
      let value: T = Default::default();
      value.into()
    }
  }

  // Implement rkyv traits for BindingCell<T>

  impl<T: rkyv::ArchiveUnsized + ?Sized> rkyv::Archive for BindingCell<T> {
    type Archived = rkyv::boxed::ArchivedBox<T::Archived>;
    type Resolver = rkyv::boxed::BoxResolver;

    fn resolve(&self, resolver: Self::Resolver, out: rkyv::Place<Self::Archived>) {
      rkyv::boxed::ArchivedBox::resolve_from_ref(self.as_ref(), resolver, out);
    }
  }

  impl<T, S> rkyv::Serialize<S> for BindingCell<T>
  where
    T: rkyv::SerializeUnsized<S> + ?Sized,
    S: rkyv::rancor::Fallible + ?Sized,
  {
    fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
      rkyv::boxed::ArchivedBox::serialize_from_ref(self.as_ref(), serializer)
    }
  }

  impl<T, D> rkyv::Deserialize<BindingCell<T>, D> for rkyv::boxed::ArchivedBox<T::Archived>
  where
    T: rkyv::ArchiveUnsized + rkyv::traits::LayoutRaw + ?Sized + 'static,
    T::Archived: rkyv::DeserializeUnsized<T, D>,
    D: rkyv::rancor::Fallible + ?Sized,
    D::Error: rkyv::rancor::Source,
  {
    fn deserialize(&self, deserializer: &mut D) -> Result<BindingCell<T>, D::Error> {
      let boxed: Box<T> = self.deserialize(deserializer)?;
      let type_id = boxed.type_id();

      macro_rules! deserialize_variant {
        ($type_id:expr, $boxed:expr, $variant:path, $target:ty) => {
          if $type_id == TypeId::of::<Box<$target>>() {
            let ptr = Box::into_raw($boxed);
            let boxed = unsafe { Box::from_raw(ptr as *mut $target) };
            return Ok(BindingCell {
              ptr,
              heap: Arc::new(Heap {
                variant: $variant(boxed),
                jsobject: OnceCell::default(),
              }),
            });
          }
        };
      }

      deserialize_variant!(type_id, boxed, HeapVariant::AssetInfo, AssetInfo);
      deserialize_variant!(
        type_id,
        boxed,
        HeapVariant::CodeGenerationResults,
        CodeGenerationResults
      );
      deserialize_variant!(
        type_id,
        boxed,
        HeapVariant::CodeGenerationResult,
        CodeGenerationResult
      );
      deserialize_variant!(type_id, boxed, HeapVariant::Sources, FxHashMap<SourceType, BoxSource>);
      deserialize_variant!(type_id, boxed, HeapVariant::Assets, FxHashMap<String, CompilationAsset>);

      unreachable!()
    }
  }
}

#[cfg(not(feature = "napi"))]
mod sys_binding {
  pub type BindingCell<T> = Box<T>;
}

#[cfg(feature = "napi")]
pub use napi_binding::*;
#[cfg(not(feature = "napi"))]
pub use sys_binding::*;