5
5
//! This is the data structure specialized to handle compiled
6
6
//! register machine based bytecode functions.
7
7
8
- use super :: { FuncTranslationDriver , FuncTranslator , TranslationError , ValidatingFuncTranslator } ;
9
8
use crate :: {
10
9
collections:: arena:: { Arena , ArenaIndex } ,
11
- core:: { Fuel , FuelCostsProvider , FuelError , UntypedVal } ,
12
- engine:: { utils:: unreachable_unchecked, ResumableOutOfFuelError } ,
10
+ core:: { Fuel , UntypedVal } ,
11
+ engine:: utils:: unreachable_unchecked,
13
12
ir:: { index:: InternalFunc , Instruction } ,
14
- module:: { FuncIdx , ModuleHeader } ,
15
- Config ,
16
- Error ,
13
+ Config , Error ,
17
14
} ;
18
15
use alloc:: boxed:: Box ;
19
16
use core:: {
20
- fmt,
21
- mem:: { self , MaybeUninit } ,
17
+ mem:: { self } ,
22
18
ops:: { self , Range } ,
23
19
pin:: Pin ,
24
20
slice,
25
21
} ;
26
22
use spin:: Mutex ;
23
+
24
+ #[ cfg( feature = "parser" ) ]
25
+ use super :: { FuncTranslationDriver , FuncTranslator , TranslationError , ValidatingFuncTranslator } ;
26
+ #[ cfg( feature = "parser" ) ]
27
+ use crate :: {
28
+ core:: { FuelCostsProvider , FuelError } ,
29
+ engine:: ResumableOutOfFuelError ,
30
+ module:: { FuncIdx , ModuleHeader } ,
31
+ } ;
32
+ #[ cfg( feature = "parser" ) ]
33
+ use core:: { fmt, mem:: MaybeUninit } ;
34
+ #[ cfg( feature = "parser" ) ]
27
35
use wasmparser:: { FuncToValidate , ValidatorResources , WasmFeatures } ;
28
36
29
37
/// A reference to a compiled function stored in the [`CodeMap`] of an [`Engine`](crate::Engine).
@@ -71,6 +79,7 @@ impl ArenaIndex for EngineFunc {
71
79
#[ derive( Debug ) ]
72
80
pub struct CodeMap {
73
81
funcs : Mutex < Arena < EngineFunc , FuncEntity > > ,
82
+ #[ cfg( feature = "parser" ) ]
74
83
features : WasmFeatures ,
75
84
}
76
85
@@ -213,6 +222,7 @@ impl CodeMap {
213
222
pub fn new ( config : & Config ) -> Self {
214
223
Self {
215
224
funcs : Mutex :: new ( Arena :: default ( ) ) ,
225
+ #[ cfg( feature = "parser" ) ]
216
226
features : config. wasm_features ( ) ,
217
227
}
218
228
}
@@ -244,32 +254,6 @@ impl CodeMap {
244
254
func. init_compiled ( entity) ;
245
255
}
246
256
247
- /// Initializes the [`EngineFunc`] for lazy translation.
248
- ///
249
- /// # Panics
250
- ///
251
- /// - If `func` is an invalid [`EngineFunc`] reference for this [`CodeMap`].
252
- /// - If `func` refers to an already initialized [`EngineFunc`].
253
- pub fn init_func_as_uncompiled (
254
- & self ,
255
- func : EngineFunc ,
256
- func_idx : FuncIdx ,
257
- bytes : & [ u8 ] ,
258
- module : & ModuleHeader ,
259
- func_to_validate : Option < FuncToValidate < ValidatorResources > > ,
260
- ) {
261
- let mut funcs = self . funcs . lock ( ) ;
262
- let Some ( func) = funcs. get_mut ( func) else {
263
- panic ! ( "encountered invalid internal function: {func:?}" )
264
- } ;
265
- func. init_uncompiled ( UncompiledFuncEntity :: new (
266
- func_idx,
267
- bytes,
268
- module. clone ( ) ,
269
- func_to_validate,
270
- ) ) ;
271
- }
272
-
273
257
/// Returns the [`FuncEntity`] of the [`EngineFunc`].
274
258
///
275
259
/// # Errors
@@ -285,26 +269,12 @@ impl CodeMap {
285
269
) -> Result < CompiledFuncRef < ' a > , Error > {
286
270
match self . get_compiled ( func) {
287
271
Some ( cref) => Ok ( cref) ,
272
+ #[ cfg( feature = "parser" ) ]
288
273
None => self . compile_or_wait ( fuel, func) ,
289
- }
290
- }
291
-
292
- /// Compile `func` or wait for result if another process already started compilation.
293
- ///
294
- /// # Errors
295
- ///
296
- /// - If translation or Wasm validation of `func` failed.
297
- /// - If `ctx` ran out of fuel in case fuel consumption is enabled.
298
- #[ cold]
299
- #[ inline]
300
- fn compile_or_wait < ' a > (
301
- & ' a self ,
302
- fuel : Option < & mut Fuel > ,
303
- func : EngineFunc ,
304
- ) -> Result < CompiledFuncRef < ' a > , Error > {
305
- match self . get_uncompiled ( func) {
306
- Some ( entity) => self . compile ( fuel, func, entity) ,
307
- None => self . wait_for_compilation ( func) ,
274
+ #[ cfg( not( feature = "parser" ) ) ]
275
+ None => Err ( Error :: new (
276
+ "Function not compiled - parser feature required" ,
277
+ ) ) ,
308
278
}
309
279
}
310
280
@@ -327,6 +297,7 @@ impl CodeMap {
327
297
/// Returns the [`UncompiledFuncEntity`] of `func` if possible, otherwise returns `None`.
328
298
///
329
299
/// After this operation `func` will be in [`FuncEntity::Compiling`] state.
300
+ #[ cfg( feature = "parser" ) ]
330
301
#[ inline]
331
302
fn get_uncompiled ( & self , func : EngineFunc ) -> Option < UncompiledFuncEntity > {
332
303
let mut funcs = self . funcs . lock ( ) ;
@@ -353,6 +324,54 @@ impl CodeMap {
353
324
// returned `CompiledFuncRef` only references `Pin`ned data.
354
325
unsafe { mem:: transmute :: < CompiledFuncRef < ' _ > , CompiledFuncRef < ' a > > ( cref) }
355
326
}
327
+ }
328
+
329
+ #[ cfg( feature = "parser" ) ]
330
+ impl CodeMap {
331
+ /// Compile `func` or wait for result if another process already started compilation.
332
+ ///
333
+ /// # Errors
334
+ ///
335
+ /// - If translation or Wasm validation of `func` failed.
336
+ /// - If `ctx` ran out of fuel in case fuel consumption is enabled.
337
+ #[ cold]
338
+ #[ inline]
339
+ fn compile_or_wait < ' a > (
340
+ & ' a self ,
341
+ fuel : Option < & mut Fuel > ,
342
+ func : EngineFunc ,
343
+ ) -> Result < CompiledFuncRef < ' a > , Error > {
344
+ match self . get_uncompiled ( func) {
345
+ Some ( entity) => self . compile ( fuel, func, entity) ,
346
+ None => self . wait_for_compilation ( func) ,
347
+ }
348
+ }
349
+
350
+ /// Initializes the [`EngineFunc`] for lazy translation.
351
+ ///
352
+ /// # Panics
353
+ ///
354
+ /// - If `func` is an invalid [`EngineFunc`] reference for this [`CodeMap`].
355
+ /// - If `func` refers to an already initialized [`EngineFunc`].
356
+ pub fn init_func_as_uncompiled (
357
+ & self ,
358
+ func : EngineFunc ,
359
+ func_idx : FuncIdx ,
360
+ bytes : & [ u8 ] ,
361
+ module : & ModuleHeader ,
362
+ func_to_validate : Option < FuncToValidate < ValidatorResources > > ,
363
+ ) {
364
+ let mut funcs = self . funcs . lock ( ) ;
365
+ let Some ( func) = funcs. get_mut ( func) else {
366
+ panic ! ( "encountered invalid internal function: {func:?}" )
367
+ } ;
368
+ func. init_uncompiled ( UncompiledFuncEntity :: new (
369
+ func_idx,
370
+ bytes,
371
+ module. clone ( ) ,
372
+ func_to_validate,
373
+ ) ) ;
374
+ }
356
375
357
376
/// Compile and validate the [`UncompiledFuncEntity`] identified by `func`.
358
377
///
@@ -426,6 +445,7 @@ impl CodeMap {
426
445
enum FuncEntity {
427
446
/// The function entity has not yet been initialized.
428
447
Uninit ,
448
+ #[ cfg( feature = "parser" ) ]
429
449
/// An internal function that has not yet been compiled.
430
450
Uncompiled ( UncompiledFuncEntity ) ,
431
451
/// The function entity is currently compiling.
@@ -461,6 +481,7 @@ impl FuncEntity {
461
481
///
462
482
/// If `func` has already been initialized.
463
483
#[ inline]
484
+ #[ cfg( feature = "parser" ) ]
464
485
pub fn init_uncompiled ( & mut self , entity : UncompiledFuncEntity ) {
465
486
assert ! ( matches!( self , Self :: Uninit ) ) ;
466
487
* self = Self :: Uncompiled ( entity) ;
@@ -483,6 +504,7 @@ impl FuncEntity {
483
504
///
484
505
/// Returns a proper error if the [`FuncEntity`] is not uncompiled.
485
506
#[ inline]
507
+ #[ cfg( feature = "parser" ) ]
486
508
pub fn get_uncompiled ( & mut self ) -> Option < UncompiledFuncEntity > {
487
509
match self {
488
510
Self :: Uncompiled ( _) => { }
@@ -535,6 +557,7 @@ impl FuncEntity {
535
557
#[ repr( transparent) ]
536
558
pub struct TypeIndex ( u32 ) ;
537
559
560
+ #[ cfg( feature = "parser" ) ]
538
561
/// An internal uncompiled function entity.
539
562
pub struct UncompiledFuncEntity {
540
563
/// The index of the function within the Wasm module.
@@ -552,6 +575,7 @@ pub struct UncompiledFuncEntity {
552
575
validation : Option < ( TypeIndex , ValidatorResources ) > ,
553
576
}
554
577
578
+ #[ cfg( feature = "parser" ) ]
555
579
impl UncompiledFuncEntity {
556
580
/// Creates a new [`UncompiledFuncEntity`].
557
581
pub fn new (
@@ -676,6 +700,7 @@ impl UncompiledFuncEntity {
676
700
}
677
701
}
678
702
703
+ #[ cfg( feature = "parser" ) ]
679
704
impl fmt:: Debug for UncompiledFuncEntity {
680
705
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
681
706
f. debug_struct ( "UncompiledFuncEntity" )
0 commit comments